SPARQL Query for distance between nodes

120 views
Skip to first unread message

Marie Valadez

unread,
Feb 16, 2024, 4:48:47 PMFeb 16
to TopBraid Suite Users
I have searched and tested out multiple ways to get the distance between two nodes. I want a query that will show the child concepts and how far away they are from the current concept so that I can create a table on a form showcasing this.

The following query works if a ?sub concept does not have two connection through skos:broader. But once a ?sub has two parent concepts with the skos:broader it overcounts. I have included a basic example diagram on the issue I am dealing with. Any ideas on how to get an accurate count?

SELECT ?sub  (count(distinct ?mid) as ?distance)
WHERE {
  $this ^skos:broader* ?mid .
  ?mid ^skos:broader+ ?sub .
}
group by $this ?sub





stepcount-Page-2.drawio.png

Holger Knublauch

unread,
Feb 17, 2024, 4:44:20 AMFeb 17
to 'Branson, GaBriella C' via TopBraid Suite Users
Hi Marie,

we do have some built-in functions including spif:shortestObjectsPath and swa:shortestPathsBetweenNodes that may help.

To clarify your requirements, is it true that one of the nodes is always an (indirect) parent of the other node, or does the algorithm also need to walk in one direction and then in another direction of the tree structure?

And are we talking about tree structures at all, or arbitrary graphs?

Holger


--
The topics of this mailing list include TopBraid EDG and related technologies such as SHACL.
To post to this group, send email to topbrai...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "TopBraid Suite Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to topbraid-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/topbraid-users/be2ebc41-1fa7-4a70-a3ca-d8ee2cf575ddn%40googlegroups.com.
<stepcount-Page-2.drawio.png>

Marie Valadez

unread,
Feb 17, 2024, 1:15:20 PMFeb 17
to topbrai...@googlegroups.com
The way skos:broader is being used in our case is a tree like structure with some having multiple parents that can be broader but will point back to the same main broad concept which branches out to its children, grandchildren, etc. I need the shortest path from whichever instance is selected to all of its descendants. All will have the connection to each other through the skos:broader within the family tree (some can have more than 1 direct skos:broader concept (parent)). 

Do you have any example documentation on how those functions can be utilized? 

You received this message because you are subscribed to a topic in the Google Groups "TopBraid Suite Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/topbraid-users/CUgBING_53U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to topbraid-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/topbraid-users/D618E05A-6068-428F-B8DA-77C9D6C52A96%40topquadrant.com.

Holger Knublauch

unread,
Feb 18, 2024, 5:24:49 AMFeb 18
to 'Branson, GaBriella C' via TopBraid Suite Users
Here is an example:

PastedGraphic-1.png

where $this is some skos:Concept deep in the hierarchy such as g:Vienna.

For easier copy and paste:

PREFIX spif: <http://spinrdf.org/spif#>

SELECT *
WHERE {
BIND (spif:shortestObjectsPath(g:Vienna, skos:broader, g:Europe) AS ?path) .
}

HTH
Holger


Marie Valadez

unread,
Feb 18, 2024, 6:35:34 PMFeb 18
to topbrai...@googlegroups.com
Thank you for the example use case. What does g:Europe represent in this case? Is that the ending node? Is it necessary to specify the ending node? For instance if I wanted to find all the descendants of a concept using the inverse of skos:broader and the distance each one is away from the current concept, then I wouldn't want to specify a single child concept. 

Holger Knublauch

unread,
Feb 19, 2024, 2:37:20 AMFeb 19
to topbrai...@googlegroups.com
Yes, g:Europe is the end node and is optional. If no end node is specified, it will stop at whenever a node doesn't have further values of skos:broader.

You can use spif:shortestSubjectsPath for the inverse direction.

To find the paths to each child, loop over the children:

SELECT *
WHERE {
    ?child skos:broader g:Europe .
    BIND (spif:shortestObjectsPath(?child, skos:broader, g:Europe) AS ?path) .
}

For anything more complex, ADS JavaScript is often a good choice as it gives you more control:

On 19 Feb 2024, at 12:35 am, Marie Valadez <meval...@gmail.com> wrote:

Thank you for the example use case. What does g:Europe represent in this case? Is that the ending node? Is it necessary to specify the ending node? For instance if I wanted to find all the descendants of a concept using the inverse of skos:broader and the distance each one is away from the current concept, then I wouldn't want to specify a single child concept. 

On Sun, Feb 18, 2024 at 3:24 AM Holger Knublauch <hol...@topquadrant.com> wrote:
Here is an example:

Marie Valadez

unread,
Feb 20, 2024, 12:11:57 PMFeb 20
to TopBraid Suite Users
Thank Holger. 

Is there a way to return a count with this that shows the distance?

Holger Knublauch

unread,
Feb 20, 2024, 2:29:47 PMFeb 20
to 'Richard Nagelmaeker' via TopBraid Suite Users

On 20 Feb 2024, at 6:11 pm, Marie Valadez <meval...@gmail.com> wrote:

Thank Holger. 

Is there a way to return a count with this that shows the distance?

Sure. The function returns a ?path string with one space between path segments.

You can use

BIND (REPLACE(?path, "[^ ]", "") AS ?spaces) .
BIND (STRLEN(?spaces) AS ?depth) .

to count the spaces.

Holger


Marie Valadez

unread,
Feb 21, 2024, 11:15:12 PMFeb 21
to TopBraid Suite Users
Thank you so much! Appreciate all your help. 

steveray...@gmail.com

unread,
Mar 7, 2024, 11:33:49 AMMar 7
to TopBraid Suite Users
Holger, this thread was very interesting for me to read, because it addresses a navigational problem we're looking at. However, some of my colleagues do not use the TopQuadrant tools, so they don't have spif:shortestObjectsPath available. Am I correct that this was coded in another language? Would the best approach be to tackle this using the Javascript extension to SHACL mechanism? (Or even better, do you have the Javascript code?).

Holger Knublauch

unread,
Mar 7, 2024, 11:52:52 AMMar 7
to topbrai...@googlegroups.com
Hi Steve,

this is implemented in Java, but yes there is no rocket science and it could be reimplemented in other languages. Basically do a breadth-first traversal.

Holger


steveray...@gmail.com

unread,
Mar 7, 2024, 11:55:50 AMMar 7
to TopBraid Suite Users
Thanks. Would you also agree that this approach is better than trying to do something in native SPARQL? I have always seen SPARQL as better suited to declarative, set-based problems rather than navigational ones.

Matt Goldberg

unread,
Mar 7, 2024, 11:56:36 AMMar 7
to TopBraid Suite Users
On a semi-related note, tasks like this are why I suggested a pathfinding extension to the SPARQL specification such that these types of operations would be supported across implementations https://github.com/w3c/sparql-dev/issues/191

On Thursday, March 7, 2024 at 11:52:52 AM UTC-5 Holger Knublauch wrote:

Holger Knublauch

unread,
Mar 7, 2024, 11:58:03 AMMar 7
to 'Richard Nagelmaeker' via TopBraid Suite Users
If SPARQL could express this then the extra functions wouldn't be needed. So I guess this goes beyond what SPARQL can do.

I guess Matt just sent something along those lines.

Holger


Reply all
Reply to author
Forward
0 new messages