rdf - How to extract nodes that occur as both subject and object in a graph? -
i want retrieve list of nodes(vertices in graph) occur both in subject , object part of triple(not same triple).
i tried doing using sub query follows:
select ?x { ?x ?p ?o. { select ?x { ?s ?p ?x . } } }
it not giving me exact results in sense getting multiple instances of node instance. , when tried distinct, gave more instances reason.
on side note, if wanted extract nodes subject or object, how should go doing that?
please excuse if there mistakes in vocabulary used.
nodes subjects , objects short , sweet
just inquire appears subject , object:
select distinct ?x { ?s1 ?p1 ?x . ?x ?p2 ?o2 . }
making illegible (just fun) if want create bit shorter, much less readable, can utilize
prefix : <...anything...> select distinct ?x { ?x (:|!:) ?o ; ^(:|!:) ?s . }
the pattern (:|!:)
matches property either :
or not :
. means matches everything; it's wildcard. (you utilize ?p
wildcard, too, maintain reading…) path ^p
means p, in reverse direction (so, e.g., ?person foaf:name ?name
, ?name ^foaf:name ?person
match same data. since (:|!:)
wildcard, ^(:|!:)
wildcard in reverse direction. can't utilize variables in property paths, though ?p
"forward wildcard", can't utilize ^?p
"backward wildcard". ;
notation lets abbreviate, e.g., ?x :p2 :o1
, ?x :p2 :o2
?x :p1 :o1 ; :p2 :o2
. using here, can get:
?x (:|!:) ?o ; # every ?x subject ^(:|!:) ?s . # every ?x object
removing comments , linebreaks, get
?x (:|!:) ?o ; ^(:|!:) ?s .
you should utilize readable one. :)
nodes subjects or objectsthis answered in previous question computing node degree, how calculate maximum grade of directed graph using sparql?. reply there used query compute degree:
select ?x (count(*) ?degree) { { ?x ?p ?o } union { ?s ?p ?x } } grouping ?x
it can find nodes subjects or objects, too, though. alter to:
select distinct ?x { { ?x ?p ?o } union { ?s ?p ?x } }
alternatively, utilize wildcard approach here, too:
select distinct ?x { ?x (:|!:)|^(:|!:) []. }
rdf sparql
No comments:
Post a Comment