我正在使用virtuoso jena提供商查询我的图形,该图形已上传在Virtuoso上,但我也想在查询中添加推理。
我尝试了此代码,但是我在.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/>rn" +
"PREFIX ex: <http://example.org/data/>rn" +
"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();
将推理器添加到图形的正确方法是什么?如何查询结果集?
这些是我正在使用的版本:耶拿:3.1JDBC:4Virtuoso:6
编辑我从https://github.com/stardog-union/pellet安装了Pellet推理器,但是我只能正确推断存储在PC上并加载在Jena上的.OWL文件,但我仍然无法推断出将同一文件上传到virtuoso图。
VirtuosoQueryExecutionFactory
只能与 VirtGraph
/ VirtModel
datasource一起使用。
如果要在InfModel
DataSource上执行查询,则必须使用Jena查询引擎。
正确的示例是在 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();
}
}