title: SPARQL query to return matches on satisfied restrictions
body: <p>I am trying to write a query on a class with a restriction defined on it, that will determine whether the restriction has been satisfied and, if it has, return it.</p>
  
  <p>An example class is defined as follows:</p>
  
  <pre><code>:ThingB  a            owl:Class ;
      rdfs:subClassOf   :ThingA ;
      rdfs:subClassOf   [ a                  owl:Restriction ;
                          owl:allValuesFrom  [ a                   owl:Class ;
                                               owl:intersectionOf  ( :ThingC :ThingD )
                                             ] ;
                          owl:onProperty     owl:isConfirmedBy
                        ] ;
      rdfs:subClassOf   [ a                   owl:Restriction ;
                          owl:onProperty      owl:isConfirmedBy ;
                          owl:someValuesFrom  [ a            owl:Class ;
                                                owl:unionOf  ( :ThingE :ThingF :ThingG :ThingH )
                                              ]
                        ] .
  </code></pre>
  
  <p>I would like to be able to write a query that says if I have both ThingC and ThingD and any one of ThingE, ThingF, ThingG or ThingH then return ThingB.</p>
  
  <p>The following query will do the first part i.e. return ThingB only if I have both ThingC and ThingD:</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT 
  *
  WHERE
  {
      ?thing rdfs:subClassOf ?restriction .
      ?restriction (owl:allValuesFrom | owl:someValuesFrom)/(owl:intersectionOf | owl:unionOf) ?list .
  
      FILTER NOT EXISTS { 
          ?list rdf:rest*/rdf:first ?things.
          FILTER( !(?things in (:ThingC, :ThingD )) )
      }
  }
  </code></pre>
  
  <p><strong>How would I include the second part of the restriction that says there must also be at least one of ThingE, ThingF, ThingG or ThingH for this to return a match on ThingB?</strong></p>