Generating Virtuoso RDF Quad Map Storage patterns from R2RML scripts

What

Virtuoso Quad Map Storage is used for storage of relational data to RDF mappings, using the built in declarative Meta Schema or the W3C R2RML Languages for mapping SQL Data to RDF. This document describes how R2RML scripts can be converted to Virtuoso Quad Map patterns and resultant RDFView mappings generated for use.

Why

There are occasions when the Quad Map patterns may need to be viewed prior to creation, checked and amended to ensure there are no conflicts with quad map objects names previously generated and being used by other literal classes. A typical error being:

22023 Can not change literal class r2rml:virt02-96a0c768b77d2f22e151de60c65bb2d2 because it is used by other quad map objects, e.g., sys:qmv-r2rmlvirt02-96a0c768b77d2f22e151de60c65bb2d2-1731345b64b94e936f0c7d22763c4545

How

Given sample SQL table definition in Virtuoso:

CREATE TABLE "R2RML"."TEST"."PRODUCT"
  (
    id   INTEGER PRIMARY KEY ,
    name VARCHAR(100)
  );

INSERT SOFT "R2RML"."TEST"."PRODUCT" 
  VALUES 
    (
      1, 'Virtuoso'
    );

for which the following R2RML N3 script exits:

      @prefix       rr:  <http://www.w3.org/ns/r2rml#>    .
      @prefix      exa:  <http://example.com/ns#>         .
      @prefix  product:  <http://example.com/product#>    .

      <http://example.com/ns#TriplesMap1>
          a                              rr:TriplesMap  ;

          rr:logicalTable 
            [ 
              rr:tableSchema  "R2RML"    ; 
              rr:tableOwner   "TEST"     ; 
              rr:tableName    "PRODUCT" 
            ];

          rr:subjectMap 
            [ 
              rr:template  "http://example.com/product/{id}" ;
              rr:class     exa:product                       ;
              rr:graph     <http://example.com/>
            ];

          rr:predicateObjectMap
            [
              rr:predicate  product:id  ;
              rr:objectMap  
                [ 
                  rr:column  "id" 
                ];
            ];

          rr:predicateObjectMap
            [
              rr:predicate  product:name  ;
              rr:objectMap 
                [ 
                  rr:column "name" 
                ];
            ];
         .

The R2RML N3 mapping can can be loaded into Virtuoso as a RDF graph with the TTLP() function:

DB.DBA.TTLP 
  (
    ' @prefix       rr:  <http://www.w3.org/ns/r2rml#>    .
      @prefix      exa:  <http://example.com/ns#>         .
      @prefix  product:  <http://example.com/product#>    .

      <http://example.com/ns#TriplesMap1>
          a                              rr:TriplesMap  ;

          rr:logicalTable 
            [ 
              rr:tableSchema  "R2RML"    ; 
              rr:tableOwner   "TEST"     ; 
              rr:tableName    "PRODUCT" 
            ];

          rr:subjectMap 
            [ 
              rr:template  "http://example.com/product/{id}" ;
              rr:class     exa:product                       ;
              rr:graph     <http://example.com/>
            ];

          rr:predicateObjectMap
            [
              rr:predicate  product:id  ;
              rr:objectMap  
                [ 
                  rr:column  "id" 
                ];
            ];

          rr:predicateObjectMap
            [
              rr:predicate  product:name  ;
              rr:objectMap 
                [ 
                  rr:column "name" 
                ];
            ];
         .
    ', 
    'http://r2rml/product.n3',
    'http://r2rml/product.n3' 
  );

And the Virtuoso Quad Map definition for the R2RML mapping in the http://r2rml/product.n3 RDF Graph can then be obtained with the SELECT DB.DBA.R2RML_MAKE_QM_FROM_G ('http://r2rml/product.n3') command:

SQL> SELECT DB.DBA.R2RML_MAKE_QM_FROM_G ('http://r2rml/product.n3');
R2RML_MAKE_QM_FROM_G
VARCHAR
_______________________________________________________________________________

