1

I want to extract a chain of instances between two instances of my ontology by asking a SPARQL query. for example in the following figure if I want to know how A is connected to E, the result of query should be something like a list of A, B, D, F, E.

enter image description here

how the ontology should be designed and query should be built? Is it even possible?

msc87
  • 823
  • 3
  • 16
  • 32

1 Answers1

5

This isn't too hard. In RDF, your data can be something as simple as a direct encoding of the graph:

@prefix : <urn:ex:>

:A :connectedTo :B .
:B :connectedTo :C, :D .
:D :connectedTo :F .
:F :connectedTo :E, :G .

Then, using SPARQL property paths, you can find every node such that there's a path of connectedTo properties from A to it and from it to E, including A and E themselves:

prefix : <urn:ex:>

select ?mid where {
  :A :connectedTo* ?mid .
  ?mid :connectedTo* :E .
}
-------
| mid |
=======
| :D  |
| :F  |
| :B  |
| :A  |
| :E  |
-------

If you want to get those in order, you can additionally count how many things are between A and the "mid-node". (This is described in my answer to Is it possible to get the position of an element in an RDF Collection in SPARQL?)

prefix : <urn:ex:>

select ?mid (count(?premid) as ?i) where {
  :A :connectedTo* ?premid .
  ?premid :connectedTo* ?mid .
  ?mid :connectedTo* :E .
}
group by ?mid

-----------
| mid | i |
===========
| :D  | 3 |
| :F  | 4 |
| :E  | 5 |
| :B  | 2 |
| :A  | 1 |
-----------

If you actually want a single result that looks more or less like "A, B, C, D, E, F", then you adapt these queries using the techniques from my answer to Aggregating results from SPARQL query, which shows how to concatenate these into a single string.

Community
  • 1
  • 1
Joshua Taylor
  • 80,876
  • 9
  • 135
  • 306
  • your answer was correct but I added a bit more detailed which is more complex. – msc87 Aug 11 '15 at 14:15
  • I could not implement the query as you suggested. Maybe because, my connetedTo is not directed. if A is connected to B then B is connected to A too. – msc87 Aug 11 '15 at 15:57
  • 3
    If the links are undirected, then you won't be able to do this with a property path; since there's no way to specify the *length* of a property path, you can't distinguish between a path like A, B, A, C, D and the shorter version A, C, D. As an aside, it's not a great practice to significantly change a Stack Overflow question; it invalidates existing answers, leads to confusion, and if the question is really significantly different, it's usually worth asking as a new question. – Joshua Taylor Aug 11 '15 at 17:17
  • you are right, I should not.. I will remove the new part. I can ask it as a separate question but I wonder if it is solvable or not. – msc87 Aug 11 '15 at 17:43
  • @msc87 As I said, "there's no way to specify the length of a property path, you can't distinguish between a path like A, B, A, C, D and the shorter version A, C, D." With SPARQL, you won't have a way to make sure you stay on a shortest path, just that there is *a* path. – Joshua Taylor Aug 11 '15 at 17:51