Sparql query on restriction list (Equivalent To) i

2019-01-26 23:21发布

My ontology is about Cocktail. This is a cocktail named "AfterGlow"

<owl:Class rdf:about="&cocktails;AfterGlow">
    <owl:equivalentClass>
        <owl:Class>
            <owl:intersectionOf rdf:parseType="Collection">
                <owl:Restriction>
                    <owl:onProperty rdf:resource="&cocktails;aPourIngredient"/>
                    <owl:someValuesFrom rdf:resource="&cocktails;JusAnanas"/>
                </owl:Restriction>
                <owl:Restriction>
                    <owl:onProperty rdf:resource="&cocktails;aPourIngredient"/>
                    <owl:someValuesFrom rdf:resource="&cocktails;JusOrange"/>
                </owl:Restriction>
                <owl:Restriction>
                    <owl:onProperty rdf:resource="&cocktails;aPourIngredient"/>
                    <owl:someValuesFrom rdf:resource="&cocktails;SiropGrenadine"/>
                </owl:Restriction>
                <owl:Restriction>
                    <owl:onProperty rdf:resource="&cocktails;aPourDescription"/>
                    <owl:hasValue>Descriptoion AfterGlow</owl:hasValue>
                </owl:Restriction>
                <owl:Restriction>
                    <owl:onProperty rdf:resource="&cocktails;aPourTitre"/>
                    <owl:hasValue>AfterGlow</owl:hasValue>
                </owl:Restriction>
            </owl:intersectionOf>
        </owl:Class>
    </owl:equivalentClass>
    <rdfs:subClassOf rdf:resource="&cocktails;Cocktail"/>
</owl:Class>

JusOrange means Orange juice JusAnanas means Pineapple juice

aPourIngredient is a property which means "has(contain) the ingredient"

enter image description here

This is the request which list all my cocktails with theirs rectrictions

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX : <http://www.semanticweb.org/cocktails#>
SELECT *
WHERE { 
  ?Cocktail rdfs:subClassOf :Cocktail.
  ?Cocktail owl:equivalentClass ?restriction .
}

enter image description here

How can I request for example "select all cocktail which contains JusAnanas AND JusOrange"

You can find my ontology here :

https://s3-eu-west-1.amazonaws.com/ontologycero/cocktailsOnthology.owl

I've already found an ugly request, but it's not usable because we have to know the ontology for use this kind of request.

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX : <http://www.semanticweb.org/cocktails#>
SELECT *
WHERE { 
  ?Cocktail rdfs:subClassOf :Cocktail.
  ?Cocktail owl:equivalentClass ?restriction .
  ?restriction owl:intersectionOf ?intersection.
  ?intersection rdf:first ?ingredient1.
  ?intersection rdf:rest ?rest1.
  ?rest1 rdf:first ?ingredient2.
  ?rest1 rdf:rest ?rest2.
  ?rest2 rdf:first ?ingredient3.

  ?ingredient1 owl:someValuesFrom :JusAnanas.
  ?ingredient2 owl:someValuesFrom :JusOrange.
}

2条回答
Emotional °昔
2楼-- · 2019-01-26 23:31

SPARQL isn't meant to understand the formal OWL model you've set up. It's a graph pattern matching query language for RDF triples. So instead of trying to query through the model, query the data using the semantics of the model you've designed:

SELECT *
WHERE {
   ?coctails rdfs:subClassOf* :Cocktail .
   ?aoCocktail a ?coctails .
   ?aoCocktail :aPourIngredient :JusAnanas .
   ?aoCocktail :aPourIngredient :JusOrange .
}

The first two triple patterns find all member of :Cocktail and its subclasses. The last two triple patterns find all members that have both :JusOrange and :JusAnanas as values for the property :aPourIngredient.

查看更多
霸刀☆藐视天下
3楼-- · 2019-01-26 23:38

I am not sure what you are looking for, but as far as I understand you want to bypass the structure of the ontology. Since you don't know what comes after restriction, you can mention to the query to check wall the possible combinations.

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX : <http://www.semanticweb.org/cocktails#>
SELECT *
WHERE { 
  ?Cocktail rdfs:subClassOf :Cocktail.
  ?Cocktail owl:equivalentClass ?restriction .
  ?restriction (rdfs:subClassOf|(owl:intersectionOf/rdf:rest*/rdf:first))* ?ingredient1.
  ?restriction (rdfs:subClassOf|(owl:intersectionOf/rdf:rest*/rdf:first))* ?ingredient2.
  ?ingredient1 owl:someValuesFrom :JusAnanas.
  ?ingredient2 owl:someValuesFrom :JusOrange.
  }
查看更多
登录 后发表回答