Search code examples
sparqlowljenasemantic-web

SPARQL filter instances based on data property of instances along object property


I have an OWL model that has the data property hasToken that has range xsd:boolean, so it can have values either true or false. Let's say the the domain of hasToken is class X. Another class Z is domain for an object property, say p which has range Class X.

Z----p---->X----hasToken----> true/false

Every instance of class Z can have multiple assertions of p to instances of class X. Via SPARQL I would like to get all instances of Z, that only has instances of X with hasToken equals true for all objects along property p. So if an instance of Z has even one instance of X along p that is false along hasToken, its a Z that I am not interested in.

Example:

x1---hasToken --->true

x2---hasToken --->false

x3---hasToken --->true

x4---hasToken --->false

x5---hasToken --->false


z1 ---p-->x1

z1 ---p-->x2

z1 ---p-->x3


z2 ---p-->x1

z2 ---p-->x3


z3 ---p-->x1

z3 ---p-->x3

z3 ---p-->x5

Th query should return only z2 as both z1 and z3 have atleast one x that hasToken false


Solution

  • For q owl:inverseOf p, the following worked.

    SELECT ?z1
    WHERE { 
        ?z1 a :Z.
        ?x :q ?z1.
        ?x :hasToken true.
    MINUS {
        ?x1 :hasToken true.
        ?x2 :hasToken false.
        ?x1 :q ?z1.
        ?x2 :q ?z2.
        filter ((?z1=?z2))
      }
    }