6

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.

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
FJE
  • 319
  • 1
  • 3
  • 11
  • 1
    possible duplicate of [Get all nodes in a transitive relation](http://stackoverflow.com/questions/4056008/get-all-nodes-in-a-transitive-relation) – RobV Dec 11 '13 at 14:07
  • 1
    I voted to close this question because as written it's a duplicate of the question you referenced since you haven't really explained how your problem is actually different or shown what you've tried i.e. did you even attempt to modify the other query to solve your problem? – RobV Dec 11 '13 at 14:09
  • @RobV I agree, and have also voted to close as duplicate. FJE, I'm glad you found the earlier answer helpful, but it's not at all clear what you're trying to do here that isn't already covered in those answers. All you're asking for is solutions to `:a :eastOf* ?mid . ?mid :eastOf* :e`, aren't you? That would bind `?m` to each of `:a`, `:c` and `:e`. – Joshua Taylor Dec 11 '13 at 17:34
  • 1
    @FJE you might find this helpful too: [Calculate length of path between nodes?](http://stackoverflow.com/q/5198889/1281433). – Joshua Taylor Dec 11 '13 at 17:39
  • @JoshuaTaylor and RobV, I edited my question. I hope the explanation is clear enough. – FJE Dec 12 '13 at 10:54
  • @FJE Ah, so you're trying to collect all the distinct paths from `:a` to `:h`, yes? I'm not sure whether you'll be able to do that (I think that you can't), but the question is definitely clearer. Thanks for the update! – Joshua Taylor Dec 12 '13 at 13:12
  • @FJE Yes new question is much clearer, removed my close vote however I tend to agree with Joshua in that I don't know that there is a pure SPARQL solution. – RobV Dec 12 '13 at 13:51

2 Answers2

1

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)?"

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • Thanks for the answer @Joshua, So it seems that it couldn't be answered by pure SPARQL then.. The choice that I think is possible is to have a function to answer this query. – FJE Dec 16 '13 at 00:39
0

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
FJE
  • 319
  • 1
  • 3
  • 11