I have a rather advanced question and there may not be a general solution, but 
I'll try anyway...

Problem: Given an arbitrary SPARQL WHERE clause, I want to insert a line

        ?this a ?SOME_TYPE .

so that it always gets executed "first" - I want ?this to be bound to all 
instances of a given class before the rest of the WHERE clause executes. For 
example if I have 

WHERE {
    {
        BIND (ex:fun(?this) AS ?result) .
    }
    FILTER (?result != 1)
}

then I want to programmatically rewrite this to

WHERE {
    {
        ?this a ?SOME_TYPE .
        BIND (ex:fun(?this) AS ?result) .
    }
    FILTER (?result != 1)
}

Is there any clean way of implementing this? My naive approach would be to walk 
the open parentheses until I find the first one with a matching closing 
parenthesis (or do the equivalent operation on the Syntax tree instead of 
strings. Given the policy that SPARQL is executed from the inside out, is this 
a safe assumption? Are there alternative approaches to achieving the goal (of 
iterating over all instances of a class)? Are there cases where I need to 
repeat the binding of ?this in multiple sub-blocks? Or will it be more 
efficient to do the iteration "outside" and simply ask the same query for each 
instance, with ?this pre-bound with initial bindings?

Thanks a lot
Holger

Reply via email to