通过OWL API查询OWL本体时出现问题 [英] Problems querying OWL ontology through OWL API

查看:112
本文介绍了通过OWL API查询OWL本体时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拥有OWL API示例中使用的本体.

I have the ontology used in the OWL API examples.

private static final String KOALA = "<?xml version=\"1.0\"?>\n"
    + "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\" xmlns:owl=\"http://www.w3.org/2002/07/owl#\" xmlns=\"http://protege.stanford.edu/plugins/owl/owl-library/koala.owl#\" xml:base=\"http://protege.stanford.edu/plugins/owl/owl-library/koala.owl\">\n"
    + "  <owl:Ontology rdf:about=\"\"/>\n"
    + "  <owl:Class rdf:ID=\"Female\"><owl:equivalentClass><owl:Restriction><owl:onProperty><owl:FunctionalProperty rdf:about=\"#hasGender\"/></owl:onProperty><owl:hasValue><Gender rdf:ID=\"female\"/></owl:hasValue></owl:Restriction></owl:equivalentClass></owl:Class>\n"
    + "  <owl:Class rdf:ID=\"Marsupials\"><owl:disjointWith><owl:Class rdf:about=\"#Person\"/></owl:disjointWith><rdfs:subClassOf><owl:Class rdf:about=\"#Animal\"/></rdfs:subClassOf></owl:Class>\n"
    + "  <owl:Class rdf:ID=\"Student\"><owl:equivalentClass><owl:Class><owl:intersectionOf rdf:parseType=\"Collection\"><owl:Class rdf:about=\"#Person\"/><owl:Restriction><owl:onProperty><owl:FunctionalProperty rdf:about=\"#isHardWorking\"/></owl:onProperty><owl:hasValue rdf:datatype=\"http://www.w3.org/2001/XMLSchema#boolean\">true</owl:hasValue></owl:Restriction><owl:Restriction><owl:someValuesFrom><owl:Class rdf:about=\"#University\"/></owl:someValuesFrom><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasHabitat\"/></owl:onProperty></owl:Restriction></owl:intersectionOf></owl:Class></owl:equivalentClass></owl:Class>\n"
    + "  <owl:Class rdf:ID=\"KoalaWithPhD\"><owl:versionInfo>1.2</owl:versionInfo><owl:equivalentClass><owl:Class><owl:intersectionOf rdf:parseType=\"Collection\"><owl:Restriction><owl:hasValue><Degree rdf:ID=\"PhD\"/></owl:hasValue><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasDegree\"/></owl:onProperty></owl:Restriction><owl:Class rdf:about=\"#Koala\"/></owl:intersectionOf></owl:Class></owl:equivalentClass></owl:Class>\n"
    + "  <owl:Class rdf:ID=\"University\"><rdfs:subClassOf><owl:Class rdf:ID=\"Habitat\"/></rdfs:subClassOf></owl:Class>\n"
    + "  <owl:Class rdf:ID=\"Koala\"><rdfs:subClassOf><owl:Restriction><owl:hasValue rdf:datatype=\"http://www.w3.org/2001/XMLSchema#boolean\">false</owl:hasValue><owl:onProperty><owl:FunctionalProperty rdf:about=\"#isHardWorking\"/></owl:onProperty></owl:Restriction></rdfs:subClassOf><rdfs:subClassOf><owl:Restriction><owl:someValuesFrom><owl:Class rdf:about=\"#DryEucalyptForest\"/></owl:someValuesFrom><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasHabitat\"/></owl:onProperty></owl:Restriction></rdfs:subClassOf><rdfs:subClassOf rdf:resource=\"#Marsupials\"/></owl:Class>\n"
    + "  <owl:Class rdf:ID=\"Animal\"><rdfs:seeAlso>Male</rdfs:seeAlso><rdfs:subClassOf><owl:Restriction><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasHabitat\"/></owl:onProperty><owl:minCardinality rdf:datatype=\"http://www.w3.org/2001/XMLSchema#int\">1</owl:minCardinality></owl:Restriction></rdfs:subClassOf><rdfs:subClassOf><owl:Restriction><owl:cardinality rdf:datatype=\"http://www.w3.org/2001/XMLSchema#int\">1</owl:cardinality><owl:onProperty><owl:FunctionalProperty rdf:about=\"#hasGender\"/></owl:onProperty></owl:Restriction></rdfs:subClassOf><owl:versionInfo>1.1</owl:versionInfo></owl:Class>\n"
    + "  <owl:Class rdf:ID=\"Forest\"><rdfs:subClassOf rdf:resource=\"#Habitat\"/></owl:Class>\n"
    + "  <owl:Class rdf:ID=\"Rainforest\"><rdfs:subClassOf rdf:resource=\"#Forest\"/></owl:Class>\n"
    + "  <owl:Class rdf:ID=\"GraduateStudent\"><rdfs:subClassOf><owl:Restriction><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasDegree\"/></owl:onProperty><owl:someValuesFrom><owl:Class><owl:oneOf rdf:parseType=\"Collection\"><Degree rdf:ID=\"BA\"/><Degree rdf:ID=\"BS\"/></owl:oneOf></owl:Class></owl:someValuesFrom></owl:Restriction></rdfs:subClassOf><rdfs:subClassOf rdf:resource=\"#Student\"/></owl:Class>\n"
    + "  <owl:Class rdf:ID=\"Parent\"><owl:equivalentClass><owl:Class><owl:intersectionOf rdf:parseType=\"Collection\"><owl:Class rdf:about=\"#Animal\"/><owl:Restriction><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasChildren\"/></owl:onProperty><owl:minCardinality rdf:datatype=\"http://www.w3.org/2001/XMLSchema#int\">1</owl:minCardinality></owl:Restriction></owl:intersectionOf></owl:Class></owl:equivalentClass><rdfs:subClassOf rdf:resource=\"#Animal\"/></owl:Class>\n"
    + "  <owl:Class rdf:ID=\"DryEucalyptForest\"><rdfs:subClassOf rdf:resource=\"#Forest\"/></owl:Class>\n"
    + "  <owl:Class rdf:ID=\"Quokka\"><rdfs:subClassOf><owl:Restriction><owl:hasValue rdf:datatype=\"http://www.w3.org/2001/XMLSchema#boolean\">true</owl:hasValue><owl:onProperty><owl:FunctionalProperty rdf:about=\"#isHardWorking\"/></owl:onProperty></owl:Restriction></rdfs:subClassOf><rdfs:subClassOf rdf:resource=\"#Marsupials\"/></owl:Class>\n"
    + "  <owl:Class rdf:ID=\"TasmanianDevil\"><rdfs:subClassOf rdf:resource=\"#Marsupials\"/></owl:Class>\n"
    + "  <owl:Class rdf:ID=\"MaleStudentWith3Daughters\"><owl:equivalentClass><owl:Class><owl:intersectionOf rdf:parseType=\"Collection\"><owl:Class rdf:about=\"#Student\"/><owl:Restriction><owl:onProperty><owl:FunctionalProperty rdf:about=\"#hasGender\"/></owl:onProperty><owl:hasValue><Gender rdf:ID=\"male\"/></owl:hasValue></owl:Restriction><owl:Restriction><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasChildren\"/></owl:onProperty><owl:cardinality rdf:datatype=\"http://www.w3.org/2001/XMLSchema#int\">3</owl:cardinality></owl:Restriction><owl:Restriction><owl:allValuesFrom rdf:resource=\"#Female\"/><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasChildren\"/></owl:onProperty></owl:Restriction></owl:intersectionOf></owl:Class></owl:equivalentClass></owl:Class>\n"
    + "  <owl:Class rdf:ID=\"Degree\"/>\n  <owl:Class rdf:ID=\"Gender\"/>\n"
    + "  <owl:Class rdf:ID=\"Male\"><owl:equivalentClass><owl:Restriction><owl:hasValue rdf:resource=\"#male\"/><owl:onProperty><owl:FunctionalProperty rdf:about=\"#hasGender\"/></owl:onProperty></owl:Restriction></owl:equivalentClass></owl:Class>\n"
    + "  <owl:Class rdf:ID=\"Person\"><rdfs:subClassOf rdf:resource=\"#Animal\"/><owl:disjointWith rdf:resource=\"#Marsupials\"/></owl:Class>\n"
    + "  <owl:ObjectProperty rdf:ID=\"hasHabitat\"><rdfs:range rdf:resource=\"#Habitat\"/><rdfs:domain rdf:resource=\"#Animal\"/></owl:ObjectProperty>\n"
    + "  <owl:ObjectProperty rdf:ID=\"hasDegree\"><rdfs:domain rdf:resource=\"#Person\"/><rdfs:range rdf:resource=\"#Degree\"/></owl:ObjectProperty>\n"
    + "  <owl:ObjectProperty rdf:ID=\"hasChildren\"><rdfs:range rdf:resource=\"#Animal\"/><rdfs:domain rdf:resource=\"#Animal\"/></owl:ObjectProperty>\n"
    + "  <owl:FunctionalProperty rdf:ID=\"hasGender\"><rdfs:range rdf:resource=\"#Gender\"/><rdf:type rdf:resource=\"http://www.w3.org/2002/07/owl#ObjectProperty\"/><rdfs:domain rdf:resource=\"#Animal\"/></owl:FunctionalProperty>\n"
    + "  <owl:FunctionalProperty rdf:ID=\"isHardWorking\"><rdfs:range rdf:resource=\"http://www.w3.org/2001/XMLSchema#boolean\"/><rdfs:domain rdf:resource=\"#Person\"/><rdf:type rdf:resource=\"http://www.w3.org/2002/07/owl#DatatypeProperty\"/></owl:FunctionalProperty>\n"
    + "  <Degree rdf:ID=\"MA\"/>\n</rdf:RDF>";

