How to query the path between two IRI within the same sparql endpoint - sparql

How to query the path between two IRI within the same sparql endpoint,Can I control the length and direction of the path.For example, I have some triples,input IRI_A and IRI_B,Can all the paths between IRI_A and IRI_B be detected? now i know that sparql can use this statement,However, the length and direction of the path cannot be controlled
SELECT ?s ?p ?o
WHERE {
GRAPH ?g {
BIND ( <http://localhost/XXX/IRI_A> AS ?start ) .
BIND ( <http://localhost/XXX/IRI_B> AS ?end ) .
?start (<>|!<>)* ?s .
?s ?p ?o .
?o (<>|!<>)* ?end .
}
}
I want to find all the paths between two IRIs

Related

Find orphan nodes with SPARQL

I am trying to find orphan nodes (nodes which do not have any incoming relations) with SPARQL in a Fuseki database.
I tried several queries which all do not return correct results.
I tried the following:
Query 1 (got this from linkedIn)
select ?o ?isOrphan where { GRAPH <http://localhost:8080/catalog/-1305288727> {
?s ?p ?o .
FILTER(!isLiteral(?o))
bind(!(EXISTS {?o ?p1 ?o2}) as ?isOrphan)}}
Query 2
SELECT ?source ?s ?p ?o
WHERE { GRAPH <http://localhost:8080/catalog/-1305288727>{
?s ?p ?o .
FILTER EXISTS {?source ?p ?s } .
}
}
Query 3 - unbound variable pp in FILTER
SELECT ?source ?s ?p ?o
WHERE { GRAPH <http://localhost:8080/catalog/-1305288727>{
?s ?p ?o .
FILTER EXISTS {?source ?pp ?s } .
}
}
Any help is highly appreciated.
This query finds each entity that is the subject of any triple, and then checks that this entity is not the object of any triple.
SELECT ?orphan
FROM <http://localhost:8080/catalog/-1305288727>
WHERE {
?orphan ?p1 [] .
FILTER NOT EXISTS { ?linkingNode ?p2 ?orphan . }
}

Return values under same column in SPARQL query

Given three possible objects for triples, foaf:name, foaf:givenName, and foaf:familyName, where statements either have foaf:name or foaf:givenName + foaf:familyName, e.g.:
<uri1> <foaf:name> "Lolly Loozles" .
<uri2> <foaf:givenName> "Stotly" .
<uri2> <foaf:familyName> "Styles" .
wondering how to write a SPARQL query to return a new variable like pretty_name that is either the value of foaf:name or a concatenation of the values from foaf:givenName and foaf:familyName.
Resulting in something like:
?o | ?pretty_name
----------------------
<uri1> | Lolly Loozles
<uri2> | Stotly Styles
This is what I have so far, but unsure how to proceed:
PREFIX : <https://example.org/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
# select two variables, not ideal...
SELECT ?foaf_fullName ?pretty_name
WHERE {
# Find all triples
?s ?p ?o .
# Binds
OPTIONAL { ?s foaf:name ?foaf_fullName }
OPTIONAL { ?s foaf:givenName ?givenName }
OPTIONAL { ?s foaf:familyName ?familyName }
# Filter where predicate is part of list
FILTER (?p IN (foaf:name, foaf:givenName, foaf:familyName ) )
# Binds
BIND( CONCAT(?givenName, ' ', ?familyName) AS ?pretty_name ) .
}
I had imagined, and tried, adding another BIND to add to ?pretty_name, but the SPARQL engine wouldn't have it:
BIND( ?foaf_fullName AS ?pretty_name ) .
I also had luck writing a CONSTRUCT statement to get the values I'm looking for, but don't have the ability to write back to this triplestore (for a number of reasons):
CONSTRUCT {
?s :hasPrettyName ?foaf_fullName .
?s :hasPrettyName ?pretty_name .
}
I had thought that CONSTRUCT could accompany SELECT, but must have been mistaken?
Any insight or suggestions would much appreciated.
Using #StanislavKralin comment/suggestion to use COALESCE without IF clauses works great:
PREFIX : <https://example.org/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
# select two variables, not ideal...
SELECT ?foaf_fullName ?pretty_name
WHERE {
# Find all triples
?s ?p ?o .
# Binds
OPTIONAL { ?s foaf:name ?foaf_fullName }
OPTIONAL { ?s foaf:givenName ?givenName }
OPTIONAL { ?s foaf:familyName ?familyName }
# Filter where predicate is part of list
FILTER (?p IN (foaf:name, foaf:givenName, foaf:familyName ) )
# Binds
BIND( COALESCE(?foaf_fullName, CONCAT(?givenName, ' ', ?familyName)) AS ?pretty_name )
}

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.

How to bind a variable to a queried item in SPARQL

In this simple sparql query I get a list of subjects whose object is 42
SELECT ?v WHERE { ?v ?p 42 }
If I add ?p as a variable
SELECT ?v ?p WHERE { ?v ?p 42 }
I will get two entities per row, the subject and the predicate.
What if I wanted three entities, so including the 42? Something like:
SELECT ?v ?p ?m WHERE { ?v ?p (42 as m) }
Another variant is to use BIND, e.g.:
SELECT ?v ?p ?m
WHERE {
BIND(42 AS ?m)
?v ?p ?m
}
The BIND statement simply adds a binding for ?m, which can then be selected for the result set.
In SPARQL 1.1, you can use VALUES for this. You would write
SELECT ?v ?p ?m WHERE {
values ?m { 42 }
?v ?p ?m
}
Standard SPARQL 1.0 does not really allow that. There may be some implementation-specific extensions for doing it, though.
As a workaround, if the data contains a triple with 42 as an object literal, you can do it e.g. like this:
SELECT ?v ?p ?m { ?v ?p 42, ?m FILTER(?m=42)}
which is equivalent with
SELECT ?v ?p ?m WHERE { ?v ?p 42 . ?v ?p ?m FILTER(?m=42)}
as you can write graph patterns sharing the same subject and predicate with the comma object list notation, and the WHERE keyword is optional.
For efficiency, you want to use basic graph patterns to reduce the working triple to a smaller set and only then apply FILTER expressions to further prune the results.
You can accomplish in two ways using BINDINGS keyword as well as FILTER
Using BINDINGS
SELECT ?v ?p ?m
WHERE { ?v ?p ?m}
BINDINGS ?m {(42)}
Using FILTER
SELECT ?v ?p ?m
WHERE {
?v ?p ?m
FILTER (?m = 42)
}
select ?v ?p ?m where { ?v ?p ?m . FILTER( ?m = 42 ) }
I know this is round-about, but I believe this is doable with a subquery.
This is a useful pattern to help you work on the query in the narrow, before you let it loose on your entire dataset:
SELECT ?v ?p ?m WHERE {
{ SELECT 42 as ?m WHERE { } }
?v ?p ?m .
}