简体   繁体   English

无法使用Jena OntModel API重建可用的OWL本体

[英]Unable to rebuild a working OWL ontology using Jena OntModel API

I am evaluating the Jena query capabilities associated with the Model API and I am facing an issue. 我正在评估与Model API相关的Jena查询功能,并且遇到了问题。 First, I test queries on restrictions. 首先,我测试有关限制的查询。 Actually, Jena is one of the available APIs which allow querying inferred models. 实际上,Jena是允许查询推断模型的可用API之一。 Furthermore, I need to split the schema from the data so, using Protégé, I created two separate RDF files with two distinct namespaces. 此外,我需要从数据中拆分模式,因此,使用Protégé,我创建了带有两个不同命名空间的两个单独的RDF文件。

In the first namespace, http://www.test.com/schema# , for the schema, there is one class: Woman ; 在模式的第一个名称空间http://www.test.com/schema# ,有一个类: Woman ; one object property: hasSpouse ; 一个对象属性: hasSpouse ; and one equivalent class on a restriction on hasSpouse : Husband . 还有一个对hasSpouse : Husband的限制的等效类hasSpouse : Husband

<?xml version="1.0"?>
<!DOCTYPE rdf:RDF [
    <!ENTITY owl "http://www.w3.org/2002/07/owl#" >
    <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
    <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
    <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
]>
<rdf:RDF xmlns="http://www.test.com/schema#"
     xml:base="http://www.test.com/schema#"
     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://www.test.com/schema"/>-->

    <owl:ObjectProperty rdf:about="http://www.test.com/schema#hasSpouse">
        <rdfs:range rdf:resource="http://www.test.com/schema#Woman"/>
    </owl:ObjectProperty>

    <owl:Class rdf:about="http://www.test.com/schema#Husband">
        <owl:equivalentClass>
            <owl:Restriction>
                <owl:onProperty rdf:resource="http://www.test.com/schema#hasSpouse"/>
                <owl:someValuesFrom rdf:resource="http://www.test.com/schema#Woman"/>
            </owl:Restriction>
        </owl:equivalentClass>
    </owl:Class>

    <owl:Class rdf:about="http://www.test.com/schema#Woman"/>
</rdf:RDF>

In the second namespace, http://www.test.com/data# , there are two individuals: john and janette . 在第二个名称空间http://www.test.com/data# ,有两个人: johnjanette janette is a Woman and john 's spouse is janette . janetteWomanjohn的配偶是janette

<?xml version="1.0"?>
<!DOCTYPE rdf:RDF [
    <!ENTITY owl "http://www.w3.org/2002/07/owl#" >
    <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
    <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
    <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
    <!ENTITY schema "http://www.test.com/schema#">
]>
<rdf:RDF xmlns="http://www.test.com/data#"
     xml:base="http://www.test.com/data#"
     xmlns:schema="http://www.test.com/schema#"
     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:NamedIndividual rdf:about="http://www.test.com/data#janette">
        <rdf:type rdf:resource="http://www.test.com/schema#Woman"/>
    </owl:NamedIndividual>

    <owl:NamedIndividual rdf:about="http://www.test.com/data#john">
        <schema:hasSpouse rdf:resource="http://www.test.com/data#janette"/>
    </owl:NamedIndividual>
</rdf:RDF>

My test query is finding every husband in the data, and I expect to get john . 我的测试查询正在查找数据中的每个husband ,我期望得到john Here is the query: 这是查询:

   PREFIX schema: <http://www.test.com/schema#>
   select ?subject where {?subject a schema:Husband}

Everything works well using the code below 使用下面的代码,一切正常

    System.out.println("QUERY ON LOADED RESTRICTION");
    String path = "....";

    Model schema = FileManager.get().loadModel("file:"+path+"married_schema_ns.xml");
    schema.write(System.out, "RDF/XML-ABBREV");
    Model data = FileManager.get().loadModel("file:"+path+"married_data_ns.xml");
    data.write(System.out, "RDF/XML-ABBREV");   

    Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
    reasoner = reasoner.bindSchema(schema);
    InfModel inf_model = ModelFactory.createInfModel(reasoner, data);    

    String query_string = "PREFIX schema: <http://www.test.com/schema#>\r\n";
    query_string += "select ?subject where {?subject a schema:Husband}";

    Query query = QueryFactory.create(query_string);      
    query.serialize(new IndentedWriter(System.out));
    QueryExecution execution = QueryExecutionFactory.create(query,model);

    ResultSet results = execution.execSelect();
    while (results.hasNext()) {
        QuerySolution solution = results.nextSolution();
        RDFNode node = solution.get("subject");
        System.out.println("subject="+node);
    } 

    System.out.println("END ....");

The system replies subject=http://www.test.com/data#john as expected. 系统按预期方式答复subject=http://www.test.com/data#john I tried to build exactly the same Model from scratch, but afterward the query doesn't work anymore. 我尝试从头开始构建完全相同的Model ,但是此后查询不再起作用。

    System.out.println("QUERY ON BUILT RESTRICTION");
    OntModel ontology = ModelFactory.createOntologyModel();
    String ns_ontology="http://www.test.com/schema#";
    String pr_ontology = "schema";
    ontology.setNsPrefix("", ns_ontology);

    ObjectProperty has_spouse = ontology.createObjectProperty(ns_ontology+"hasSpouse");   
    OntClass woman = ontology.createClass(ns_ontology+"Woman");
    has_spouse.setRange(woman);

    OntClass husband = ontology.createClass(ns_ontology+"Husband");       
    SomeValuesFromRestriction restriction = ontology.createSomeValuesFromRestriction(null, has_spouse, woman);
    husband.addEquivalentClass(restriction);

    String ns_facts = "http://www.test.com/data#";
    String pr_facts = "data";

    OntModel facts = ModelFactory.createOntologyModel();
    facts.setNsPrefix("", ns_facts);
    facts.setNsPrefix(pr_ontology, ns_ontology);
    Resource r = facts.getResource("http://www.w3.org/2002/07/owl#NamedIndividual");
    Individual john = facts.createIndividual(ns_facts+"john",r);        
    Individual janette = facts.createIndividual(ns_facts+"janette",r);
    janette.addProperty(RDF.type, woman);
    john.addProperty(has_spouse, janette);              

    Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
    reasoner.bindSchema(ontology);
    Model inf_model = ModelFactory.createInfModel(reasoner, facts);

    String query_string = "PREFIX schema: <"+ns_ontology+">\r\n";
    query_string += "select ?subject where {?subject a schema:Husband}";

    Query query = QueryFactory.create(query_string);      
    query.serialize(new IndentedWriter(System.out));
    QueryExecution execution = QueryExecutionFactory.create(query,model);

    ResultSet results = execution.execSelect();
    while (results.hasNext()) {
        QuerySolution solution = results.nextSolution();
        RDFNode node = solution.get("subject");
        System.out.println("subject="+node);
    } 
    System.out.println("END ...");

I don't understand the reason why. 我不明白原因。 The RDF/XML-ABBREV serialization of the two versions match perfectly. 两个版本的RDF / XML-ABBREV序列化完美匹配。

Moreover, when I load the built schema/data serialization in the first version the query works again . 此外,当我在第一个版本中加载构建的模式/数据序列化时,查询将再次起作用

If someone could help me to understand this point! 如果有人可以帮助我理解这一点!

我忘记了在调用他的bindSchema方法reasoner = reasoner.bindSchema(schema)后重新分配变量推理器。

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

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