如何在一个变量中读取几个OWLOntology模型?

时间:2018-04-08 09:27:07

标签: java owl owl-api


我有一个本体列表(ListofOntologiesName),我能够逐一阅读它们。目前,正如我在代码中看到的那样,我加载第一个本体,保留其类,公理,实体,然后加载第二个本体并读取它的类,公理和实体,并将它们添加到我以前的变量中。但是,这还不够。后来,在我的代码中,我需要使用“myOWLmodel”,但显然它只有我最后加载的本体,而不是所有的。
如何在一个模型中阅读几个本体? 附:我不想要一个例如“设置< OWLOntology> allModell = new HashSet();“ 因为,后来,它允许我只使用此列表的模型之一。

public static void BuildOnt(String ListofOntologiesName) {
    OWLOntology myOWLmodel = null;
    OwlOntologyManager owl = new OwlOntologyManager();
    OWLOntologyID currentOntologyID = null;
    ArrayList<OWLClass> tempClasses=new ArrayList<OWLClass>();
    Set<OWLAxiom> tempaxioms = new HashSet<OWLAxiom> ();
    Set<OWLEntity> tempEntities = new HashSet<OWLEntity> ();

    try {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        String[] nm = ListofOntologiesName.split("\n");
        for (int ontID = 0; ontID < nm.length; ontID++) {
            if (!nm[ontID].toString().equals("null")) {
                String OntName = nm[ontID];
                File file = new File(OntName);
                myOWLmodel = manager.loadOntologyFromOntologyDocument(file);

                Set<OWLAxiom> tempAx = myOWLmodel.getAxioms();
                tempaxioms.addAll(tempAx);      

                ArrayList<OWLClass> classes = owl.getClasses(myOWLmodel);
                tempClasses.addAll(classes);

                Set<OWLEntity> ent = myOWLmodel.getSignature();
                tempEntities.addAll(ent);
            }
        }

        IRI ontologyIRI = IRI.create("http://www.semanticweb.org/mergedontology.owl");
        IRI versionIRI = IRI.create(ontologyIRI + "/version1");
        OWLOntologyID newOntologyID = new OWLOntologyID(ontologyIRI, versionIRI);
        SetOntologyID setOntologyID = new SetOntologyID(myOWLmodel, newOntologyID);
        manager.applyChange(setOntologyID);
        currentOntologyID = myOWLmodel.getOntologyID();
    } catch (Exception e) {
        e.printStackTrace();
}

    //Remaining of code:
   //Do some processes on all Entities by "tempEntities", all axioms by "tempaxioms", and all classes by "tempClasses"
    //Here I need more operation on "myOWLmodel", but it only saves the last loaded ontology
}

更新:


谢谢,我明白了,下面是我的新代码:

public static OWLOntology ReadMultiOnt(String ListofOntologiesName) throws OWLOntologyCreationException {

    // Create an empty ontology
    OWLOntologyManager MergedManager = OWLManager.createOWLOntologyManager();
    File fileM = new File("NameOfMyMergedOntology");
    OWLOntology MergedOntology = MergedManager.loadOntologyFromOntologyDocument(fileM);
    OWLOntologyManager tempManager = OWLManager.createOWLOntologyManager();

    // Load each ontology, and add their axioms to my created empty ontology
    String[] nm = ListofOntologiesName.split("\n");
    for (int ontID = 0; ontID < nm.length; ontID++) {
        if (!nm[ontID].toString().equals("null")) {
            String OntName = nm[ontID];
            File file = new File(OntName);
            OWLOntology tempOntology = tempManager.loadOntologyFromOntologyDocument(file);

            Set<OWLAxiom> tempAx = tempOntology.getAxioms();
            MergedManager.addAxioms(MergedOntology, tempAx);

        }
    }

    return MergedOntology;
    // After that, I am able to do any process of all axioms, objects etc of all input ontologies
}

0 个答案:

没有答案