Difference between revisions of "Loading A DatabaseProject"

From Protege Wiki
Jump to: navigation, search
(OWLDB)
(Protege 4 OWL Database Backend (OWL API v3))
 
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
I am going to focus my attention on the v3 owlapi database backends.
+
<p style="padding:0; margin:0; font-size:1.9em; font-weight:bold; word-spacing:-2px;">Programmatic access to the Protege 4 database backends</p>
  
== OWLDB ==
+
<div style="background:#F7F7F7; border:1px solid #999999; padding:0.5em; width:75%;">
 +
This page describes how you can access programmatically (add, query, modify) an ontology stored in one of the Protege 4 database backends. There are currently 2 backends: the OWL API v3 database backend developed at Stanford, and the [http://owldb.sourceforge.net/ OWLDB] database backend that is developed by external collaborators.
 +
 
 +
This page focuses on the OWL API v3 backend. The documentation for the OWLDB backend is outdated and will be updated in future.
 +
 
 +
</div>
 +
 
 +
 
 +
__TOC__
 +
 
 +
= Protege 4 OWL Database Backend (OWL API v3) =
 +
 
 +
The Protege 4 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.stanford.edu/repos/protege/protege4/libraries/org.protege.owl.database/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.stanford.edu/repos/protege/protege4/plugins/org.semanticweb.owl.owlapi/trunk here]. 
 +
 
 +
 
 +
