How to store tree with ordered children in RDF? How to traverse such structure in SPARQL?

How do I store tree with ordered children in RDF?

Input:

1. Title 1
   Some text  1.
2. Title 2
2.1. Title 2.1
     Some text under title 2.1.
2.2. Title 2.2
     Some text under title 2.2.

Titles can be arbitrary and not necessarily contain numbering. Depth is arbitrary.

How to get back all elements still ordered in one query? Does Stardog have any functions that can help with this task?

My question based on the fact that relational databases can retrieve such structure in one query using nested set.

Desired output:

|-----------+----------------------------+---------|
| Title     | Content                    | Depth   |
|-----------+----------------------------+---------|
| Title 1   | Some text under title 1.   |      0  |
| Title 2   |                            |      0  |        
| Title 2.1 | Some text under title 2.1. |      1  |
| Title 2.2 | Some text under title 2.2. |      1  |
|-----------+----------------------------+---------|

Hi Alexander,

Modeling is easy. There’re multiple options, e.g. modeling each hierarchy level as a linked list of siblings:

:root :first :title1 .
:title1 :next :title2 .
:title2 :first :title21 .
:title21 :next :title22 .
...

that is, Lisp-style lists (you can even use the standard RDF lists though they don’t play well with SPARQL).

But getting all children in the topological order in one SPARQL query would be difficult. I can’t immediately think of an efficient single query solution. Stardog supports the so-called path queries so you can get paths to all children in one query but then the client would have to sort them according to the depth. Paths will be retrieved in the breadth-first order. We’re planning to add ORDER BY to path queries but it’s not there yet.

Cheers,
Pavel

You might find find this SO post to be helpful Is it possible to get the position of an element in an RDF Collection in SPARQL? - Stack Overflow

+1 on Pavel’s suggestion of :next or :hasNext . Then with an owl:inverseOf :hasPrevious and the reasoner on, you can easily traverse both directions, which I’ve found surprisingly useful.

Tim

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.