antoniogarrote / rdfstore-js

JS RDF store with SPARQL support
MIT License
564 stars 109 forks source link

OPTIONAL construct triggers javascript error #101

Open Public-Health-Bioinformatics opened 9 years ago

Public-Health-Bioinformatics commented 9 years ago

When running this query which apparently has an OPTIONAL expression that doesn't match anything...

  var ifm_hasDataType = ' <http://www.semanticweb.org/damion/ontologies/IFM_ontology#hasDataType> '
  var ifm_interfaceLabel = ' <http://www.semanticweb.org/damion/ontologies/IFM_ontology#interfaceLabel> '      
  var owl_Restriction = ' <http://www.w3.org/2002/07/owl#Restriction> '

  var sparql = 'PREFIX ifm:<http://www.semanticweb.org/damion/ontologies/IFM_ontology#>\
  SELECT ?datumId ?myDataType ?itemLabel\
   FROM NAMED <test> { GRAPH <test> { \
    ?datumId rdfs:subClassOf ?restriction.\
    ?restriction rdf:type '+owl_Restriction+'.\
    ?restriction owl:onProperty '+ifm_hasDataType+'.\
    ?restriction owl:onClass ?myDataType.\
     OPTIONAL {?datumId '+ifm_interfaceLabel +' ?itemLabel}\
}} LIMIT 100'

Ran into this error:

/Users/damion/Documents/IRIDA/node_js/node_modules/rdfstore/src/parser.js:865 for(var j=0; j<ts.triplesContext.length; j++) { ^ TypeError: Cannot read property 'length' of undefined at peg$consts.type (/Users/damion/Documents/IRIDA/node_js/node_modules/rdfstore/src/parser.js:865:51) at peg$parseRule (/Users/damion/Documents/IRIDA/node_js/node_modules/rdfstore/src/parser.js:3077:40) at peg$parseRule ... [Finished in 0.4s with exit code 1]

A possible error trap of the situation I tried: line 864 of parser.js - adding test for ts.triplesContext existence. Not sure if this is appropriate:

              if (ts.triplesContext)
                      for(var j=0; j<ts.triplesContext.length; j++) {
                          var triple = ts.triplesContext[j]
                          triple.graph = g;
                      }
Public-Health-Bioinformatics commented 9 years ago

I should add - this is happening server-side with node.js . I'm not familiar enough with node.js yet to know if there are differences in javascript behaviour browser side vs. server side aside from file-system access.
If there is anything I/others can do to encourage development on this project - it could be worth a page on wiki to describe. Regards, Damion

gertvv commented 9 years ago

I also encountered this issue. The parser breaks when the OPTIONAL {} is nested inside a GRAPH ?g {} block. So the query

SELECT * WHERE { GRAPH ?g { ?s ?p ?o . OPTIONAL { ?o a ?thing . } } }

fails to parse, whereas the equivalent

SELECT * WHERE { GRAPH ?g { ?s ?p ?o . } OPTIONAL { GRAPH ?g { ?o a ?thing . } } }

is fine.