2013-06-29 44 views
0

我正在評估與Model API關聯的Jena查詢功能,並且我面臨一個問題。首先,我測試限制的查詢。實際上,Jena是允許查詢推斷模型的可用API之一。此外,我需要從數據中分離模式,因此,使用Protégé,我創建了兩個獨立的RDF文件,其中包含兩個不同的名稱空間。無法使用Jena OntModel API重建可工作的OWL本體

在第一個命名空間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#,有兩個人:johnjanettejanetteWomanjohn的配偶是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

這是你正在運行的實際代碼嗎?你的查詢執行'QueryExecution execution = QueryExecutionFactory.create(query,model);'引用'model',但我沒有看到任何變量'model'。 (這是在第一個和第二個代碼塊)。 –

+0

其實我原創創建一個查詢執行的功能,我複製貼身體的可讀性。我忘了更新變量名稱對不起! – jeanmi

+0

嘗試在第二個解決方案中編寫所有模型。報道的公理是否與公理本體中的公理相符?如果要以編程方式查找差異,可以創建兩個HashSet 對象,並使用這些對象比較模型中的語句。 –

回答

0

我忘記了在呼叫他的bindSchema方法reasoner = reasoner.bindSchema(schema)後重新分配變量推理器。