How do I iterate over a SPARQL query referring to a virtual graph?

894 Views Asked by At

I am developing a Virtuoso stored procedure. I want to perform a loop over the result of a SPARQL query to a graph. The problem comes when the query contains a reference to a virtual graph (a graph not physically in the triplestore, being the result of a R2RML mapping operation). In all my attempts I get no error but an empty resultset as well. I tried the following

create procedure R2RML.DBA.try() returns integer
{
  for (sparql define input:storage ""
       select ?s ?p
       from <http://example.com/resource>
       where {
         ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?o .
       }  LIMIT 5 ) do
    {
      use_the_value("s");
    }
};

and also the following

create procedure R2RML.DBA.try() returns integer
{
  declare srcgraph varchar;
  srcgraph := 'http://ec.example.com/resource';
  for (sparql define input:storage ""
        select ?s ?p
        where {
        GRAPH `iri(?:srcgraph)` 
          {
            ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?o .
          }
        }  LIMIT 5 ) do
    {
      use_the_value("s");
    }
};

In both cases no iteration is performed, despite the same query, when executed in the SPARQL endpoint, returns a result.

If I remove the reference to the graph the iterations are executed:

create procedure R2RML.DBA.try() returns integer
{
  for (sparql define input:storage ""
        select ?s ?p
        where {
          ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?o .
        }  LIMIT 5 ) do
    {
      use_the_value("s");
    }
};

Has somebody any idea of what I get wrong?

1

There are 1 best solutions below

0
On

Since the graph is virtual, the trick is to add the name of the quad storage in the define directive in the SPARQL statement. For example if the Virtuoso quad map storage is used the define directive will become the following.

sparql define input:storage "http://www.openlinksw.com/schemas/virtrdf#DefaultQuadStorage"