[英]Reasoner in Virtuoso Jena program
我正在使用Virtuoso Jena Provider查詢我上傳到Virtuoso的圖形,但我也想在查詢中添加推理。
我已經嘗試過此代碼,但是.execSelect();
上出現錯誤.execSelect();
線
Exception in thread "main" java.lang.NullPointerException
at mypackage.Main.main(Main.java:49)
這是到目前為止我嘗試過的代碼。
VirtGraph vg = new VirtGraph(graph, url, username, password);
VirtModel model = new VirtModel(vg);
InfModel ont = ModelFactory.createInfModel(ReasonerRegistry.getOWLReasoner(), model);
Query sparql = QueryFactory.create("PREFIX sosa: <http://www.w3.org/ns/sosa/>\r\n" +
"PREFIX ex: <http://example.org/data/>\r\n" +
"SELECT ?s ?o FROM <http://147.27.60.65/sensorOntology> WHERE {?s sosa:isHostedBy ?o}");
QueryExecution vqe = VirtuosoQueryExecutionFactory.create(sparql, ont);
ResultSet results = vqe.execSelect();
將推理機添加到圖形中的正確方法是什么?如何查詢結果集?
這些是我正在使用的版本:Jena:3.1 JDBC:4 Virtuoso:6
編輯我從https://github.com/stardog-union/pellet安裝了Pellet推理機,但我只能正確推斷存儲在我的PC上並加載到Jena上的.owl文件,但是我仍然無法推斷出作為虛擬圖上傳的同一文件。
VirtuosoQueryExecutionFactory
僅適用於VirtGraph
/ VirtModel
數據源。
如果要在InfModel
數據源上執行查詢,則必須使用Jena查詢引擎。
正確的示例在Virtuoso Jena Example14 =>中的public static void test4() { ... }
來自test4()
的代碼
...
public static void exec_select(String header, Model m, String query) {
String h = header==null?"":header;
System.out.println("===========["+h+"]==========");
System.out.println("Exec: "+ query);
Query jquery = QueryFactory.create(query) ;
QueryExecution qexec = QueryExecutionFactory.create(jquery, m) ;
ResultSet results = qexec.execSelect();
ResultSetFormatter.out(System.out, results, jquery);
qexec.close();
System.out.println("============================\n");
}
...
public static void test4() {
try {
System.out.println("--------------- TEST 4 -------------------");
VirtModel vdata = VirtModel.openDatabaseModel("test:inf4", URL, uid, pwd);
vdata.removeAll();
String NS = PrintUtil.egNS;
Resource c1 = vdata.createResource(NS + "C1");
Resource c2 = vdata.createResource(NS + "C2");
Resource c3 = vdata.createResource(NS + "C3");
vdata.add(c2, RDFS.subClassOf, c3);
vdata.add(c1, RDFS.subClassOf, c2);
OntModel om = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_RDFS_INF, vdata);
exec_select("Data in DB", vdata, "select * where {?s ?p ?o}");
exec_select("Data in Ontology Model", om, "select * where {?s ?p ?o}");
exec_select("Data in Ontology", om, "select * where {<"+c1+"> <"+RDFS.subClassOf+"> ?o}");
} catch (Exception e) {
System.out.println("ERROR Test Failed.");
e.printStackTrace();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.