Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You count the number of edges in a unique path using SPARQL's property paths and aggregate functions. For instance, with data like this, which contains two paths that we care about (<em>a</em> to <em>c</em> with two edges, and <em>d</em> to <em>g</em> with three edges):</p> <pre><code>@prefix : &lt;https://stackoverflow.com/questions/19587520/sparql-path-between-two-instance/&gt; . :a :p :b . # a to c is a path of length 2 :b :p :c . :d :p :e . # d to g is a path of length 3 :e :p :f . :f :p :g . </code></pre> <p>you can use a query like the following one. Notice that I've used the specific property <code>:p</code>, rather than a variable. This is necessary, because <a href="http://www.w3.org/TR/sparql11-query/#pp-language" rel="nofollow noreferrer">9.1 Property Path Syntax</a> from the SPARQL 1.1 specification doesn't allow variables in property paths.</p> <pre><code>prefix : &lt;https://stackoverflow.com/questions/19587520/sparql-path-between-two-instance/&gt; select ?start ?end (count(?mid) as ?length) where { values (?start ?end) { (:a :c) (:d :g) } ?start :p+ ?mid . ?mid :p* ?end . } group by ?start ?end </code></pre> <p>and get results like this:</p> <pre><code>$ sparql --query query.rq --data data.n3 ------------------------ | start | end | length | ======================== | :d | :g | 3 | | :a | :c | 2 | ------------------------ </code></pre> <p>A fuller description of what's happening here can be found in:</p> <ul> <li><a href="https://stackoverflow.com/q/5198889/1281433">Calculate length of path between nodes?</a> (which is actually look at paths in a tree)</li> <li><a href="https://stackoverflow.com/a/18032019/1281433">this answer</a> to <a href="https://stackoverflow.com/q/18024413/1281433">Finding all steps in property path</a> (Note that the accepted answer says you can't do this, but the linked answer shows that you actually <em>can</em>); and</li> <li>the accepted answer to <a href="https://stackoverflow.com/q/17523804/1281433">Is it possible to get the position of an element in an RDF Collection in SPARQL?</a>.</li> </ul> <p>The basic idea, though, is that if you have a path from <code>?start</code> to <code>?end</code>, then you've also got, for a bunch of different values of <code>?mid</code>, a path from <code>?start</code> to <code>?mid</code> and a path from <code>?mid</code> to <code>?end</code>. The <em>number</em> of different values that you can pick for <code>?mid</code> (if you allow one of the endpoints, and disallow the other) is exactly the length of the path.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload