Generating RDF list for sh:in

44 views
Skip to first unread message

Felix Sasaki

unread,
Mar 14, 2022, 11:17:36 AM3/14/22
to topbrai...@googlegroups.com
This is rather a SPARQL question, but asking here since I have a SHACL use case.

I want to generate the items in sh:in out of existing RDF data. I have a CONSTRUCT query that looks like below:

CONSTRUCT {
ex:fooShape a sh:PropertyShape;
sh:path ex:foo
sh:in @@@ .
}
WHERE 
{
SELECT ?listItem WHERE {
# Query that creates bindings for ?listItem
}
}

How should the part @@@ be written so that the object of the predicate sh:in becomes an RDF list, filled with ?listItem?

Best,

Felix

Holger Knublauch

unread,
Mar 14, 2022, 6:36:29 PM3/14/22
to topbrai...@googlegroups.com

Hi Felix,

constructing rdf:Lists from SPARQL is notoriously hard. We have a work-around "magic property" that works for some use cases but not others, but I mention it here:

/**
<span class="style1">tops:constructRDFList</span>
<p>(Experimental) This function binds variables for constructing an RDF list.
It can be best used in a CONSTRUCT query. It is an experimental function, so it may work
only in certain cases.</p>
<ul>
<li><i>Syntax:</i> <span class="style1">(?list ?subList ?element ?rest) tops:constructRDFList (?s ?p ?o) .</span></li>
<li><i>Description:</i> The function iterates through <span class="style1">?s ?p ?o</span> to
bind variables for constructing a list. Currently, only one variable in <span class="style1">?s ?p ?o</span>
should be unassigned, and the other two should be assigned. Also the property "rdf:type" should be used instead of "a".
<span class="style1">?list</span> variable is used as the head
of the list and stays constant throughout the iteration, <span class="style1">?subList</span>
is used as the sub heads of the list and changes with each iteration, <span class="style1">?element</span>
is the unbound variable in <span class="style1">?s ?p ?o</span> and changes with each iteration,
and <span class="style1">?rest</span> is the bound <span class="style1">?subList</span> of the
next iteration, which the current iteration points to.</li>
<li><i>Example:</i> In the following query, <span class="style1">tops:constructRDFList</span> constructs
an <span class="style1">owl:oneOf</span> list of colleges. Here is the query and its usage in kennedys ontology:</li>
<pre>
CONSTRUCT {
    kennedys:College owl:oneOf ?list .
    ?subList rdf:first ?element .
    ?subList rdf:rest ?rest .
}
WHERE {
      (?list ?subList ?element ?rest) tops:constructRDFList (?s rdf:type kennedys:College) .
}
</pre>
</ul>

<p><b>Known limitations:</b> Only one variable in <span class="style1">?s ?p ?o</span>
should be unassigned, and the other two should be assigned. The property "rdf:type" should be used instead of "a" in the function.</p>
*/

It depends on how the WHERE clause looks like whether this could be used in your scenario. The tops namespace is http://www.topbraid.org/tops

But for complex tasks involving recursive data structures such as rdf:Lists you'll be better off using a generic scripting language such as TopBraid's ADS JavaScript support.

Holger

--
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/CAL58czrLFr-496KOMKEMFoBj-Awh36XM-7QPtCMbjqRNMhkWOQ%40mail.gmail.com.

Felix Sasaki

unread,
Mar 15, 2022, 12:55:29 PM3/15/22
to TopBraid Suite Users
Hi Holger,

thanks, that is very helpful for me and for deciding how to move forward. One follow-up question: I would like to generate a complete SHACL query via Wikidata, see an example here
in the example query, I don't create one RDF list, but several sh:in predicates. Would that work as well, for SHACL validation?

Best,

Felix

Holger Knublauch

unread,
Mar 15, 2022, 6:20:44 PM3/15/22
to topbrai...@googlegroups.com


On 2022-03-16 2:55 am, 'Felix Sasaki' via TopBraid Suite Users wrote:
Hi Holger,

thanks, that is very helpful for me and for deciding how to move forward. One follow-up question: I would like to generate a complete SHACL query via Wikidata, see an example here
in the example query, I don't create one RDF list, but several sh:in predicates. Would that work as well, for SHACL validation?

No, the values of sh:in are expected to be rdf:Lists, i.e. a single, closed list for all states in your example. If you want to have an extensible list of values, and you had control over the data, you could create a class such as schema:GermanState and just use sh:class to point at it.

Holger


Felix Sasaki

unread,
Mar 16, 2022, 11:12:13 AM3/16/22
to TopBraid Suite Users
Thanks, Holger, that sounds like a good approach to implement my requirement.

Felix

Reply all
Reply to author
Forward
0 new messages