在导入的 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.
代码工作正常,除了导入的超类.我可以链接到我自己的本体中的超类,但不能链接到超类在导入的本体中的类.超类存在,如果我在调用 getClasesInSignature() 方法之后打印它,我可以看到它,因为我指定了 true 来添加导入的类.
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]
}
}
}
推荐答案
好吧,经过一番研究,我发现 OWLReasoner 也有获取超类的功能.该方法包括来自导入本体的超类,甚至可以区分直接和间接超类.虽然 getClassesInSignature() 包括那些没有访问推理器的那些,但这有点奇怪,但这工作正常并解决了我的问题.
OK, after some research, I found out that OWLReasoner also has a function to get super classes. 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.
代码是
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();
这篇关于在导入的 OWL 本体中获取超类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!