10.2 API Support for RDF Views

Subprograms are included in the SEM_APIS package for creating, dropping, and exporting (that is, materializing the content of) RDF views.

An RDF view graph is created as an RDF graph, but the RDF graph physically contains only the mapping metadata. The actual data remains in the relational tables for which the RDF view graph has been created. (The SEM_APIS subprograms are documented in SEM_APIS Package Subprograms.)

Once an RDF view graph is created, you can also materialize the RDF triples into a staging table by using the SEM_APIS.EXPORT_RDFVIEW_GRAPH subprogram.

For the examples throughout this chapter, assume that the relational tables, EMP and DEPT, are present in the TESTUSER schema (see Section 10.3 for the definitions of these two tables). Also, assume that a schema-private network, named NET1 and owned by the RDFUSER schema, already exists and RDFUSER has READ privilege on these two tables.

For the example illustrating the use of exporting of RDF triples, assume that the staging table to which the materialized RDF triples will be stored are owned by TESTUSER and the network owner has INSERT privilege on that table.

10.2.1 Creating an RDF View Graph with Direct Mapping

Example 10-1 creates an RDF view graph using direct mapping of two tables, EMP and DEPT (see Section 10.3 for the definitions of these two tables), with a base prefix of http://empdb/ in a schema-private network. The (virtual) RDF terms are generated according to A Direct Mapping of Relational Data to RDF, W3C Recommendation.

Example 10-1 Creating an RDF View Graph with Direct Mapping in a Schema-Private Network

BEGIN
  sem_apis.create_rdfview_graph(
    rdf_graph_name => 'empdb_model',
    tables => SYS.ODCIVarchar2List('"TESTUSER"."EMP"', '"TESTUSER"."DEPT"'),
    prefix => 'http://empdb/',
    options => 'KEY_BASED_REF_PROPERTY=T',
    network_owner=>'RDFUSER',
    network_name=>'NET1'
  );
END;
/

To see the properties that are generated, enter the following query:

SELECT p
  FROM TABLE(SEM_MATCH(
    'SELECT DISTINCT ?p {?s ?p ?o} ORDER BY ?p',
    SEM_Models('empdb_model'),
    NULL, NULL, NULL, NULL,
    NULL, NULL, NULL,
   'RDFUSER', 'NET1'));
P
--------------------------------------------------------------------------------
http://empdb/TESTUSER.EMP#EMPNO
http://empdb/TESTUSER.EMP#JOB
http://empdb/TESTUSER.EMP#ENAME
http://empdb/TESTUSER.EMP#DEPTNO
http://www.w3.org/1999/02/22-rdf-syntax-ns#type
http://empdb/TESTUSER.EMP#ref-DEPTNO
http://empdb/TESTUSER.DEPT#DEPTNO
http://empdb/TESTUSER.DEPT#DNAME
http://empdb/TESTUSER.DEPT#LOC

9 rows selected.

10.2.2 Creating an RDF View Graph with R2RML Mapping

You can create an RDF view graph using the two tables EMP and DEPT, but with your own customizations, by creating an R2RML mapping document specified using Turtle, as shown:

@prefix rr: <http://www.w3.org/ns/r2rml#>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
@prefix ex: <http://example.com/ns#>.
 
ex:TriplesMap_Dept
    rr:logicalTable [ rr:tableName "TESTUSER.DEPT" ];
    rr:subjectMap [
        rr:template "http://data.example.com/department/{DEPTNO}";
        rr:class ex:Department;
    ];
    rr:predicateObjectMap [
        rr:predicate ex:deptNum;
        rr:objectMap [ rr:column "DEPTNO" ; rr:datatype xsd:integer ];
    ];
    rr:predicateObjectMap [
        rr:predicate ex:deptName;
        rr:objectMap [ rr:column "DNAME" ];
    ];
    rr:predicateObjectMap [
        rr:predicate ex:deptLocation;
        rr:objectMap [ rr:column "LOC" ];
    ].
 
ex:TriplesMap_Emp
    rr:logicalTable [ rr:tableName "TESTUSER.EMP" ];
    rr:subjectMap [
        rr:template "http://data.example.com/employee/{EMPNO}";
        rr:class ex:Employee;
    ];
    rr:predicateObjectMap [
        rr:predicate ex:empNum;
        rr:objectMap [ rr:column "EMPNO" ; rr:datatype xsd:integer ];
    ];
    rr:predicateObjectMap [
        rr:predicate ex:empName;
        rr:objectMap [ rr:column "ENAME" ];
    ];
    rr:predicateObjectMap [
        rr:predicate ex:jobType;
        rr:objectMap [ rr:column "JOB" ];
    ];
    rr:predicateObjectMap [
        rr:predicate ex:worksForDeptNum;
        rr:objectMap [ rr:column "DEPTNO" ; rr:dataType xsd:integer ];
    ];
    rr:predicateObjectMap [
        rr:predicate ex:worksForDept;
        rr:objectMap [ 
          rr:parentTriplesMap ex:TriplesMap_Dept ; 
          rr:joinCondition [ rr:child "DEPTNO"; rr:parent "DEPTNO" ]]].