prefix virtrdf: <http://www.openlinksw.com/schemas/virtrdf#>
prefix ns1: <http://r2rml/>
prefix ns2: <http://example.com/>
prefix ns3: <http://example.com/ns#>
prefix ns4: <http://example.com/product#>
create IRI class <r2rml:virt02-cd094728ae072cd2cc3245397877e360> "http://example.com/product/%d" (in id integer) .
create Literal class <r2rml:virt02-6fb9323fc3b6ca1f11fba499de44d6a8> "http://example.com/product/%d" (in id integer) .
alter quad storage virtrdf:DefaultQuadStorage
from "R2RML"."TEST"."PRODUCT" as tbl0
  {
    create ns1:product.n3 as graph ns2: option (exclusive) {
            <r2rml:virt02-cd094728ae072cd2cc3245397877e360> (tbl0."id")
                    a ns3:product ;
                    ns4:id tbl0."id" ;
                    ns4:name tbl0."name" }
  }


1 Rows. -- 162 msec.
SQL>

To which any amendments can be made to the created Quad mappings, for example to change/correct a literal class names that may conflict with existing ones, for example literal class name r2rml:virt02-6fb9323fc3b6ca1f11fba499de44d6a8 can be changed to r2rml:virt02-6fb9323fc3b6ca1f11fba499de44d6a9 to make it unique. Then physical Quad Map patterns generated in the Virtuoso Quad map storage with the EXEC ('SPARQL ' || 'Amended Quad Map Definition'); command:

SQL> EXEC ('SPARQL ' || 'prefix virtrdf: <http://www.openlinksw.com/schemas/virtrdf#>
prefix ns1: <http://r2rml/>
prefix ns2: <http://example.com/>
prefix ns3: <http://example.com/ns#>
prefix ns4: <http://example.com/product#>
create IRI class <r2rml:virt02-cd094728ae072cd2cc3245397877e360> "http://example.com/product/%d" (in id integer) .
create Literal class <r2rml:virt02-6fb9323fc3b6ca1f11fba499de44d6a9> "http://example.com/product/%d" (in id integer) .
alter quad storage virtrdf:DefaultQuadStorage
from "R2RML"."TEST"."PRODUCT" as tbl0
  {
    create ns1:product.n3 as graph ns2: option (exclusive) {
            <r2rml:virt02-cd094728ae072cd2cc3245397877e360> (tbl0."id")
                    a ns3:product ;
                    ns4:id tbl0."id" ;
                    ns4:name tbl0."name" }
  }
');
STATE    MESSAGE
VARCHAR  VARCHAR
_______________________________________________________________________________

00000    IRI class <r2rml:virt02-cd094728ae072cd2cc3245397877e360> has been defined (inherited from rdfdf:sql-integer-uri-nullable)
00000    Literal class <r2rml:virt02-6fb9323fc3b6ca1f11fba499de44d6a9> has been defined (inherited from rdfdf:sql-integer-literal-nullable)
00000    Quad storage <http://www.openlinksw.com/schemas/virtrdf#DefaultQuadStorage> is flagged as being edited
00000    Quad map <http://r2rml/product.n3> has been created and added to the <http://www.openlinksw.com/schemas/virtrdf#DefaultQuadStorage>
00000    Quad map <sys:qm-7dd9287d0a1dd13b44c059d28b8b4147> has been created and added to the <http://www.openlinksw.com/schemas/virtrdf#DefaultQuadStorage>
00000    Quad map <sys:qm-efc65c932703bca4346e56158119c2d8> has been created and added to the <http://www.openlinksw.com/schemas/virtrdf#DefaultQuadStorage>
00000    Quad map <sys:qm-cdc99e07a2823165e6bd4225702cc588> has been created and added to the <http://www.openlinksw.com/schemas/virtrdf#DefaultQuadStorage>
00000    Quad storage <http://www.openlinksw.com/schemas/virtrdf#DefaultQuadStorage> is unflagged and can be edited by other transactions
00000    Transaction committed, SPARQL compiler re-configured
00000    9 RDF metadata manipulation operations done

10 Rows. -- 692 msec.
SQL>

and the resultant RDFView Virtual Graph <http://example.com/> created in Quad Map storage can queried:

SQL> sparql select * from <http://example.com/> where {?s ?p ?o};
s                                                                                 p                                                                                 o
LONG VARCHAR                                                                      LONG VARCHAR                                                                      LONG VARCHAR
_______________________________________________________________________________

http://example.com/product/1                                                      http://example.com/product#id                                                     1
http://example.com/product/1                                                      http://example.com/product#name                                                   Virtuoso
http://example.com/product/1                                                      http://www.w3.org/1999/02/22-rdf-syntax-ns#type                                   http://example.com/ns#product

3 Rows. -- 1 msec.
SQL> 

Related