source: TraumaOntologies/src/test/java/tudelft/healthpsychology/traumaontologies/owltree/OwlOntologyNodeTest.java

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

Intermediate update

File size: 7.6 KB
Line 
1package tudelft.healthpsychology.traumaontologies.owltree;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertNotNull;
5import static org.junit.Assert.assertNull;
6import static org.junit.Assert.assertTrue;
7
8import java.io.File;
9import java.util.Arrays;
10import java.util.Collection;
11import java.util.Collections;
12import java.util.HashMap;
13import java.util.List;
14import java.util.Map;
15import java.util.Set;
16import java.util.stream.Collectors;
17
18import org.junit.Before;
19import org.junit.Test;
20import org.semanticweb.owlapi.apibinding.OWLManager;
21import org.semanticweb.owlapi.model.OWLOntology;
22import org.semanticweb.owlapi.model.OWLOntologyCreationException;
23import org.semanticweb.owlapi.model.OWLOntologyManager;
24
25import tudelft.healthpsychology.traumaontologies.answerstate.Property;
26import tudelft.healthpsychology.traumaontologies.questiontypes.TypedQuestion;
27import tudelft.utilities.junit.GeneralTests;
28import tudelft.utilities.tree.TreeNode;
29
30public class OwlOntologyNodeTest extends GeneralTests<OwlOntologyNode> {
31 private static final String MEUBEL = "Meubel";
32 private static final File ONTO_FILE = new File(
33 "src/main/resources/Child Sexual Abuse.owl");
34 private OWLOntology ontology;
35 private OwlTreeReasoner reasoner;
36 private OwlOntologyNode node1, node1a, node2, node3;
37
38 @Before
39 public void before() throws OWLOntologyCreationException {
40 OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
41 ontology = manager.loadOntologyFromOntologyDocument(ONTO_FILE);
42 reasoner = new OwlTreeReasoner(ontology);
43
44 node1 = new OwlOntologyNode(reasoner.getObject("Bed"), reasoner);
45 node1a = new OwlOntologyNode(reasoner.getObject("Bed"), reasoner);
46 node2 = new OwlOntologyNode(reasoner.getObject("Kast"), reasoner);
47 node3 = new OwlOntologyNode(reasoner.getObject("Meubel"), reasoner);
48
49 }
50
51 @Override
52 public List<List<OwlOntologyNode>> getGeneralTestData() {
53 return Arrays.asList(Arrays.asList(node1, node1a), Arrays.asList(node2),
54 Arrays.asList(node3));
55 }
56
57 @Override
58 public List<String> getGeneralTestStrings() {
59 return Arrays.asList("Node\\[\\<.*.owl#Bed\\>\\]",
60 "Node\\[\\<.*.owl#Kast\\>\\]", "Node\\[\\<.*.owl#Meubel\\>\\]");
61 }
62
63 @Test
64 public void smokeTest() {
65 new OwlOntologyNode(reasoner.getObject("Bed"), reasoner);
66 }
67
68 @Test
69 public void smokeTest1() throws OWLOntologyCreationException {
70 OwlOntologyNode.fromFile("Object", ONTO_FILE);
71 }
72
73 @Test
74 public void getAttrTest() {
75 OwlOntologyNode node = new OwlOntologyNode(reasoner.getObject("Bed"),
76 reasoner);
77 List<Property> attris = node.getAttribute();
78 assertEquals(3, attris.size());
79 System.out.println(attris);
80
81 }
82
83 @Test
84 public void getMeubelQuestionsTest() {
85
86 OwlOntologyNode node = new OwlOntologyNode(reasoner.getObject("Meubel"),
87 reasoner);
88 List<Property> attris = node.getAttribute();
89 System.out.println(attris);
90 assertEquals(2, attris.size()); // Beschrijving_voorwerp and
91 // Locatie_meubel
92
93 Set<String> questions = attris.stream()
94 .map(attri -> attri.getQuestionType().getQuestion())
95 .collect(Collectors.toSet());
96
97 System.out.println(questions);
98 assertTrue(questions.contains(
99 "Kan je beschrijven waar in de ruimte dit meubel stond?"));
100 assertTrue(questions.contains(
101 "Beschrijf nu hoe dit voorwerp eruit zag, en waarom je je deze is bijgebleven."));
102 }
103
104 @Test
105 public void getPersoonAttrTest() {
106 OwlOntologyNode node = new OwlOntologyNode(
107 reasoner.getObject("Persoon"), reasoner);
108 List<Property> attris = node.getAttribute();
109 assertEquals(2, attris.size());
110
111 Set<String> questions = attris.stream()
112 .map(attri -> attri.getQuestionType().getQuestion())
113 .collect(Collectors.toSet());
114 assertTrue(questions.contains("Wat is het geslacht van [Naam]?"));
115 assertTrue(questions
116 .contains("Geef de naam van een persoon op de locatie."));
117
118 }
119
120 @Test(expected = IllegalArgumentException.class)
121 public void getNonexistingTest() {
122 new OwlOntologyNode(reasoner.getObject("Nonsense"), reasoner);
123
124 }
125
126 @Test
127 public void getChildTest() {
128 OwlOntologyNode node = new OwlOntologyNode(reasoner.getObject(MEUBEL),
129 reasoner);
130 OwlOntologyNode child = node.getChild("Bed");
131 assertNotNull(child);
132 }
133
134 @Test
135 public void getNonsenseChildTest() {
136 OwlOntologyNode node = new OwlOntologyNode(reasoner.getObject(MEUBEL),
137 reasoner);
138 OwlOntologyNode child = node.getChild("Nonsense");
139 assertNull(child);
140 }
141
142 @Test
143 public void getLabelTest() {
144 OwlOntologyNode node = new OwlOntologyNode(reasoner.getObject(MEUBEL),
145 reasoner);
146 assertEquals(MEUBEL, node.getLabel());
147 }
148
149 @Test
150 public void getChildrenTest() {
151 OwlOntologyNode node = new OwlOntologyNode(reasoner.getObject(MEUBEL),
152 reasoner);
153 List<? extends TreeNode<String, Collection<Property>>> childs = node
154 .getChildren();
155 System.out.println(childs);
156 assertTrue("Meubel does not have the expected children",
157 childs.toString().matches(
158 ".*Node.*Bureau.*Node.*Tafel.*Node.*Kast.*Node.*Stoel.*Node.*Bed.*"));
159
160 }
161
162 @Test
163 public void getChildrenDepth1Test() {
164 OwlOntologyNode node = new OwlOntologyNode(
165 reasoner.getObject("Persoon"), reasoner);
166 List<? extends TreeNode<String, Collection<Property>>> childs = node
167 .getChildren(1);
168 System.out.println(childs);
169 assertEquals(2, childs.size());
170 assertTrue("Familie does not have the expected children",
171 childs.toString().matches(".*Node.*Jijzelf.*Node.*Anderen.*"));
172
173 }
174
175 @Test
176 public void getChildrenDepth2Test() {
177 OwlOntologyNode node = new OwlOntologyNode(
178 reasoner.getObject("Persoon"), reasoner);
179 List<? extends TreeNode<String, Collection<Property>>> childs = node
180 .getChildren(2);
181 System.out.println(childs);
182 assertEquals(6, childs.size());
183 assertTrue("Familie does not have the expected children",
184 childs.toString().matches(
185 ".*Node.*Jijzelf.*Node.*Leraar.*Node.*Collega.*Node.*Vriend.*Node.*Geestelijke.*"));
186
187 }
188
189 @Test
190 public void getParentTest() {
191 OwlOntologyNode node = new OwlOntologyNode(
192 reasoner.getObject("Anderen"), reasoner);
193 assertEquals(
194 new OwlOntologyNode(reasoner.getObject("Persoon"), reasoner),
195 node.getParent());
196 }
197
198 @Test
199 public void getParentOfRootTest() {
200 OwlOntologyNode node = new OwlOntologyNode(
201 reasoner.getObject("Type_Locatie"), reasoner);
202 assertEquals(null, node.getParent());
203 }
204
205 @Test
206 public void ParkStateE2Etest() throws OWLOntologyCreationException {
207 OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
208 OWLOntology ontology = manager.loadOntologyFromOntologyDocument(
209 new File("src/main/resources/Child Sexual Abuse.owl"));
210 OwlTreeReasoner reas = new OwlTreeReasoner(ontology);
211 OwlOntologyNode park = new OwlOntologyNode("Park", reas);
212
213 assertEquals(Collections.EMPTY_LIST, park.getChildren());
214
215 }
216
217 @Test
218 public void getCollegaProperties() {
219 OwlOntologyNode node = new OwlOntologyNode(
220 reasoner.getObject("Collega"), reasoner);
221 List<Property> attris = node.getAttribute();
222 System.out.println(attris);
223 assertEquals(4, attris.size()); // Beschrijving_voorwerp and
224 // Locatie_meubel
225
226 Property relatieCollega = attris.stream()
227 .filter(prop -> "Relatie_met_jou_collega"
228 .equals(prop.getQuestionType().getId()))
229 .findFirst().get();
230 System.out.println(relatieCollega.getQuestionType().getQuestion());
231
232 Map<String, String> naamIsPiet = new HashMap<>();
233 naamIsPiet.put("\\[Naam\\]", "Piet");
234 TypedQuestion substiRelatie = relatieCollega.getQuestionType()
235 .substitute(naamIsPiet);
236 System.out.println(substiRelatie.getQuestion());
237 assertEquals(
238 "Beschrijf nu jou relatie tot Piet. Bij welke baan hebben jullie elkaar leren kennen, en hoe was de relatie tussen jullie?",
239 substiRelatie.getQuestion());
240 }
241
242}
Note: See TracBrowser for help on using the repository browser.