We will give some examples of useful code for accessing the database ontology api.  There is now a [http://smi-protege.stanford.edu/repos/protege/protege4/libraries/org.protege.owl.database/trunk/src/test/java/org/protege/owlapi/database/TestWiki.java unit test] that runs [http://bmir-hudson1.stanford.edu/view/1-Protege4/job/org.protege.owl.database/ nightly] mirroring the code and functionality below.
 +
 
 +
To add an ontology from the web or a file into the database use the following code:
 +
<pre>
 +
        System.out.println("Adding the ontology " + ONTOLOGY + " to the database with the database prefix " + DB_PREFIX);
 +
        ProtegeOWLOntologyManager manager = ProtegeOWLManager.createOWLOntologyManager();
 +
        DatabaseOntologyFactory dbFactory =
 +
            DatabaseSupport.useDatabaseOntologyFactory(manager,
 +
                                                      "jdbc:postgresql://localhost/protege4",
 +
                                                      "protege",
 +
                                                      "troglodyte");
 +
        dbFactory.setPrefix(DB_PREFIX); // optional
 +
        manager.loadOntologyFromOntologyDocument(IRI.create(ONTOLOGY));
 +
        OWLOntology ontology = manager.getOntology(IRI.create(ONTOLOGY));
 +
        System.out.println("Axiom count = " + ontology.getAxioms().size());
 +
</pre>
 +
This example uses the following two declarations of an ontology id and a prefix to be used in the database:
 +
<pre>
 +
    public static String ONTOLOGY  = "http://www.co-ode.org/ontologies/pizza/pizza.owl";
 +
    public static String DB_PREFIX  = "pizza";
 +
</pre>
 +
 
 +
To query the database to determine what ontologies can be found or to delete an ontology from the database, use the DatabaseSupport class:
 +
<pre>
 +
        System.out.println("Asking the database what it has");
 +
        DatabaseSupport dbSupport = new DatabaseSupport("jdbc:postgresql://localhost/protege4",
 +
                                                        "protege", "troglodyte");
 +
        try {
 +
            for (BasicOntologyInfo info : dbSupport.getBasicOntologyInfo()) {
 +
                System.out.println("Database contains ontology " + info.getId() + " in the tables with the prefix " + info.getTablePrefix());
 +
            }
 +
        }
 +
        finally {
 +
            dbSupport.dispose();
 +
        }
 +
</pre>
 +
 
 +
To load an ontology already in the database into the OWL api based on the ontology ID use the following code:
 +
<pre>
 +
        System.out.println("Loading using ontology name from database: " + ONTOLOGY);
 +
        ProtegeOWLOntologyManager manager = ProtegeOWLManager.createOWLOntologyManager();
 +
        DatabaseSupport.useDatabaseOntologyFactory(manager,
 +
                                                  "jdbc:postgresql://localhost/protege4",
 +
                                                  "protege",
 +
                                                  "troglodyte");
 +
        OWLOntology ontology = manager.loadOntology(IRI.create(ONTOLOGY));
 +
        System.out.println("Ontology has java class " + ontology.getClass());
 +
        System.out.println("Axiom count = " + ontology.getAxioms().size());
 +
</pre>
 +
 
 +
To load an ontology already in the database into the OWL api based on the database prefix use the following code:
 +
<pre>
 +
        System.out.println("Loading using " + DB_PREFIX + " database prefix");
 +
        ProtegeOWLOntologyManager manager = ProtegeOWLManager.createOWLOntologyManager();
 +
        DatabaseSupport.useDatabaseOntologyFactory(manager,
 +
                                                  "jdbc:postgresql://localhost/protege4",
 +
                                                  "protege",
 +
                                                  "troglodyte");
 +
        IRI dbIri = DatabaseIRIMapper.createDatabaseIRI("jdbc:postgresql://localhost/protege4", DB_PREFIX);
 +
        OWLOntology ontology = manager.loadOntology(dbIri);
 +
        System.out.println("Ontology has java class " + ontology.getClass());
 +
        System.out.println("Axiom count = " + ontology.getAxioms().size());
 +
</pre>
 +
 
 +
There is a more user friendly utility for examing the contents of the database that is included in the source tree for the database code:
 +
<pre>
 +
    http://smi-protege.stanford.edu/repos/protege/protege4/libraries/org.protege.owl.database/trunk
 +
</pre>
 +
This utility can be invoked from ant using the command
 +
<pre>
 +
      ant db.manager
 +
</pre>
 +
Finally there is a protege plugin that will load database projects.  Protege has not yet been modified so that this will perform well for large ontologies but it can be used to help work with ontologies in the database.  The source code for this plugin can be found here:
 +
<pre>
 +
        http://smi-protege.stanford.edu/repos/protege/protege4/plugins/org.protege.editor.owl.database/trunk
 +
</pre>
 +
 
 +
 
 +
'''Further References'''
 +
 
 +
The Protege 4 database plugin has been described in this [http://www.webont.org/owled/2010/papers/owled2010_submission_20.pdf OWLEd 2010 short paper].
 +
 
 +
= OWLDB (external plugin) =
 +
 
 +
<div style="font-weight:bold;color:#CC0000;">The OWLDB documentation is out of date and wrong - but perhaps it will get fixed up at some later time.</div>
  
 
The OWLDB takes a hibernate approach to creating a database backend for Protege.  There is a [http://owldb.sourceforge.net project page] and the svn sources for the project can be downloaded from [https://owldb.svn.sourceforge.net/svnroot/owldb/OWLDB-Core/trunk here].  The svn sources include some [https://owldb.svn.sourceforge.net/svnroot/owldb/OWLDB-Core/trunk/examples/de/fraunhofer/iitb/owldb/examples very simple examples] that  show how to setup the owldb.  The examples worked immediately for me.
 
The OWLDB takes a hibernate approach to creating a database backend for Protege.  There is a [http://owldb.sourceforge.net project page] and the svn sources for the project can be downloaded from [https://owldb.svn.sourceforge.net/svnroot/owldb/OWLDB-Core/trunk here].  The svn sources include some [https://owldb.svn.sourceforge.net/svnroot/owldb/OWLDB-Core/trunk/examples/de/fraunhofer/iitb/owldb/examples very simple examples] that  show how to setup the owldb.  The examples worked immediately for me.
Line 71: Line 170:
 
</pre>
 
</pre>
 
and got an axiom count of 939.
 
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 [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.
 

Latest revision as of 21:41, September 30, 2011

Programmatic access to the Protege 4 database backends

This page describes how you can access programmatically (add, query, modify) an ontology stored in one of the Protege 4 database backends. There are currently 2 backends: the OWL API v3 database backend developed at Stanford, and the OWLDB database backend that is developed by external collaborators.

This page focuses on the OWL API v3 backend. The documentation for the OWLDB backend is outdated and will be updated in future.


Protege 4 OWL Database Backend (OWL API v3)

The Protege 4 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. There is now a unit test that runs nightly mirroring the code and functionality below.

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 =
            DatabaseSupport.useDatabaseOntologyFactory(manager, 
                                                       "jdbc:postgresql://localhost/protege4", 
                                                       "protege", 
                                                       "troglodyte");
        dbFactory.setPrefix(DB_PREFIX); // optional
        manager.loadOntologyFromOntologyDocument(IRI.create(ONTOLOGY));
        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";

To query the database to determine what ontologies can be found or to delete an ontology from the database, use the DatabaseSupport class:

        System.out.println("Asking the database what it has");
        DatabaseSupport dbSupport = new DatabaseSupport("jdbc:postgresql://localhost/protege4", 
                                                        "protege", "troglodyte");
        try {
            for (BasicOntologyInfo info : dbSupport.getBasicOntologyInfo()) {
                System.out.println("Database contains ontology " + info.getId() + " in the tables with the prefix " + info.getTablePrefix());
            }
        }
        finally {
            dbSupport.dispose();
        }

To load an ontology already in the database into the OWL api based on the ontology ID use the following code:

        System.out.println("Loading using ontology name from database: " + ONTOLOGY);
        ProtegeOWLOntologyManager manager = ProtegeOWLManager.createOWLOntologyManager();
        DatabaseSupport.useDatabaseOntologyFactory(manager, 
                                                   "jdbc:postgresql://localhost/protege4", 
                                                   "protege", 
                                                   "troglodyte");
        OWLOntology ontology = manager.loadOntology(IRI.create(ONTOLOGY));
        System.out.println("Ontology has java class " + ontology.getClass());
        System.out.println("Axiom count = " + ontology.getAxioms().size());

To load an ontology already in the database into the OWL api based on the database prefix use the following code:

        System.out.println("Loading using " + DB_PREFIX + " database prefix");
        ProtegeOWLOntologyManager manager = ProtegeOWLManager.createOWLOntologyManager();
        DatabaseSupport.useDatabaseOntologyFactory(manager, 
                                                   "jdbc:postgresql://localhost/protege4", 
                                                   "protege", 
                                                   "troglodyte");
        IRI dbIri = DatabaseIRIMapper.createDatabaseIRI("jdbc:postgresql://localhost/protege4", DB_PREFIX);
        OWLOntology ontology = manager.loadOntology(dbIri);
        System.out.println("Ontology has java class " + ontology.getClass());
        System.out.println("Axiom count = " + ontology.getAxioms().size());

There is a more user friendly utility for examing the contents of the database that is included in the source tree for the database code:

     http://smi-protege.stanford.edu/repos/protege/protege4/libraries/org.protege.owl.database/trunk

This utility can be invoked from ant using the command

      ant db.manager

Finally there is a protege plugin that will load database projects. Protege has not yet been modified so that this will perform well for large ontologies but it can be used to help work with ontologies in the database. The source code for this plugin can be found here:

        http://smi-protege.stanford.edu/repos/protege/protege4/plugins/org.protege.editor.owl.database/trunk


Further References

The Protege 4 database plugin has been described in this OWLEd 2010 short paper.

OWLDB (external plugin)

The OWLDB 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"
                                         "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 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.