elixir
elixir

Reputation: 193

How to get a path between IRIs or between two nodes of certain rdf:type using a SPARQL query?

Trying to execute a query using rdf4j console against a sparql endpoint to find the path between 2 nodes using property wildcards but no luck. The first query gives an error as

Malformed query: Not a valid (absolute) IRI:

The second query crashes the console. Should I try to use the query using a different way to query the endpoint as this maybe an rdf4j issue or is the query itself wrong?

PREFIX xy: <http://mainuri/>

select
*

where

{

  <http://uriOfInstanceOfData> ((<>|!<>)|^(<>|!<>))* ?x .
  ?x ?p ?o .
  ?o ((<>|!<>)|^(<>|!<>))* <http://uriOfInstanceOfData>.
  }

AND

PREFIX xy: <http://mainuri/>

select
*

where

{

  <http://uriOfInstanceOfData> (xy:|!xy:)* ?x .
  ?x ?p ?o .
  ?o (xy:|!xy:)* <http://uriOfInstanceOfData>.
  }

Upvotes: 1

Views: 691

Answers (1)

Jeen Broekstra
Jeen Broekstra

Reputation: 22042

The first query is syntactically incorrect: <> is not a valid IRI reference. The SPARQL grammar allows the empty string, but the specification also notes that any IRI reference must be a string that (after escape processing results) in a valid RFC3987 IRI. Since an IRI requires, at a mimimum, a scheme identifier, an empty string can by definition not be a valid IRI.

The second query works when I try it on a small test dataset. However it is likely very expensive to process.

EDIT the query I actually tried:

PREFIX xy: <http://mainuri/>
select
*
where
{
  rdfs:domain (xy:|!xy:)* ?x .
  ?x ?p ?o .
  ?o (xy:|!xy:)* rdf:Property.
}

On a local in-memory database with basic RDFS inferencing enabled, that gives the following result:

Evaluating SPARQL query...
+------------------------+------------------------+------------------------+
| x                      | p                      | o                      |
+------------------------+------------------------+------------------------+
| rdfs:domain            | rdf:type               | rdf:Property           |
| rdfs:domain            | rdfs:domain            | rdf:Property           |
+------------------------+------------------------+------------------------+
2 result(s) (28 ms)

Upvotes: 2

Related Questions