Loading A DatabaseProject
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 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.