package tudelft.healthpsychology.traumaontologies.owltree; import java.io.File; import java.util.Collection; import java.util.List; import java.util.stream.Collectors; import org.semanticweb.owlapi.apibinding.OWLManager; import org.semanticweb.owlapi.model.OWLClass; import org.semanticweb.owlapi.model.OWLOntologyCreationException; import tudelft.healthpsychology.traumaontologies.answerstate.OntologyNode; import tudelft.healthpsychology.traumaontologies.answerstate.Property; import tudelft.utilities.tree.TreeNode; /** * Default implementation of {@link OntologyNode} that pulls its relations from * a owl file. An ontology node points to a {@link OWLClass} object in the * ontology. * * The OWL Ontology must contain only trees. It is not allowed to have a node * with multiple parents (but multiple nodes may have no parents at all). */ public class OwlOntologyNode implements OntologyNode { @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((object == null) ? 0 : object.hashCode()); result = prime * result + ((reasoner == null) ? 0 : reasoner.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; OwlOntologyNode other = (OwlOntologyNode) obj; if (object == null) { if (other.object != null) return false; } else if (!object.equals(other.object)) return false; if (reasoner == null) { if (other.reasoner != null) return false; } else if (!reasoner.equals(other.reasoner)) return false; return true; } private final OwlTreeReasoner reasoner; private OWLClass object; public OwlOntologyNode(OWLClass object, OwlTreeReasoner reasoner) { this.reasoner = reasoner; this.object = object; } public OwlOntologyNode(String nodename, OwlTreeReasoner reas) { this(reas.getObject(nodename), reas); } /** * * @param rootname the name of the root {@link OWLClass} in the ontology. * This is the name after the "#" in the full object iri. * This does not need to be a root node but usually it will * be. * @param owlfile the owlfile to read the node from * @throws OWLOntologyCreationException */ public static OwlOntologyNode fromFile(String rootname, File owlfile) throws OWLOntologyCreationException { OwlTreeReasoner reas = new OwlTreeReasoner( OWLManager.createOWLOntologyManager() .loadOntologyFromOntologyDocument(owlfile)); return new OwlOntologyNode(reas.getObject(rootname), reas); } @Override public List getAttribute() { return reasoner.getProperties(object).stream() .map(axiom -> new OwlProperty(axiom, reasoner)) .collect(Collectors.toList()); } @Override public OwlOntologyNode getChild(String label) { for (OWLClass obj : reasoner.getChildren(object)) { if (obj.toStringID().endsWith(label)) // TEST. bit hacky? return new OwlOntologyNode(obj, reasoner); } return null; } @Override public List>> getChildren() { return reasoner.getChildren(object).stream() .map(owlclass -> new OwlOntologyNode(owlclass, reasoner)) .collect(Collectors.toList()); } @Override public String getLabel() { return object.getIRI().getShortForm(); } @Override public List getChildren(int depth) { return reasoner.getChildren(object, depth).stream() .map(obj -> new OwlOntologyNode(obj, reasoner)) .collect(Collectors.toList()); } @Override public OntologyNode getParent() { OWLClass parent = reasoner.getParent(object); if (parent == null) return null; return new OwlOntologyNode(parent, reasoner); } @Override public String toString() { return "Node[" + object + "]"; } }