SPARQL How to get the type of an individual - sparql

I made this ontology:
SensorOntology:MedicalCabinet-01 rdf:type owl:NamedIndividual ,
SensorOntology:MedicalCabinetSensor ;
SensorOntology:hasId "57"^^xsd:int ;
SensorOntology:hasValue "0"^^xsd:int .
I am trying to write a query to get the type of MedicalCabinet-01 which is MedicalCabinetSensor by given id number. However, I am printing owl:NamedIndividual and SensorOntology:MedicalCabinetSensor. Is it anyway I print just SensorOntology:MedicalCabinetSensor. This is my query:
SELECT DISTINCT ?sensor ?sensorclass
WHERE {?sensor :hasId "100"^^xsd:int.
?sensor rdf:type ?sensorclass}

There are multiple ways to ignore results, so it's not clear which one you want to use - especially since some are quite obvious. Here are two options:
As you tagged it with Jena, use Java and ignore resources whose URI matches owl:NamedIndividual.
Use a FILTER in SPARQL, i.e. add FILTER(?sensorclass != owl:NamedIndividual) to the graph pattern of the SPARQL query.

Indeed, there are multiple ways. Here's another:
SELECT DISTINCT ?sensor ?sensorclass
WHERE {?sensor :hasId "100"^^xsd:int.
?sensor rdf:type ?sensorclass
FILTER ( strstarts(str(?sensorclass), "[sensor ontology URI]") )
}

Related

Improve sparql query to extract all the words of different categories from a Dbnary (dbpedia)

I am trying to retrieve from Dbnary database based on dbpedia all words belongigs to categorie of noun, adjectif, adverb and verb and their synonyms.
So far I get can access it just for one categorie but I would like to know how to improve my query to get all words from the 4 categories and their synonym with only one query rather than making one query per category (i.e 4 query and 4 files)
#Query for noun which work
SELECT DISTINCT * where {
?l a ontolex:LexicalEntry;
lime:language "fr";
ontolex:canonicalForm ?f ;
lexinfo:partOfSpeech lexinfo:noun ;
ontolex:sense ?s .
?f ontolex:writtenRep ?w .
OPTIONAL { ?l dbnary:synonym ?syn ; rdfs:label ?synonyme .}
}
LIMIT 100
This query work , you can test it since dbnary in online : http://kaiko.getalp.org/sparql
but I wanted to upgrade the query below. When adding the following line on the previous query, it does not work :
#lexinfo:partOfSpeech lexinfo:verb ;
Any help will be fine.

Assigning specific values to multiple variable in SPARQL

I am trying to query the WikiData database using their SPARQL endpoint. Here I want to assign specific values to certain variables independent from each other. I know that the VALUES keyword can be used for this, however, is there a way of doing this for multiple variables without having to specify each possible combination? For example, the query looks like this:
SELECT ?relation1 ?item1?
WHERE {wd:Q31 ?relation1 ?item1 .}
I would like to specify the values for both ?relation1 and ?item1 without having to go over each possible combination.
Thank you.
Edited answer:
Yes, you can use multiple VALUES statements.
This can be written like this:
SELECT *
WHERE {
VALUES ?person {wd:Q35332 wd:Q23844} #Brad Pitt and Geroge Clooney
VALUES ?property {wdt:P27 wdt:P19} #Country of nationality and place of birth
?person ?property ?o ;
rdfs:label ?personLabel .
?o rdfs:label ?oLabel .
FILTER(LANG(?personLabel) = 'en' && LANG(?oLabel) = 'en')
}
You will then see these results:
This link will contain more information.

How can I use SPARQL to find instances where two properties do not share any of the same objects?

In my company's taxonomy, all concepts have a value for skos:prefLabel, and most have a bunch of values for a custom property -- let's call it ex:keyword -- whose values have the datatype rdf:langString. I want to find concepts where the value of skos:prefLabel does NOT exactly match ANY value of ex:keyword, when case and language are ignored.
It is fairly straightforward to write a query for concepts where the values DO match, like this:
SELECT *
WHERE {
?concept skos:prefLabel ?label ;
ex:keyword ?kw
FILTER (lcase(str(?label)) = lcase(str(?kw)))
}
Where I'm getting tripped up is in trying to negate this.
Using != in the FILTER would just return a bunch of cases where ?label and ?kw don't match, which is not what I'm after.
What I would like is to be able to use a FILTER NOT EXISTS, but that's invalid with an expression like (?a = ?b); it only works with something like {?a ?b ?c}.
I suspect that there is a proper way to express FILTER NOT EXISTS (?a = ?b) in SPARQL, but I don't know what it is. Can someone help?
The trick is to put the triple pattern for matching the keyword in the FILTER NOT EXISTS, like so:
SELECT *
WHERE {
?concept skos:prefLabel ?label .
FILTER NOT EXISTS { ?concept ex:keyword ?kw .
FILTER(lcase(str(?label)) = lcase(str(?kw)))
}
}
This query says "I want all concepts with a preflabel such that the concept has no keyword value that matches that prefLabel".

Getting a list of available hierarchies from statistics.gov.scot

I'm interested in obtaining a list of available distinct hierarchies from statistics.gov.scot. The best-fit hierarchies, which I would like to list, are as follow:
http://statistics.gov.scot/def/hierarchy/best-fit#community-health-partnership
http://statistics.gov.scot/def/hierarchy/best-fit#council-area
http://statistics.gov.scot/def/hierarchy/best-fit#country
As available through API section of this sample geography.
Desired results
I would like for the desired results to return:
community-health-partnership
council-area
country
How can I construct query that would actually produce that, I can get a list of available all geographies via:
PREFIX sdmx: <http://purl.org/linked-data/sdmx/2009/dimension#>
SELECT DISTINCT ?framework
WHERE {
?a sdmx:refArea ?framework .
} LIMIT 10
I was trying something on the lines:
PREFIX fits: <http://statistics.gov.scot/def/hierarchy/best-fit#>
SELECT DISTINCT ?framework
WHERE {
?a fits ?framework .
} LIMIT 10
but naturally this syntax is not correct.
Starting on their SPARQL endpoint, you could do something like this --
DESCRIBE <http://statistics.gov.scot/def/hierarchy/best-fit#country>
Then, based on those results, you might try something like this, which results aren't exactly what you say you want, but might be better --
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?hierarchy
?label
WHERE
{ ?hierarchy rdfs:subPropertyOf <http://statistics.gov.scot/def/hierarchy/best-fit>
; rdfs:label ?label
}

SPARQL query failing to match literal on Pubchem RDF data

I have loaded part of the PubChem RDF data in Virtuoso and am trying to use SPARQL to query it, through iSQL.
While the following query works:
SELECT ?syno ?type ?value
WHERE {
?syno sio:is-attribute-of <http://rdf.ncbi.nlm.nih.gov/pubchem/compound/CID1829049> .
?syno rdf:type ?type .
?syno sio:has-value ?value .
} LIMIT 10;
I am not able to get any results for a query like (the value being taken from one of the above results):
SELECT ?syno
WHERE {?syno sio:has-value "AC1LXI26"};
In the previous case, I am simply trying to match a litteral.
Do I need to build an extra index? Is exact text match not supported in Virtuoso?
I solved my problem by simply adding #en at the end of the query string!
SELECT ?syno
WHERE {?syno sio:has-value "AC1LXI26"#en};