1

I have data that describes sub-type relationships. In Turtle it could look like this:

@prefix a:    <#>
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

a:test1
  a a:A ;
  a a:B ;
  a a:C ;
.
a:test2
  a a:A ;
  a a:D ;
.
a:B rdfs:subClassOf a:A .
a:C rdfs:subClassOf a:B .
a:D rdfs:subClassOf a:A .

In the above example, a:C is a subtype of a:B, which in turn is a subtype of a:A. a:D also is a subtype of a:A. I want to select the least upper bound of a:test1, which is a:C. I can write a query, which selects all upper bounds of a:test1, which would be:

prefix a:    <#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select distinct ?type where {
  a:test1 a ?type .
}

But I'm not able to restrict ?type any further. In a functional language with a Java like syntax I would write:

setOfTypes.filter(type =>
  not setOfTypes.exists(subtype =>
    subtype.isSubClassOf(type) && subtype != type))

My translation to SPARQL looks like this:

prefix a:    <#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select distinct ?type where {
  a:test1 a ?type .
  filter not exists {
    ?subtype rdfs:subClassOf ?type .
    filter (?subtype in ?type && ?subtype != ?type)
  }
}

Unfortunately, ?subtype in ?tpe is invalid syntax. Can someone show me a query that would only select a:C?

kiritsuku
  • 52,967
  • 18
  • 114
  • 136
  • You are looking for the most specific class resp. type of an instance in RDF. This has already be asked and answered several times here, e.g. http://stackoverflow.com/questions/19632992/retrieving-most-specific-classes-of-instances . Note, that there can be several such most specific classes. – UninformedUser Dec 21 '16 at 21:06
  • And why do you need this `?subtype in ?type`? What is your idea behind this? – UninformedUser Dec 21 '16 at 21:07
  • I have read the linked question. The difference between mine is that I need to limit it to only one result, which I don't know how to do. The `?subtype in ?type` is syntactically and semantically not correct, I understand that - this is why I'm asking the question because I don't know how else I could limit the result to only one result (the least upper bound). – kiritsuku Dec 21 '16 at 23:17
  • Isn't limiting to one result just adding `LIMIT 1` to the SPARQL query? – UninformedUser Dec 22 '16 at 10:02
  • No, because I can not order the result and therefore `LIMIT 1` would give an arbitrary result back. – kiritsuku Dec 22 '16 at 14:27
  • And why can't you add `ORDER BY DESC(?type) LIMIT 1`? – UninformedUser Dec 22 '16 at 14:45
  • The ordering is exactly what this question is about. The correct ordering depends on the hierarchal structure, which can't be described easily in SPARQL (see the Turtle example for the hierarchy). – kiritsuku Dec 22 '16 at 18:12

0 Answers0