1

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.

Stelios Botonakis
  • 183
  • 1
  • 3
  • 11
  • First thought, because these bite many people, is to confirm the version of everything involved -- [Jena Provider, JDBC Driver](http://vos.openlinksw.com/owiki/wiki/VOS/VOSDownload#Jena%20Provider), Jena, Java, Virtuoso, etc. -- is up-to-date and appropriately version-matched. Then, take a look at [this sample query](http://vos.openlinksw.com/owiki/wiki/VOS/VirtJenaSPARQLExample14) from the [Virtuoso Jena Provider docs](http://vos.openlinksw.com/owiki/wiki/VOS/VirtJenaProvider). – TallTed May 08 '19 at 14:50

1 Answers1

0

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

TallTed
  • 9,069
  • 2
  • 22
  • 37
  • The problem is that my triples are in the graph and I can access them with VirtGraph I cant access them with OntModel/InfModel. When i use the example you pointed I get an empty set. ``` ===========[Data in DB]========== Exec: select * where {?s ?p ?o} ------------- | s | p | o | ============= ------------- ============================ ``` – Stelios Botonakis May 10 '19 at 09:50
  • Also in the examples you pointed there is the variable _ruleset that is being used. How am I supposed to create it. Isn't the point to use an reasoner like Pellet that infers on its own? – Stelios Botonakis May 10 '19 at 09:54
  • Are you sure, that you checked the example from link above? Where did you found var `_ruleset` in the example? The var `_ruleset` us used an another example. – Sergey Malinin May 14 '19 at 01:01
  • **1.** when you use `VirtuosoQueryExecutionFactory`, the queries are executed in server side with data stored in VirtuosoDB. **2.** when you use Jena Query Engine the queries are executed in client side and Jena Query engine could use all data from client side and from server side, but it will work slower. **3.** Jena stored the inference related triples (for OntModel/InfModel) only in the memory on client side by default. – Sergey Malinin May 14 '19 at 01:31
  • What is this ruleset var that is used. How am I supposed to generate it. I built my ontology on protege. – Stelios Botonakis May 19 '19 at 15:56
  • Where did you see `ruleset` var in the example above ? – Sergey Malinin May 20 '19 at 16:15
  • You are right I am sorry ```ruleset``` is used in example 13 but still I am wondering how to create it. Also some other questions. What is the difference between VirtuosoDB data and a graph stored on virtuoso(which is the .owl file I uploaded). Secondly, how can I access the inferred triples that you mention - through OntoModel/Model?- I want to be able to SPARQL query the VirtGraph from Jena but with inferences. Lastly, I added pellet reasoner from this link in Github https://github.com/stardog-union/pellet but still I didnt manage anything – Stelios Botonakis May 20 '19 at 16:30