raeX
raeX

Reputation: 3557

SPARQL Query returns nothing from a specific RDF graph

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 ?

Upvotes: 0

Views: 452

Answers (2)

raeX
raeX

Reputation: 3557

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

Upvotes: 0

AndyS
AndyS

Reputation: 16630

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.

Upvotes: 2

Related Questions