The problem is how to calculate the distance between two nodes (concepts) in a Triple Store (RDF) using sparql queries without know the type of edges. Essencially, is to use Dij
You can use the same technique that's used in Calculate length of path between nodes?, but you'll need to use a wildcard instead of a particular property. The pattern (<>|!<>)
is a wildcard, because every property is either <>
or it isn't. You could also use (:|!:)
, but that will only work if you have a :
prefix defined. (<>|!<>)
will always work. Here's an example:
@prefix : <urn:ex:>
:a :p :b .
:b :q :c .
:c :r :d .
:d :s :e .
prefix : <urn:ex:>
select ?start ?end (count(?mid) as ?length) {
?start (<>|!<>)* ?mid .
?mid (<>|!<>)+ ?end .
}
group by ?start ?end
------------------------
| start | end | length |
========================
| :a | :b | 1 |
| :a | :c | 2 |
| :a | :d | 3 |
| :a | :e | 4 |
| :b | :c | 1 |
| :b | :d | 2 |
| :b | :e | 3 |
| :c | :d | 1 |
| :c | :e | 2 |
| :d | :e | 1 |
------------------------