Difference between revisions of "LoadOWLOntologyFromDB"

From Protege Wiki
Jump to: navigation, search
(migrating this page from old cim3 wiki)
 
m (getOwlModel method name correction)
 
(10 intermediate revisions by 3 users not shown)
Line 6: Line 6:
  
 
<code>
 
<code>
Project prj = Project.loadProjectFromFile("/home/pizza_db.pprj",errors);<br />
+
<pre>
OWLModel owlModel = (OWLModel) prj.getKnowledgeBase();<br />
+
    Project prj = Project.loadProjectFromFile("/home/pizza_db.pprj",errors);
...
+
    OWLModel owlModel = (OWLModel) prj.getKnowledgeBase();
 +
</pre>
 
</code><br /><br />
 
</code><br /><br />
  
Line 16: Line 17:
  
 
<code>
 
<code>
OWLDatabaseKnowledgeBaseFactory factory = new OWLDatabaseKnowledgeBaseFactory();<br />
+
<pre>
Project prj = Project.createNewProject(factory, errors);<br />
+
    JenaKnowledgeBaseFactory.useStandalone = false;
OWLDatabaseKnowledgeBaseFactory.setSources(prj.getSources(), "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/protege", "pizza", "user", "password");<br />
+
    Collection errors = new ArrayList();
prj.createDomainKnowledgeBase(factory, errors, true);<br /><br />
+
    //the "false" argument means that it won't overide the existing table
OWLModel owlModel = (OWLModel) prj.getKnowledgeBase();<br />
+
    OwlDatabaseCreator creator = new OwlDatabaseCreator(false);
...
+
    creator.setDriver("com.mysql.jdbc.Driver");
 +
    creator.setURL("jdbc:mysql://localhost/protege");
 +
    creator.setUsername("protege");
 +
    creator.setPassword("mypass");
 +
    creator.setTable("tableName");
 +
    try {
 +
          creator.create(errors);
 +
          OWLModel owlModel = creator.getOwlModel();
 +
    } catch (OntologyLoadException e) {
 +
          e.printStackTrace();
 +
    }
 +
   
 +
</pre>
 
</code>
 
</code>
 +
 +
The "false" argument to the <pre>OwlDatabaseCreator</pre> class tells it to use an existing database rather than overwrite it.
 +
 +
[[Category:Protege developer documentation]]

Latest revision as of 04:03, May 1, 2010

How do I load a Protege-OWL ontology from a Protege database using the API?

Method 1

The simplest way to load a Protege-OWL ontology from a Protege database is to programatically load the project file (.pprj), which already contains all of the necessary database connection information:

    Project prj = Project.loadProjectFromFile("/home/pizza_db.pprj",errors);
    OWLModel owlModel = (OWLModel) prj.getKnowledgeBase();



Method 2

The second method should be used if there is no project file (.pprj) associated with the Protege-OWL database ontology:

    JenaKnowledgeBaseFactory.useStandalone = false;
    Collection errors = new ArrayList();
    //the "false" argument means that it won't overide the existing table
    OwlDatabaseCreator creator = new OwlDatabaseCreator(false);
    creator.setDriver("com.mysql.jdbc.Driver");
    creator.setURL("jdbc:mysql://localhost/protege");
    creator.setUsername("protege");
    creator.setPassword("mypass");
    creator.setTable("tableName");
    try {
          creator.create(errors);
          OWLModel owlModel = creator.getOwlModel();
     } catch (OntologyLoadException e) {	
          e.printStackTrace();
     }
    

The "false" argument to the
OwlDatabaseCreator
class tells it to use an existing database rather than overwrite it.