我使用了此代码( https://neo4j.com/blog/using -owl-with-neo4j/)来查询猫头鹰本体:

I used this code (https://neo4j.com/blog/using-owl-with-neo4j/) to query the owl ontology:

OWLReasoner reasoner = reasonerFactory.createNonBufferingReasoner(ontology);
    if (!reasoner.isConsistent()) {

        logger.log(Level.INFO, "Ontology is inconsistent");
        //throw your exception of choice here
        throw new Exception("Ontology is inconsistent");
    }

    try{

        // Node thingNode = getOrCreateNodeWithUniqueFactory("owl:Thing");  

        for (OWLClass c :ontology.getClassesInSignature(true)) {
            String classString = c.toString();
            if (classString.contains("#")) {
                classString = classString.substring(
                classString.indexOf("#")+1,classString.lastIndexOf(">"));
            }
            System.out.println("classString: "+classString);

            // Node classNode = getOrCreateNodeWithUniqueFactory(classString); 

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

            if (superclasses.isEmpty()) {
                //classNode.createRelationshipTo(thingNode,DynamicRelationshipType.withName("isA")); 
                } else {
                for (org.semanticweb.owlapi.reasoner.Node<OWLClass>
                parentOWLNode: superclasses) {
                     OWLClassExpression parent = parentOWLNode.getRepresentativeElement();
                     String parentString = parent.toString();
                     if (parentString.contains("#")) {
                          parentString = parentString.substring(
                          parentString.indexOf("#")+1,
                          parentString.lastIndexOf(">"));
                     }

                     System.out.println("parentString: "+parentString);
                 //    Node parentNode = getOrCreateNodeWithUniqueFactory(parentString);   
                 //    classNode.createRelationshipTo(parentNode, DynamicRelationshipType.withName("isA"));  
                }
            }

            for (org.semanticweb.owlapi.reasoner.Node<OWLNamedIndividual> in
                    : reasoner.getInstances(c, true)) { 
                    OWLNamedIndividual i = in.getRepresentativeElement();
                    String indString = i.toString();
                    if (indString.contains("#")) {
                         indString = indString.substring(
                              indString.indexOf("#")+1,indString.lastIndexOf(">"));
                    }

                    System.out.println("indString: "+indString);

                //    Node individualNode = getOrCreateNodeWithUniqueFactory(indString);  
                //    individualNode.createRelationshipTo(classNode, DynamicRelationshipType.withName("isA"));  


                    for (OWLObjectPropertyExpression objectProperty:
                        ontology.getObjectPropertiesInSignature()) { System.out.println("objectProperty: "+objectProperty); 
                             for
                             (org.semanticweb.owlapi.reasoner.Node<OWLNamedIndividual>
                             object: reasoner.getObjectPropertyValues(i,
                             objectProperty)) { 
                                  String reltype = objectProperty.toString();
                                  reltype = reltype.substring(reltype.indexOf("#")+1,
                                  reltype.lastIndexOf(">"));
                                  String s =
                                  object.getRepresentativeElement().toString();
                                  s = s.substring(s.indexOf("#")+1,
                                  s.lastIndexOf(">"));

                                  System.out.println("reltype: "+reltype+" s: "+s);
                            //      Node objectNode = getOrCreateNodeWithUniqueFactory(s);
                            //      individualNode.createRelationshipTo(objectNode, DynamicRelationshipType.withName(reltype));
                             }
                       }
                       for (OWLDataPropertyExpression dataProperty:
                       ontology.getDataPropertiesInSignature()) { System.out.println("dataProperty: "+dataProperty.asOWLDataProperty()); 
                             for (OWLLiteral object: reasoner.getDataPropertyValues(
                             i, dataProperty.asOWLDataProperty())) {
                                  String reltype =
                                  dataProperty.asOWLDataProperty().toString();
                                  reltype = reltype.substring(reltype.indexOf("#")+1, 
                                  reltype.lastIndexOf(">"));
                                  String s = object.toString();

                                  System.out.println("reltype: "+reltype+" s: "+s);

                             //     individualNode.setProperty(reltype, s);
                             }
                        }

            }
        }

但是我观察到它没有为指定的个体和对象属性表达式返回对象属性值.

but I observed that it doesn't return object property values for the specified individual and object property expression.

reasoner.getObjectPropertyValues(i, objectProperty)

不返回任何项目.为什么?

returns no items. Why?

在其他帖子中,我甚至尝试过与其他Reasoner(HermiT,Pellet)一起尝试,但结果没有改变. 我该怎么解决?

I tried even with others Reasoner (HermiT, Pellet) as I read in other post, but the outcome does not change. How can I solve it?

推荐答案

您正在使用的本体中没有objectProperties assertted ;因此,推理者什么也找不到,因为什么也找不到.

There is no objectProperties asserted in the ontology you are using; So the reasoner find nothing, because there is nothing to find.

在加载本体后添加以下代码,以便您可以测试代码.

Add the following code after loading your ontology, so you can test your code.

final OWLDataFactory factory = manager.getOWLDataFactory();
ontology.add(factory.getOWLObjectPropertyAssertionAxiom(factory.getOWLObjectProperty("#HelloWorldProperty"), factory.getOWLNamedIndividual("#FirstIndividual"), factory.getOWLNamedIndividual("#SecondIndividual")));
reasoner.flush();

您的代码看起来不错,但有点复杂. 您可以使用"getFlattened()"获得较短的代码

Your code look good but a bit complicated. You could get shorter code with 'getFlattened()'

for (final OWLNamedIndividual i : reasoner.getInstances(c, true).getFlattened())

这篇关于通过OWL API查询OWL本体时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