Querying two RDF graphs in SPARQL - sparql

I have two RDF knowledge bases
KB1 (path/to/file1.rdf) wkich includes the two following triples
a b c
a f e
and KB2 (path/to/file2.rdf) with the following triple:
c t p
I want to get all paths include ones like ?a ?b ?c & ?c ?t ?p as c is common.
How can I do that in SPARQL?

In case of two KB, it is what we call a "Federated Query". Here is an example :
SELECT * WHERE {
SERVICE URI_for_path/to/file1.rdf {
?a ?b ?c .
OPTIONAL {
SERVICE URI_for_path/to/file2.rdf {
?c ?t ?p . } }
}
}
You will get ?t ?p only if it exists by the way.
Simpliest way is to load both files in a single KB and therefore have a simple query :
SELECT * WHERE {
?a ?b ?c .
?c ?t ?p .
}

Related

SPARQL delete if exists

Let's assume the following Graph as an turtle file:
pf:A a CLASSA
rdfs:label "a"
pf:B a CLASSB
rdfs:label "b"
pf:has A
My Goal is an query to delete A, and if B exists also B.
The current Query expects A and B to exist, if A exists without B nothing is deleted as the query is not satisfied.
DELETE {
?A ?p ?o .
?B ?p ?o .
}
WHERE {
?s ?p ?o
{
SELECT ?b WHERE
{
?b a CLASSB .
?b pf:has A;
}
}
}
Now my question: Is there an way SPARQL offers me to ask for the existence of B and if it exists to also delete it?
If I wrap it in OPTIONAL {} it deletes all instances of CLASSB from the turtle file.

SPARQL Query Involving Chinese Characters

