views:

33

answers:

2

Is it somehow possible to get the list of nodes connected through a transient relation with SPARQL?

I have elements that are connected in this way:

?a g:eastOf ?b
?b g:eastOf ?c
...

Not all nodes are connected with each other, because some are further down south. Only when nodes are vertically at the same plain there might be a g:eastOf relationship. This means there are several groups of nodes that are not connected with each other.

So now I'd like to get all these groups of nodes (basically a list of lists). Is there a way to do that in SPARQL? Because in the SELECT statement you have to list a finite number of variables and can't somehow express "a list of" something.

Also I'm only interested in the lists where a xsd:integer property of all nodes is ascending when going from west to east. But that should be relatively easy after I have the solution for my first problem.

A: 

OpenLink Virtuoso supports such thing: Transitivity in SPARQL This requires custom syntax in SPARQL queries.

Pēteris Caune
Unfortunately I have to use Jena.
panzi
+1  A: 

AFAIK you can't get a variable list of elements as an SPARQL solution. I think the easiest way to do what you need is just to get the pairs (a,b) from ?a g:eastOf ?b and sort them.

SELECT ?a ?b WHERE {
?a g:eastOf ?b
} ORDER BY ASC(?a) ASC(?b)

It should be pretty straight forward to put some logic in place to convert the list of tuples on a list of elements.

If you use reasoning with transitivity you won't be able to trace back what's the path that connects A with C. In case you have something like (A,g:eastOf,B) and (B,g:eastOf,C). Basically in this case using reasoning will make the things more complicated because you want to construct the full list of connected elements. Transitivity in SPARQL will give you the start and end node but you'll loose traceability of all the intermediate points.

If you don't have millions of statements I think that the above SPARQL query plus some logic to transform the resultset will do the job.

Edited to give pointers to Jena + owl:TransitiveProperty

So first make sure that you load an ontology with the following axiom into your Jena Model:

g:eastOf rdf:type owl:TransitiveProperty .

Then enable the Jena OWL reasoner see documentation here : Jena OWL coverage

And connect Jena ARQ to your model so to be able to launch SPARQL queries.

Once you've done that ... if you had something like ...

:x g:eastOf :y .
:y g:eastOf :t .
:t g:eastOf :z .

and you run a query like ...

SELECT ?a ?b WHERE {
?a g:eastOf ?b
} ORDER BY ASC(?a) ASC(?b)

you'll get ...

:x g:eastOf :y .
:x g:eastOf :t .
:x g:eastOf :z .
:y g:eastOf :t .
:y g:eastOf :z .
(...)

Also with Jena consider to use Property Paths. It'll do the job as well.

Let us know if its works or if you run into any issues, cheers !!!

msalvadores
"Transitivity in SPARQL will give you the start and end node but you'll loose traceability of all the intermediate points." Well this might be good enough because all nodes also have x and y coordinates and I could just sort them.
panzi
@panzi fair enough if Transitivity does the job and you have to use Jena then ... question edited to give you some pointers for that.
msalvadores
Property paths are part of sparql 1.1, btw. http://www.w3.org/TR/sparql11-property-paths/