List Info

Thread: SPARQL Query Puzzler




SPARQL Query Puzzler
user name
2007-01-25 16:45:55
Dear List Members,

I am stuck with formulating a SPARQL query which checks wheter one graph structure is contained in another.
Consider the following example of an imaginery "theory-of-sets" ontology:

setA a Set
setB a Set
setC a Set
setD a Set

el_1 a Element
el_2 a Element
el_3 a Element
el_4 a Element

setA contains el_1, el_2, el_3
setB contains el_1, el_3
setC contains el_2, el_4
setD contains el_3

Now here is the question: Is it possible (with a SPARQL query) to retrieve all sets, which are sub-sets of setA?
To be more precise: Is there a SPARQL query, which returns all Sets containing only Elements, which are also contained in setA?

Premise: you know the Elements of setA explicitly, but you know nothing about the other Sets and their Elements.


Here's my try (Problem: it doesn't filter out setC):
SELECT ?set
WHERE {
        ?set a :Set .
        ?set :contains ?x .
        FILTER (?x = :Element_1 || ?x = :Element_2 || ?x = :Element_3 )
}
 ;  

The obvious solution would be setB and setD (and maybe setA itself, too), but not setC.
I have tried to solve this puzzle for days now, without success. I wonder if such a query is possible with SPARQL.

Any suggestions are welcome!

Best regards,
Torben
Re: SPARQL Query Puzzler
user name
2007-01-26 02:34:45
Hello Torben

I think this should work:
1) Select all elements in some set, which are NOT contained
in SetA.
   If such elements exist, this set is one we do not want to
select.
2) Select all sets, which are not such 'bad' sets.

SELECT ?set 
WHERE {
  OPTIONAL {
    ?badSet a :Set;
            :contains ?x.
    OPTIONAL { 
      :setA :contains ?y.
      FILTER (?x = ?y).
    }
    FILTER ((!BOUND(?y)) && (?badSet = ?set)).
  }

  ?set a :Set.
  FILTER (!BOUND(?badSet)).
}

Looks a bit weird, because what we do here is model
"all sets, where all
elements are in SetA" as "all sets which are not
sets with elements not
in SetA". As there is nothing like FORALL X in SPARQL,
we need to
express this as NOT EXISTS NOT X. The negations are
expressed using
OPTIONAL ... !BOUND and make the query a bit ugly. Some
syntactic sugar
would make SPARQL easier to use for such cases.

Hope that helps.

Best regards,
Simon

Am Donnerstag, den 25.01.2007, 23:45 +0100 schrieb Torben
Knerr:
> Dear List Members,
> 
> I am stuck with formulating a SPARQL query which checks
wheter one
> graph structure is contained in another.
> Consider the following example of an imaginery
"theory-of-sets"
> ontology:
> 
> setA a Set
> setB a Set
> setC a Set
> setD a Set
> 
> el_1 a Element
> el_2 a Element
> el_3 a Element
> el_4 a Element
> 
> setA contains el_1, el_2, el_3
> setB contains el_1, el_3
> setC contains el_2, el_4
> setD contains el_3
> 
> Now here is the question: Is it possible (with a SPARQL
query) to
> retrieve all sets, which are sub-sets of setA? 
> To be more precise: Is there a SPARQL query, which
returns all Sets
> containing only Elements, which are also contained in
setA? 
> 
> Premise: you know the Elements of setA explicitly, but
you know
> nothing about the other Sets and their Elements.
> 
> Here's my try (Problem: it doesn't filter out setC):
> SELECT ?set
> WHERE { 
>         ?set a :Set .
>         ?set :contains ?x .
>         FILTER (?x = :Element_1 || ?x = :Element_2 ||
?x
> = :Element_3 ) 
> }
>     
> 
> The obvious solution would be setB and setD (and maybe
setA itself,
> too), but not setC. 
> I have tried to solve this puzzle for days now, without
success. I
> wonder if such a query is possible with SPARQL.
> 
> Any suggestions are welcome!
> 
> Best regards,
> Torben



[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )