On Mon, Apr 15, 2013 at 1:15 PM, aarthi <[email protected]> wrote:
> hi i have two ontology.. consider ont1 and ont2. if both ont1 and ont2
> have a class named employee. And ont1's employee class have a sub
> class named age. but ont2's employee class doesn't have that class. i
> need to specify a relation that age "is-a" subclass of ont2's employee
> class. how to do that using jena? anyone help me
I don't know how much sense it makes to talk about "Age" being a
subclass of "Employee", since ages aren't employees (in any meaningful
sense that I'm aware of, anyhow). But it's not hard to state that
ont1's Employees is an equivalent class to ont2's Employee, and
thereby be able to infer that ont2's Employee has the same subclasses.
Here's some code that does just that. The output is:
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.RDFS;
public class MappingOntologies {
public static void main(String[] args) {
// In most cases, the ontologies already exist, but since we do
not have
// sample data here, we create minimal ontologies that have the
classes
// that were mentioned, namely Ont1 that has a class Employee
with a
// subclass Age, and Ont2 that has a class Employee.
// Ont1
String NS1 = "http://www.example.com/ont1/";
OntModel ont1 = ModelFactory.createOntologyModel(
OntModelSpec.OWL_DL_MEM );
OntClass employee1 = ont1.createClass( NS1 + "Employee" );
OntClass age1 = ont1. createClass( NS1 + "Age" );
employee1.addSubClass( age1 );
// Ont2
String NS2 = "http://www.example.com/ont2/";
OntModel ont2 = ModelFactory.createOntologyModel(
OntModelSpec.OWL_DL_MEM );
OntClass employee2 = ont2.createClass( NS2 + "Employee" );
// Usually when we merge or map ontologies, we are not
modifying either
// ontology, but actually creating some third ontology that
imports the
// others, and adding the mapping axioms to that third
ontology. In OWL
// we would probably do this using owl:imports, but in the Jena
API we
// can just create the third model and add the first two as
submodels.
// Ont3; we make this one an inference model so that we can
get the
// inference that employee2 has age1 is a subclass of employee2.
OntModel ont3 = ModelFactory.createOntologyModel(
OntModelSpec.OWL_DL_MEM_RULE_INF );
// add the submodels
ont3.addSubModel( ont1 );
ont3.addSubModel( ont2 );
// assert that employee1 is equivalent to employee2
ont3.add( employee1, OWL.equivalentClass, employee2 );
// To see the subclasses of employee2 in the merged/mapped
ontology,
// ask for statements of the form [x, rdfs:subClassOf,
employee2]. Each
// x is a subclass of employee2.
StmtIterator axioms = ont3.listStatements( null,
RDFS.subClassOf, employee2 );
System.out.println( "Subclasses of "+employee2 );
while ( axioms.hasNext() ) {
System.out.println( "\t"+axioms.next().getSubject() );
}
// Alternatively, you could get the employee2 OntClass from the
merged
// model and list its subclasses. It is important to retrieve
the
// OntClass from the merged model, because that is the model
that
// OntClass#listSubClasses will query.
//
// Note: when I run this query, I only one less result than I
do
// in the previous query. I do not see the (trivial) result
that
// employee2 is a subclass of itself. Depending on your
intended
// use, this might be a reason to favor the first approach.
OntClass employee32 = ont3.getOntClass( NS2 + "Employee" );
ExtendedIterator<OntClass> subclasses =
employee32.listSubClasses();
System.out.println( "Subclasses of "+employee32 );
while ( subclasses.hasNext() ) {
System.out.println( "\t"+subclasses.next() );
}
}
}
This outputs the following. See the comments in the code to explain
the different results from different query methods.
Subclasses of http://www.example.com/ont2/Employee
http://www.example.com/ont1/Age
http://www.example.com/ont1/Employee
http://www.example.com/ont2/Employee
Subclasses of http://www.example.com/ont2/Employee
http://www.example.com/ont1/Age
http://www.example.com/ont1/Employee
Hope this helps,
//JT
--
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/