簡體   English   中英

Sparql查詢與推理

[英]Sparql Query With Inferencing

我有一些rdf和rdfs文件,我想使用jena 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();

我在rdf文件中有一個wordNet本體,我想在我的查詢中使用這個本體自動進行推理(當我查詢查詢的人應該返回例如。男人,女人)那么如何將本體鏈接到我的查詢? 請幫我。

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

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

提前致謝。

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

雖然您可以直接創建推理模型 ,但創建具有所需推理支持度OntModel通常最簡單:

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

如果您需要不同的推理器或OWL支持,則可以選擇不同的OntModelSpec常量。 請注意,大型和/或復雜模型可能會導致查詢速度變慢。

更新 (編輯原始問題后)

要推理兩個模型,你需要聯合。 你可以通過OntModel的子模型實現這一點。 我會改變你的例子如下(注意:我沒有測試過這段代碼,但它應該可以工作):

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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM