SPARQL - how to do a join operation - sparql

I have a model, where I have 3 classes:
SR - <http://data.sample.com/vocab/StudentRecord>
HCVR - <http://data.sample.com/vocab/HealthCenterVisitRecord>
CR - <http://data.sample.com/vocab/ClassRoom>
StudentRecord has a property classRoom.
HealthCenterVisitRecord has a property studentRecord.
Given a particular ClassRoom URI, how do I find all the StudentRecords, who are properties of HealthCenterVisitRecords.
Query that I tried (The initial part of the query fetches the student belonging to a ClassRoom, but the second part is screwed up and I know it):
SELECT ?hcvr
WHERE {
?sr rdf:type <http://data.sample.com/vocab/StudentRecord>.
?sr <http://data.sample.com/vocab/classRoom> <http://data.sample.com/resource/ClassRoom/1156>.
?sr <http://data.sample.com/vocab/healthCentreVisitRecord> ?hcvr.
}
Another query I tried:
SELECT DISTINCT ?sr WHERE {
{
?sr rdf:type <http://data.sample.com/vocab/StudentRecord>.
?sr <http://data.latize.com/vocab/classRoom> <http://data.latize.com/resource/ClassRoom/1156>.
}
UNION
{
?hcvr rdf:type <http://data.sample.com/vocab/HealthCentreVisitRecord>.
?hcvr <http://data.sample.com/vocab/studentRecord> ?sr.
}
}
LIMIT 1000

Figured it out, here is the query:
SELECT ?hcvr
WHERE {
?sr rdf:type <http://data.sample.com/vocab/StudentRecord>.
?sr <http://data.sample.com/vocab/classRoom> <http://data.sample.com/resource/ClassRoom/1156>.
?hcvr rdf:type <http://data.sample.com/vocab/healthCentreVisitRecord>.
?hcvr <http://data.sample.com/vocab/studentRecord> ?sr.
}

Related

How to filter distinct regex matches with SPARQL?

I am querying a (kind of) bibliographic database and would like to find all the distinct matches of a certain regex (matching the signature of typescripts (TS) and manuscripts (MS)); i.e. I would like to return all documents that are currently in the database.
I came up with
SELECT ?document
WHERE
{
{
?documentURI a witt:MS;
rdfs:label ?document.
}
UNION
{
?documentURI a witt:TS;
rdfs:label ?document.
}
FILTER (regex(?document, "(Ms|Ts)\\-((1|2|3)\\d{2}\\w?\\d?)"))
}
(endpoint); this returns all the signatures but I would like to filter the result for the distinct regex matches, i.e. the distinct signatures up to and excluding the comma.
How can this be achieved?
Ok, I think I found a solution with strbefore:
SELECT DISTINCT ?document
WHERE
{
{
?documentURI a witt:MS;
rdfs:label ?documentFull.
}
UNION
{
?documentURI a witt:TS;
rdfs:label ?documentFull.
}
BIND (strbefore(?documentFull, ",") AS ?document)
}
Try it.
Would appreciate opinions on the query though, is this effective/good style?

SPARQL: get object only if predicate exists

I want to get the sources of a dbo:Cheese. Some dbo:Cheese have dbo:Animal as dbp:source. Some others have only text. I would like to get the source information in the ?sources variable regardless of that the source is dbo:Animal or text . How should I do it ?
SELECT ?cheese
CONCAT(GROUP_CONCAT(DISTINCT ?source; SEPARATOR=", "), ", ", GROUP_CONCAT(DISTINCT ?source_label; SEPARATOR=", ")) AS ?sources
WHERE {
?cheese a dbo:Cheese .
optional { # some cheeses don't have a source informed
?cheese dbp:source ?source .
}
optional { # some sources are dbo:Animal, some others are xsd:string
?source a dbo:Animal ;
rdfs:label ?source_label .
FILTER(langMatches(lang(?source_label), "EN"))
}
}
LIMIT 10
I tried this. Problem is that I have both http://dbpedia.org/resource/Sheep and Sheep in ?sources.

Why are some of the classes in my ontolgoy not being returned in my SPARQL query?

