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

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

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 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 )
}

Filter by predicate attributes

How can I get all chairpersons, which are still holding this job (where the predicate has no end date?).
My current version returns all chairpersons:
SELECT ?chairperson ?x WHERE {
?university wdt:P488 ?chairperson.
}

SPARQL - how to do a join operation

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.
}

Searching semantically tagged documents in MarkLogic

Can any one please point me to some simple examples of semantic tagging and querying semantically tagged documents in MarkLogic?
I am fairly new in this area,so some beginner level examples will do.
When you say "semantically tagged" do you mean regular XML documents that happen to have some triples in them? The discussion and examples at http://docs.marklogic.com/guide/semantics/embedded are pretty good for that.
Start by enabling the triple index in your database. Then insert a test doc. This is just XML, but the sem:triple element represents a semantic fact.
xdmp:document-insert(
'test.xml',
<test>
<source>AP Newswire</source>
<sem:triple date="1972-02-21" confidence="100">
<sem:subject>http://example.org/news/Nixon</sem:subject>
<sem:predicate>http://example.org/wentTo</sem:predicate>
<sem:object>China</sem:object>
</sem:triple>
</test>)
Then query it. The example query is pretty complicated. To understand what's going on I'd insert variations on that sample document, using different URIs instead of just test.xml, and see how the various query terms match up. Try using just the SPARQL component, without the extra cts query. Try cts:search with no SPARQL, just the cts:query.
xquery version "1.0-ml";
import module namespace sem = "http://marklogic.com/semantics"
at "/MarkLogic/semantics.xqy";
sem:sparql('
SELECT ?country
WHERE {
<http://example.org/news/Nixon> <http://example.org/wentTo> ?country
}
',
(),
(),
cts:and-query((
cts:path-range-query( "//sem:triple/#confidence", ">", 80) ,
cts:path-range-query( "//sem:triple/#date", "<", xs:date("1974-01-01")),
cts:or-query((
cts:element-value-query( xs:QName("source"), "AP Newswire"),
cts:element-value-query( xs:QName("source"), "BBC"))))))
In case you are talking about enriching your content using semantic technology, that is not directly provided by MarkLogic.
You can enrich your content externally, for instance by calling a public service like the one provided by OpenCalais, and then insert the enrichments to the content before insert.
You can also build lists of lookup values, and then using cts:highlight to mark such terms within your content. That could be as simple as:
let $labels := ("MarkLogic", "StackOverflow")
return
cts:highlight($doc, cts:word-query($labels), <b>{$cts:text}</b>)
Or with a more dynamic replacement using spraql:
let $labels := map:new()
let $_ :=
for $result in sem:sparql('
PREFIX demo: <http://www.marklogic.com/ontologies/demo#>
SELECT DISTINCT ?label
WHERE {
?s a demo:person.
{
?s demo:fullName ?label
} UNION {
?s demo:initialsName ?label
} UNION {
?s demo:email ?label
}
}
')
return
map:put($labels, map:get($result, 'label'), 'person')
return
cts:highlight($doc, cts:word-query(map:keys($labels)),
let $result := sem:sparql(concat('
PREFIX demo: <http://www.marklogic.com/ontologies/demo#>
SELECT DISTINCT ?s ?p
{
?s a demo:', map:get($labels, $cts:text), ' .
?s ?p "', $cts:text, '" .
}
'))
return
if (map:contains($labels, $cts:text))
then
element { xs:QName(fn:concat("demo:", map:get($labels, $cts:text))) } {
attribute subject { map:get($result, 's') },
attribute predicate { map:get($result, 'p') },
$cts:text
}
else ()
)
HTH!