Hi Jena Community,
I have the following questions for you:
*Q1:* How to “convert” OntoFileA.java below into an .OWL file?
Specifically, I would like to extract and store the ontology assembled in
“buildOntology()” in a separate OWL file that I can call/load, access,
modify and reuse in various applications.
*Q2*: I have another ontology “OntoFileB.java” with similar structure as
“OntoFileA.java” (jut replace A by B) that I want to merge with
“OntoFileA.java” into a unique file “OntoMergedFile.java” while preserving
the integrity of initial ontologies. The 2 ontologies do not share any
property or class. I have imported all 2 java OntoFiles in “OntoMergeFile”
and I would like to be able to access and modify the imported
ontologies(add/delete
classes, properties, individuals, etc.). I’ve done this before in
Protege4.2 by simply importing the 2 ontologies in the merged one and would
like to do it with Jena. I’ve tried to make use of “get” methods in
original ontologies to “import” classes and properties in the merged
ontology (as shown below) but it doesn.t work (see excerpt below of error
at step 02). Moreover, this approach obliges me to create duplicated copies
of initial ontologies classes and dataproperties in the merged ontology. Does
anyone know about an efficient way to merge these ontologies? Any code
accompaning your explanation will be welcome.
*// OntoFileA.java*
* public* *class* DomainA_OWL {
// Base URI and *namespace* declarations
*static* *final* String *baseURI* = "http://example.org/domainA";
*static* *final* String *ns* = "http://example.org/domainA#";
// *Ontology* model declaration ...
*private* OntModel modelDomainA;
*private* Ontology *ontoDomainA*;
*private* *static* OntClass *DomainAEntity*;
*private* *static* OntClass *SubdomainA1*;
*private* *static* OntClass *SubdomainA2*;
*private* *static* DatatypeProperty *hasDatatypePropertyA1*;
*private* *static* DatatypeProperty *hasDatatypePropertyA2*;
*private* *static* DatatypeProperty *hasDatatypePropertyA3*;
// ============================================================
// Constructor method to create an empty model and *ontology* ...
// ============================================================
*public* DomainA_OWL() {
// Create simplified "DOMAIN A ONTOLOGY" model ....
modelDomainA = ModelFactory.*createOntologyModel*();
ontoDomainA = modelDomainA.createOntology( *baseURI* );
}
// ============================================================
// Manually assemble DOMAIN A *ontology* ..
// ============================================================
*public* *void* buildOntology() {
// Define CLASSES ...
DomainAEntity = modelDomainA.createClass( *ns* + "DomainAEntity");
SubDomainA1 = modelDomainA.createClass( *ns* + "SubDomainA1");
SubDomainA2 = modelDomainA.createClass( *ns* + "SubDomainA2");
// Define RELATIONSHIP among classes ...
DomainAEntity.addSubClass ( SubDomainA1 );
DomainAEntity.addSubClass ( SubDomainA2);
// Create DATA PROPERTIES for the class DomainAEntity ...
hasDatatypePropertyA1 = modelDomainA.createDatatypeProperty( ns +
"hasDatatypePropertyA1");
hasDatatypePropertyA1.setDomain(DomainAEntity);
hasDatatypePropertyA1.setRange( XSD.*xdouble*);
hasDatatypePropertyA2 = modelDomainA.createDatatypeProperty( *ns* +
"hasDatatypePropertyA2");
hasDatatypePropertyA2.setDomain(DomainAEntity);
hasDatatypePropertyA2.setRange( XSD.*xdouble*); // No unit
hasDatatypePropertyA3 = modelDomainA.createDatatypeProperty( *ns* +
"hasDatatypePropertyA3");
hasDatatypePropertyA3.setDomain(DomainAEntity);
hasDatatypePropertyA3.setRange( XSD.*xdouble*); // *mm*
} // end BuildOntology
// domainA entity
*public* *static* OntClass getDomainAEntityOntoClass(){
*return* *DomainAEntity* ;
}
// SubdomainA1
*public* *static* OntClass getSubdomainA1OntoClass(){
*return* *SubdomainA1* ;
}
// SubdomainA2
*public* *static* OntClass getSubdomainA2OntoClass(){
*return* *SubdomainA2* ;
}
// hasDatatypePropertyA1
*public* *static* DatatypeProperty hasDatatypePropertyA1OntoProperty(){
*return* *hasDatatypePropertyA1* ;
}
// hasDatatypePropertyA2
*public* *static* DatatypeProperty hasDatatypePropertyA2OntoProperty(){
*return* *hasDatatypePropertyA2* ;
}
// hasDatatypePropertyA3
*public* *static* DatatypeProperty hasDatatypePropertyA3OntoProperty(){
*return* *hasDatatypePropertyA3* ;
}
// modelDomainA
*public* *static* OntModel modelDomainA(){
*return* *modelDomainA* ;
}
*// OntoMergedFile.java*
*import* demo.OntoFileA;
*import* demo.OntoFileB;
*public* *class* DomainAB_OWL {
// Base URI and *namespace* declarations
*static* *final* String *baseURI* = "http://example.org/domainAB";
*static* *final* String *ns* = "http://example.org/domainAB#";
// *Ontology* model declaration ...
*private* OntModel modelDomainAB;
*private* Ontology *ontoDomainAB*;
*private* OntClass DomainABEntity;
*private* DatatypeProperty hasDatatypePropertyAB1, hasDatatypePropertyAB2,
hasDatatypePropertyAB3;
*private* Individual indivA_1, indivB_1, indivAB_1;
*private* OntClass DomainAEntityCopy,SubdomainA1Copy, *SubdomainA2Copy*;
*private* OntClass DomainBEntityCopy,SubdomainB1Copy, SubdomainB2Copy;
*private* DatatypeProperty hasDatatypePropertyA1Copy, *
hasDatatypePropertyA2Copy*, *hasDatatypePropertyA3Copy*;
*private* DatatypeProperty hasDatatypePropertyB1Copy, *
hasDatatypePropertyB2Copy*, *hasDatatypePropertyB3Copy*;
//
=========================================================================
// Constructor method to create an empty “merged” model and *ontology*...
//
=========================================================================
*public* DomainAB_OWL() {
// Create simplified "DOMAIN AB ONTOLOGY" model ....
modelDomainAB = ModelFactory.*createOntologyModel*();
ontoDomainAB = modelDomainAB.createOntology( *baseURI* );
}
//
======================================================================
// Manually merge DOMAIN A and B *ontologies into* DOMAIN AB ontology..
// ======================================================================
*public* *void* mergeOntologies() {
// Define CLASSES ... in DomainAB
DomainABEntity = modelDomainAB.createClass( *ns* + "DomainABEntity"); //root
of the “merged” domain
DomainAEntityCopy = DomainA_OWL.*getDomainAEntityOntoClass*();
SubdomainA1Copy = DomainA_OWL.*getSubdomainA1OntoClass*();
SubdomainA2Copy = DomainA_OWL.*getSubdomainA2OntoClass*();
DomainBEntityCopy = DomainB_OWL.*getDomainBEntityOntoClass*();
SubdomainB1Copy = DomainB_OWL.*getSubdomainB1OntoClass*();
SubdomainB2Copy = DomainB_OWL.*getSubdomainB2OntoClass*();
// Define RELATIONSHIP among classes ...in DomainAB
DomainABEntity.addSubClass (DomainAEntityCopy); // would like to be
able to do this or equivalent
DomainABEntity.addSubClass (DomainBEntityCopy);
// Create DATA PROPERTIES for the class DomainABEntity ...
hasDatatypePropertyAB1 = modelDomainAB.createDatatypeProperty( *ns* +
"hasDatatypePropertyAB1");
hasDatatypePropertyAB1.setDomain(DomainABEntity);
hasDatatypePropertyAB1.setRange( XSD.*xdouble*);
// Import DATA PROPERTIES from the DomainA and B ...
hasDatatypePropertyA1Copy = DomainA_OWL.*
hasDatatypePropertyA1OntoProperty*();
hasDatatypePropertyA2Copy = DomainA_OWL.*
hasDatatypePropertyA2OntoProperty*();
hasDatatypePropertyA3Copy = DomainA_OWL.*
hasDatatypePropertyA3OntoProperty*();
hasDatatypePropertyB1Copy = DomainB_OWL.*
hasDatatypePropertyB1OntoProperty*();
hasDatatypePropertyB2Copy = DomainB_OWL.*
hasDatatypePropertyB2OntoProperty*();
hasDatatypePropertyB3Copy = DomainB_OWL.*
hasDatatypePropertyB3OntoProperty*();
} // end mergeOntology
//
==========================================================================
// Create individuals ...from ontologies A and B as well as merged
ontology
//
==========================================================================
*public* *void* addIndividuals() {
// Add indivA_1, indivB_1, indivB_2, indivAB_1;
indivA_1 = SubdomainA1Copy.createIndividual(*ns* + "indivA_1");
indivB_1 = SubdomainB1Copy.createIndividual(*ns* + "indivB_1");
indivB_2 = SubdomainB2Copy.createIndividual(*ns* + "indivB_2");
// WOULD LIKE TO DO THE FOLLOWING INSTEAD…..
indivA_1 = SubDomainA1.createIndividual(ns + "indivA_1"); //
would ought to be able to do this too
indivB_1 = SubDomainB1.createIndividual(ns + "indivB_1");
indivB_2 = SubDomainB2.createIndividual(ns + "indivB_2");
indivAB_1 = DomainABEntity.createIndividual(*ns* + "indivAB_1");
// Create statements “within” domain related statements as statements
of the “merged” domain
// Create statements “within” domain related statements as statements of
the “merged” domain
Literal mA1 = modelDomainAB.createTypedLiteral("0.00",
XSDDatatype.*XSDdouble*);
Statement indivA_1Prop1 = modelDomainAB.createStatement( indivA_1,
hasDatatypePropertyA1Copy, mA1 );
modelDomainAB.add (indivA_1Prop1 );
Literal fdr1 = modelDomainAB.createTypedLiteral("2.50",
XSDDatatype.*XSDdouble*);
Statement indivB_1fdr = modelDomainAB.createStatement( indivB_1,
hasDatatypePropertyB1Copy, fdr1 );
modelDomainAB.add (indivB_1fdr);
// WOULD LIKE TO DO THE FOLLOWING INSTEAD…..
Literal mA1 = modelDomainAB.createTypedLiteral("0.00",
XSDDatatype.XSDdouble);
Statement indivA_1Prop1 = modelDomainAB.createStatement( indivA_1,
hasDatatypePropertyA1, mA1 );
modelDomainAB.add (indivA_1Prop1 );
Literal fdr1 = modelDomainAB.createTypedLiteral("2.50",
XSDDatatype.XSDdouble);
Statement indivB_1fdr = modelDomainAB.createStatement( indivB_1,
hasDatatypePropertyB1, fdr1 );
modelDomainAB.add (indivB_1fdr);
Literal noAct = modelDomainAB.createTypedLiteral("Tel", XSDDatatype.*
XSDstring*);
Statement indivAB_1noAct = modelDomainAB.createStatement(indivAB_1,
hasDatatypePropertyAB1, noAct );
modelDomainAB.add (indivAB_1noAct);
}
//
============================================================================
// Creates a Domain AB Model and dumps the content of an OWL
representation
//
============================================================================
*public* *static* *void* main(String args[]) {
System.*out*.println("Run demo.domainAB_OWL() ... ");
System.*out*.println("============================================");
// Generate and print DomainABEntity *ontology* .....
System.*out*.println("Step 01. Manually generate DomainABEntity
ontology ... ");
System.*out*.println("====================================================
");
DomainAB_OWL domainAB = *new* DomainAB_OWL();
System.*out*.println("Step 02. Print Empty DomainABEntity Model
...
");
System.*out*.println("============================================= ");
domainAB.printStatements("Empty model ... ");
domainAB.mergeOntologies();
// Add individuals to the model .....
System.*out*.println("");
System.*out*.println("Step 03. // ABdd domainAB1, domainAB2, domainAB3,
domainAB4 to the model ... ");
System.*out*.println(
"========================================================");
domainAB.addIndividuals();
System.*out*.println("");
}
Error at step 02 (mergeOntologies)
[java] =============================================================
[java]
[java] Exception in thread "main" java.lang.NullPointerException
[java] at
com.hp.hpl.jena.ontology.impl.OntClassImpl.addSubClass(OntClassImpl.java:282)
[java] at
demo.DomainAB_OWL.mergeOntologies(DomainAB_OWL.java:99)
[java] at demo.DomainAB_OWL.main(DomainAB_OWL.java:343)