I'm rather confused about why I'm receiving too little results (256 expected, 224 returned). When I run the code below, everything returns exactly as I want it, except that I miss all the classes in my ontology which lie at the highest level, or one below the highest level. I don't understand where I'm being too "strict" in my query so that these classes are not being returned in the table. They also all have parent classes (whether that be the topmost class "top concept" of the ontology, or something like an enumeration type class, either way, the leaf should still be found. Would be thankful for tips or pointers where my code might be inadvertently filtering out those classes.
SELECT DISTINCT ?leaf ?parentclasses
WHERE {
GRAPH <>
#a leaf is the lowest level class: A class that does not have a subclass
{
{
{
#I want anything which is a class
{
?leaf rdf:type owl:Class.
}
#i also want the subclass of any superclass if that exists
{
?leaf rdfs:subClassOf ?superclass .
}
#squeezed to specific section of OTL.
filter strstarts(str(?leaf), "URIgoeshere")
#Only keep the results that do not have a preflabel
OPTIONAL {
?leaf skos:prefLabel ?subclasslabel.
}
#make sure the subclasslabel is in dutch
#filter( langMatches(lang(?subclasslabel),"nl") )
#give me the label of the superclass
OPTIONAL {
?superclass skos:prefLabel ?superclasslabel.
}
#make sure it's in dutch
FILTER (lang(?superclasslabel) = "nl")
#if it exists, give me also the superclass of the superclass creating a supersuperclass
{
?superclass rdfs:subClassOf ?supersuperclass.
#give me the label of the supersuperclass
OPTIONAL {
?supersuperclass skos:prefLabel ?supersuperclasslabel.
}
#make sure it's in dutch
FILTER (lang(?supersuperclasslabel) = "nl")
#keep the leafs that are NOT The values whereby the subclass is not empty. (double negative for removing leafs where the subclass has a subclass below it)
FILTER NOT EXISTS {
?subclass rdfs:subClassOf ?leaf
FILTER (?subclass != owl:Nothing )
}
#concatenate the two parentclass variables into one
BIND(concat(str(?superclasslabel), str("-"), str(?supersuperclasslabel) ) as ?parentclasses)
}
}
}
}
}
Here is a ttl file with the same structure as my database: https://file.io/jjwkAWbK4jrF
Below is my end solution to my problem. It was more complicated than I expected, but it works.
The problem was that these classes that didn't have a parent class were not being accepted by the query. With some union cases this could be covered for 0 parent classes, 1 parent class or 2 parent classes.
SELECT DISTINCT ?leaf ?parentclasses
WHERE {
GRAPH <>
#a leaf is the lowest level class: A class that does not have a subclass
{{{{{
#I want anything which is a class
{?leaf rdf:type owl:Class.}
#squeezed
filter strstarts(str(?leaf), "graph")
#keep the leafs that are NOT The values whereby the subclass is not empty.
(double negative for removing leafs where the subclass has a subclass below it)
FILTER NOT EXISTS {?subclass rdfs:subClassOf ?leaf
FILTER (?subclass != owl:Nothing ) }
}
{
{?leaf rdfs:subClassOf ?superclass .}
#grab dutch label if available
optional {
?superclass skos:prefLabel ?superclassnllabel .
filter( langMatches(lang(?superclassnllabel),"nl") )
}
# take either as the label, but dutch over empty
bind( coalesce( ?superclassnllabel, replace(str(?
superclass),"^[^#]*#", "" ) ) as ?superclasslabel )
{
{?superclass rdfs:subClassOf ?supersuperclass.}
#grab dutch label if available
?supersuperclass skos:prefLabel ?supersuperclassnllabel .
filter( langMatches(lang(?supersuperclassnllabel),"nl") )
# take either as the label, but dutch over empty
bind( coalesce( ?supersuperclassnllabel, replace(str(?
supersuperclass),"^[^#]*#", "" ) ) as ?supersuperclasslabel )
BIND(concat(str(?superclasslabel), str(" - "), str(?
supersuperclasslabel) ) as ?parentclasses)
}
union
{
{?superclass ?p ?o.filter(!isblank(?superclass))}
FILTER NOT EXISTS {?superclass rdfs:subClassOf ?supersuperclass}
BIND(concat(str(?superclasslabel), str(" - "), str("Top
Concept") ) as ?parentclasses)
#concatenate the two parentclass variables into one
}
}
}
union
{
#figure this out, WHY IS IT HERE?
{?leaf rdf:type owl:Class .filter(!isblank(?leaf))}
FILTER strstarts(str(?leaf), "graph")
FILTER NOT EXISTS {?leaf rdfs:subClassOf ?superclass}
FILTER NOT EXISTS {?subclass rdfs:subClassOf ?leaf
FILTER (?subclass != owl:Nothing ) }
BIND (str("Top Class") as ?parentclasses )
}
}}}}

SPARQL FILTER() using Jena not working properly

i'm running Ontology project on Netbeans using Jena for SPARQL (version 2.6.2 ) Queries, and now i'm trying to FILTERS for Temperature , this code doesn't work as expected and return no result
PREFIX ns: <http://www.semanticweb.org/pavilion/ontologies/2017/5/untitled-ontology-66#>
SELECT ?StarName ?Temperature
WHERE {
?star a ns:Star ;
ns:possessesSpectralType ?SpectralType ;
ns:possessesStarName ?StarName ;
ns:possessesTemperature ?Temperature .
FILTER (?Temperature > 10 ).
}
on the other hand i've tried this code and it works but only with Equal operator
PREFIX ns: <http://www.semanticweb.org/pavilion/ontologies/2017/5/untitled-ontology-66#>
SELECT ?star
WHERE {
?star a ns:Star ;
ns:possessesSpectralType ?SpectralType ;
ns:possessesStarName ?StarName ;
ns:possessesTemperature ?Temperature .
FILTER (?Temperature = ns:168 )
}

How to simulate or change the GROUP BY in an SPARQL Query?

I have executed successfully this query in D2RQ, but, since performance problems I'm using now Ontop which performs faster but it does not support aggregated functions like GROUP BY, which in the case of this query is key to get the desired results.
Any idea how can I do a similar query without using the GROUP BY?
PREFIX d2r: <http://sites.wiwiss.fu-berlin.de/suhl/bizer/d2r-server/config.rdf#>
PREFIX v: <http://www.vocab.com/resource/vocab/>
SELECT ?hour ((?latest - ?earliest) as ?value) {
{
SELECT ?hour (MIN(?value) as ?earliest) WHERE {
?s v:time ?time;
v:value ?value
}
GROUP BY (HOURS(?time) as ?hour)
}
{ SELECT ?hour (MAX(?value) as ?latest) WHERE {
?s v:time ?time;
v:value ?value
}
GROUP BY (HOURS(?time) as ?hour)
}
FILTER (?hour>= timeFrom && ?hour <= timeTo)
}
ORDER BY ?hour