Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>This is based on the same technique used to compute the position of an element in an RDF list using SPARQL that is described in: <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></em> </p> <p>If you have data like this: </p> <pre><code>@prefix : &lt;http://example.org&gt; . :orgA :hasSuborganization :orgB, :orgC, :orgD. :orgB :hasSuborganization :orgE, :orgF. :orgE :hasSuborganization :orgG. :orgG :hasSuborganization :orgH. </code></pre> <p>which describes a hierarchy like this:</p> <p><img src="https://i.stack.imgur.com/Py51g.png" alt="organization hierarchy"></p> <p>then you can use a query like this:</p> <pre><code>prefix : &lt;http://example.org&gt; select ?super ?sub (count(?mid) as ?distance) { ?super :hasSuborganization* ?mid . ?mid :hasSuborganization+ ?sub . } group by ?super ?sub order by ?super ?sub </code></pre> <p>to get results like these:</p> <pre><code>$ sparql --query query.rq --data subs.n3 ---------------------------- | super | sub | distance | ============================ | :orgA | :orgB | 1 | | :orgA | :orgC | 1 | | :orgA | :orgD | 1 | | :orgA | :orgE | 2 | | :orgA | :orgF | 2 | | :orgA | :orgG | 3 | | :orgA | :orgH | 4 | | :orgB | :orgE | 1 | | :orgB | :orgF | 1 | | :orgB | :orgG | 2 | | :orgB | :orgH | 3 | | :orgE | :orgG | 1 | | :orgE | :orgH | 2 | | :orgG | :orgH | 1 | ---------------------------- </code></pre> <p>The trick here is to recognize that any path from X to Y can be viewed as a (possibly empty) path from X to some intermediate node Z (nonempty means that you can choose X as Z) concatenated with a (non empty) path from Z to Y. The number of possible ways of picking Z indicates 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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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