0

After creating an RDF graph using RDFLib in python to apply a sensor ontology ( I used for that a sensor ontology, used also namespace and Bnode which is a blank node representing a resource for which a URI or literal is not given). I tried to query the data in java using sparql therefore I had to store the graph using Jena TDB first then I executed a really simple query which is :

String qs1 = "SELECT * {?s ?p ?o} LIMIT 10" ;

and I used

String source = "/path/graph.rdf";
        FileManager.get().readModel( tdb, source);
         dataset.begin(ReadWrite.READ) ;
         String qs1 = "SELECT * {?s ?o ?p } " ;

     try(QueryExecution qExec = QueryExecutionFactory.create(qs1, dataset)) {
            ResultSet rs = qExec.execSelect() ;
             ResultSetFormatter.outputAsJSON(rs) ;
     }` 

to execute the query and observe the data in json format. The problem I m facing is that it returns nothing! this is the output:

{
  "head": {
    "vars": [ "s" , "o" , "p" ]
  } ,
  "results": {
    "bindings": [

    ]
  }
}

I made a simple code to verify if the data are stored :

StmtIterator iter = tdb.listStatements();
        // print out the predicate, subject and object of each statement
        while (iter.hasNext()) {
            Statement stmt      = iter.nextStatement();  // get next statement
            Resource  subject   = stmt.getSubject();     // get the subject
            Property  predicate = stmt.getPredicate();   // get the predicate
            RDFNode   object    = stmt.getObject();      // get the object

            System.out.print(subject.toString());
            System.out.print("     " + predicate.toString() + "               ");
            if (object instanceof Resource) {
               System.out.print(object.toString());
            } else {
                // object is a literal
                System.out.print(" \"" + object.toString() + "\"");
            }

            System.out.println(" .");
        } 

and indeed they are stored on the TDB database. This is some of the output, which include a bizarres representation of the Bnode and according to some articles its the way the TDB react with Bnode which makes it looks like that.

6f98bd70:1543430b66e:-7fc3     http://www.loa-cnr.it/ontologies/DUL.owl#hasDataValue                "37^^file:///data/rbe/workspace/openmtc-python/openmtc-gevent/xsd.float" .
-6f98bd70:1543430b66e:-7fc2     http://purl.oclc.org/NET/UNIS/fiware/iot-lite#hasunit               http://purl.oclc.org/NET/ssnx/qu/unit#hPa .
-6f98bd70:1543430b66e:-7fc2     http://www.loa-cnr.it/ontologies/DUL.owl#hasDataValue                "996.94^^file:///data/rbe/workspace/openmtc-python/openmtc-gevent/xsd.float" .
-6f98bd70:1543430b66e:-7fc1     http://purl.oclc.org/NET/UNIS/fiware/iot-lite#hasunit               http://purl.oclc.org/NET/ssnx/qu/unit# .
-6f98bd70:1543430b66e:-7fc1     http://www.loa-cnr.it/ontologies/DUL.owl#hasDataValue                "OK^^file:///data/rbe/workspace/openmtc-python/openmtc-gevent/xsd.float" .
-6f98bd70:1543430b66e:-7fc0     http://purl.oclc.org/NET/UNIS/fiware/iot-lite#hasunit               http://purl.oclc.org/NET/ssnx/qu/unit#C .
-6f98bd70:1543430b66e:-7fc0     http://www.loa-cnr.it/ontologies/DUL.owl#hasDataValue                "24.2^^file:///data/rbe/workspace/openmtc-python/openmtc-gevent/xsd.float" .

I also tried another graph which uses the friend of a friend ontology and it works fine and correctly. is it possible that the Bnode is causing this issue ?

raeX
  • 3,197
  • 2
  • 14
  • 21
  • Two questions: (i) Are you sure that the query is being executed against the correct graph? (ii) You show us one use of the result set, `rs`, but are you doing anything with it before that? ResultSets, by default, only allow you access to the results once. – Joshua Taylor Apr 20 '16 at 15:37
  • @Joshua I edited my question with the code where i used the ResultSets. I actually saved two different graphs in TDB one with the FOAF and the one I created and each time i execute the query it gets me back data from the FOAF graph therefore I changed the dataset directory to another path so it will store only one graph. – raeX Apr 20 '16 at 16:02
  • I don'[t have the docs in front of me, but I think that `FileManager.get().readModel( tdb, source);` reads the content into a model `tdb`, but then you use `dataset` for your query. Is `tdb` actually a model in the `dataset`? – Joshua Taylor Apr 20 '16 at 16:12
  • I used this before executing the query : `Model tdb = dataset.getNamedModel("I used here a random name")` the same code i used with different graph and i got the results, maybe it is due to the way I created the graph in RDFLib ? – raeX Apr 20 '16 at 16:27

2 Answers2

2

try: SELECT * { { ?s ?p ?o } UNION { GRAPH ?g { ?s ?p ?o } } }

Your comments suggest the data is in a named graph but you asked the query of the unnamed/default graph only. The query suggested finds everything, anywhere in the dataset.

AndyS
  • 16,345
  • 17
  • 21
  • is ther any way to specify the graph in a way that i dont have to use the union part ? – raeX Apr 22 '16 at 17:59
  • Once you know where the graph is, you can use: `GRAPH { ... pattern... }`. But beware that is a legal, full URI, not some short name (short names get resolved on parsing, to become full URIs). – AndyS Apr 23 '16 at 09:04
0

As @AndyS montionned the query suggested works fine. In case you don't want to use the union part just do as Andy suggested by adding the name of the graph you need. It should be like this :

QueryExecution qExec = QueryExecutionFactory.create(qs1, YourGraphNameHERE));
            ResultSet rs = qExec.execSelect() ;
             ResultSetFormatter.outputAsJSON(rs) ;
raeX
  • 3,197
  • 2
  • 14
  • 21