I’m using SPARQL for RDF and am having trouble coming up with a query that will allow me to select a set of vertices on a tree, and have all connected (direct and transitive) vertices to those selected be returned, and in the order of depth that they exist on the dependency tree (deepest vertices first).
Here is a visual representation of the graph (tree) I’m working with, which contains nodes of RDF type :dependency
. In this tree, dependencies have outgoing edges to each dependency that they require, and this is represented on the graph using a :requires
edge.
Here is the data that creates this graph:
INSERT DATA {
:A rdf:type :dependency .
:B rdf:type :dependency .
:C rdf:type :dependency .
:D rdf:type :dependency .
:E rdf:type :dependency .
:F rdf:type :dependency .
:G rdf:type :dependency .
:H rdf:type :dependency .
:I rdf:type :dependency .
:J rdf:type :dependency .
:K rdf:type :dependency .
:A :requires :B .
:A :requires :C .
:A :requires :I .
:B :requires :J .
:B :requires :E .
:C :requires :D .
:E :requires :F .
:E :requires :G .
:D :requires :G .
:H :requires :I .
:H :requires :K .
}
I would like to form a query that will allow me to select a set of dependencies, and return all dependencies of that selection in an order in which the lowest :required
dependencies are returned first, such that if "Dependency X" :requires "Dependency Y", then Dependency X must come after Dependency Y in the result set.
Essentially, I want to ask the graph:
"Give me all vertices that are required by this set of vertices, and return them in the order so that the lowest level dependencies are prioritized first on the result set"
For example, in the case of specifying this selection on the above graph: [B, H]
A valid result set would be:
[G, F, E, J, B, I, K, H]
(Since G
and F
are the lowest dependencies of B
, followed by E
, J
and so on…)
The following result set would be also considered valid:
[J, F, G, E, B, I, K, H]
since it does not matter whether J
is returned before or after F
, G
, and H
as it doesn't depend on them (all that matters is it is returned before B
, since B
requires all of them)
So far, I have attempted this query for the selection [B, H]
, which is able to return all required dependencies of B
and H
, but they are not returned in the order of lowest dependency priority.
SELECT ?requires {
?dependency a :dependency .
FILTER (?dependency IN (:B, :H))
?dependency :requires+ ?requires .
}
which returns the result set in the incorrect order:
[E, F, G, J, I, K]
Anyone have any ideas how I could construct a query that essentially performs a depth-first search ordering of the vertices?