Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 views
in Technique[技术] by (71.8m points)

rdf - Querying a Graph path in SPARQL

I am trying to write a SPARQL query to return a path from a source to a destination. Below is the Turtle file representing the data set.

@prefix node: <http://prism.uvsq.fr/>.
@prefix edge: <http://prism.uvsq.fr#>.
node:a edge:p node:b.
node:a edge:q node:f.
node:a edge:p node:g.
node:b edge:p node:c.
node:c edge:q node:h.
node:c edge:p node:i.
node:c edge:p node:d.
node:d edge:p node:e.
node:f edge:p node:g.
node:f edge:q node:l.
node:f edge:p node:k.
node:g edge:p node:c.
node:g edge:p node:f.
node:h edge:p node:n.
node:i edge:q node:j.
node:j edge:p node:o.
node:j edge:q node:n.
node:k edge:p node:l.
node:l edge:p node:g.
node:m edge:q node:g.
node:n edge:p node:m.

The image next presents the same information, for easier visualization.

Graph nodes and edges

The query I wrote so far is the following:

prefix graph: <http://prism.uvsq.fr/>
prefix node: <http://prism.uvsq.fr/>
prefix edge: <http://prism.uvsq.fr#>
SELECT * FROM graph: WHERE {
   node:a (edge:p|edge:q) ?des.
   ?des (edge:p|edge:q)* node:h.
}

The returned information only shows one level of the solution (it shows the possible neighbor nodes for reaching the destination). Thanks in advance for your help. Best Regards

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Property paths in SPARQL are not things that you can query directly, but you can use property paths to help extract the edges along a path between two nodes. For instance, the following query returns the edges in paths from a to h. The basic idea is to use a property path to from a to some node u which has an edge to some node v from which there is a path to h. The values block just limits the value of e to be either p or q.

prefix node: <http://prism.uvsq.fr/>
prefix edge: <http://prism.uvsq.fr#>

select distinct ?u ?e ?v where {
  values ?e { edge:p edge:q }
  node:a (edge:p|edge:q)* ?u .
  ?u ?e ?v .
  ?v (edge:p|edge:q)* node:h .
}
----------------------------
| u      | e      | v      |
============================
| node:a | edge:p | node:g |
| node:a | edge:p | node:b |
| node:g | edge:p | node:f |
| node:g | edge:p | node:c |
| node:f | edge:p | node:k |
| node:f | edge:p | node:g |
| node:k | edge:p | node:l |
| node:l | edge:p | node:g |
| node:c | edge:p | node:i |
| node:n | edge:p | node:m |
| node:h | edge:p | node:n |
| node:b | edge:p | node:c |
| node:a | edge:q | node:f |
| node:f | edge:q | node:l |
| node:c | edge:q | node:h |
| node:i | edge:q | node:j |
| node:j | edge:q | node:n |
| node:m | edge:q | node:g |
----------------------------

That doesn't give you the actual paths, but it gives you all and only the edges that are on paths from a to h. From that you can reconstruct paths by putting the graph back together and performing a depth first traversal to enumerate the paths.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...