Last active
March 13, 2023 10:24
-
-
Save tomsaleeba/ff8e145b3efd1127e48baa6512df24e2 to your computer and use it in GitHub Desktop.
Extracting RDF subgraphs using 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
# this query extracts a subgraph from the selected subject (level 1) and two child levels | |
PREFIX aekos: <http://www.aekos.org.au/ontology/1.0.0#> | |
PREFIX some_dataset: <http://www.aekos.org.au/ontology/1.0.0/some_dataset#> | |
CONSTRUCT { | |
?s1 ?p1 ?o1 . | |
?s1 ?pv1 ?v1 . | |
?s2 ?p2 ?o2 . | |
?s2 ?pv2 ?v2 . | |
?s3 ?p3 ?o3 . | |
?s3 ?pv3 ?v3 . | |
} | |
FROM wadec_ravensthorpe: | |
WHERE { | |
# level 1 | |
?s1 ?p1 ?o1 FILTER (?s1 = some_dataset:ENTITY-123) . # identify starting subject | |
?s1 ?pv1 ?v1 . | |
BIND (?o1 as ?s2) . # rename binding for readability | |
# level 2 | |
?s2 ?p2 ?o2 . # get links to other subjects | |
?s2 ?pv2 ?v2 . # get primitives | |
BIND (?o2 as ?s3) . | |
# level 3 | |
?s3 ?p3 ?o3 . | |
?s3 ?pv3 ?v3 . | |
} |
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
# this query extracts a subgraph from the selected subject (level 1) until no more paths can be traversed | |
# BEWARE: you're traversing a *graph*, not a tree, so if you have cycles then this query will never finish. | |
PREFIX aekos: <http://www.aekos.org.au/ontology/1.0.0#> | |
PREFIX some_dataset: <http://www.aekos.org.au/ontology/1.0.0/some_dataset#> | |
CONSTRUCT { | |
?s1 ?p1 ?o1 . | |
?sN ?pN ?oN . | |
} | |
FROM some_dataset: | |
WHERE { | |
?s1 ?p1 ?o1 . # level 1 | |
values ?s1 { | |
some_dataset:ENTITY-123 # identify starting subject | |
} | |
BIND (?o1 as ?s2) . # rename binding for readability | |
?s2 (!<urn:nothing>)+ [] . # any length property path where the property != "something it will never equal" (i.e. find everything) | |
?sN ?pN ?oN . # bind everything | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment