1

I'm trying to parse an ontology (complete including the imported ontology) to store it into a graph database. To do this, I first list all classes in the ontology and then link them to their respective super classes.

The code works fine, except for imported super classes. I can link to super classes within my own ontology but not from a class whose superclass is in the imported ontology. The superclass exists, I can see it if I print it after the getClasesInSignature() method call because I specified true to add imported classes.

In this code example, an output of the superclasses set would be empty for classes as described above. Is there a way to include them?

public void importOntology(String ontologyFile) throws Exception {
    try {
        File file = new File(ontologyFile);
        if (file.exists()) {
            OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
            OWLOntology ontology = manager.loadOntologyFromOntologyDocument(file);
            OWLReasonerFactory reasonerFactory = PelletReasonerFactory.getInstance();
            OWLReasoner reasoner = reasonerFactory.createReasoner(ontology, new SimpleConfiguration());
            if (!reasoner.isConsistent()) {
                throw new Exception("Ontology is inconsistent");
            }

            Transaction tx = db.beginTx();
            try {
                //create starting node
                Node thingNode = getOrCreateNodeWithUniqueFactory("owl:Thing");

                //add all classes
                for (OWLClass c :ontology.getClassesInSignature(true)) {
                    String classString = c.toString();
                    if (classString.contains("#")) {
                        classString = classString.substring(classString.indexOf("#")+1,classString.lastIndexOf(">"));
                    }
                    //create node
                    Node classNode = getOrCreateNodeWithUniqueFactory(classString);

                    Set<OWLClassExpression> superclasses = c.getSuperClasses(ontology);

                    //top level node
                    if (superclasses.isEmpty()) {
                        //link to thing 
                    } else {
                        //link to superclass(es)
                    }

                    //[rest of code removed]
            }
        }
    }
4

1 回答 1

3

OK,经过一番研究,我发现OWLreasoner还有一个获取超类的功能。这种方法包括来自导入本体的超类,甚至可以区分直接超类和间接超类。虽然 getClassesInSignature() 包括那些没有访问推理器的人,但这有点奇怪,但这工作正常并解决了我的问题。

代码是

NodeSet<OWLClass> superclasses = reasoner.getSuperClasses(c, true);

上课。返回类型不同,这也是为什么以下内容也必须更改的原因:

for (org.semanticweb.owlapi.reasoner.Node<OWLClass> parentOWLNode: superclasses) {                      
    OWLClassExpression parent = parentOWLNode.getRepresentativeElement();
于 2013-07-12T10:14:49.840 回答