hey im trying to get all the receipes that only contains some ingredients. For example if I have a receipe with tomato and bread and I only said that Im going to use tomate that receipe shouldnt show up. If I have any receipe with only this ingredient then it should show up. I did the following query:
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 rec:<http://www.receta.org#>
SELECT reduced ?r
WHERE {
?x rdf:type rec:Receta .
?x rdfs:label ?r.
?x rec:Ingrediente rec:Tomato.
?x rec:Ingrediente ?i.
FILTER (?i=rec:Tomato)
}
But the problem is that it shows all the receipes that contains that ingredient and much more. How can I restrict this? Can someone help me?
Your first query
SELECT reduced ?r WHERE {
?x rdf:type rec:Receta .
?x rdfs:label ?r.
?x rec:Ingrediente rec:Tomato.
?x rec:Ingrediente ?i.
FILTER (?i=rec:Tomato)
}
matches any recipe that has Tomato as some ingredient. Using a filter with a constant value like that is the same as having written:
SELECT reduced ?r WHERE {
?x rdf:type rec:Receta .
?x rdfs:label ?r.
?x rec:Ingrediente rec:Tomato.
}
Instead, you want to select recipes and remove those that have ingredients other than some set. AndyS's answer mentions filter not exist, and that's a start. Specifically, if you want to ensure that all the ingredients are, e.g., Tomato, Basil, and Cheese, then you would write a query like:
SELECT reduced ?r WHERE {
?x rdf:type rec:Receta .
?x rdfs:label ?r.
filter not exists {
?x rec:Ingrediente ?i
filter( ?i not in (rec:Tomato, rec:Basil rec:Cheese) )
}
This finds all recipes, and then removes any which have an ingredient which is not one of the allowed ingredients.
Look at FILTER NOT EXISTS.
So something like (depends of the exact nature of your data that isn't shown):
WHERE {
?x rdf:type rec:Receta .
?x rdfs:label ?r.
FILTER NOT EXISTS {?x rec:Ingrediente rec:Tomato }
}
which is all ?x, ?r where ?x does not have property/value "rec:Ingrediente rec:Tomato".
Related
Hello I am trying to find the total number of municipalities a region has along with the name of each region and the total number of municipalities a regional unit has along with the name of the regional unit. A region consists of regional units and a regional unit consists of municipalities. Below is my query that unfortunately returns wrong results. What I am basically trying to do is group by region and get the name and the total municipalities of each region and group by regional unit and take the name and the total municipalities of each unit. Any suggestions to the right direction would be appreciated. Cheers!:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema
PREFIX strdf: <http://strdf.di.uoa.gr/ontology
PREFIX gag: <http://geo.linkedopendata.gr/gag/ontology/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT ?region ?municipality_region ?unit ?municipality_unit
WHERE
{
{ SELECT ?region (COUNT(?municipality) AS ?municipality_region)
{?m rdf:type gag:Δήμος .
?m gag:έχει_επίσημο_όνομα ?municipality .
?m gag:ανήκει_σε ?reg_un .
?reg_un gag:ανήκει_σε ?reg .
?reg gag:έχει_επίσημο_όνομα ?region .
}GROUP BY ?region}
{ SELECT ?unit (COUNT(?municipality_un) AS ?municipality_unit)
{ ?m rdf:type gag:Δήμος .
?m gag:έχει_επίσημο_όνομα ?municipality_un .
?m gag:ανήκει_σε ?reg_un .
?reg_un gag:έχει_επίσημο_όνομα ?unit .
} GROUP BY ?unit}
};
Below I am giving a mapping of properties in english:
Δήμος = municipality
έχει_επίσημο_όνομα = has name
ανήκει_σε = belongs to
And here is the ontology I am working with:
link
I have an "EquivalentTo" definition in Protege of a class EquivClass as (hasObjProp some ClassA) and (has_data_prop exactly 1 rdfs:Literal)
Is there a form of SPARQL query for GraphDB 9.4 to get the "direct" answer to a select query of an equivalent class without having to collect and traverse all the constituent blank nodes explicitly? Basically, I'm looking for a short cut. I'm not looking to get instances of the equivalent class, just the class definition itself in one go. I've tried to search for answers, but I'm not really clear on what possibly related answers are saying.
I'd like to get something akin to
(hasObjProp some ClassA) and (has_data_prop exactly 1 rdfs:Literal)
as an answer to the SELECT query on EquivClass. If the answer is "not possible", that's enough. I can write the blank node traversal with the necessary properties myself.
Thanks!!
Files are -
Ontology imported into GraphDB: tester.owl - https://pastebin.com/92K7dKRZ
SELECT of all triples from GraphDB *excluding* inferred triples: tester-graphdb-sparql-select-all-excl-inferred.tsv - https://pastebin.com/fYdG37v5
SELECT of all triples from GraphDB *including* inferred triples: tester-graphdb-sparql-select-all-incl-inferred.tsv - https://pastebin.com/vvqPH1FZ
Added sample query in response to #UninformedUser. I use "select *" for example, but really I'm interested in the "end results", ie, ?fp, ?fo, ?rop, ?roo. Essentially, I'm looking for something simpler and more succinct than what I have below.The example I posted only has a single intersection ("and" clause). In my real world set, there are multiple equiv classes with different numbers of "and" clauses.
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX : <http://www.semanticweb.org/ontologies/2020/9/tester#>
select * where {
:EquivClass owl:equivalentClass ?bneq .
?bneq ?p ?bnhead .
?bnhead rdf:first ?first .
?first ?fp ?fo .
?bn3 rdf:rest ?rest .
?rest ?rp ?ro .
?ro ?rop ?roo .
filter(?bn3 != owl:Class && ?ro!=rdf:nil)
}
You can unroll the list using a property path:
prefix : <http://www.semanticweb.org/ontologies/2020/9/tester#>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select * {
:EquivClass owl:equivalentClass/owl:intersectionOf/rdf:rest*/rdf:first ?restr.
?restr a owl:Restriction .
optional {?restr owl:onProperty ?prop}
optional {?restr owl:cardinality ?cardinality}
optional {?restr owl:someValuesFrom ?class}
}
This returns:
| | restr | prop | cardinality | class |
| 1 | _:node3 | :hasObjProp | | :ClassA |
| 2 | _:node5 | :has_data_prop | "1" ^^xsd:nonNegativeInteger | |
I am trying to select one label and comment of classes but group by doesn't work as expected. The following query is an example of the select.
SELECT ?class ?label ?comment WHERE
{
{SELECT DISTINCT ?class WHERE { {?uri rdf:type ?class}UNION {?class rdf:type owl:Class} UNION {?class rdf:type rdfs:Class} }OFFSET 0 LIMIT 100}
.optional{?class rdfs:label ?label}
.optional{?class rdfs:comment ?comment}
}GROUP BY ?class
The goal is to to have every class uri with one label and comment
But am getting results as :
http://dbpedia.org/ontology/Activity "attività"#it
http://dbpedia.org/ontology/Activity "活動"#ja
Any idea ?
Your query is actually illegal - it uses non-aggregate, non-group key in the outer SELECT.
You need to use "SAMPLE" to one pick (random) item from a group if you think there might be multiple labels or comments.
SELECT ?class (sample(?labelX) as ?label) (sample(?commentX) as ?comment) WHERE
{
SELECT DISTINCT ?class {
{?uri rdf:type ?class} UNION
{?class rdf:type owl:Class} UNION
{?class rdf:type rdfs:Class}
} LIMIT 100
optional{?class rdfs:label ?labelX}
optional{?class rdfs:comment ?commentX}
} GROUP BY ?class
In the following example the classes enter code here :Class3 and :Class4 are inferred by OWL reasoner (e.g. Pellet) as types of the individual :Ind1:
#prefix : <http://www.semanticweb.org/test/2015/1/ontology#> .
#prefix owl: <http://www.w3.org/2002/07/owl#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix xml: <http://www.w3.org/XML/1998/namespace> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#base <http://www.semanticweb.org/test/2015/1/ontology> .
<http://www.semanticweb.org/test/2015/1/ontology> rdf:type owl:Ontology .
:Prop1 rdf:type owl:DatatypeProperty .
:Prop2 rdf:type owl:DatatypeProperty .
:Prop3 rdf:type owl:DatatypeProperty .
:Class1 rdf:type owl:Class ;
owl:equivalentClass [ rdf:type owl:Restriction ;
owl:onProperty :Prop1 ;
owl:someValuesFrom xsd:string
] .
:Class2 rdf:type owl:Class ;
owl:equivalentClass [ rdf:type owl:Restriction ;
owl:onProperty :Prop2 ;
owl:someValuesFrom xsd:string
] .
:Class3 rdf:type owl:Class ;
owl:equivalentClass [ rdf:type owl:Restriction ;
owl:onProperty :Prop3 ;
owl:someValuesFrom xsd:string
] .
:Class4 rdf:type owl:Class ;
owl:equivalentClass [ rdf:type owl:Class ;
owl:intersectionOf ( :Class1
:Class2
)
] .
:Class5 rdf:type owl:Class ;
owl:equivalentClass [ rdf:type owl:Class ;
owl:unionOf ( :Class3
:Class4
)
] .
:Ind1 rdf:type owl:NamedIndividual ;
:Prop2 "prop2" ;
:Prop1 "prop1" ;
:Prop3 "prop3" .
The :Class4 e.g. is inferred by reasoner based on properties :Prop1 and :Prop2 of the :Ind1.
I need to construct an individual of type :Class4 from the :Ind1, something like this:
:Ind_Class4 rdf:type :Class4
:Ind_Class4 :Prop1 "Prop1"
:Ind_Class4 :Prop2 "Prop2"
I'm looking how to select the properties :Prop1 and :Prop2 of the :Ind1 as the properties of the class :Class4.
I've tried the SPARQL query
select * where {
?s rdf:type :Class4 .
?s ?p ?o .
}
but it returns all properties of :Ind1 - :Prop1, :Prop2 and :Prop3:
:Ind1 :Prop1 "Prop1"
:Ind1 :Prop2 "Prop2"
:Ind1 :Prop3 "Prop3"
If I change ontology as suggested in Answer1:
:Prop1 rdf:type owl:DatatypeProperty ;
rdfs:domain :Class1 .
:Prop2 rdf:type owl:DatatypeProperty ;
rdfs:domain :Class2 .
:Prop3 rdf:type owl:DatatypeProperty ;
rdfs:domain :Class3 .
:Class1 rdf:type owl:Class .
:Class2 rdf:type owl:Class .
:Class3 rdf:type owl:Class .
:Class4 rdf:type owl:Class ;
owl:equivalentClass [ rdf:type owl:Class ;
owl:intersectionOf ( :Class1
:Class2
)
] .
:Class5 rdf:type owl:Class ;
owl:equivalentClass [ rdf:type owl:Class ;
owl:unionOf ( :Class3
:Class4
)
] .
:Ind1 rdf:type owl:NamedIndividual ;
:Prop1 "Prop1" ;
:Prop3 "Prop3" ;
:Prop2 "Prop2" .
then the suggested SPARQL query
select * where {
?p rdfs:domain :Class4 .
?s ?p ?o .
}
returns an empty resultset.
Thanks.
It's not entirely clear what you're asking, since properties don't belong to classes in OWL. Instead, properties can have domains; when a property P has domain D, it means that any time there is a triple x P y, you can infer that x rdf:type D. Now, you could ask for the properties and values for an individual where a domain of the property is some particular class. That is, you could do something like:
select ?property ?value where {
?property rdfs:domain :Class4 .
:individual ?property ?value .
}
However, note one caveat: properties don't have a single domain, and if you're using inference, they'll often have a lot. Remember that "p's domain is D" means (in OWL) that "x p y implies x rdf:type D." Suppose you have a class A and a subclass of it B. Suppose that a domain of a property P is B. That means that whenever x p y, we have that x rdf:type B. But, since B is a subclass of A, that means that it's also the case that x rdf:type A. That means that x p y also implies that x rdf:type A. That, in turn, means that A is a domain of P as well. I point this out, because this means that when you ask
select ?property ?value where {
?property rdfs:domain :Class4 .
:individual ?property ?value .
}
you'll also be getting any properties that have a declared domain that is a subclass of Class4.
The following code queries DBpedia for places within a bounded geographic area and returns the name, lat, and long of the place. I'd also like the query to return the category of the place--e.g., park, restaurant, museum, etc.
The following code works fine.
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery("""
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX category: <http://dbpedia.org/resource/Category:>
SELECT * WHERE {
?s a dbo:Place .
?s geo:lat ?lat .
?s geo:long ?long .
I tried to add the following code to get categories for places, but this doesn't work:
?s category:cat ?cat .
What should I add/change? Thanks.
You can get the category of a place (assuming you mean the type) by finding the type (rdfs:type) or the subject (dcterms:subject) of a resource. In DBPedia the first relates to the DBPedia and Yago ontologies and the second is a SKOS hierarchy in DBPedia. Here is an example query:
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dcterms: <http://purl.org/dc/terms/>
SELECT * WHERE {
?s a dbo:Place .
?s geo:lat ?lat .
?s geo:long ?long .
?s a ?type .
?s dcterms:subject ?sub
}
Note that you will get multiple types and subjects for each place.