Search code examples
javardfsparqljenaontology

Datatype dataTime insertion in ontology with SPARQL queries


I am manually trying to insert the data in owl file with following SPARQL command:

qry = "PREFIX : <http://www.example.com/tempsensor#>" + 
       "INSERT DATA" +
           "{" + 
 ":ind1 :locatedIn :Delhi ;" + ":onDate "+ "2014-10-01T00:10:10"^^xsd:dateTime +" ;" + ":measures 13 ;" + " :hasUnit Celsius   ." + "}" ;
        UpdateAction.parseExecute(qry,ontmod);

On running, I am getting exception:

Encountered " <INTEGER> "10 "" at line 1, column 96. Was expecting one of:
"graph" ...
"}" ...
";" ...
"," ...
"." ...
at com.hp.hpl.jena.sparql.lang.ParserSPARQL11Update._parse(ParserSPARQL11Update.java:78)

How should I format datetime so that sparql will allow to execuate the query. Ontology used is give at link.


Solution

  • If you print your created query string you should quickly be able to see that it is invalid i.e.

    System.out.println(qry);
    

    The problem is that you haven't put quotes around your date time constant as is required for literals in SPARQL.

    So your update needs to look more like this:

    qry = "PREFIX : <http://www.example.com/tempsensor#>\n" + 
          "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n" +
          "INSERT DATA\n" +
          "{\n" + 
          ":ind1 :locatedIn :Delhi ;\n" + 
          ":onDate \"2014-10-01T00:10:10\"^^xsd:dateTime ;\n" + 
          ":measures 13 ;" + " :hasUnit Celsius .\n" + 
          "}" ;
    

    Note the need to use \" to escape the quotes so Java doesn't interpret them as the start/end of a string.

    I also added \n i.e. newlines into your string as this will help the parser give you a more meaningful error message with a more precise error location than the line 1, column 96 you get with the existing query.

    Generally if you need to inject constants into a query/update you are better off using the Parameterized SPARQL String support in Jena which is considerably less error prone and not vulnerable to SPARQL injection as your current approach is.