FJE
FJE

Reputation: 329

Get all nodes in a transitive relation - Extension

This is an extension of Get all nodes in a transitive relation

Is it possible to get all transitive nodes between two transitive nodes using SPARQL? I tried to dig it out from this site and answer-semanticweb, but it seems that it's currently not possible if the length of the path is not defined as suggested in Can JENA ARQ show property path details. But I see a ray of hope from the Joshua Taylor answer to the first question.

[Edited] I provide the picture of my data sample to show the extent of the problem:

Data Sample

[Edited] I wanted to find the path between :a and :h, which should be resulted in four results:
a -> b -> c -> d -> h.
a -> b -> c -> e -> h.
a -> f -> h.
a -> g -> h.

[Edited] Using the solution from Joshua Taylor in the comment, I got every node grouped into single result, which is the closest solution that I got for this problem so far.

Upvotes: 6

Views: 284

Answers (2)

FJE
FJE

Reputation: 329

In case anyone else bumped into this question later, after sometime (almost 10 years!), there is a solution as an extension for path findings developed by OntoText (for GraphDB triplestore).

They extend the SPARQL property path with a few capabilities, e.g., searching for all paths (path:allPaths), shortest path (path:shortestPath), and distance (path:distance), where you can find it in the following link GraphDB path search.

Quoting from their website:

GraphDB solves this problem by extending SPARQL with the Graph Path Search functionality that allows you to not only find complex relationships between resources but also explore them and use them as filters to identify graph patterns. It includes features for Shortest path and All paths search, which enable you to explore the connecting edges (RDF statements) between resources for the shortest property paths and subsequently for all connecting paths. Other supported features include finding the shortest distance between resources and discovering cyclical dependencies in a graph.

Upvotes: 0

Joshua Taylor
Joshua Taylor

Reputation: 85853

I don't think this is possible in pure SPARQL. The key to the solutions to related questions is that there's something to group by that different rows of the results. In general, I don't know if there's something that would be unique to each path that you could group by so as to get one result row per path.

However, in your particular data (and this is a an attibute of your particular data, not graphs in general), there is an attribute that distinguishes the different paths: each path contains a node which is unique to that path. That is, for each of the nodes d, e, f, and g, there is exactly one path containing the node. That means that for this particular example, you might be able to do this, if you can find a way to identify the unique node in the query. I'm not confident that that's possible either, though, since it amounts to asking the same question: "how many paths are there between ?start and x (where x is one of d, e, f, and g in this case)?"

Upvotes: 1

Related Questions