0

I have the following scenario modelled in OWL:

Producer ----producesResource---> Resource <------consumesResource ---- Consumer

Producer, Resource and Consumer are OWL Classes, while producesResource and consumesResource are object properties. The scenario is quite intuitive in that that each producer produces one or more resources that is consumed by one or more consumers. Conversely, each consumer can consume one or more resources. The ontology is populated with instances / individuals accordingly.

I would like to check if there exists a resource that is consumed by a consumer that is not produced by a Producer. What is an elegant way to get this information via a:

  1. Query in SPARQL
  2. SHACL Shapes graph (if possible).
user0221441
  • 368
  • 4
  • 11

1 Answers1

2

Negations are possible in SPARQL using the NOT BOUND filter or more easily in SPARQL 1.1 using MINUS:

SELECT ?resource WHERE
{
  ?resource a :Resource.

  ?consumer a :Consumer;
    ?consumer :consumesResource ?resource.

 MINUS {?producer a :Producer; :producesResource ?resource.}
}

You can also use ASK to get a boolean result but SELECT allows easier debugging to verify if your query is working correctly.

As SHACL allows integrating SPARQL queries, this answers your second question too but in that case it is easier to just use the SPARQL query on its own.

Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118
  • `FILTER NOT EXISTS` is the intended way to replace `OPTIONAL {...} FILTER(!BOUND(...))` way – UninformedUser Jan 24 '22 at 11:34
  • @UninformedUser: Thanks for the correction! However in this specific case I assume MINUS works as well, because the triple patterns share a common variable, or is there an advantage to use FILTER NOT EXISTS in this case? – Konrad Höffner Jan 24 '22 at 11:58