c# - Limit the SPARQL query result to First level in hierarchy -
i'm using c# execute query on n3 data files. how can limit result first level of children of node. example:
project | |__ main | |__m1 | |__m2 | |__ sub |__a | |__a1 | |__a2 | |__b |__c | |__c1 | |__d an example query result level of nodes sub:
select ?object { :sub rdfs:superclassof* ?object } the result be:
|__a | |__a1 | |__a2 | |__b |__c | |__c1 | |__d but want limit result first level of children this:
|__a |__b |__c |__d
selecting paths of length one
the property path using * finds paths of length zeor or greater. if want paths of length one, remove *:
select ?object { :sub rdfs:superclassof ?object } i'd note though rdfs defines rdfs:subclassof, not rdfs:superclassof you've used in query. i'll assume it's typo in question, though. think actual query you'd want would be:
select ?subclass { ?subclass rdfs:subclassof :sub } selecting arbitrary length paths
the solutions in section based on an answer question finding position of elements in rdf list. consider data:
@prefix : <urn:ex:> . :a :p :b, :c . :b :p :d, :e . this query finds chains on p along lengths of chains:
prefix : <urn:ex:> select ?sub ?sup (count(?mid)-1 ?distance) { ?sub :p* ?mid . ?mid :p* ?sup . } group ?sub ?sup order ?sub ?sup $ arq --data data.n3 --query query.sparql ------------------------ | sub | sup | distance | ======================== | :a | :a | 0 | | :a | :b | 1 | | :a | :c | 1 | | :a | :d | 2 | | :a | :e | 2 | | :b | :b | 0 | | :b | :d | 1 | | :b | :e | 1 | | :c | :c | 0 | | :d | :d | 0 | | :e | :e | 0 | ------------------------ since can length of paths can filter on length , ones want. instance:
prefix : <urn:ex:> select ?sub ?sup { { select ?sub ?sup (count(?mid)-1 ?distance) { ?sub :p* ?mid . ?mid :p* ?sup . } group ?sub ?sup order ?sub ?sup } filter( ?distance = 2 ) # filter ( ?distance > 2 ) # alternative # filter ( ?distance < 10 ) # alternative } $ arq --data data.n3 --query query.sparql ------------- | sub | sup | ============= | :a | :d | | :a | :e | ------------- when want paths of small, specific length, can expand property path manually. e.g., paths of length two:
prefix : <urn:ex:> select ?sub ?sup { ?sub :p/:p ?sup . } for range of numbers, e.g., 1–2, can use ? matches 0 or one:
prefix : <urn:ex:> select ?sub ?sup { ?sub :p/:p? ?sup . } for more property paths, sure take @ section 9 property paths in sparql 1.1 specification.
Comments
Post a Comment