简体   繁体   中英

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. First, I test queries on restrictions. Actually, Jena is one of the available APIs which allow querying inferred models. Furthermore, I need to split the schema from the data so, using Protégé, I created two separate RDF files with two distinct namespaces.

In the first namespace, http://www.test.com/schema# , for the schema, there is one class: Woman ; one object property: hasSpouse ; and one equivalent class on a restriction on 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 . janette is a Woman and john 's spouse is 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 . 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. I tried to build exactly the same Model from scratch, but afterward the query doesn't work anymore.

    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.

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)后重新分配变量推理器。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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