I want all the triples in the graph from a specific subject / node.
I can get all the triples...
SELECT $s $p $o WHERE { $s $p $o }
... not really helpful
I can get all the directly connect triples...
SELECT $s $p $o WHERE {
$s _some_predicate_ _some_object_specifier .
$s $p $o }
What I want is to start at $s (selected by predicate and object)
and then get
something like $s $p* $o
Thoughts?
Graph
So I'd like to start at ID_6 and get all node below that including the nodes 1 and 2. I also need the predicates because not all the predicates will be HAS
So more specifically using the graph below I want all the triples below object "P2" I need the subject, predicate and object for each of the 1xx and 2xx nodes... I can recursively query each node to get the data but I'm hoping there is a simple (ish) query to get all the data in a single query...
More Details
There is a way to do this, but whether it is "simple" is somewhat subjective.
Property paths allow you to query paths of unknown length. However, you can only specify such arbitrary-length property paths on specific property names (so not on property variables).
A way around this is to use a negation, like so:
:P3 (:overrides|!:overrides)+ ?child
This will gives you back all "children" connected via paths of length 1 or longer where the connecting property is either :overrides
or NOT :overrides
(which is, of course, true for every property).
However, using property paths, you can only match these properties to get their values back, you can't get actually get the property names themselves back. So we will need to make our query a bit more extensive and just query for everything connected to both our original subject (:P3
) as well as to all possible "child" subjects (?child
).
The full query then becomes:
CONSTRUCT {
:P3 ?prop ?val .
?child ?childProp ?childPropVal .
?someSubj ?incomingChildProp ?child .
}
WHERE {
:P3 ?prop ?val ;
(:overrides|!:overrides)+ ?child .
?child ?childProp ?childPropVal.
?someSubj ?incomingChildProp ?child.
}
The reason this works is that we match and return both incoming and outgoing properties for every child along the way: so for a child connected to :P3
via a path of length 2, we get the first component of the path back because :P3 ?prop ?val
matches it, and the last part because ?someSubj ?incomingChildProp ?child
matches it. For a path of length 3, the middle part of the path will be matched because it was already retrieved by ?child ?childProp ?childPropVal
when the path was still at length 2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With