Sparql - Traverse to top (broadest)

355 Views Asked by At

I have an internal triples dataset.

I am trying to implement a typeahead feature for an application using the dataset.

I am trying to figure out how I can conditional traverse to the Top Concept

enter image description here

In this picture, if I searched for dreamworks, It would be 3 layers down (Business Organizations -> Private Company -> Dreamworks).

If I do something else, obviously it will be at different layers of the graph.

I am trying to get the value "Organization" or "Person" as the top level.

This very straightforward query works.

        SELECT DISTINCT  ?subjectPrefLabel ?a ?b ?o
    WHERE
        { 
            ?subject skosxl:prefLabel/skosxl:literalForm ?subjectPrefLabel .
    ?subject skos:broader/skos:broader/skos:broader/skosxl:prefLabel/skosxl:literalForm ?a


    FILTER regex(?subjectPrefLabel, "Dreamworks", 'i')
        }
    ORDER BY ?subjectPrefLabel

enter image description here

However, obviously this is unsustainable, since the developer would need to know how many levels down the hierarchy in order to issue the correct number of skos:broader.

Is there anyway I can conditionally discover the highest level of the hierarchy?

As this is an internal Ontology, I cannot share any data, so I know it will be hard to work with, any advice is appreciated.

1

There are 1 best solutions below

0
On BEST ANSWER

Using the kleene star operator * incombination with a FILTER that there is no more broad concept should return the top concepts only:

SELECT DISTINCT  ?subjectPrefLabel ?a ?b ?o WHERE { 
    ?subject skosxl:prefLabel/skosxl:literalForm ?subjectPrefLabel .
    ?subject skos:broader* ?concept. 
    ?concept skosxl:prefLabel/skosxl:literalForm ?a
    FILTER NOT EXISTS { ?concept skos:broader ?supConcept } 
    FILTER regex(?subjectPrefLabel, "Dreamworks", 'i')
}
ORDER BY ?subjectPrefLabel