简体   繁体   English

使用OWL API获取本体中两个对象的属性值交集

[英]Get Property Value Intersection of two objects in an ontology using OWL API

I am new to OWL API and trying to experiment with it. 我是OWL API的新手,正在尝试尝试它。 I have created a simple ontology in protege and reciprocal application in eclipse using OWL API. 我使用OWL API在Eclipse中的互惠应用程序中创建了一个简单的本体。 Ontology structure is as follows:- 本体结构如下:

----------------------------------------------------------------------------------------  
 Class                Object       (Data Property)StringValue  ObjectProperty relatedTo    
----------------------------------------------------------------------------------------  
 WordString             WS1                    "One"                   DS1
                        WS2                    "Two"                   DS2
                        WS3                    "Three"                 DS3

 DigiString             DS1                    "1"  
                        DS2                    "2"  
                        DS3                    "3"  
----------------------------------------------------------------------------

Complete ontology is as follows:- 完整的本体如下:

<rdf:RDF xmlns="http://localhost:3030/DigiWord.owl#"
 xml:base="http://localhost:3030/DigiWord.owl"
 xmlns:DigiWord="http://localhost:3030/DigiWord.owl#"
 xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
 xmlns:owl="http://www.w3.org/2002/07/owl#"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<owl:Ontology rdf:about="http://localhost:3030/DigiWord.owl"/>
<!-- 
// Object Properties
///////////////////////////////////////////////////////////////////////////////////////
 -->
<!-- http://localhost:3030/DigiWord.owl#relatedTo -->
<owl:ObjectProperty rdf:about="&DigiWord;relatedTo">
    <rdfs:domain rdf:resource="&DigiWord;DigiString"/>
    <rdfs:range rdf:resource="&DigiWord;WordString"/>
</owl:ObjectProperty>
<!-- 
//
// Data properties
///////////////////////////////////////////////////////////////////////////////////////
 -->
<!-- http://localhost:3030/DigiWord.owl#stringValue -->

<owl:DatatypeProperty rdf:about="&DigiWord;stringValue">
    <rdfs:domain rdf:resource="&DigiWord;DigiString"/>
    <rdfs:domain rdf:resource="&DigiWord;WordString"/>
    <rdfs:range rdf:resource="&xsd;string"/>
</owl:DatatypeProperty>
<!-- 
// Classes
///////////////////////////////////////////////////////////////////////////////////////
 -->
<!-- http://localhost:3030/DigiWord.owl#DigiString -->
<owl:Class rdf:about="&DigiWord;DigiString"/>
<!-- http://localhost:3030/DigiWord.owl#WordString -->
<owl:Class rdf:about="&DigiWord;WordString"/>
<!-- 
// Individuals
///////////////////////////////////////////////////////////////////////////////////////
 -->
<!-- http://localhost:3030/DigiWord.owl#DS1 -->
<owl:NamedIndividual rdf:about="&DigiWord;DS1">
    <rdf:type rdf:resource="&DigiWord;DigiString"/>
    <stringValue rdf:datatype="&xsd;string">1</stringValue>
    <relatedTo rdf:resource="&DigiWord;WS1"/>
</owl:NamedIndividual>
<!-- http://localhost:3030/DigiWord.owl#DS2 -->
<owl:NamedIndividual rdf:about="&DigiWord;DS2">
    <rdf:type rdf:resource="&DigiWord;DigiString"/>
    <stringValue rdf:datatype="&xsd;string">2</stringValue>
    <relatedTo rdf:resource="&DigiWord;WS2"/>
</owl:NamedIndividual>
<!-- http://localhost:3030/DigiWord.owl#DS3 -->
<owl:NamedIndividual rdf:about="&DigiWord;DS3">
    <rdf:type rdf:resource="&DigiWord;DigiString"/>
    <stringValue rdf:datatype="&xsd;string"></stringValue>
    <relatedTo rdf:resource="&DigiWord;WS3"/>
</owl:NamedIndividual>
<!-- http://localhost:3030/DigiWord.owl#WS1 -->
<owl:NamedIndividual rdf:about="&DigiWord;WS1">
    <rdf:type rdf:resource="&DigiWord;WordString"/>
    <stringValue rdf:datatype="&xsd;string">One</stringValue>
</owl:NamedIndividual>
<!-- http://localhost:3030/DigiWord.owl#WS2 -->
<owl:NamedIndividual rdf:about="&DigiWord;WS2">
    <rdf:type rdf:resource="&DigiWord;WordString"/>
    <stringValue rdf:datatype="&xsd;string">Two</stringValue>
</owl:NamedIndividual>
<!-- http://localhost:3030/DigiWord.owl#WS3 -->
<owl:NamedIndividual rdf:about="&DigiWord;WS3">
    <rdf:type rdf:resource="&DigiWord;WordString"/>
    <stringValue rdf:datatype="&xsd;string">Three</stringValue>
</owl:NamedIndividual>
</rdf:RDF>

I want to retrieve Object WS1 for DS1, WS2 for DS2 ie upon providing strings "1", "2" etc my code should retrieve "One" , "Two" respectively. 我想为DS1检索对象WS1,为DS2检索WS2,即在提供字符串“ 1”,“ 2”等后,我的代码应分别检索“ One”,“ Two”。 I have not come across any relevant code of this type on net.Any help would be appreciated. 我在网上还没有遇到过这种类型的相关代码。任何帮助将不胜感激。 Thanks in Advance. 提前致谢。

The following example (OWLAPI 4) should help: 以下示例(OWLAPI 4)应该会有所帮助:

    OWLOntology o = ... your ontology here
    String ns = "http://localhost:3030/DigiWord.owl#";
    OWLDataFactory df = o.getOWLOntologyManager().getOWLDataFactory();
    OWLObjectProperty relatedto = df.getOWLObjectProperty(IRI.create(ns + "relatedTo"));
    OWLDataProperty stringValue = df.getOWLDataProperty(IRI.create(ns + "stringValue"));
    SimpleRenderer renderer = new SimpleRenderer();
    for (OWLNamedIndividual i : o.getIndividualsInSignature()) {
        for (OWLLiteral lit : EntitySearcher.getDataPropertyValues(i, stringValue, o)) {
            System.out.println(i + " has values " + renderer.render(lit));
        }
        for (OWLIndividual related : EntitySearcher.getObjectPropertyValues(i, relatedto, o)) {
            for (OWLLiteral lit : EntitySearcher.getDataPropertyValues(related, stringValue, o)) {
                System.out.println(i + " related to " + related + " which has values " + renderer.render(lit));
            }
        }
    }

Edit: to find matches for your query: 编辑:查找与您的查询匹配的内容:

    for (OWLNamedIndividual i : o.getIndividualsInSignature()) {
        for (OWLIndividual related : EntitySearcher.getObjectPropertyValues(i, relatedto, o)) {
            for (OWLLiteral lit : EntitySearcher.getDataPropertyValues(related, stringValue, o)) {
                if(lit.getLexicalForm().equals("1")){
                    for (OWLLiteral lit : EntitySearcher.getDataPropertyValues(i, stringValue, o)) {
                        System.out.println(i + " has values " + renderer.render(lit));
                    }
                }
            }
        }
    }

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

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