简体   繁体   English

在导入的OWL本体中获取超类

[英]Getting superclasses in imported OWL ontology

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. 超类存在,如果我在getClasesInSignature()方法调用之后打印它,我可以看到它,因为我指定了true来添加导入的类。

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]
            }
        }
    }

OK, after some research, I found out that OWLReasoner also has a function to get super classes. 好吧,经过一些研究,我发现OWLReasoner也有一个获得超级课程的功能。 This method includes the super classes from imported ontologies and has even the possibility to distinguish between direct and indirect superclasses. 该方法包括来自导入本体的超类,甚至可以区分直接和间接超类。 It's a bit strange though that getClassesInSignature() includes those without accessing the reasoner but this works fine and solved my problem. 虽然getClassesInSignature()包含那些没有访问推理器但是工作正常并解决了我的问题,这有点奇怪。

The code would be 代码将是

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

to get the classes. 获得课程。 The return type is different, whch is why the following has to be changed as well: 返回类型不同,这就是为什么还必须更改以下内容:

for (org.semanticweb.owlapi.reasoner.Node<OWLClass> parentOWLNode: superclasses) {                      
    OWLClassExpression parent = parentOWLNode.getRepresentativeElement();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM