ProtegeOWLJenaIntegration

From Protege Wiki
Jump to: navigation, search

Protege-OWL & Jena Integration

This page applies to Protege 3.x series only.

There are two ways to generate a Jena model from a Protege-OWL model using the API:



Get a snapshot of the current Protege-OWL model as a Jena OntModel

You can do this by calling the getOntModel() method from the OntModelProvider interface. This interface is implemented both by JenaOWLModel and OWLDatabaseModel. This approach is described in the Protege-OWL API Programmer's Guide.


Sample code:

JenaOWLModel owlModel = ProtegeOWL.createJenaOWLModel(); //or use an existing owlModel

OWLClass protegeCls = owlModel.createOWLNamedClass("MyClass");	
OWLDatatypeProperty protegeProp = owlModel.createOWLDatatypeProperty("prop");

protegeCls.setPropertyValue(protegeProp, "aaa");
//Protege: MyClass.prop = "aaa"

//get the Jena OntModel
Model jenaModel = owlModel.getOntModel();
String namespace = owlModel.getNamespaceManager().getDefaultNamespace();

Resource jenaCls = jenaModel.getResource(namespace + "MyClass");					
Property jenaProp = jenaModel.getProperty(namespace + "prop");
//Protege: MyClass.prop = "aaa"
//Jena:    MyClass.prop = "aaa" (same value because it is a snapshot)

//Protege: MyClass.prop --> "bbb"
protegeCls.setPropertyValue(protegeProp, "bbb");
//Protege: MyClass.prop = "bbb"
//Jena:    MyClass.prop = "aaa" (not synchronized) 

//Jena: MyClass.prop --> "ccc"
jenaCls.addProperty(jenaProp, "ccc");
//Protege: MyClass.prop = "bbb"
//Jena:    MyClass.prop = "ccc" (not synchronized)


Get a live and synchronized copy of the Protege-OWL model as a Jena Model

You can do this by calling the getJenaModel() method from the OWLModel interface. The synchronization of the two models is one-directional. If you make a change to the Protege-OWL model, the change is reflected in the Jena model, but not the other way around. If you try to change the Jena model by using the Jena API, you will get a runtime exception that the operation is not supported. This is documented in more detail on the Protege Web site.


Sample code:

JenaOWLModel owlModel = ProtegeOWL.createJenaOWLModel();

OWLClass protegeCls = owlModel.createOWLNamedClass("MyClass");	
OWLDatatypeProperty protegeProp = owlModel.createOWLDatatypeProperty("prop");

protegeCls.setPropertyValue(protegeProp, "aaa");
//Protege: MyClass.prop = "aaa"

Model jenaModel = owlModel.getJenaModel();		
String namespace = owlModel.getNamespaceManager().getDefaultNamespace();

Resource jenaCls = jenaModel.getResource(namespace + "MyClass");					
Property jenaProp = jenaModel.getProperty(namespace + "prop");
//Protege: MyClass.prop = "aaa"
//Jena:    MyClass.prop = "aaa"

//Protege: MyClass.prop --> "bbb"
protegeCls.setPropertyValue(protegeProp, "bbb");
//Protege: MyClass.prop = "bbb"
//Jena:    MyClass.prop = "bbb" (same value because they are synchronized)

//Jena: MyClass.prop --> "ccc"		
jenaCls.addProperty(jenaProp, "ccc");
//Exception -> java.lang.RuntimeException: Not supported