For each section there will about 15 minutes to solve exercises. There is probably more work than 15 minutes allows for, but there are exercises of different degree of difficulty, so choose the level you are comfortable with.

1 D2R

1.1 Exercise: Set up a D2R server

This is a walk-trough of how to get a D2R server with the world database. The steps are tested on both Windows Vista™ Ultimate and Ubuntu Linux.

  1. Download the D2R software: http://www4.wiwiss.fu-berlin.de/bizer/d2r-server/, if you have not already done so.
  2. … and in the meanwhile read the quickstart instructions on the same page. These are the instructions we will follow.
  3. Extract the downloaded archive into a suitable location.
  4. To be able to translate the data from a relational database format to RDF, the D2R server needs a mapping. Luckily, D2R is capable of generating a mapping based on the database schema. Change into the D2R Server directory and run (on one line) (Update: The database after the tutorial, but you can easily setup your own MySQL database running on your computer.):
    generate-mapping -o mapping.n3 -u semdays2010 -p putyourhandsintheair jdbc:mysql://mysql.data2000.no/semdays2010

    The D2R server connects to the MySQL database and creates a mapping based on the database schema. This may take a few seconds, because of the network communication with the database server.

  5. The mapping generates the file mapping.n3.
  6. Start the D2R server with the command
    d2r-server mapping.n3
  7. Wait until you get the message
    [[[ Server started at http://localhost:2020/ ]]]
  8. Open http://localhost:2020 in your web browser.
  9. That's it!

Note that using your local D2R server will be slower than the server at sws.ifi.uio.no. Your server needs to communicate over the Internet with the database at mysql.data2000.no, while the D2R server at sws.ifi.uio.no communicates with a local database. It is quite easy to setup a MySQL database running on your local computer. A dump of the world database can download be downloaded from sws.ifi.uio.no.

1.2 Exercise

You will notice that the data in the server you have setup is different than the server running on http://sws.ifi.uio.no/d2rq/. This is because we have changed the generated mapping file slightly, extracting continents, districts and regions to own classes, changing property names and adding datatypes to literals.

Changing the mapping is described in the section Customizing the database mapping and there is also a specification of the D2R Mapping Language.

The current mapping file for the world database at http://sws.ifi.uio.no/d2rq/ is sws_mapping.n3, which is available for download.

Download the mapping file by clicking on the link above and restart your D2R server with this mapping.

1.3 Exercise

Download the jar file D2RQueryEngine from the /download catalogue on the tutorial homepage. The program reads a D2R mapping file, an ontology file and a query file. The dataset and the ontology are combined to one model and inferred triples are added in a reasoning step. Then query is send to the dataset with the inferred triples and output is written to the console/standard out. The program reads a D2R mapping file, an ontology file and a query, and is executed like this:

java -jar D2RQueryEngine mapping.n3 http://sws.ifi.uio.no/ont/world.owl query.rq

Write a query which lists all capitols which are metropolises and run the query with the D2RQueryEngine like shown above.

1.3.1 D2RQueryEngine

The java code for the D2RQueryEngine program is listed below—FYI. If you know a little java you will see that it is quite simple to get started with programming of semantic technologies.

Import necessary external classes. All but the two last are Jena's. The two last are to be able to connect to a D2R server and use the Pellet reasoner, respectively.

1:  import com.hp.hpl.jena.ontology.*;
2:  import com.hp.hpl.jena.query.*;
3:  import com.hp.hpl.jena.reasoner.*;
4:  import com.hp.hpl.jena.rdf.model.*;
5:  import com.hp.hpl.jena.util.*;
6:  import de.fuberlin.wiwiss.d2rq.ModelD2RQ;
7:  import org.mindswap.pellet.jena.PelletReasonerFactory;
8:  public class D2RQueryEngine {

A method which creates a Jena model, i.e., a representation of an RDF graph, by reading from file:

 9:    public Model readModel(String file) {
10:      return FileManager.get().loadModel(file);
11:    }

A method which takes a query object and a model object, queries the model with the query according to the type of the query, SELECT, CONSTRUCT or ASK, and returns the results accordingly:

12:    protected void queryModel(Query query, Model model){
13:      QueryExecution qexec = QueryExecutionFactory.create(query, model);
15:      if(query.isSelectType()){
16:        ResultSet rs = qexec.execSelect();
17:        ResultSetFormatter.out(rs, query);
18:      } 
19:      else if(query.isConstructType()){
20:        Model result = qexec.execConstruct();
21:        result.write(System.out, "TTL");
22:      } 
23:      else if(query.isAskType()){
24:        boolean result = qexec.execAsk();
25:        System.out.println(result);
26:      } 
27:      else{ System.err.println("Error writing query result"); 
28:      }
29:      qexec.close();
30:    }

A method which runs the whole shebang:

  1. Reads input, i.e., the path to a D2R mapping file, an ontology and a query.
  2. Creates model with an attached Pellet reasoner and the ontology.
  3. Sets the type of ontology model
  4. Adds the D2R data, "automatically" causing reasoning and inferred triples to be added to the model.
  5. Queries the model.
31:    public void run(String d2r, String ont, String q){
33:      // Read input
34:      Model d2rData = new ModelD2RQ(readModel(d2r), null);                
35:      Model ontData = readModel(ont);
36:      Query query = QueryFactory.read(q);
38:      // Create ontology model
39:      Reasoner reasoner = PelletReasonerFactory.theInstance().create();
40:      InfModel infModel = ModelFactory.createInfModel(reasoner, ontData);
41:      OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM);
42:      OntModel ontModel = ModelFactory.createOntologyModel(spec, infModel);
44:      // Add d2r data
45:      ontModel.add(d2rData);
47:      // Query model and write results to stdout
48:      queryModel(query, ontModel);
49:    }


50:    public static void main(String[] args) {
51:      D2RQueryEngine dave = new D2RQueryEngine();
52:      dave.run(args[0], args[1], args[2]);
53:    }
54:  } //end class

Author: Martin G. Skjæveland <martige@ifi.uio.no>

Date: 2010-05-31 08:28:10 CEST

HTML generated by org-mode 6.34trans in emacs 23