Example 2.3: Mapping a Simple Table

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

<#TriplesMap1>
    rr:logicalTable [ rr:tableName "EMP" ];
    rr:subjectMap [
        rr:template "http://data.example.com/employee/{EMPNO}";
        rr:class ex:Employee;
    ];
    rr:predicateObjectMap [
        rr:predicate ex:name;
        rr:objectMap [ rr:column "ENAME" ];
    ].
Prefix sml: <http://ns.aksw.org/sml/#>
Prefix ex: <http://example.com/ns#>

Create View TriplesMap1 As
  Construct {
    ?s
      a ex:Employee ;
      ex:name ?n
  }
  With
    ?s = uri('http://data.example.com/employee/', ?EMPNO)
    ?n = plainLiteral(?ENAME)
  From
    EMP

Example 2.4: Computing a Property with an R2RML View

<#DeptTableView> rr:sqlQuery """
SELECT DEPTNO,
       DNAME,
       LOC,
       (SELECT COUNT(*) FROM EMP WHERE EMP.DEPTNO=DEPT.DEPTNO) AS STAFF
FROM DEPT;
""".

<#TriplesMap2>
    rr:logicalTable <#DeptTableView>;
    rr:subjectMap [
        rr:template "http://data.example.com/department/{DEPTNO}";
        rr:class ex:Department;
    ];
    rr:predicateObjectMap [
        rr:predicate ex:name;
        rr:objectMap [ rr:column "DNAME" ];
    ];
    rr:predicateObjectMap [
        rr:predicate ex:location;
        rr:objectMap [ rr:column "LOC" ];
    ];
    rr:predicateObjectMap [
        rr:predicate ex:staff;
        rr:objectMap [ rr:column "STAFF" ];
    ].
Create Sql View DeptTableView As """
SELECT DEPTNO,
       DNAME,
       LOC,
       (SELECT COUNT(*) FROM EMP WHERE EMP.DEPTNO=DEPT.DEPTNO) AS STAFF
FROM DEPT
"""

Create View TriplesMap2 As
  Construct {
    ?s
      a ex:Department ;
      ex:name ?n ;
      ex:location ?l ;
      ex:staff ?st
  }
  With
    ?s  = uri('http://data.example.com/department/', ?DEPTNO)
    ?n  = plainLiteral(?DNAME)
    ?l  = plainLiteral(?LOC)
    ?st = plainLiteral(?STAFF)
  From
    DeptTableView
Note: The choice of [[ ... ]] for escaping may be suboptimal. We will switch to """ ... """ instead, as this is also known from SPARQL. In practice it means, that such escaping routine which may be provided by Semantic Web frameworks can just be reused. Note: Our convention is to use upper case for SQL and Upper camel case for Sparql.

Example 2.5

<#TriplesMap1>
    rr:predicateObjectMap [
        rr:predicate ex:department;
        rr:objectMap [
            rr:parentTriplesMap <#TriplesMap2>;
            rr:joinCondition [
                rr:child "DEPTNO";
                rr:parent "DEPTNO";
            ];
        ];
    ].
Create View EmployeeToDepartment As
    ?s ex:department ?o.
  With
    ?s = uri('http://data.example.com/employee/', ?e.EMPNO)
    ?o = uri('http://data.example.com/department/', ?d.DEPTNO)
  From
    EMP e Join DEPT d On (d.DEPTNO = e.EMPNO)
Create View EmployeeToDepartment As
    ?s ex:department ?o
  With
    ?s = uri('http://data.example.com/employee/', ?EMPNO)
    ?o = uri('http://data.example.com/department/', ?DEPTNO)
  From
    EMP
Note: Joining in R2RML serves two purposes:
  1. Referencing another triplesmaps' subject without repeating its term-map
  2. Joining tables as their union of columns are needed for creating RDF terms
For SML, at present we recommend to just repeat the corresponding expression. //?s = Employee.?o

Example 2.6: Many-to-Many Tables

<#TriplesMap3>
    rr:logicalTable [ rr:tableName "EMP2DEPT" ];
    rr:subjectMap [ rr:template "http://data.example.com/employee={EMPNO}/department={DEPTNO}" ];
    rr:predicateObjectMap [
        rr:predicate ex:employee;
        rr:objectMap [ rr:template "http://data.example.com/employee/{EMPNO}" ];
    ];
    rr:predicateObjectMap [
        rr:predicate ex:department;
        rr:objectMap [ rr:template "http://data.example.com/department/{DEPTNO}" ];
    ].
Create View TriplesMap3 As
  Construct {
    ?s
      ex:employee ?e ;
      ex:department ?d
  With
    ?s = uri('http://data.example.com/employee=', ?EMPNO, '/department=', ?DEPTNO)
    ?e = uri('http://data.example.com/employee/' ?EMPNO)
    ?d = uri('http://data.example.com/department/', ?DEPTNO)
  From
    EMP

2.7 Example: Translating database type codes to IRIs

<#TriplesMap3>
    rr:logicalTable [ rr:tableName "EMP2DEPT" ];
    rr:subjectMap [ rr:template "http://data.example.com/employee={EMPNO}/department={DEPTNO}" ];
    rr:predicateObjectMap [
        rr:predicate ex:employee;
        rr:objectMap [ rr:template "http://data.example.com/employee/{EMPNO}" ];
    ];
    rr:predicateObjectMap [
        rr:predicate ex:department;
        rr:objectMap [ rr:template "http://data.example.com/department/{DEPTNO}" ];
    ].
Create View TriplesMap3 As
  Construct {
    ?s
      ex:employee ?e ;
      ex:department ?d
  With
    ?s = uri('http://data.example.com/employee=', ?EMPNO, '/department=', ?DEPTNO)
    ?e = uri('http://data.example.com/employee/' ?EMPNO)
    ?d = uri('http://data.example.com/department/', ?DEPTNO)
  From
    EMP2DEPT

SML and D2RQ

D2RQ is centered around the idea of first creating maps for classes and then using another set of maps for attaching property-value pairs to them. The following shows how D2RQ Mapping Example looks in SML. Note that with D2RQ, in constrast to R2RML and SML, database connectivity related attributes are expressed as part of the mapping.
map:Conference a d2rq:ClassMap;
    d2rq:dataStorage map:Database1;
    d2rq:class :Conference;
    d2rq:uriPattern "http://conferences.org/comp/confno@@Conferences.ConfID@@";
    .
map:eventTitle a d2rq:PropertyBridge;
    d2rq:belongsToClassMap map:Conference;
    d2rq:property :eventTitle;
    d2rq:column "Conferences.Name";
    d2rq:datatype xsd:string;
    .
map:location a d2rq:PropertyBridge;
    d2rq:belongsToClassMap map:Conference;
    d2rq:property :location;
    d2rq:column "Conferences.Location"; 
    d2rq:datatype xsd:string;
    .
Create View Conference As
  Construct {
    ?s
      a :Conference ;
      :eventTitle ?et ;
      :location ?l
  With
    ?s  = uri('http://conferences.org/comp/confno', ?ConfID)
    ?et = plainLiteral(?Name)
    ?d  = typedLiteral(?Location, xsd:string)
  From
    Conferences

Essential Keywords

CategoryR2RMLSML
View Definition(implicit by properties)Create, View, As, Construct
Quad Pattern SpecificationgraphMap, subjectMap, predicateObjectMap, predicateMap, objectMap(sparql syntax)
RDF Term Specificationconstant, template, column, language, dataType, termTypeuri, bNode, plainLiteral, typedLiteral
Logical Table Specification (Simple)logicalTable, tableName, sqlQueryFrom
Logical Table Specification (Join)parentTriplesMap, joinCondition, child, parentJoin, On
Shorthandsgraph, subject, predicate, object, class(none required due to use of sparql syntax)
Compatibility HintssqlVersion(not yet considered)
Optimization HintsinverseExpression(not yet considered)

Additional Notes

Example 2.5 Note that in SML it is possible to use nampespaces in term construction by using the 'ns:' syntax:
Prefix emp: <http://data.example.com/employee/>
Prefix dept: <http://data.example.com/department/>

Create View TriplesMap1 As
  Construct {
    ?s a ex:Employee
  }
  With
    ?s = uri(emp:, EMPNO)
  From EMP

Create View EmployeeToDepartment As
  Construct {
    ?s ex:department ?o
  }
  With
    ?s = uri(emp:, ?EMPNO)
    ?o = uri(dept:, ?DEPTNO)
  From
    EMP