I'm looking for an easy way to insert triples from two or more named graphs (but not the entire unnamed default graph) into another named graph. I'm using GraphDB.
I guess this could be done by writing out the same query multiple times in the WHERE section, wrapped in multiple GRAPH specifications, and then unioning them together, but my WHEREs are long and I'd prefer not to write them out multiple times.
Let's say I have loaded some data like this:
INSERT DATA {
GRAPH <http://example.com/ngA> {
<http://example.com/person1> <http://example.com/name> "Arthur" .
}
GRAPH <http://example.com/ngB> {
<http://example.com/person1> <http://example.com/name> "Brian" .
}
GRAPH <http://example.com/ngC> {
<http://example.com/person1> <http://example.com/name> "Charlie" .
}
}
I can copy all of the triples of a certain pattern from the default unnamed graph into a new named graph with something like this:
INSERT {
GRAPH <http://example.com/ngZ> {
?s <http://example.com/moniker> ?o .
}
}
WHERE
{ ?s <http://example.com/name> ?o }
An easy way to SELECT for triples of a given pattern from two or more (but not all) named graphs is
SELECT *
FROM <http://example.com/ngA>
FROM <http://example.com/ngB>
WHERE
{ ?s <http://example.com/name> ?o }
What if I want to copy those triples, from those specified graphs, into another graph?
I'm getting an error from GraphDB 8.3 (and from the sparql.org validator) when I try to
INSERT {
GRAPH <http://example.com/ngZ> {
?s <http://example.com/moniker> ?o .
}
}
WHERE
{ SELECT *
FROM <http://example.com/ngA>
FROM <http://example.com/ngB>
WHERE
{ ?s <http://example.com/name> ?o } }
Try this query:
PREFIX ex: <http://example.com/>
INSERT {
GRAPH ex:ngZ { ?s ex:moniker ?o }
}
WHERE {
GRAPH ?g { ?s ex:name ?o }
FILTER (?g IN ( ex:ngA, ex:ngB ) )
}
And then:
PREFIX ex: <http://example.com/>
SELECT *
FROM NAMED ex:ngZ
WHERE {
GRAPH ?g { ?s ?p ?o }
} LIMIT 100
Is it what you need?
By the way, there exist COPY (use with caution!) and ADD.
SPARQL Update provides USING and USING NAMED analogous to FROM and FROM NAMED in queries:
The USING and USING NAMED clauses affect the RDF Dataset used while evaluating the WHERE clause. This describes a dataset in the same way as FROM and FROM NAMED clauses
You can express the requirement as an UPDATE like so:
INSERT {
GRAPH <http://example.com/ngZ> {
?s <http://example.com/moniker> ?o .
}
}
USING <http://example.com/ngA>
USING <http://example.com/ngB>
WHERE
{ ?s <http://example.com/name> ?o }
Also note that, according to the SPARQL query grammar, a subquery does not admit a dataset clause. This is why the SPARQL parsers are rejecting your query.
Thanks, #Stanislav Kralin
Come to think of it, this also works:
PREFIX ex: <http://example.com/>
INSERT {
GRAPH ex:ngZ {
?s ex:moniker ?o
}
}
WHERE {
values ?g {
ex:ngA ex:ngB
}
GRAPH ?g {
?s ex:name ?o
}
}
Related
I want to edit a set of URIs replacing a substring "iso-693" with "iso-639" using a SPARQL query. I am using REPLACE but it doesn't seem to do anything.
I have a large SKOS taxonomy with URIs that have an incorrect string. They should have this string: "iso-639" but I made a mistake when creating it and put "iso-693". I'd like to correct it. I used the SPARQL query shown below, which when run returns a message "update successful", but none of the triples data actually changes. Where am I going wrong?
INSERT
{
?s ?p ?o2
}
WHERE
{
?s ?p ?o .
FILTER (regex(str(?s), "iso-693") || regex(str(?o), "iso-693"))
BIND(REPLACE(?o, "iso-693", "iso-639", "i") AS ?o2) .
}
I expected all of the occurrences of the substring to change to the desired value, but nothing seems to change at all despite the success message.
You are missing the bit that removes the old value (INSERT just adds a new triples). To replace a triple, you should DELETE the old triple at the same time as you are INSERTing the new one, like this:
DELETE
{
?s ?p ?o
}
INSERT
{
?s ?p ?o2
}
WHERE
{
?s ?p ?o .
FILTER (regex(str(?s), "iso-693") || regex(str(?o), "iso-693"))
BIND(REPLACE(?o, "iso-693", "iso-639", "i") AS ?o2) .
}
If you are targeting URIs then you need to construct new IRIs with the required substitution and use these in the INSERT part of the update along with the original values fro ?s and ?o for DELETE part. The REPLACE will produce Literals which is not correct fro subjects.
Suggest using something along following lines:
DELETE {
?s ?p ?o
}
INSERT {
?newS ?p ?newO
} WHERE {
?s ?p ?o .
bind("iso-693" as ?match) .
bind("iso-639" as ?replacement) .
bind (regex(str(?s), ?match) as ?subjMatch) .
bind (regex(str(?o), ?match) as ?objMatch) .
filter (?subjMatch || ?objMatch)
bind (if(?subjMatch, IRI(replace(str(?s), ?match, ?replacement)), ?s) as ?newS)
bind (if(?objMatch, IRI(replace(str(?o), ?match, ?replacement)), ?o) as ?newO)
}
With the RDF query language SPARQL, I'm trying to find a way to do a boolean query (or any other query) for anything not in a Named Graph.
ASK { GRAPH null { ?s ?p ?o } }
Can't find really any documentation on searching specifically within an empty Named Graph. Also tried replacing null with <>, empty, and (nothing).
This query will look for triples in the default graph, then remove ones that are also in a named graph:
SELECT ?s ?p ?o {
?s ?p ?o
FILTER NOT EXISTS { GRAPH ?g { ?s ?p ?o } }
}
Is there a way to make the following query case insensitive?
ASK {
VALUES (?r) { (dbr:Game_of_Thrones) }
{ ?r ?p ?o }
UNION
{ ?s ?r ?o }
UNION
{ ?s ?p ?r }
}
It shall return true no matter if I use dbr:Game_of_Thrones or dbr:game_of_thrones.
I tried using LCASE() but it's not allowed inside the triples. Is there another way?
I'm using the DBpedia SPARQL endpoint (https://dbpedia.org/sparql).
IRIs are case-sensitive for good reasons. If you really want to workaround this then you have to use FILTER(lcase(str(?tmp)) = lcase(str(?r))) in each UNION clause:
ASK {
VALUES (?r) { (dbr:Game_of_Thrones) }
{ ?tmp ?p ?o. FILTER(LCASE(STR(tmp)) = LCASE(STR(?r))) }
UNION
{ ?s ?r ?o. FILTER(LCASE(STR(tmp)) = LCASE(STR(?r))) }
UNION
{ ?s ?p ?r. FILTER(LCASE(STR(tmp)) = LCASE(STR(?r))) }
}
Note, this can result in poor performance at it has to scan over all triples and do String comparison.
For fuzzy search, you should really think about a full-text index.
I'd also suggest omitting the second UNION clause as long as you're looking for resources that are probably not schema entities, thus, never occur in predicate position of an RDF triple.
I have the following query where I'm trying to use the id of an element from one graph to retrieve some values in another graph. However this doesn't work:
select * from <mygraph2#> where {
?s ?p ?id in {select ?id from <mygraph1#> where { ?id ?t "MyService" }
}
You don't select from a subselect. You just execute the sub-select, and it provides some variables to the enclosing query. I'm not sure where you found an example like what you showed in the question. It's not what any of the examples in the standard (see Section 12, Subqueries) look like. Your query would be something like this (note that this isn't actually legal, since you can't use FROM in subqueries):
select * from <mygraph2#> where {
?s ?p ?id
{ select ?id from <mygraph1#>
where { ?id ?t "MyService" } }
}
However, the the graphs that you're selecting from are available as named graphs in the dataset, there's no real need for the sub-select here. You can just do
select * where {
graph <mygraph2#> { ?s ?p ?id }
graph <mygraph1#> { ?id ?t "MyService" }
}
Let me start with an example query to explain my problem:
SELECT ?g ?s ?p ?o WHERE
{
{GRAPH ?g
{ ?s ?p ?o.
OPTIONAL{ ?s
ab:temperature ?temperature.}
FILTER (?temperature = 20)
FILTER NOT EXISTS {?s ab:person ?person}
}
}
}
This query gives me all graphs (in this case representing context data) that have a temperature of 20 but don't have a person associated. My problem is I want to query the graphs for certain optional properties but they shouldn't have any other properties. At the time of the query I only know the OPTIONAL part but I don't know which additional property might be there. Is there an easy way to do this with SPARQL or is that something that would be easier to check after I received the graph and converted it to an object which I can handle with my programm?
If i understand your question correctly, you are searching for graphs that only have that subjects with some properties but not others. In that case i'd run something like this:
SELECT ?g ?s ?p ?o WHERE {
GRAPH ?g {
?s ?p ?o.
FILTER NOT EXISTS {
?s ?bad [] .
FILTER (?bad NOT IN ( ab:temperature, ... ) )
}
}
}