Search code examples
sparqlsemantic-weblinked-datapropertypath

Is it possible to use variables as integers in SPARQL property paths?


I am currently trying to create pointers to datatype values as they cannot be linked directly. However, I would like to be able to evaluate the pointers from within the SPARQL environment, which raised specifically in the case that the desired value is part of an ordered rdf:List some questions for me. My approach is to use property paths within a SPARQL query in which I can use the defined individual, property and index of the ordered list that the pointer has attached to it.

Given the following example data with the shortened syntax for ordered lists by ttl:

 ex:myObject ex:somePropery ("1" "2" "3") .
 ex:myPointer ex:lookAtIndividual ex:myObject;
   ex:lookAtProperty ex:someProperty ;
   ex:lookAtIndex "3"^^xsd:integer .

Now I would like to create a SPARQL query that -- based on the pointer -- returns the value at the given index. To my understanding the query could/should look something like this:

 SELECT ?value
 WHERE {
   ex:myPointer ex:lookAtIndividual ?individual ;
      ex:lookAtProperty ?prop ;
      ex:lookAtIndex ?index .
   ?individual ?prop/rdf:rest{?index-1}/rdf:first ?value .
}

But if I try to execute this query with TopBraid, it shows an error message that ?index has been found when <INTEGER> was expected. I also tried binding the index in the SPARQL query via BIND(?index-1 AS ?i), again without success. If the pointed value is not stored in a list, the query without property path works fine.

Is it in general possible to use a value that is connected via datatype property within a SPARQL query as path length for property paths?


Solution

  • This syntax: rdf:rest{<number>} is not standard SPARQL. So the short answer is, regrettably: no, you can't use variables as integers in SPARQL property paths, for the simple reason that you can't use integers in SPARQL property paths at all.

    In an earlier draft of the SPARQL standard, there was a proposal to use this kind of syntax to allow specifying the min and max length of a property path, e.g. rdf:rest{1, 3} would match any paths using rdf:rest properties between length 1 and 3. But this was never fully standardized and most SPARQL engines don't implement it.

    If you happen to use a SPARQL engine that does implement it, you will have to get in touch with the developers directly to ask if they can extend the mechanism to allow use of variables in this position (the error message suggests to me that it's currently just not possible).

    As an aside: there's a SPARQL 1.2 community initiative going on. It only just got started but one of the proposals on the table is re-introducing this particular piece of functionality to the standard.