Replacing domain in JENA Triplestore - sparql

I want to replace the server of all my subjects stored in my Jena based triple store.
I tried it this way but the server isn't replaced...
DELETE { ?s ?p ?o }
INSERT { ?s1 ?p ?o }
WHERE {
{ SELECT (uri(concat('http://localhost:8080/', SUBSTR(str(?s),22))) AS ?s1)
{
?s ?p ?o .
FILTER regex(str(?s), '^https://somedomain.org/')
}
}
}
When I only run the following query
SELECT (uri(concat('http://localhost:8080/', SUBSTR(str(?s),22)) ) AS ?s1) ?s
{
?s ?p ?o .
FILTER regex(str(?s), '^https://somedomain.org/')
}
'?s' and '?s1' do have the correct values.
It seems like that '?s' and '?s1' arent available inside the DELETE/INSERT block.
Whats wrong with my update query?

Your problem is that you are using a sub-query in your update and you only project ?s1 from your sub-query.
This means ?s, ?p and ?o aren't visible outside the sub-query. Therefore when the DELETE and INSERT templates try to build triples to delete and insert all the triples they build are invalid (because not all the variables are visible) and so nothing changes.
To fix this you should either project all the relevant variables:
DELETE { ?s ?p ?o }
INSERT { ?s1 ?p ?o }
WHERE
{
{
SELECT ?s ?p ?o (uri(concat('http://localhost:8080/', SUBSTR(str(?s),22))) AS ?s1)
{
?s ?p ?o .
FILTER regex(str(?s), '^https://somedomain.org/')
}
}
}
Or follow Joshua Taylor's suggestion from the comments. This is actually nicer since it simplifies the overall query:
DELETE { ?s ?p ?o }
INSERT { ?s1 ?p ?o }
WHERE
{
?s ?p ?o .
FILTER regex(str(?s), '^https://somedomain.org/')
BIND(uri(concat('http://localhost:8080/', SUBSTR(str(?s),22))) AS ?s1)
}

Related

Wikidata SPARQL : get subclasses but also return parent class

I am writing SPARQL queries on Wikidata entities, and I would like to get all the entities matching "is entity Q3 OR one of its subclasses".
I know how to get the subclasses only with the following query :
SELECT DISTINCT ?item
WHERE {
{ ?item wdt:P279 wd:Q3 . }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
But I would like to also have the original parent entity (here Q3) in the response. How should I modify my query so that Q3 is also returned ?
In order to get entities matching something, you will need to use wdt:P31, i.e. "instance of". wdt:P279 is "subclass of"
In terms of your query, I'd use something like this:
SELECT DISTINCT ?itemLabel ?superClassLabel
WHERE {
?item wdt:P31/wdt:P279* ?superClass
VALUES ?superClass {wd:Q3}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
I ended up finding the solution myself :
I needed to add a ? (which means "0 or 1") after the property in my query.
So it becomes :
SELECT DISTINCT ?item
WHERE {
{ ?item wdt:P279? wd:Q3 . } # here is the extra "?" after "wdt:P279"
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
And now this properly returns Q3 AND all of its subclasses.

SPARQL CONSTRUCT+INSERT

I have an XMLHttpRequest with which I call a SPARQL query.
This should create a triple with a UUID and send the UUID back.
How do I correctly formulate this query?
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX schem: <http://schema.org/>
PREFIX rd: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
CONSTRUCT {
<myns:url> <schem:url> ?s
}
INSERT {GRAPH <file:///home/pi/python_rdf/models.ttl>
{
?s ?p ?o.
}
WHERE
{ SELECT ?s ?p ?o
WHERE
{
BIND(UUID() AS ?s).
BIND($p AS ?p).
BIND($o AS ?o).
}
}
}
Thank you in advance for your support

SPARQL: Number of Statements in each Named Graph

The following query returns the number of statements in a repository:
SELECT (COUNT(*) AS ?count)
WHERE {
?s ?p ?o
}
Is there a way to return the number of statements for each Named Graph?
This following query does not work, only an example:
SELECT ?graphName ?count
WHERE {
GRAPH ?graphName {
?s ?p ?o.
BIND(COUNT(?s ?p ?o.) AS ?count)
}
}
I realize that COUNT can't be in the WHERE, and it can't take variables.
Just add a GROUP BY clause to your query --
SELECT ?graphName
( COUNT ( * ) AS ?count )
WHERE
{
GRAPH ?graphName
{
?s ?p ?o
}
}
GROUP BY ?graphName
See the query and its live results on DBpedia

Sparql query containment result

Not familiar with semantic web, under the following context:
// owl in terms of Java-like syntax
Class Person {}
Class GraduateStudent extends Person {reference takesCourse [*] : GraduateCourse}
Class Student intersect Person {reference takesCourse [*] : Course}
Class UndergradStudent extends Student {}
Class Course{}
Class GraduateCourse extends Course{}
Class CsCourse extends Course{}
My question is why Q1 ⊑ Q2 does not hold? In particular, if I understand correctly, Q1 select undergraduate students take CsCourse and graduate students take GraduateCourse, and Q2 select undergraduate students take any Course and graduate students take any Course:
// Q1:
SELECT ?x ?y WHERE
{
{ ?x a :UndergradStudent .
?x :takesCourse ?y .
?y a :CsCourse
}
UNION
{ ?x a :GraduateStudent .
?x :takesCourse ?y
}
}
// Q2:
SELECT ?x ?y WHERE
{
{
{
?x a :UndergradStudent .
?x :takesCourse ?y
}
UNION
{
?x a :GraduateStudent .
?x :takesCourse ?y
}
}
?y a :Course .
}
edit: this example can be found at: http://sparql-qc-bench.inrialpes.fr/UCQrdfs.html, Q43b and Q43c in particular.

Why am I obliged to filter the graph URI in a SPARQL query?

To fetch all the triples from a named graph in my triplestore (OpenLink Virtuoso v6.1), I have written the SPARQL query:
SELECT ?s ?p ?o
WHERE {
GRAPH eg:myGraph {
?s ?p ?o.
}
}
But it seems I can't define the graph URI in the GRAPH declaration; the query doesn't return any triples.
If I use an intermediate variable ?g instead of the URI of my graph, the request works:
SELECT ?s ?p ?o
WHERE {
FILTER(?g = eg:myGraph).
GRAPH ?g {
?s ?p ?o.
}
}
I don't see the difference between the two queries.
Is my first syntax a wrong query? Is this a subtlety of Virtuoso?
you can try to run
SELECT ?s ?p ?o
FROM NAMED eg:myGraph
WHERE {
GRAPH eg:myGraph {
?s ?p ?o.
}
}
or
SELECT ?s ?p ?o
FROM eg:myGraph
WHERE {
?s ?p ?o.
}