Skip to content

Instantly share code, notes, and snippets.

@salekh
Created May 21, 2017 01:02
Show Gist options
  • Save salekh/095e87f3c0584d5dbc9c5382c8dfef7e to your computer and use it in GitHub Desktop.
Save salekh/095e87f3c0584d5dbc9c5382c8dfef7e to your computer and use it in GitHub Desktop.
Code to traverse an ontology via DFS
public static OWLClass getUnvisitedChild(OWLClass owl, OWLOntology pOnt){
Set<OWLClassExpression> children = owl.getSubClasses(pOnt);
Iterator<OWLClassExpression> childIt = children.iterator();
while(childIt.hasNext()){
OWLClass currentChild = childIt.next().asOWLClass();
if(!visited.contains(currentChild)){
return currentChild;
}
}
return owl;
}
private static Boolean altCompFieldContainsConcept(OWLClass pCompField, OWLOntology pOnt, String pConceptName) {
ArrayList<OWLClass> levelIndicator = new ArrayList<>();
Mapping.found = false;
logger.info("Searching for: " + pConceptName);
logger.info("BEGIN : " + pCompField.toString());
Stack<OWLClass> stack = new Stack<>();
stack.push(pCompField);
visited.add(pCompField);
System.out.println(pCompField);
Set<OWLClassExpression> children = pCompField.getSubClasses(pOnt);
if(pConceptName.toLowerCase().equals(pCompField.getIRI().getShortForm())){
levelIndicator.add(pCompField);
}
while (!(stack.isEmpty())) {
OWLClass search = stack.peek();
OWLClass child = getUnvisitedChild(search, pOnt);
if (child != search) {
visited.add(child);
stack.push(child);
System.out.println(child);
} else {
stack.pop();
}
}
visited.clear();
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment