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

时间:2013-06-29 09:11:59

标签: java sparql jena owl

我正在评估与Model API相关的Jena查询功能,我正面临一个问题。首先,我测试有关限制的查询。实际上,Jena是允许查询推断模型的可用API之一。此外,我需要从数据中分离模式,因此,使用Protégé,我创建了两个具有两个不同名称空间的独立RDF文件。

在第一个命名空间http://www.test.com/schema#中,对于架构,有一个类:Woman;一个对象属性:hasSpouse;以及对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>

在第二个命名空间http://www.test.com/data#中,有两个人:johnjanettejanetteWoman,而john的配偶是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>

我的测试查询是查找数据中的每个husband,我希望得到john。这是查询:

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

使用下面的代码,一切运作良好

    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 ....");

系统按预期回复subject=http://www.test.com/data#john。我尝试从头开始构建完全相同的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 ...");

我不明白原因。两个版本的RDF / XML-ABBREV序列化完美匹配。

此外,当我在第一个版本中加载构建的架构/数据序列化时,查询再次工作

如果有人能帮我理解这一点!

1 个答案:

答案 0 :(得分:0)

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