2011-05-14 36 views
4

我有一些RDF & RDFS文件,我想用耶拿SPARQL實現查詢它和我的代碼如下所示:SPARQL查詢隨着推論

//model of my rdf file 
Model model = ModelFactory.createMemModelMaker().createDefaultModel(); 
model.read(inputStream1, null); 
//model of my ontology (word net) file 
Model onto = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_RDFS_INF); 
onto.read(inputStream2,null); 
    String queryString = 
         "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> " 
         + "PREFIX wn:<http://www.webkb.org/theKB_terms.rdf/wn#> " 
         + "SELECT ?person " 
         + "WHERE {" 
         + " ?person rdf:type wn:Person . " 
         + "  }"; 

    Query query = QueryFactory.create(queryString); 
    QueryExecution qe = QueryExecutionFactory.create(query, ????); 
    ResultSet results = qe.execSelect(); 
    ResultSetFormatter.out(System.out, results, query); 
    qe.close(); 

和我有一個WORDNET本體的RDF文件,我想在我的查詢中使用這個本體來做自動推理(當我查詢人的查詢應該返回例如男人,女人) 那麼如何將本體與我的查詢連接?請幫幫我。

更新:現在我有兩個模型:從哪個我應該運行我的查詢?

QueryExecution qe = QueryExecutionFactory.create(query, ????); 

在此先感謝。

回答

6

關鍵是要認識到,在耶拿,Model是中央抽象的一個。推理模型只是一個Model,其中一些三元組存在,因爲它們是由推理規則引起的,而不是從源文檔讀入。因此,您只需更改示例的第一行,即最初創建模型的位置。

雖然您可以create inference models直接,它往往是最簡單的只是create an OntModel所要求的推理支持度:

Model model = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_RDFS_INF); 

如果你想有一個不同的推理,或OWL支持,您可以選擇不同的OntModelSpec不變。請注意,大型和/或複雜模型可能導致查詢速度緩慢。

更新(以下原題的編輯)

理性了兩個模型,你想要的工會。你可以通過OntModel的sub-model factivity來做到這一點。我會改變您的示例如下(注意:我還沒有測試此代碼,但它應該工作):

String rdfFile = "... your RDF file location ..."; 
Model source = FileManager.get().loadModel(rdfFile); 

String ontFile = "... your ontology file location ..."; 
Model ont = FileManager.get().loadModel(ontFile); 

Model m = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_RDFS_INF, ont); 
m.addSubModel(source); 

String queryString = 
        "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> " 
        + "PREFIX wn:<http://www.webkb.org/theKB_terms.rdf/wn#> " 
        + "SELECT ?person " 
        + "WHERE {" 
        + " ?person rdf:type wn:Person . " 
        + "  }"; 

Query query = QueryFactory.create(queryString); 
QueryExecution qe = QueryExecutionFactory.create(query, m); 
ResultSet results = qe.execSelect(); 
ResultSetFormatter.out(System.out, results, query); 
qe.close(); 
+0

感謝您的幫助,我使用這個模型來加載我RDF,我要查詢,但如何在查詢運行時加載本體文件(.rdf)並將其用於推理? – Radi 2011-05-15 15:20:49

+0

查看Javadocs的'Model.read()'或'FileManager.readModel()'。就個人而言,我會使用'FileManager'路線。 – 2011-05-15 17:48:13

+0

請檢查我的編輯。謝謝 – Radi 2011-05-16 18:33:46