简体   繁体   中英

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.

I have tried this code but I get an error on the .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

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.

VirtuosoQueryExecutionFactory could work only with VirtGraph / VirtModel datasource.

You must to use Jena Query Engine, if you want to execute queries on InfModel datasource.

The properly example is in Virtuoso Jena Example14 => in public static void test4() { ... }

The code from 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();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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