ConvertingToDatabaseProject

From Protege Wiki
Revision as of 17:04, January 3, 2008 by TaniaTudorache (talk | contribs)

Jump to: navigation, search

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

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); //optional
        if (!errors.isEmpty()) {
            return null;
        }

        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);  //optional
        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 void 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.setOntologyFileURI(uri);
       creator.setUseExistingSources(true);

       Project p = creator.createProject();
       List errors = new ArrayList();
       p.save(errors);
  }


See also

Database schema used by the Protege database backend Protege-OWL API Programmer's Guide