获取所有" someValuesFrom" Jena的对象属性类

时间:2017-12-28 23:41:19

标签: jena ontology

我希望得到" someValuesFrom "的所有价值。将每个Object Property作为列表​​。

我有猫头鹰文件的这一部分:

<owl:Class rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#ErgativeFrame">
    <owl:equivalentClass>
        <owl:Restriction>
            <owl:onProperty rdf:resource="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#hasAgent"/>
            <owl:someValuesFrom>
                <owl:Class>
                    <owl:unionOf rdf:parseType="Collection">
                        <rdf:Description rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#Animal"/>
                        <rdf:Description rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#Object"/>
                        <rdf:Description rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#Person"/>
                    </owl:unionOf>
                </owl:Class>
            </owl:someValuesFrom>
        </owl:Restriction>
    </owl:equivalentClass>
    <owl:equivalentClass>
        <owl:Restriction>
            <owl:onProperty rdf:resource="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#hasMainAction"/>
            <owl:someValuesFrom rdf:resource="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#ErgativeVerb"/>
        </owl:Restriction>
    </owl:equivalentClass>
    <owl:equivalentClass>
        <owl:Restriction>
            <owl:onProperty rdf:resource="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#hasOptionalPatient"/>
            <owl:someValuesFrom>
                <owl:Class>
                    <owl:unionOf rdf:parseType="Collection">
                        <rdf:Description rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#Animal"/>
                        <rdf:Description rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#Beverage"/>
                        <rdf:Description rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#BodyPart"/>
                        <rdf:Description rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#ClosedLocation"/>
                        <rdf:Description rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#Clothing"/>
                        <rdf:Description rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#Food"/>
                        <rdf:Description rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#Furniture"/>
                        <rdf:Description rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#NatureElement"/>
                        <rdf:Description rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#NaturePhenomenon"/>
                        <rdf:Description rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#OpenLocation"/>
                        <rdf:Description rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#Person"/>
                        <rdf:Description rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#Toy"/>
                        <rdf:Description rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#Transport"/>
                        <rdf:Description rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#Vegetal"/>
                    </owl:unionOf>
                </owl:Class>
            </owl:someValuesFrom>
        </owl:Restriction>
    </owl:equivalentClass>
    <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#Frame"/>
</owl:Class>

我尝试过这样的事情:

OntClass ontClass = this.ontModel.getOntClass(NS_OWL + "ErgativeFrame");
List<OntClass> list = ontClass.listEquivalentClasses().toList();

for (OntClass equiv: list) {
    if (equiv.isRestriction()) {
        System.out.println("is restriction");
        System.out.println(equiv.getLocalName());
    }
}

&#34;的数量是限制&#34;打印在屏幕上等于属性的长度,但getLocalName总是&#34; null&#34;。

我该怎么做?

编辑:我已经正确地对代码进行了一些更改(见下文)。 现在我仍然无法获得&#34; someValueFrom &#34;。

OntClass ontClass = this.ontModel.getOntClass(NS_OWL + "ErgativeFrame");
List<OntClass> list = ontClass.listEquivalentClasses().toList();

for (OntClass equiv: list) {
    if (equiv.isRestriction()) {
        System.out.println("is restriction");
        System.out.println(equiv.asRestriction().getOnProperty().getLocalName());
    }
}

2 个答案:

答案 0 :(得分:0)

getLocalName返回null的原因是因为您使用的是匿名类和属性,即没有分配URI的类和属性。由于Jena找不到URI,因此返回null

为了说明,这里是你的本体的一个子集,它有命名的类和属性,而不是匿名的类和属性:

             

<rdf:Description rdf:about="http://www.w3.org/2002/07/owl#topObjectProperty">
    <rdfs:range rdf:resource="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#AnimalObjectPerson"/>
</rdf:Description>

<owl:Class rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#Frame"/>

<owl:Class rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#Object"/>

<owl:Class rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#Person"/>

<owl:Class rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#Animal"/>

<owl:Class rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#AnimalObjectPerson">
    <owl:equivalentClass>
        <owl:Class>
            <owl:unionOf rdf:parseType="Collection">
                <rdf:Description rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#Animal"/>
                <rdf:Description rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#Object"/>
                <rdf:Description rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#Person"/>
            </owl:unionOf>
        </owl:Class>
    </owl:equivalentClass>
</owl:Class>

<owl:Class rdf:about="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#ErgativeFrame">
    <owl:equivalentClass>
        <owl:Restriction>
            <owl:onProperty rdf:resource="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#hasAgent"/>
            <owl:someValuesFrom rdf:resource="http://www.w3.org/2002/07/owl#Thing"/>
        </owl:Restriction>
    </owl:equivalentClass>
    <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/gumaro/ontologies/2017/11/untitled-ontology-26#Frame"/>
</owl:Class>

以下是检索属性范围的代码:

        OntClass ontClass = ontModel.getOntClass(NS_OWL + "ErgativeFrame");
        List<OntClass> list = ontClass.listEquivalentClasses().toList();

        for (OntClass equiv: list) {
            if (equiv.isRestriction()) {                    
                OntProperty ontProperty = equiv.asRestriction().getOnProperty();                                        
                OntResource range = ontProperty.getRange();
                List<OntClass> rangeList = range.asClass().listEquivalentClasses().toList();
                for (OntClass rangeEquiv: rangeList) {;
                    for (OntClass rangeUnionClass: rangeEquiv.asUnionClass().listOperands().toList()) {
                        System.out.println("classes in union = " + rangeUnionClass);
                    }
                }

            }
        }

我希望有所帮助。

答案 1 :(得分:0)

在提供的片段中存在不明确的情况:限制'...#hasMainAction'+'...#ErgativeVerb'可以是数据属性或对象属性存在量化限制(请参阅owl2 guide)。在代码片段中没有明确的声明来判断它是什么。

所有限制都是匿名资源(类表达式) - 因此它们没有uri和本地名称。

Jena-API(包org.apache.jena.ontology)仅适用于OWL(-1),不适用于OWL-2。看来纯粹的OWL已经过时了。有一个基于jena的模拟org.apache.jena.ontology.OntModel特别为OWL-2 - OntGraphModel。虽然它与原始问题没有直接关系,但在这种情况下可能更有用。这是一个例子:

    String fullOwlAsString = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
            "<rdf:RDF xmlns=\"http://www.semanticweb.org/owlapi/test#\"\n" +
            "     xml:base=\"http://www.semanticweb.org/owlapi/test\"\n" +
            "     xmlns:xsd=\"http://www.w3.org/2001/XMLSchema#\"\n" +
            "     xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\"\n" +
            "     xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n" +
            "     xmlns:owl=\"http://www.w3.org/2002/07/owl#\">" +
            "<owl:Ontology rdf:about=\"#\" />" + yourSnippet + "</rdf:RDF>";
    InputStream in = new ByteArrayInputStream(fullOwlAsString.getBytes(StandardCharsets.UTF_8));
    OntGraphModel ont = OntManagers.createONT().loadOntologyFromOntologyDocument(in).asGraphModel();
    ont.ontObjects(OntCE.ObjectSomeValuesFrom.class).map(OntCE.Value::getValue)
            .filter(OntCE.ComponentsCE.class::isInstance)
            .map(OntCE.ComponentsCE.class::cast)
            .flatMap(OntCE.Components::components)
            .map(RDFNode.class::cast).filter(RDFNode::isResource).map(RDFNode::asResource).map(Resource::getLocalName)
            .forEach(System.out::println);

输出:

Animal
Beverage
BodyPart
ClosedLocation
Clothing
Food
Furniture
NatureElement
NaturePhenomenon
OpenLocation
Person
Toy
Transport
Vegetal
Animal
Object
Person