The concepts or properties in an ontology often form a hierarchy. The ontology class provides methods for retrieving these hierarchies that can then be easily traversed.
This listing shows output from a sample program for printing the concept hierarchy:
Area
City
Country
Federal State
Weather Station
Measurement
Date
CityWithID
Retrieving a Hierarchy
There are two methods available for retrieving hierarchies: getConceptHierarchy and getPropertyHierarchy. For both, the object returned is of the type Hierarchy.
Hierarchy hierarchy = ontology.getConceptHierarchy(false);
Traversing a Hierarchy
The traversal can be started at the root nodes proceeding the child nodes, or by jumping to a concrete node corresponding to a particular concept or property; the following code snippet demonstrates the first option (see the sample program for a slightly more sophisticated implementation):
for (Term t : hier.getRootTerms()) {
Node n = hier.getNodeFor(t);
printNodeHierarchy(n);
}
void printNodeHierarchy(Node n) {
System.out.println(n.getTerm());
for (Node cn : n.getChildNodes())
printNodeHierarchy(cn);
}