SPARQL - extract subset of graph with filter conditions

415 Views Asked by At

I am trying to extract a subset of a graph with a multi-hierarchy.

Example Structure:

                                         root
                                   /                     \ 
                               SubWant                   subNotWant
                             /         \                           \
                     childWant        childWant                   gCWant
                        /                 \
                       gCWant       gCNotWant
                      /
                   gGCWant

the subWant subtree is what I would like to get (assume all {'/','\'} or broader/narrower relationships)

My current query is:

CONSTRUCT
{
  ?children a skos:Concept ;
    skos:broader ?parent ;
    skos:prefLabel ?childPrefLiteral .
   }  

    WHERE {
    ?children skos:broader+ <http://example.com#subWant> ;
        skos:prefLabel ?childPrefLiteral .

    filter(STRBEFORE(?childPrefLiteral, " ") NOT IN (<criteria to prune by>) )
    ?children skos:broader ?parent ;


    # filter parent to only have transitive broader relation to desired concept

    ?parent skos:broader+ <http://example.com#subWant>
  }

This query almost works except that it will not include direct children of the subWant node. Any recommendations on how to elegantly accomplish this? Note the reason for the last triple ?parent skos:broader <http://example.com#subWant> is necessary as nodes have multiple parents (multi-hierarchy) - see gCWant node in sample graph.

1

There are 1 best solutions below

1
On BEST ANSWER

Try changing

?parent skos:broader+ <http://example.com#subWant>

into

?parent skos:broader* <http://example.com#subWant>

The difference between "*" and "+" is that "*" also matches results where ?parent is <http://example.com#subWant>.