简体   繁体   English

Virtuoso Jena程序中的推理程序

[英]Reasoner in Virtuoso Jena program

I am using Virtuoso Jena Provider to query my graph which is uploaded on Virtuoso but I also want to add reasoning in my queries. 我正在使用Virtuoso Jena Provider查询我上传到Virtuoso的图形,但我也想在查询中添加推理。

I have tried this code but I get an error on the .execSelect(); 我已经尝试过此代码,但是.execSelect();上出现错误.execSelect(); line 线

Exception in thread "main" java.lang.NullPointerException
    at mypackage.Main.main(Main.java:49)

Here is the code I have tried so far. 这是到目前为止我尝试过的代码。

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

What is the correct way to add a reasoner to my graph and how can I query the resulting set? 将推理机添加到图形中的正确方法是什么?如何查询结果集?

These are the versions I am using: Jena: 3.1 JDBC: 4 Virtuoso: 6 这些是我正在使用的版本:Jena:3.1 JDBC:4 Virtuoso:6

EDIT I installed Pellet reasoner from https://github.com/stardog-union/pellet but I can correctly infer only the .owl file stored on my pc and loaded on Jena but I still cannot infer the same file uploaded as a virtuoso graph. 编辑我从https://github.com/stardog-union/pellet安装了Pellet推理机,但我只能正确推断存储在我的PC上并加载到Jena上的.owl文件,但是我仍然无法推断出作为虚拟图上传的同一文件。

VirtuosoQueryExecutionFactory could work only with VirtGraph / VirtModel datasource. VirtuosoQueryExecutionFactory仅适用于VirtGraph / VirtModel数据源。

You must to use Jena Query Engine, if you want to execute queries on InfModel datasource. 如果要在InfModel数据源上执行查询,则必须使用Jena查询引擎。

The properly example is in Virtuoso Jena Example14 => in public static void test4() { ... } 正确的示例在Virtuoso Jena Example14 =>中的public static void test4() { ... }

The code from 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