Loading A DatabaseProject

From Protege Wiki
Revision as of 14:18, May 24, 2011 by Tredmond (talk | contribs)

Jump to: navigation, search

I am going to focus my attention on the v3 owlapi database backends.

Protege OWL Database Backend

The Protege OWL Database backend takes a very different approach than OWLDB. This backend was developed by using jdbc directly. The svn sources for the Protege database backend can be found here and to run this code you will also need to compile the protege owl api library project which can be found here. We will give some examples of useful code for accessing the database ontology api.

To add an ontology from the web or a file into the database use the following code:

        System.out.println("Adding the ontology " + ONTOLOGY + " to the database with the database prefix " + DB_PREFIX);
    	ProtegeOWLOntologyManager manager = ProtegeOWLManager.createOWLOntologyManager();
        DatabaseOntologyFactory dbFactory =
        dbFactory.setPrefix(DB_PREFIX); // optional
        OWLOntology ontology = manager.getOntology(IRI.create(ONTOLOGY));
        System.out.println("Axiom count = " + ontology.getAxioms().size());

This example uses the following two declarations of an ontology id and a prefix to be used in the database:

    public static String ONTOLOGY   = "http://www.co-ode.org/ontologies/pizza/pizza.owl";
    public static String DB_PREFIX  = "pizza";


This documentation is out of date and wrong - but perhaps it will get fixed up at some later time.

The OWLDB takes a hibernate approach to creating a database backend for Protege. There is a project page and the svn sources for the project can be downloaded from here. The svn sources include some very simple examples that show how to setup the owldb. The examples worked immediately for me.

The first thing that you need to do is setup the hibernate configuration file. This needs to live in at the root of the classpath. The OWLDB svn sources contain a couple of examples in the config directory. My configuration is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  <!-- Database connection settings -->
  <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="connection.username">protege</property>
  <property name="connection.password">troglodyte</property>
  <!-- JDBC connection pool (use the built-in) -->
  <property name="connection.pool_size">1</property>
  <!-- SQL dialect -->
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  <!-- Enable Hibernate's automatic session context management -->
  <property name="current_session_context_class">thread</property>

	<property name="cache.provider_class">com.opensymphony.oscache.hibernate.OSCacheProvider</property>

  <!-- Echo all executed SQL to stdout -->
  <property name="show_sql">false</property>
  <!-- Drop and re-create the database schema on startup -->
  <property name="hbm2ddl.auto">update</property>
  <property name = "hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>

  <mapping resource="OWLDBMapping.hbm.xml"/>

I made some very minor modifications to the example and ran the code:

     * @param args
     * @throws OWLOntologyCreationException 
     * @throws OWLOntologyStorageException 
    public static void main(String[] args) throws OWLOntologyCreationException, OWLOntologyStorageException {
        final OWLOntologyManager manager = OWLDBManager.createOWLOntologyManager (OWLDataFactoryImpl.getInstance ());

        final IRI iri = IRI.create ("http://www.co-ode.org/ontologies/pizza/2007/02/12/pizza.owl");
        final OWLOntology ontology = manager.loadOntology (iri);

        final IRI targetIri = IRI.create ("jdbc:mysql://localhost/owldb");
        final OWLDBOntologyFormat format = new OWLDBOntologyFormat ();
        final OWLDBOntologyOutputTarget target = new OWLDBOntologyOutputTarget (targetIri);
        manager.saveOntology (ontology, format, target);

This created several tables in the database. I then tested loading this ontology from the database with the following code:

    public static void main(String[] args) throws OWLOntologyCreationException, OWLOntologyStorageException {
        final OWLOntologyManager manager = OWLDBManager.createOWLOntologyManager (OWLDataFactoryImpl.getInstance ());

        final IRI targetIri = IRI.create ("jdbc:mysql://localhost/owldb");
        final OWLOntology ontology = manager.loadOntology (targetIri);
        System.out.println("Axioms = " + ontology.getAxiomCount());

and got an axiom count of 939.