Created
October 14, 2019 17:38
-
-
Save jaw111/1b149fd1111f774a3613f10955686617 to your computer and use it in GitHub Desktop.
Generate list in SPARQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
prefix : <http://example.com/> | |
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> | |
prefix sh: <http://www.w3.org/ns/shacl#> | |
# insert some sample data | |
insert data { | |
#boxes | |
:red_box a :Box ; | |
:color "Red" . | |
:green_box a :Box ; | |
:color "Green" . | |
:blue_box a :Box ; | |
:color "Blue" . | |
#tubes | |
:red_tube a :Tube ; | |
:color "Red" . | |
:yellow_tube a :Tube ; | |
:color "Yellow" . | |
#balls | |
:red_ball a :Ball ; | |
:color "Red" . | |
}; | |
# insert list stub for each unique type-property-object | |
insert { | |
?type ?p [ rdf:first ?o ] . | |
} | |
where { | |
select distinct ?type ?p ?o { | |
?s a ?type ; | |
?p ?o . | |
filter (?p != rdf:type) | |
} | |
}; | |
# insert rdf:rest links to next value | |
delete { | |
?type ?p ?rest . | |
} | |
insert { | |
?b1 rdf:rest ?rest . | |
} | |
where { | |
{ | |
# select the next larger value for each bnode | |
select ?type ?p ?b1 (min(?o2) as ?next) { | |
?type <http://example.com/color> ?b1 , ?b2 . | |
?b1 rdf:first ?o1 . | |
?b2 rdf:first ?o2 . | |
filter (?o1 < ?o2) | |
} | |
group by ?type ?p ?b1 | |
} | |
#find the bnode for next value | |
?type ?p ?rest . | |
?rest rdf:first ?next . | |
}; | |
# insert NodeShape and property for value list | |
delete { | |
?type ?p ?b . | |
} | |
insert { | |
?type a sh:NodeShape ; | |
sh:property [ | |
sh:path ?p ; | |
sh:in ?b ; | |
] . | |
} | |
where { | |
?type ?p ?b . | |
?b rdf:first ?o . | |
}; | |
# insert nil at end of list | |
insert { | |
?b rdf:rest rdf:nil . | |
} | |
where { | |
?b rdf:first ?o . | |
minus { | |
?b rdf:rest [] | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi @jaw111!
Nice tricks here (lists are indeed nasty).
But what are you trying to do? It seems to me there's some mistake in how the SHACL is formed: