Difference between revisions of "Loading A DatabaseProject"

From Protege Wiki
Jump to: navigation, search
(OWLDB)
(Protege OWL Database Backend)
Line 77: Line 77:
 
== Protege OWL Database Backend ==
 
== 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 [http://smi-protege/repos/protege/protege4/libraries/org.protege.owlapi/trunk here] and to run this code you will also need to compile the protege owl api library project which can be found [http://smi-protege/repos/protege/protege4/plugins/org.semanticweb.owl.owlapi/trunk here].
+
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 [http://smi-protege/repos/protege/protege4/libraries/org.protege.owlapi/trunk here] and to run this code you will also need to compile the protege owl api library project which can be found [http://smi-protege/repos/protege/protege4/plugins/org.semanticweb.owl.owlapi/trunk here].  The Protege owl database project loads the pizza project with the following code:
 +
<pre>
 +
    public static String ONTOLOGY = "http://www.co-ode.org/ontologies/pizza/pizza.owl";
 +
 
 +
    /**
 +
    * @param args
 +
    * @throws ClassNotFoundException
 +
    * @throws SQLException
 +
    * @throws IOException
 +
    * @throws OWLOntologyCreationException
 +
    */
 +
    public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException, OWLOntologyCreationException {
 +
        ProtegeOWLOntologyManager manager = ProtegeOWLManager.createOWLOntologyManager();
 +
        DatabaseSupport.useDatabaseOntologyFactory(manager,
 +
                                                  "jdbc:postgresql://localhost/protege4",
 +
                                                  "protege",
 +
                                                  "troglodyte",
 +
                                                  "postgres.properties",
 +
                                                  false, false);
 +
        manager.loadOntologyFromOntologyDocument(IRI.create("http://www.co-ode.org/ontologies/pizza/2007/02/12/pizza.owl"));
 +
        OWLOntology ontology = manager.getOntology(IRI.create(ONTOLOGY));
 +
        System.out.println("Axiom count = " + ontology.getAxioms().size());
 +
        manager.useStandardFactories();
 +
    }
 +
</pre>
 +
Interestingly, the mysterious "postgres.properties" parameter appears to be loosely correlated with the mysql dialect  that is used in the OWLDB example.

Revision as of 13:41, March 22, 2010

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

OWLDB

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"
                                         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory>
  <!-- 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"/>
 </session-factory>
</hibernate-configuration>

I had to do one more thing before this example worked for me. By tomorrow I am sure this will be fixed. In the database I had to create a table:

    create table OntologyIRI (id long, ontologyIRI longtext);

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.

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. The Protege owl database project loads the pizza project with the following code:

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

    /**
     * @param args
     * @throws ClassNotFoundException 
     * @throws SQLException 
     * @throws IOException 
     * @throws OWLOntologyCreationException 
     */
    public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException, OWLOntologyCreationException {
        ProtegeOWLOntologyManager manager = ProtegeOWLManager.createOWLOntologyManager();
        DatabaseSupport.useDatabaseOntologyFactory(manager, 
                                                   "jdbc:postgresql://localhost/protege4", 
                                                   "protege", 
                                                   "troglodyte", 
                                                   "postgres.properties", 
                                                   false, false);
        manager.loadOntologyFromOntologyDocument(IRI.create("http://www.co-ode.org/ontologies/pizza/2007/02/12/pizza.owl"));
        OWLOntology ontology = manager.getOntology(IRI.create(ONTOLOGY));
        System.out.println("Axiom count = " + ontology.getAxioms().size());
        manager.useStandardFactories();
    }

Interestingly, the mysterious "postgres.properties" parameter appears to be loosely correlated with the mysql dialect that is used in the OWLDB example.