I am running the following query to obtain translations for the English word "father" on this public endpoint:
PREFIX dbnary: <http://kaiko.getalp.org/dbnary#>
select distinct ?l ?written where
{
dbnary-eng:father dbnary:describes ?le .
?t dbnary:isTranslationOf ?le .
?t dbnary:targetLanguage ?l .
?t dbnary:writtenForm ?written .
}
Among the strings returned, I can also find the Chinese translation "爸爸".
When I try to reverse the query, I do not receive any result for Chinese words (it works for other languages):
PREFIX dbnary: <http://kaiko.getalp.org/dbnary#>
select distinct ?c where
{
?c dbnary:describes ?le .
?t dbnary:isTranslationOf ?le .
?t dbnary:targetLanguage ?l .
?t dbnary:writtenForm "爸爸" .
}
What am I doing wrong?
As Stanislav Kralin correctly points out, the reason for the query not working is the missing language annotation. The correct query formulation is as follows:
PREFIX dbnary: <http://kaiko.getalp.org/dbnary#>
select distinct ?c where
{
?c dbnary:describes ?le .
?t dbnary:isTranslationOf ?le .
?t dbnary:targetLanguage ?l .
{?t dbnary:writtenForm "爸爸"#yue .}
UNION {?t dbnary:writtenForm "爸爸"#cmn .}
}

SPARQL Subquery Graph Name

I have following SPARQL query that contains a sub-select. The data contains multiple graphs and I want to know what graph the values for ?b and ?m come from:
select ?b, ?m, ?g1
where {
{
select ?o1, ?o2, ?e
where{
graph ?g{
?s <http://ndssl.bi.vt.edu/chicago/vocab/dendrogram_infector_pid> ?o1.
?s <http://ndssl.bi.vt.edu/chicago/vocab/dendrogram_infectee_pid> ?o2.
?s <http://ndssl.bi.vt.edu/chicago/vocab/dendrogram_iteration> '0'^^xsd:decimal.
?s <http://ndssl.bi.vt.edu/chicago/vocab/dendrogram_exposureday> ?e.
?s1 <http://ndssl.bi.vt.edu/chicago/vocab/contactnetwork_pid1> ?o1.
?s1 <http://ndssl.bi.vt.edu/chicago/vocab/contactnetwork_pid2> ?o2.
?s1 <http://ndssl.bi.vt.edu/chicago/vocab/contactnetwork_acttype1> '5'^^xsd:decimal.
?s1 <http://ndssl.bi.vt.edu/chicago/vocab/contactnetwork_acttype2> '5'^^xsd:decimal
}
}ORDER BY ASC(?e) LIMIT 1
}
{
graph ?g1 {
?b <http://ndssl.bi.vt.edu/chicago/vocab/getInfectedBy> ?o1.
?m <http://ndssl.bi.vt.edu/chicago/vocab/getInfectedBy>* ?b.
}
}
}
The second graph pattern contains a transitive property path and the query provides following correct result:
b m g1
----------------------------------------------------- ----------------------------------------------------- -------------------------------------------------------
<http://ndssl.bi.vt.edu/chicago/person/pid#446734805> <http://ndssl.bi.vt.edu/chicago/person/pid#446753456> <http://ndssl.bi.vt.edu/chicago/dendrogram/replicate1/>
However, I want to see the intermediate nodes and count the path length from transitive relationship. If I remove graph ?g1 from the query, then it shows the intermediate node information like following:
b m
--------------------------------------------------- ---------------------------------------------------
http://ndssl.bi.vt.edu/chicago/person/pid#446718746 http://ndssl.bi.vt.edu/chicago/person/pid#446718746
http://ndssl.bi.vt.edu/chicago/person/pid#446734805 http://ndssl.bi.vt.edu/chicago/person/pid#446734805
http://ndssl.bi.vt.edu/chicago/person/pid#446734805 http://ndssl.bi.vt.edu/chicago/person/pid#446753456
The Purpose of the query is to figure out graph name for matching ?b and ?m. Hence, I want to use graph ?g1. Is it possible to show intermediate nodes by retaining the graph keyword? I am using Virtuoso.
Since you're not using g the first GRAPH statement is not necessary. Note also that the second GRAPH statement only uses ?o1, so the following query do what you want it to. You may also want to check SPARQL syntax in your select clause.
PREFIX ndssl: <http://ndssl.bi.vt.edu/chicago/vocab/>
SELECT ?b ?m ?g1
WHERE {
{
SELECT ?o1
WHERE {
?s ndssl:dendrogram_infector_pid ?o1 .
?s ndssl:dendrogram_infectee_pid ?o2 .
?s ndssl:dendrogram_iteration '0'^^xsd:decimal .
?s ndssl:dendrogram_exposureday ?e .
?s1 ndssl:contactnetwork_pid1 ?o1 .
?s1 ndssl:contactnetwork_pid2 ?o2 .
?s1 ndssl:contactnetwork_acttype1 '5'^^xsd:decimal .
?s1 ndssl:contactnetwork_acttype2 '5'^^xsd:decimal
} ORDER BY ASC(?e) LIMIT 1
}
GRAPH ?g1 {
?b ndssl:getInfectedBy ?o1 .
?m ndssl:getInfectedBy* ?b .
}
}
In the endpoint provided there isn't a match for ?b or ?m, regardless of whether a GRAPH statement is used or not.

Restrict property from being rdf:type

In a query like this, how can I avoid ?p being bound to rdf:type?
select ?p ?c where {
<http://dbpedia.org/resource/Istance_1> ?p ?c.
<http://dbpedia.org/resource/Istance_2> ?p ?c.
}
Add a filter to your query:
select ?p ?c where {
<http://dbpedia.org/resource/Istance_1> ?p ?c.
<http://dbpedia.org/resource/Istance_2> ?p ?c.
filter( ?p != rdf:type )
}
It's typical to use the prefix dbpedia: for http://dbpedia.org/resource/, and I expect that Istance is suppose to be Instance, so with a bit of cleanup, you'll have
prefix dbpedia: <http://dbpedia.org/resource>
select ?p ?c where {
dbpedia:Instance_1 ?p ?c .
dbpedia:Instance_2 ?p ?c .
filter( ?p != rdf:type )
}
If you're copying and pasting into the public DBpedia SPARQL endpoint, you won't need to define that prefix because it, and bunch of others are predefined, but if you'll calling it some other fashion, you will need to.

Sparql: Arithmetic operators between variables?

Hi I have a query like this:
SELECT ?a ?b
WHERE
{
?c property:name "myThing"#en
?c property:firstValue ?b
?c property:secondValue ?a
}
How can I divide the first nuber and the second? idealy somthing like this:
SELECT ?a/?b
WHERE
{
?c property:name "myThing"#en
?c property:firstValue ?b
?c property:secondValue ?a
}
Thank you
In SPARQL 1.1 you can do it using Project expressions like so:
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT (xsd:float(?a)/xsd:float(?b) AS ?result)
WHERE
{
?c property:name "myThing"#en
?c property:firstValue ?b
?c property:secondValue ?a
}
You may alternately use xsd:double(?var) to cast to a double, xsd:integer(?var) to cast to an integer and xsd:decimal(?var) to cast to a decimal.
Note that SPARQL specifies type promotion rules so for example:
integer / integer = decimal
float / double = double
If you really need the result in a guaranteed datatype you can cast the whole divide expression e.g.
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT (xsd:double(xsd:float(?a)/xsd:float(?b)) AS ?result)
WHERE
{
?c property:name "myThing"#en
?c property:firstValue ?b
?c property:secondValue ?a
}
There are two ways to achieve this:
SELECT ((?a/?b) AS ?result) WHERE {
?c property:name "myThing"#en .
?c property:firstValue ?b .
?c property:secondValue ?a .
}
or
SELECT ?result WHERE {
BIND((?a/?b) AS ?result) .
?c property:name "myThing"#en .
?c property:firstValue ?b .
?c property:secondValue ?a .
}