当前位置: 首页 > 知识库问答 >
问题:

通过OWL API查询OWL本体的问题

微生弘
2023-03-14
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>";
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);
                             }
                        }

            }
        }
reasoner.getObjectPropertyValues(i, objectProperty)

我甚至尝试与其他推理者(隐士,佩莱),因为我读到其他的帖子,但结果没有改变。我该怎么解决呢?

共有1个答案

宁良平
2023-03-14

您正在使用的本体中没有断言objectProperties;所以推理者什么也找不到,因为没有什么可找的。

加载您的本体后添加下面的代码,这样您就可以测试您的代码了。

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

您的代码看起来不错,但有点复杂。使用“get flatened()”可以得到更短的代码

for (final OWLNamedIndividual i : reasoner.getInstances(c, true).getFlattened())
 类似资料:
  • 我想通过使用Eclipse的OWL API访问我的本体和SWRL规则。有没有人能帮我做一个确切的程序来告诉我该怎么做? 我尝试了以下代码,但似乎没有得到任何响应。请记住,我的Java技能很差。 我需要一个确切的程序来解决这个问题。 我已经有的代码是:

  • 我开始学习如何对owl本体进行推理,并且在确定我尝试做的事情是否可能时遇到了一些问题。 我使用的本体论是位于这里的葡萄酒本体论;它引用了这个食物本体。我在Protege和Jena都玩过推理引擎。 所以现在我尝试使用属性来确定什么类型的葡萄酒可以搭配这道菜。在本体论中为可以与此属性关联的实例定义了几个限制:、等--定义如下: 我可以A)获得这些限制,这样我就可以确定什么类型的葡萄酒将与我的MealC

  • 我正在为一个项目使用OWLAPI,我需要比较两个本体之间的差异。这将忽略空节点,例如,我可以确定两个本体中是否存在相同的OWL限制。我不仅需要知道是否存在差异,而且需要找出那些差异是什么。OWLAPI和oz中是否存在这样的功能,有没有一个相对简单的方法来实现这一点?

  • 我刚刚开始使用OWL API,以便生成一些使用其他本体的示例。情况是这样的:我有两个本体A和B,它们有很多元素,从其他本体导入。这两个本体是一个标准的一部分,因此它们是密切相关的。我需要生成一个标准的元素示例,它涉及导入这两个本体,并使用和组合来自这两个本体的类和元素,但我不知道如何开始。我尝试过使用API,但我唯一实现的是加载一个本体,获取一些类和属性,并将它们组合成一个新的本体。我也不知道如何

  • 我正在学习这门基于数据的知识课,我试图弄清楚如何安装第一个OWL API。首先,我在Protege中创建一个.owl文件。我的老师告诉我们,我们必须编写一个小的Java程序来读取我们的.owl文件。他告诉我们包括猫头鹰的API;他没有就OWL API进行任何指示和任何讲座。你知道我如何安装OWL API是非常混乱的吗?如果有什么好的教程可以解释如何将.owl文件实现到Java程序中呢? 谢谢

  • 我遇到了一个奇怪的问题,使用Oracle Pivot语法的sql查询。我可以毫无问题地在SqlDeveloper中运行查询;然而,使用行映射器通过JdbcTemplate运行它会产生关于无效列名的奇怪错误。 SQL语句: 行映射器: DAO调用类: