source: TraumaOntologies/src/main/java/tudelft/healthpsychology/traumaontologies/owltree/OwlOntologyNode.java

Last change on this file was 5, checked in by Bart Vastenhouw, 5 years ago

Intermediate update

File size: 3.8 KB
Line 
1package tudelft.healthpsychology.traumaontologies.owltree;
2
3import java.io.File;
4import java.util.Collection;
5import java.util.List;
6import java.util.stream.Collectors;
7
8import org.semanticweb.owlapi.apibinding.OWLManager;
9import org.semanticweb.owlapi.model.OWLClass;
10import org.semanticweb.owlapi.model.OWLOntologyCreationException;
11
12import tudelft.healthpsychology.traumaontologies.answerstate.OntologyNode;
13import tudelft.healthpsychology.traumaontologies.answerstate.Property;
14import tudelft.utilities.tree.TreeNode;
15
16/**
17 * Default implementation of {@link OntologyNode} that pulls its relations from
18 * a owl file. An ontology node points to a {@link OWLClass} object in the
19 * ontology.
20 *
21 * The OWL Ontology must contain only trees. It is not allowed to have a node
22 * with multiple parents (but multiple nodes may have no parents at all).
23 */
24public class OwlOntologyNode implements OntologyNode {
25 @Override
26 public int hashCode() {
27 final int prime = 31;
28 int result = 1;
29 result = prime * result + ((object == null) ? 0 : object.hashCode());
30 result = prime * result
31 + ((reasoner == null) ? 0 : reasoner.hashCode());
32 return result;
33 }
34
35 @Override
36 public boolean equals(Object obj) {
37 if (this == obj)
38 return true;
39 if (obj == null)
40 return false;
41 if (getClass() != obj.getClass())
42 return false;
43 OwlOntologyNode other = (OwlOntologyNode) obj;
44 if (object == null) {
45 if (other.object != null)
46 return false;
47 } else if (!object.equals(other.object))
48 return false;
49 if (reasoner == null) {
50 if (other.reasoner != null)
51 return false;
52 } else if (!reasoner.equals(other.reasoner))
53 return false;
54 return true;
55 }
56
57 private final OwlTreeReasoner reasoner;
58 private OWLClass object;
59
60 public OwlOntologyNode(OWLClass object, OwlTreeReasoner reasoner) {
61 this.reasoner = reasoner;
62 this.object = object;
63 }
64
65 public OwlOntologyNode(String nodename, OwlTreeReasoner reas) {
66 this(reas.getObject(nodename), reas);
67 }
68
69 /**
70 *
71 * @param rootname the name of the root {@link OWLClass} in the ontology.
72 * This is the name after the "#" in the full object iri.
73 * This does not need to be a root node but usually it will
74 * be.
75 * @param owlfile the owlfile to read the node from
76 * @throws OWLOntologyCreationException
77 */
78 public static OwlOntologyNode fromFile(String rootname, File owlfile)
79 throws OWLOntologyCreationException {
80 OwlTreeReasoner reas = new OwlTreeReasoner(
81 OWLManager.createOWLOntologyManager()
82 .loadOntologyFromOntologyDocument(owlfile));
83 return new OwlOntologyNode(reas.getObject(rootname), reas);
84 }
85
86 @Override
87 public List<Property> getAttribute() {
88 return reasoner.getProperties(object).stream()
89 .map(axiom -> new OwlProperty(axiom, reasoner))
90 .collect(Collectors.toList());
91 }
92
93 @Override
94 public OwlOntologyNode getChild(String label) {
95 for (OWLClass obj : reasoner.getChildren(object)) {
96 if (obj.toStringID().endsWith(label)) // TEST. bit hacky?
97 return new OwlOntologyNode(obj, reasoner);
98 }
99 return null;
100 }
101
102 @Override
103 public List<? extends TreeNode<String, Collection<Property>>> getChildren() {
104 return reasoner.getChildren(object).stream()
105 .map(owlclass -> new OwlOntologyNode(owlclass, reasoner))
106 .collect(Collectors.toList());
107 }
108
109 @Override
110 public String getLabel() {
111 return object.getIRI().getShortForm();
112 }
113
114 @Override
115 public List<OntologyNode> getChildren(int depth) {
116 return reasoner.getChildren(object, depth).stream()
117 .map(obj -> new OwlOntologyNode(obj, reasoner))
118 .collect(Collectors.toList());
119 }
120
121 @Override
122 public OntologyNode getParent() {
123 OWLClass parent = reasoner.getParent(object);
124 if (parent == null)
125 return null;
126 return new OwlOntologyNode(parent, reasoner);
127 }
128
129 @Override
130 public String toString() {
131 return "Node[" + object + "]";
132 }
133
134}
Note: See TracBrowser for help on using the repository browser.