Virtuoso RDF Graph Dump based on Inference Rule Utility

The graph_dump procedure below can be used to export triples from Named RDF Graphs in N3 triple format to file, filtering based on the specified Inference rule and predicate/property URI:

Params:


create procedure graph_dump (in srcgraph varchar, in format varchar := 'N3', in inf varchar := null, in pred varchar := null,
			     in out_file varchar, in file_length_limit integer := 1000000000)
{
  declare qr, file_name varchar;
  declare env, ses, meta, data, h any;
  declare ses_len, max_ses_len, file_len, file_idx integer;
  set isolation = 'uncommitted';
  max_ses_len := 10000000;
  file_len := 0;
  file_idx := 1;
  if (format <> 'N3')
    signal ('22023', 'The output format is not supported');
  file_name := sprintf ('%s%06d.ttl', out_file, file_idx);
  string_to_file (file_name || '.graph', srcgraph, -2);
  string_to_file (file_name, sprintf ('# Dump of graph <%s>, as of %s\n', srcgraph, cast (now() as varchar)), -2);
  --env := vector (dict_new (16000), 0, '', '', '', 0, 0);
  env := vector (dict_new (16000), 0, '', '', '', 0, 0, 0, 0);
  ses := string_output ();
  if (inf is not null)
    inf := sprintf ('define input:inference "%s"', inf);
  else  
    inf := '';
  if (pred is not null)
    pred := sprintf ('<%s>', pred);  
  else  
    pred := '?p';
  qr := sprintf ('select * from (sparql define input:storage "" %s select ?s %s as ?p ?o { graph <%S> { ?s %s ?o } } ) as sub option (loop)',
  		inf, pred, srcgraph, pred);
  exec (qr, null, null, vector (), 0, null, null, h);
  while (0 = exec_next (h, null, null, data))
    {
      declare "s", "p", "o" any;
      "s" := data[0];
      "p" := data[1];
      "o" := data[2];
      http_ttl_triple (env, "s", "p", "o", ses);
      ses_len := length (ses);
      if (ses_len > max_ses_len)
        {
          file_len := file_len + ses_len;
          if (file_len > file_length_limit)
            {
              http (' .\n', ses);
              string_to_file (file_name, ses, -1);
              file_len := 0;
              file_idx := file_idx + 1;
              file_name := sprintf ('%s%06d.ttl', out_file, file_idx);
              string_to_file (file_name, sprintf ('# Dump of graph <%s>, as of %s (part %d)\n', srcgraph, cast (now() as varchar), file_idx), -2);
              env := vector (dict_new (16000), 0, '', '', '', 0, 0);
            }
          else
            string_to_file (file_name, ses, -1);
          ses := string_output ();
        }
    }
  exec_close (h);
  if (length (ses))
    {
      http (' .\n', ses);
      string_to_file (file_name, ses, -1);
    }
}
;

Example

SUMO ontology was recently mapped to DBpedia by its creators using RDF/XML and loaded into the DBpedia SPARQL Endpoint.

Issues:Solution: