R2D2: PHP implementation of the D2RQ Mapping Language

The Semantic Web aims at giving data more structure and computer understandable meaning. For that RDF enables to add metadata to the unmanageable mass of data in the World Wide Web. Hence, the transformation of data located in relational databases to RDF plays an essential role. To provide such functionality our implementation R2D2 maps an RDF query to a legacy relational database without having to replicate the data.

R2D2 is be implemented in PHP and uses the RDF API for PHP (RAP) for querying a virtual R2D2-Graph. An R2D2-Graph is read only and may be used like other models in RAP by instantiating a new model named R2D2model. An R2D2model rewrites find(spo) and sparql() queries to SQL queries and transforms the SQL result set back into RDF triples. A database schema is mapped by the D2RQ Mapping Language – a declarative mapping language for describing the relation between an ontology and an relational data model.

R2D2 supports:

  • querying a non-RDF database using SPARQL or find(spo).
  • publishing the content of a non-RDF database in RDF using the RAP NetAPI.
  • automatic generation of a mapping file for any database so you can query it in RDF

Detailed information:

  • R2D2 user manual (soon here – see below for a simple example)
  • Benchmark results in comparison to the RAP database backend (soon here)
  • R2D2 PHPDocs (soon here)
  • D2RQ Language Specification for creating a mapping file


The current version is available at Sourceforge-CVS, click here.


Version Comment Date
V0.1 first version of R2D2 01.10.2006

R2D2 Developers

  • Christian Lehmann (Mail: Lehmann.Christian@gmx.net)

Thanks to

  • Sebastian Dietzold for his great ideas and support
  • Sören Auer for making all this possible
  • the D2RQ Developers for the base implementation and the D2RQ Mapping language
  • the RAP Developers for the API

Installation and Use

R2D2 is a RAP Plug In. So first, you need to download RAP. For installing R2D2 you simple have to copy the R2D2 directory into the main RAP folder.

Some example scipts are stored in folder testR2D2. You can copy this folder into the main RAP folder, too. After this you can use them with your mapping files . To include R2D2 you have to add the class R2D2Model (in R2D2/R2D2Model.php) into RAP. After this you are able to create a new virtual RDF-graph by $model = new R2D2Model($mapFile).

A map is represented as an RDF-graph. So if you want the RAP database to manage your map you can copy a map to RAP by RAP-methods. Maps that are stored in RAP are used my $model = new R2D2DbModel($db,$modelName).

For more information check the manual (coming soon).


This short examle script shows how user information in a WordPress is mapped to the FOAF vocabulary.

  • Mapping file: file:wordpressd2rq.n3
  • Database dump: file:wordpressMySQL.sql (My SQL database)
  • foaf vocabulary: foaf-specification
  • find(spo) results of different queries against the database: Test Find.txt
  • SPARQL results of queries against the database: Test RDQL.txt

Example Query: Find all user names


// Include RAP and r2d2
define('RDFAPI_INCLUDE_DIR', './../api/');
define('R2D2_INCLUDE_DIR', './../R2D2/');
include(RDFAPI_INCLUDE_DIR . 'RdfAPI.php');
include(R2D2_INCLUDE_DIR . 'R2D2Model.php');

// create a new R2D2 model
$model = new R2D2Model('wordpress-d2rq.n3');

$s = null;
$p = new Resource('http://xmlns.com/foaf/0.1/name');
$o = null;

$result = find($s,$p,$o);

// the same in SPARQL

$sparqlQuery = "PREFIX foaf: <http://xmlns.com/foaf/0.1></http:>
SELECT DISTINCT ?resource ?name WHERE {
?resource foaf:name ?name .
} ORDER BY ?name";

$result = $R2D2Model->sparqlQuery($sparqlQuery);

Using these parts of the D2RQ map

:CMUser rdf:type d2rq:ClassMap;                      
        d2rq:class foaf:Person;
        d2rq:uriPattern "//data#user@@wp_users.ID@@";
        d2rq:dataStorage :database;

:UserDisplayName rdf:type d2rq:DatatypePropertyBridge;
         d2rq:property foaf:name;
        d2rq:column "wp_users.display_name";
        d2rq:belongsToClassMap :CMUser;

both find and sparql query will be translated into following SQL command:

SELECT wp_users.ID, wp_users.display_name FROM wp_users

the result set may contain following lines:

wp_users.ID wp_users.display_name
1001 Christian Lehmann
1002 Bob Wheeler

and is translated into the RDF result triple:

Subject //data#user1001
Predicate http://xmlns.com/foaf/0.1/name
Object Christian Lehmann
Subject //data#user1002
Predicate http://xmlns.com/foaf/0.1/name
Object Bob Wheeler

Known Issues

R2D2 implements a new SPARQL2SQL rewriter. This feature is still in progress and buggy. Up to now only group patterns are supported! (for more information see the SPARQL complexity catalog (german version) Regular expressions in find(spo) or SPARQL are not yet supported.

Project Team


AKSW Colloquium, 17.10.2016, Version Control for RDF Triple Stores + NEED4Tweet ( 2016-10-17T09:55:50+02:00 by Marvin Frommhold)

2016-10-17T09:55:50+02:00 by Marvin Frommhold

In the upcoming Colloquium, October the 17th at 3 PM, two papers will be presented: Version Control for RDF Triple Stores Marvin Frommhold will discuss the paper “Version Control for RDF Triple Stores” by Steve Cassidy and James Ballantine which forms the foundation … Continue reading → Read more about "AKSW Colloquium, 17.10.2016, Version Control for RDF Triple Stores + NEED4Tweet"

LIMES 1.0.0 Released ( 2016-10-14T11:38:31+02:00 by Kleanthi Georgala)

2016-10-14T11:38:31+02:00 by Kleanthi Georgala

Dear all, the LIMES Dev team is happy to announce LIMES 1.0.0. LIMES, the Link Discovery Framework for Metric Spaces, is a link discovery framework for the Web of Data. Read more about "LIMES 1.0.0 Released"

DL-Learner 1.3 (Supervised Structured Machine Learning Framework) Released ( 2016-10-11T21:41:00+02:00 by Dr. Jens Lehmann)

2016-10-11T21:41:00+02:00 by Dr. Jens Lehmann

Dear all, the Smart Data Analytics group at AKSW is happy to announce DL-Learner 1.3. DL-Learner is a framework containing algorithms for supervised machine learning in RDF and OWL. Read more about "DL-Learner 1.3 (Supervised Structured Machine Learning Framework) Released"

OntoWiki 1.0.0 released ( 2016-10-05T16:50:05+02:00 by Natanael Arndt)

2016-10-05T16:50:05+02:00 by Natanael Arndt

Dear Semantic Web and Linked Data Community, we are proud to finally announce the releases of OntoWiki 1.0.0 and the underlying Erfurt Framework in version 1.8.0. Read more about "OntoWiki 1.0.0 released"

AKSW Colloquium, 05.09.2016. LOD Cloud Statistics, OpenAccess at Leipzig University. ( 2016-08-31T11:23:10+02:00 by Ivan Ermilov)

2016-08-31T11:23:10+02:00 by Ivan Ermilov

On the upcoming Monday (05.09.2016), AKSW group will discuss topics related to Semantic Web and LOD Cloud Statistics. Also, we will have invited speaker from University of Leipzig Library (UBL) Dr. Astrid Vieler talking about OpenAccess at Leipzig University. Read more about "AKSW Colloquium, 05.09.2016. LOD Cloud Statistics, OpenAccess at Leipzig University."