繁体   English   中英

Virtuoso Jena程序中的推理程序

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM