Difference between revisions of "ConvertingToDatabaseProject"

From Protege Wiki
Jump to: navigation, search
 
(12 intermediate revisions by 3 users not shown)
Line 1: Line 1:
The following routine will convert a owl project to a owl database project. If you don't need the file model after this call you need to dispose it to save memory.
+
= Converting an OWL file to OWL Database backend =
 +
 
 +
There are 2 ways in which the conversion from an OWL file to the database backend can be done:
 +
 
 +
* '''Non-streaming conversion''': Loading the OWL file in memory and then dumping it into a database
 +
* '''Streaming''': Stream the OWL file directly in the database
 +
 
 +
 
 +
'''Note: We do provide a script that does the streaming and non-streaming conversion that use the code snippets from below. Instead of using the code snippets, please consider using the [[ConvertToDBScript|conversion script]].'''
 +
 
 +
 
 +
__TOC__
 +
 
 +
 
 +
== Non-streaming conversion ==
 +
 
 +
The following routine will convert an OWL file project to an OWL database project by first loading it into memory and then by dumping it into a database backend.
 +
 
 +
This is a good option for medium to large ontologies that can be loaded in memory. It is also faster than the streaming solution.
  
 
One flaw of this method is that the forms information is lost.  We will provide a better version later.   
 
One flaw of this method is that the forms information is lost.  We will provide a better version later.   
  
 
<code>
 
<code>
 +
<pre>
 +
  private final static String driver = "com.mysql.jdbc.Driver";
 +
  private final static String url = "jdbc:mysql://localhost/protege";
 +
  private final static String table = "newtable";
 +
  private final static String user = "myUser";
 +
  private final static String password = "myPassword";
 +
  //private final static String uri = "file:/tmp/pizza/pizza.owl";
 +
 
     @SuppressWarnings("unchecked")
 
     @SuppressWarnings("unchecked")
 
     private static OWLModel convertToDatabaseProject(OWLModel fileModel) throws Exception {
 
     private static OWLModel convertToDatabaseProject(OWLModel fileModel) throws Exception {
Line 10: Line 36:
 
         Project fileProject = fileModel.getProject();
 
         Project fileProject = fileModel.getProject();
 
         OWLDatabaseKnowledgeBaseFactory factory = new OWLDatabaseKnowledgeBaseFactory();
 
         OWLDatabaseKnowledgeBaseFactory factory = new OWLDatabaseKnowledgeBaseFactory();
         PropertyList sources =
+
         PropertyList sources = PropertyList.create(fileProject.getInternalProjectKnowledgeBase());
                    PropertyList.create(fileProject.getInternalProjectKnowledgeBase());
+
   
         DatabaseKnowledgeBaseFactory.setSources(sources,  
+
         DatabaseKnowledgeBaseFactory.setSources(sources, driver, url, table, user, password);
                                                driver, url, table, user, password);
 
 
         factory.saveKnowledgeBase(fileModel, sources, errors);
 
         factory.saveKnowledgeBase(fileModel, sources, errors);
         displayErrors(errors);
+
 
 +
         displayErrors(errors); //forget this and it will be a mystery when things go wrong, see method below
 
         if (!errors.isEmpty()) {
 
         if (!errors.isEmpty()) {
 
             return null;
 
             return null;
 
         }
 
         }
 +
 +
        //Following lines are optional.
 +
        //Use them if you want to have a pprj generated for the DB project (you usually want this)
 +
        //If not, you can always create a new project from existing sources in the Protege editor
 +
 
         Project dbProject = Project.createNewProject(factory, errors);
 
         Project dbProject = Project.createNewProject(factory, errors);
         DatabaseKnowledgeBaseFactory.setSources(dbProject.getSources(),  
+
         DatabaseKnowledgeBaseFactory.setSources(dbProject.getSources(), driver, url, table, user, password);
                                                driver, url, table, user, password);
+
 
 
         dbProject.createDomainKnowledgeBase(factory, errors, true);
 
         dbProject.createDomainKnowledgeBase(factory, errors, true);
 
         dbProject.setProjectURI(URIUtilities.createURI(dbProjectFile));
 
         dbProject.setProjectURI(URIUtilities.createURI(dbProjectFile));
 
         dbProject.save(errors);
 
         dbProject.save(errors);
         displayErrors(errors);      
+
 
 +
         displayErrors(errors); //forget this and it will be a mystery when things go wrong, see method below
 
         return (OWLModel) dbProject.getKnowledgeBase();
 
         return (OWLModel) dbProject.getKnowledgeBase();
 
     }
 
     }
 +
</pre>
 +
</code>
 +
 +
If you don't need the file model after this call you need to dispose it to save memory. To dispose the file model call:
 +
 +
<code>
 +
<pre>
 +
      fileModel.dispose()
 +
</pre>
 +
</code>
 +
 +
== Streaming conversion ==
 +
 +
The streaming conversion should be used for very large ontologies that cannot be loaded in memory. This method dumps the ontology directly in the database without loading it completely in memory.
 +
 +
<code>
 +
<pre>
 +
 +
  private final static String driver = "com.mysql.jdbc.Driver";
 +
  private final static String url = "jdbc:mysql://localhost/protege";
 +
  private final static String table = "newtable";
 +
  private final static String user = "myUser";
 +
  private final static String password = "myPassword";
 +
  //private final static String uri = "file:/tmp/pizza/pizza.owl";
 +
 +
  public static Project convertToDatabaseProjectStreaming(URI uri) throws URISyntaxException {
 +
      CreateOWLDatabaseFromFileProjectPlugin creator = new  CreateOWLDatabaseFromFileProjectPlugin();
 +
      creator.setKnowledgeBaseFactory(new OWLDatabaseKnowledgeBaseFactory());
 +
      creator.setDriver(driver);
 +
      creator.setURL(url);
 +
      creator.setTable(table);
 +
      creator.setUsername(user);
 +
      creator.setPassword(password);
 +
      creator.setOntologyInputSource(uri);
 +
      //creator.setOntologyFileURI(uri); //versions of Protege pre 3.4 beta 500
 +
      creator.setUseExistingSources(true);
 +
 +
      Project p = creator.createProject();
 +
      List errors = new ArrayList();
 +
      p.save(errors);
 +
      displayErrors(errors); // if you forget to do this it will be a mystery when things go wrong, see method below
 +
      return p;
 +
  }
 +
 +
</pre>
 +
</code>
 +
 +
If you don't need the the project after this call you need to dispose it to save memory. To dispose the file model call:
 +
 +
<code>
 +
<pre>
 +
      p.dispose()
 +
</pre>
 +
</code>
 +
 +
 +
== displayErrors method ==
 +
 +
This is a utility method used to display the errors during convert.
 +
 +
<code>
 +
<pre>
 +
public static void displayErrors(Collection errors) {
 +
        Iterator i = errors.iterator();
 +
        while (i.hasNext()) {
 +
            Object elem = i.next();
 +
            if (elem instanceof Throwable) {
 +
                Log.getLogger().log(Level.WARNING, "Warnings at loading changes project", (Throwable)elem);
 +
            } else if (elem instanceof MessageError) {
 +
                Log.getLogger().log(Level.WARNING, ((MessageError)elem).getMessage(), ((MessageError)elem).getException());
 +
            } else {
 +
                Log.getLogger().warning(elem.toString());
 +
            }
 +
        }
 +
    }
 +
</pre>
 
</code>
 
</code>
 +
 +
 +
= Other resources =
 +
* [http://protege.stanford.edu/doc/design/jdbc_backend.html Database schema used by the Protege database backend]
 +
* [http://protege.stanford.edu/plugins/owl/api/guide.html Protege-OWL API Programmer's Guide]
 +
 +
[[Category:Protege developer documentation]]

Latest revision as of 16:31, April 10, 2009

Converting an OWL file to OWL Database backend

There are 2 ways in which the conversion from an OWL file to the database backend can be done:

  • Non-streaming conversion: Loading the OWL file in memory and then dumping it into a database
  • Streaming: Stream the OWL file directly in the database


Note: We do provide a script that does the streaming and non-streaming conversion that use the code snippets from below. Instead of using the code snippets, please consider using the conversion script.



Non-streaming conversion

The following routine will convert an OWL file project to an OWL database project by first loading it into memory and then by dumping it into a database backend.

This is a good option for medium to large ontologies that can be loaded in memory. It is also faster than the streaming solution.

One flaw of this method is that the forms information is lost. We will provide a better version later.

   private final static String driver = "com.mysql.jdbc.Driver";
   private final static String url = "jdbc:mysql://localhost/protege";
   private final static String table = "newtable";
   private final static String user = "myUser";
   private final static String password = "myPassword";
   //private final static String uri = "file:/tmp/pizza/pizza.owl"; 

    @SuppressWarnings("unchecked")
    private static OWLModel convertToDatabaseProject(OWLModel fileModel) throws Exception {
        System.out.println("In Convert to Database Project");
        List errors = new ArrayList();
        Project fileProject = fileModel.getProject();
        OWLDatabaseKnowledgeBaseFactory factory = new OWLDatabaseKnowledgeBaseFactory();
        PropertyList sources = PropertyList.create(fileProject.getInternalProjectKnowledgeBase());
     
        DatabaseKnowledgeBaseFactory.setSources(sources, driver, url, table, user, password);
        factory.saveKnowledgeBase(fileModel, sources, errors);

        displayErrors(errors); //forget this and it will be a mystery when things go wrong, see method below
        if (!errors.isEmpty()) {
            return null;
        }

        //Following lines are optional.
        //Use them if you want to have a pprj generated for the DB project (you usually want this)
        //If not, you can always create a new project from existing sources in the Protege editor

        Project dbProject = Project.createNewProject(factory, errors);
        DatabaseKnowledgeBaseFactory.setSources(dbProject.getSources(), driver, url, table, user, password);

        dbProject.createDomainKnowledgeBase(factory, errors, true);
        dbProject.setProjectURI(URIUtilities.createURI(dbProjectFile));
        dbProject.save(errors);

        displayErrors(errors);  //forget this and it will be a mystery when things go wrong, see method below
        return (OWLModel) dbProject.getKnowledgeBase();
    }

If you don't need the file model after this call you need to dispose it to save memory. To dispose the file model call:

       fileModel.dispose()

Streaming conversion

The streaming conversion should be used for very large ontologies that cannot be loaded in memory. This method dumps the ontology directly in the database without loading it completely in memory.


  private final static String driver = "com.mysql.jdbc.Driver";
   private final static String url = "jdbc:mysql://localhost/protege";
   private final static String table = "newtable";
   private final static String user = "myUser";
   private final static String password = "myPassword";
   //private final static String uri = "file:/tmp/pizza/pizza.owl";

   public static Project convertToDatabaseProjectStreaming(URI uri) throws URISyntaxException {
       CreateOWLDatabaseFromFileProjectPlugin creator = new  CreateOWLDatabaseFromFileProjectPlugin();
       creator.setKnowledgeBaseFactory(new OWLDatabaseKnowledgeBaseFactory());
       creator.setDriver(driver);
       creator.setURL(url);
       creator.setTable(table);
       creator.setUsername(user);
       creator.setPassword(password);
       creator.setOntologyInputSource(uri);
       //creator.setOntologyFileURI(uri); //versions of Protege pre 3.4 beta 500
       creator.setUseExistingSources(true);

       Project p = creator.createProject();
       List errors = new ArrayList();
       p.save(errors);
       displayErrors(errors); // if you forget to do this it will be a mystery when things go wrong, see method below
       return p;
  }

If you don't need the the project after this call you need to dispose it to save memory. To dispose the file model call:

       p.dispose()


displayErrors method

This is a utility method used to display the errors during convert.

public static void displayErrors(Collection errors) {
        Iterator i = errors.iterator();
        while (i.hasNext()) {
            Object elem = i.next();
            if (elem instanceof Throwable) {
                Log.getLogger().log(Level.WARNING, "Warnings at loading changes project", (Throwable)elem);
            } else if (elem instanceof MessageError) {
                Log.getLogger().log(Level.WARNING, ((MessageError)elem).getMessage(), ((MessageError)elem).getException());
            } else {
                Log.getLogger().warning(elem.toString());
            }
        }
    }


Other resources