Example 10-2 Creating an RDF View Graph with an R2RML Mapping String

The following example creates an RDF view graph directly from an R2RML string, using the preceding R2RML mapping:

DECLARE
  r2rmlStr CLOB;

BEGIN

  r2rmlStr := 
   '@prefix rr: <http://www.w3.org/ns/r2rml#>. '||
   '@prefix xsd: <http://www.w3.org/2001/XMLSchema#>. '||
   '@prefix ex: <http://example.com/ns#>. '||'

    ex:TriplesMap_Dept
        rr:logicalTable [ rr:tableName "TESTUSER.DEPT" ];
        rr:subjectMap [
            rr:template "http://data.example.com/department/{DEPTNO}";
            rr:class ex:Department;
        ];
        rr:predicateObjectMap [
            rr:predicate ex:deptNum;
            rr:objectMap [ rr:column "DEPTNO" ; rr:datatype xsd:integer ];
        ];
        rr:predicateObjectMap [
            rr:predicate ex:deptName;
            rr:objectMap [ rr:column "DNAME" ];
        ];
        rr:predicateObjectMap [
            rr:predicate ex:deptLocation;
            rr:objectMap [ rr:column "LOC" ];
        ].'||'

    ex:TriplesMap_Emp
        rr:logicalTable [ rr:tableName "TESTUSER.EMP" ];
        rr:subjectMap [
            rr:template "http://data.example.com/employee/{EMPNO}";
            rr:class ex:Employee;
        ];
        rr:predicateObjectMap [
            rr:predicate ex:empNum;
            rr:objectMap [ rr:column "EMPNO" ; rr:datatype xsd:integer ];
        ];
        rr:predicateObjectMap [
            rr:predicate ex:empName;
            rr:objectMap [ rr:column "ENAME" ];
        ];
        rr:predicateObjectMap [
            rr:predicate ex:jobType;
            rr:objectMap [ rr:column "JOB" ];
        ];
        rr:predicateObjectMap [
            rr:predicate ex:worksForDeptNum;
            rr:objectMap [ rr:column "DEPTNO" ; rr:dataType xsd:integer ];
        ];
        rr:predicateObjectMap [
            rr:predicate ex:worksForDept;
            rr:objectMap [ 
              rr:parentTriplesMap ex:TriplesMap_Dept ; 
              rr:joinCondition [ rr:child "DEPTNO"; rr:parent "DEPTNO" ]]].';

  sem_apis.create_rdfview_graph(
    rdf_graph_name => 'empdb_model',
    tables => NULL,
    r2rml_string => r2rmlStr,
    r2rml_string_fmt => 'TURTLE',
    network_owner=>'RDFUSER',   
    network_name=>'NET1'
  );

END;
/

10.2.3 Dropping an RDF View Graph

An RDF view graph can be dropped using the SEM_APIS.DROP_RDFVIEW_GRAPH procedure, as shown in Example 10-3.

Example 10-3 Dropping an RDF View graph

BEGIN
  sem_apis.drop_rdfview_model(
    rdf_graph_name => 'empdb_model',
    network_owner =>'RDFUSER',   
    network_name =>'NET1'
  );
END;
/

10.2.4 Exporting Virtual Content of an RDF View Graph into a Staging Table

The content of an RDF view graph is virtual; that is, the RDF triples corresponding to the underlying relational data, as mapped by direct mapping or R2RML mapping, are not materialized and stored anywhere. The SEM_APIS.EXPORT_RDFVIEW_GRAPH subprogram lets you materialize the virtual RDF triples of an RDF view graph into a staging table. The staging table can then be used for loading into an RDF graph.

Example 10-4 Exporting an RDF View Graph in a Schema-Private Network

Example 10-4 materializes (in N-Triples format) the content of RDF view empdb_model into the staging table TESTUSER.R2RTAB.

BEGIN
  sem_apis.export_rdfview_graph(
    rdf_graph_name => 'empdb_model',
    rdf_table_owner => 'TESTUSER',
    rdf_table_name => 'R2RTAB',
    network_owner => 'RDFUSER',
    network_name => 'NET1'
  );
END;
PL/SQL procedure successfully completed.