2

I need all years in a given range as values in sparql (especially for the sesame implementation).

Is there any way to "generate" new numbers in sparql like:

SELECT (RANGE(2,10) AS ?numbers)
WHERE{}
Raphael Schumann
  • 162
  • 1
  • 10
  • There's nothing like that built into the language, though you could check whether your implementation has support for additional functions. What exactly would you want ?numbers to be bound to, anyhow? Something like `select ?year where { ?year ex:inRange [ ex:min 2 ; ex:max 10 ] }` could be supported with a property function in Jena, I think. – Joshua Taylor Jun 18 '15 at 21:45
  • i took a look on how to create custom functions in sesame ( http://www.rivuli-development.com/further-reading/sesame-cookbook/creating-custom-sparql-functions/ ), but i think you can only return one value – Raphael Schumann Jun 18 '15 at 21:55
  • @RaphaelSchumann well, yes, but it is pretty much the _definition_ of a function that it returns a single value :) Can you elaborate a bit on your use case? There may be an alternative way to achieve what you need. – Jeen Broekstra Jun 18 '15 at 23:28
  • i have a min year and a max year and need all years in between as values – Raphael Schumann Jun 19 '15 at 01:07
  • I got that much already. Why do you need this? What are you trying to achieve? – Jeen Broekstra Jun 19 '15 at 02:10
  • @JeenBroekstra I think there's something like this in Jena's property functions. It's kind of like Prolog, in that a property can act like a generator. E.g., with a pattern like `?s ex:specialProperty ?o`, the property function can look at the current bindings of ?s and ?o and either check (return a boolean) or return candidate values for ?s or ?o. Kind of like some of the text indexing properties in other endpoints. `?s bif:matches "foo"` (I don't remember which implementations or exact syntax) can check whether ?s matches "foo". It could be handy to have something like – Joshua Taylor Jun 19 '15 at 11:23
  • 1
    `?x ex:in [ ex:min 10 ; ex:max 30 ]` and get ?x bound to each value between 10 and 30. The problem is that this pretty quickly gives the user a whole lot of programming power (essentially iteration), so it's kind of a dangerous feature for a query language. – Joshua Taylor Jun 19 '15 at 11:25
  • @JoshuaTaylor it's certainly possible to add these kinds of property functions to Sesame, by way of a custom SAIL layer, but it takes quite a lot of programming and there's no Howto or step-by-step-guide. I'll have a think about this and see if we can provide something a little easier to customize. – Jeen Broekstra Jun 19 '15 at 20:35
  • However, for this particular case I feel there are better solutions than adding some custom function to SPARQL. If the OP would explain _why_ he needs this, we might be able to recommend alternatives. – Jeen Broekstra Jun 19 '15 at 21:16
  • I will explain it in detail, but I don't have time these days. Thx for the answers so far – Raphael Schumann Jun 20 '15 at 01:17

1 Answers1

0

Without taking a deeper look, one naive answer in the spirit of RDF is to materialize the numbers and then use FILTER for the range. In other words, just insert them into the graph:

  ex:year ex:anumber "1900"^^xsd:integer .
  ex:year ex:anumber "1901"^^xsd:integer .
  ...  
  ex:year ex:anumber "2100"^^xsd:integer .

And have a pattern in the WHERE section:

  SELECT ?number
  WHERE {
      ex:year ex:number ?number .
      FILTER (?number < 2020)
      FILTER (?number > 1999)
  }

As commented above, it is hard to imagine a use case for "knowing" numbers: what you get will be cross-product with everything else in the query, unless getting numbers is the only purpose of the query or some filtering being applied (in the latter case there is no need to generate numbers at all).

Roman Susi
  • 4,135
  • 2
  • 32
  • 47