Skip to content
Eleni Mikroyannidi edited this page Jun 14, 2013 · 16 revisions

Table of Contents


Load or Create an ontology

There are different methods to load and create an ontology in the OWLOntologyManager interface. The following code creates an empty ontology:

IRI example_iri = IRI
     .create("http://www.semanticweb.org/ontologies/ont.owl");
OWLOntologyManager m = create();
OWLOntology o = m.createOntology(example_iri);

In the above code the create() method is defined as follows:

public static OWLOntologyManager create() {
		OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
		manager.addIRIMapper(new AutoIRIMapper(new File("ontologies"), true));
		return manager;
	}

Alternative loading methods

How to load from a String source:

OWLOntologyManager m = create();
IRI pizza_iri = IRI
     .create("http://www.co-ode.org/ontologies/pizza/pizza.owl");
OWLOntology o = m.loadOntologyFromOntologyDocument(pizza_iri);
StringDocumentTarget target = new StringDocumentTarget();
m.saveOntology(o, target);
m.removeOntology(o);
OWLOntology o2 = m
  .loadOntologyFromOntologyDocument(
     new StringDocumentSource(target.toString()));

Loading an ontology using an IRIMapper:

OWLOntologyManager m = OWLManager.createOWLOntologyManager();
// map the ontology IRI to a physical IRI (files for example)
File output = File.createTempFile("saved_pizza", "owl");
IRI documentIRI = IRI.create(output);
// Set up a mapping, which maps the ontology to the document 
IRI SimpleIRIMapper mapper =
                  new SimpleIRIMapper(example_save_iri, documentIRI); m.addIRIMapper(mapper);
// set up a mapper to read local copies of ontologies
File localFolder = new File("materializedOntologies");
// the manager will look up an ontology IRI by checking
// localFolder first for a local copy
m.addIRIMapper(new AutoIRIMapper(localFolder, true));
// Now create the ontology using the ontology IRI (not the physical URI)
OWLOntology o = m.createOntology(example_save_iri);
// save the ontology to its physical location - documentIRI
m.saveOntology(o);

Add Axioms

###Add SubClassOf Axioms The following code shows how to add a Subclass axiom between two named classes

OWLOntologyManager m = create();
OWLOntology o = m.createOntology(pizza_iri);
// class A and class B
OWLClass clsA = df.getOWLClass(IRI.create(pizza_iri + "#A")); 
OWLClass clsB = df.getOWLClass(IRI.create(pizza_iri + "#B")); 
// Now create the axiom
OWLAxiom axiom = df.getOWLSubClassOfAxiom(clsA, clsB);
// add the axiom to the ontology.
AddAxiom addAxiom = new AddAxiom(o, axiom);
// We now use the manager to apply the change 
m.applyChange(addAxiom);
// remove the axiom from the ontology
RemoveAxiom removeAxiom = new RemoveAxiom(o,axiom); 
m.applyChange(removeAxiom);

Get subclasses of a class

OWLOntologyManager m = OWLManager.createOWLOntologyManager();
IRI example_iri = IRI.create("http://www.semanticweb.org/ontologies/ont.owl");
OWLOntology o = m.createOntology(example_iri);
OWLClass clsA = df.getOWLClass(IRI.create(example_iri + "#A"));
Set<OWLClassExpression> superClasses = clsA.getSuperClasses(o);
// for each superclass there will be a corresponding axiom
// the ontology indexes axioms in a variety of ways
Set<OWLSubClassOfAxiom> sameSuperClasses = o
		 .getSubClassAxiomsForSubClass(clsA);
assertEquals(superClasses.size(), sameSuperClasses.size());

Print the class hierarchy of an ontology

The following code prints the asserted or inferred class hierarchy of an ontology.

OWLOntologyManager m = OWLManager.createOWLOntologyManager();
OWLOntology o = m.loadOntologyFromOntologyDocument(pizza_iri); 
// Get Thing
OWLClass clazz = df.getOWLThing();
System.out.println("Class : " + clazz);
// Print the hierarchy below thing
printHierarchy(o, clazz, new HashSet<OWLClass>());

where the printHierarchy(OWLOntology o, OWLClass clazz) is a method that initialises a reasoner and calls the recursive method printHierarchy(OWLReasoner reasoner, OWLClass clazz, int level, Set<OWLClass> visited), which prints the hierarchy from a given class down.

public void printHierarchy(OWLOntology o, OWLClass clazz)
			throws OWLException {
		/* The StructuralReasonerFactory() is used for printing the 
                 * asserted class hierarchy. For printing the inferred class
		 * hierarchy, other reasoners like Fact++, Hermit etc can be used.
		 */
		OWLReasonerFactory reasonerFactory = new StructuralReasonerFactory();
		OWLReasoner reasoner = reasonerFactory.createNonBufferingReasoner(o);
		printHierarchy(reasoner, clazz, 0, new HashSet<OWLClass>());
		/* Now print out any unsatisfiable classes */
		for (OWLClass cl : o.getClassesInSignature()) {
			if (!reasoner.isSatisfiable(cl)) {
				System.out.println("XXX: " + labelFor(cl, o));
			}
		}
		reasoner.dispose();
	}

/* Method for geting the label of a class */
private String labelFor(OWLEntity clazz, OWLOntology o) {
		Set<OWLAnnotation> annotations = clazz.getAnnotations(o);
		for (OWLAnnotation anno : annotations) {
			String result = anno.accept(le);
			if (result != null) {
				return result;
			}
		}
		return clazz.getIRI().toString();
	}

public void printHierarchy(OWLReasoner reasoner, OWLClass clazz, int level,
			Set<OWLClass> visited) throws OWLException {
		// Only print satisfiable classes to skip Nothing
		if (!visited.contains(clazz) && reasoner.isSatisfiable(clazz)) {
			visited.add(clazz);
			for (int i = 0; i < level * 4; i++) {
				System.out.print(" ");
			}
			System.out.println(labelFor(clazz, reasoner.getRootOntology()));
			/* Find the children and recurse */
			NodeSet<OWLClass> subClasses = reasoner.getSubClasses(clazz, true);
			for (OWLClass child : subClasses.getFlattened()) {
				printHierarchy(reasoner, child, level + 1, visited);
			}
		}
	}