source: TraumaOntologies/src/test/java/tudelft/healthpsychology/traumaontologies/OwlTest.java

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

Added traumaontologies

File size: 14.9 KB
RevLine 
[4]1package tudelft.healthpsychology.traumaontologies;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertFalse;
5import static org.junit.Assert.assertTrue;
6
7import java.io.File;
8import java.io.IOException;
9import java.util.Collection;
10import java.util.LinkedList;
11import java.util.List;
12import java.util.Set;
13import java.util.stream.Collectors;
14
15import org.junit.Before;
16import org.junit.Test;
17import org.semanticweb.owlapi.apibinding.OWLManager;
18import org.semanticweb.owlapi.model.AxiomType;
19import org.semanticweb.owlapi.model.IRI;
20import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom;
21import org.semanticweb.owlapi.model.OWLAxiom;
22import org.semanticweb.owlapi.model.OWLClass;
23import org.semanticweb.owlapi.model.OWLDataFactory;
24import org.semanticweb.owlapi.model.OWLDataProperty;
25import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom;
26import org.semanticweb.owlapi.model.OWLDataPropertyDomainAxiom;
27import org.semanticweb.owlapi.model.OWLDataPropertyRangeAxiom;
28import org.semanticweb.owlapi.model.OWLException;
29import org.semanticweb.owlapi.model.OWLLiteral;
30import org.semanticweb.owlapi.model.OWLNamedIndividual;
31import org.semanticweb.owlapi.model.OWLObjectProperty;
32import org.semanticweb.owlapi.model.OWLObjectPropertyDomainAxiom;
33import org.semanticweb.owlapi.model.OWLOntology;
34import org.semanticweb.owlapi.model.OWLOntologyCreationException;
35import org.semanticweb.owlapi.model.OWLOntologyManager;
36import org.semanticweb.owlapi.model.OWLOntologyStorageException;
37import org.semanticweb.owlapi.model.OWLSubClassOfAxiom;
38import org.semanticweb.owlapi.reasoner.InferenceType;
39import org.semanticweb.owlapi.reasoner.NodeSet;
40import org.semanticweb.owlapi.reasoner.OWLReasoner;
41import org.semanticweb.owlapi.reasoner.OWLReasonerConfiguration;
42import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;
43import org.semanticweb.owlapi.reasoner.SimpleConfiguration;
44import org.semanticweb.owlapi.reasoner.structural.StructuralReasonerFactory;
45
46import tudelft.utilities.translator.BasicCsvTranslator;
47
48/**
49 * Tests basically to check what OWL is really doing and which functions are
50 * actually implemented and returning useful data.
51 *
52 */
53public class OwlTest {
54 private final String base = "http://www.owl-ontologies.com/Ontology1444730995.owl";
55
56 private OWLOntology ontology;
57 private OWLDataFactory factory;
58 final OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
59
60 private BasicCsvTranslator translator;
61
62 // https://github.com/apache/jena/blob/master/jena-arq/src-examples/arq/examples/riot/ExRIOT1_ReadModel.java
63 @Before
64 public void before() throws OWLOntologyCreationException, IOException {
65
66 ontology = manager.loadOntologyFromOntologyDocument(
67 new File("src/main/resources/Child Sexual Abuse.owl"));
68 factory = manager.getOWLDataFactory();
69
70 translator = new BasicCsvTranslator(
71 getClass().getResourceAsStream("/NL.csv"));
72
73 }
74
75 @Test
76 public void smokeTest() {
77
78 }
79
80 @Test
81 public void addAnnotationTest() throws OWLOntologyStorageException {
82 OWLClass stringDocuClass = factory.getOWLClass(
83 IRI.create("http://mysite.com/my_ontology.owl#StringDocu"));
84 System.out.println("got " + stringDocuClass);
85
86 // add a annotation
87 OWLAnnotationAssertionAxiom axiom = factory
88 .getOWLAnnotationAssertionAxiom(factory.getRDFSLabel(),
89 IRI.create("http://barf"), factory.getOWLLiteral(true));
90 ontology.add(axiom);
91 // ontology.saveOntology();
92 }
93
94 @Test
95 public void addIndividual() throws OWLOntologyStorageException {
96
97 // add a annotation
98 OWLDataPropertyAssertionAxiom axiom = factory
99 .getOWLDataPropertyAssertionAxiom(
100 factory.getOWLDataProperty(base + "hasName"),
101 factory.getOWLNamedIndividual(base + "PersonJohn"),
102 factory.getOWLLiteral("John"));
103 ontology.add(axiom);
104 // ontology.saveOntology();
105 }
106
107 @Test
108 public void testGetApparaatStatusProperties() {
109 OWLClass apparaatStatus = factory
110 .getOWLClass(IRI.create(base + "#Apparaat"));
111 ontology.axioms(AxiomType.DATA_PROPERTY_DOMAIN).forEach(dp -> {
112 if (dp.getDomain().equals(apparaatStatus)) {
113 dp.dataPropertiesInSignature().forEach(odp -> {
114 System.out.println(
115 "found property " + odp.getIRI().getShortForm()
116 + " with signature " + odp.getSignature());
117 });
118
119 }
120
121 });
122
123 }
124
125 @Test
126 public void testAssertedSuperclasses() throws OWLException {
127 // keukenapparaat subclassof apparaat
128 OWLClass quokkaCls = factory
129 .getOWLClass(IRI.create(base + "#Keukenapparaat"));
130 Collection<OWLSubClassOfAxiom> classes = ontology
131 .getSubClassAxiomsForSubClass(quokkaCls);
132 // for each superclass there will be a corresponding axiom
133 // the ontology indexes axioms in a variety of ways
134 assertEquals(1, classes.size());
135 System.out
136 .println(classes.iterator().next().getSuperClass().toString());
137 assertTrue(classes.iterator().next().getSuperClass().toString()
138 .endsWith("#Apparaat>"));
139 }
140
141 @Test
142 public void testGetAllClasses() {
143
144 Set<OWLClass> classes = ontology.classesInSignature()
145 .collect(Collectors.toSet());
146
147 System.out.println("Classes");
148 System.out.println("--------------------------------");
149 for (OWLClass cls : classes) {
150 System.out.println("+: " + cls.getIRI().getShortForm());
151
152 System.out.println("\tObject Property Domain");
153 ontology.axioms(AxiomType.OBJECT_PROPERTY_DOMAIN).forEach(op -> {
154 if (op.getDomain().equals(cls)) {
155 System.out.println("\tComments:"
156 + op.getAnnotations(factory.getRDFSComment()));
157 op.objectPropertiesInSignature().forEach(oop -> {
158 System.out.println(
159 "\t\t-: " + oop.getIRI().getShortForm());
160 });
161 }
162 });
163
164 System.out.println("\tData Property Domain");
165 ontology.axioms(AxiomType.DATA_PROPERTY_DOMAIN).forEach(dp -> {
166 if (dp.getDomain().equals(cls)) {
167 dp.dataPropertiesInSignature().forEach(odp -> {
168 System.out
169 .println("\t\t-: " + odp.getIRI().getShortForm()
170 + " " + odp.getSignature());
171
172 });
173
174 }
175 System.out.println(dp);
176
177 });
178
179 }
180 System.out.println("\tAnnotation Property Range");
181 ontology.axioms(AxiomType.ANNOTATION_PROPERTY_RANGE).forEach(dp -> {
182 System.out.println("\t\t" + dp);
183 });
184
185 System.out.println("\tAnnotation Assertion");
186 ontology.axioms(AxiomType.ANNOTATION_ASSERTION).forEach(dp -> {
187 System.out.println("\t\t" + dp);
188 });
189
190 System.out.println("\tAnnotation Property Range");
191 ontology.axioms(AxiomType.ANNOTATION_PROPERTY_RANGE).forEach(dp -> {
192 System.out.println("\t\t" + dp);
193 });
194
195 System.out.println("\tRDFS Comments(subset of annotations)");
196 ontology.axioms(AxiomType.ANNOTATION_ASSERTION).forEach(aa -> {
197 if (aa.getSubject().equals(factory.getRDFSComment())) {
198 System.out.println(aa);
199 }
200 });
201 System.out.println("\tSubclass of");
202 ontology.axioms(AxiomType.SUBCLASS_OF).forEach(aa -> {
203 System.out.println("\t\t" + aa);
204 });
205 System.out.println("-------------");
206
207 }
208
209 @Test
210 public void testGetIndividuals() {
211 System.out.println("Individuals");
212 System.out.println("--------------------------------");
213 ontology.individualsInSignature().forEach(
214 indiv -> System.out.println(indiv.getIRI().getShortForm()));
215
216 }
217
218 @Test
219 public void getAllSubclasses() {
220 ontology.axioms(AxiomType.SUBCLASS_OF).forEach(dp -> {
221 System.out.println("\t\t" + dp);
222 });
223 }
224
225 @Test
226 public void getAllSuperClasses() {
227
228 List<OWLClass> superClasses = getSuperclasses("Fornuis");
229 // fornuis ISA keukenapparaat ISA apparaat ISA object ISA Thing
230 System.out.println("superclases of fornuis:" + superClasses);
231 // fornuis ISA keukenapparaat ISA apparaat ISA object ISA Thing
232 assertEquals(5, superClasses.size());
233 }
234
235 List<OWLClass> getSuperclasses(String classname) {
236 IRI iri = IRI
237 .create("http://www.owl-ontologies.com/Ontology1444730995.owl#"
238 + classname);
239 OWLClass fornuis = manager.getOWLDataFactory().getOWLClass(iri);
240
241 // use reasoner, to do class hierarchy the inference stuff
242 OWLReasonerFactory reasonerFactory = new StructuralReasonerFactory();
243 OWLReasonerConfiguration config = new SimpleConfiguration();
244 OWLReasoner reasoner = reasonerFactory.createReasoner(ontology, config);
245 reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY);
246 assertTrue(reasoner.isConsistent());
247
248 NodeSet<OWLClass> superClasses = reasoner.getSuperClasses(fornuis,
249 false);
250
251 LinkedList<OWLClass> all = new LinkedList<OWLClass>(
252 superClasses.getFlattened());
253 all.add(fornuis);
254 return all;
255
256 }
257
258 @Test
259 public void getAllPropertiesOfFornuis() {
260 System.out.println(
261 "Found the following properties for fornuis and superclasses");
262 for (OWLClass clas : getSuperclasses("Fornuis")) {
263 // System.out.println(clas);
264 for (OWLDataPropertyDomainAxiom dp : ontology
265 .getAxioms(AxiomType.DATA_PROPERTY_DOMAIN)) {
266 if (dp.getDomain().equals(clas)) {
267 System.out.println(dp);
268 dp.dataPropertiesInSignature().forEach(odp -> {
269 System.out.println("odp=" + odp);
270 System.out.println(odp.getIRI() + "\n comment="
271 + getCommentAnnotation(odp));
272 });
273
274 }
275 }
276 }
277
278 }
279
280 @Test
281 public void getAllPropertiesOfBed() {
282 System.out.println(
283 "Found the following properties for bed and superclasses");
284 for (OWLClass clas : getSuperclasses("Bed")) {
285 System.out.println("superclass: " + clas);
286 for (OWLDataPropertyDomainAxiom dp : ontology
287 .getAxioms(AxiomType.DATA_PROPERTY_DOMAIN)) {
288 if (dp.getDomain().equals(clas)) {
289 System.out.println("data property " + dp);
290
291 System.out.println("\n comments=" + getCommentAnnotation(
292 dp.dataPropertiesInSignature().findFirst().get()));
293
294 // why do we need a cast here? And, isIri returns FALSE???
295 IRI property = (IRI) dp.getProperty().components()
296 .findFirst().get();
297 List<OWLDataPropertyRangeAxiom> varf = ontology
298 .axioms(AxiomType.DATA_PROPERTY_RANGE)
299 .filter(p -> property.equals(p.getProperty()
300 .components().findFirst().get()))
301 .collect(Collectors.toList());
302 System.out.println(" range:" + varf.get(0).getRange());
303
304 }
305 }
306
307 }
308
309 }
310
311 @Test
312 public void rangeOfAllDataPropRangeObjects() {
313
314 for (OWLDataPropertyRangeAxiom range : ontology
315 .getAxioms(AxiomType.DATA_PROPERTY_RANGE)) {
316 System.out.println("property2="
317 + range.getProperty().components().findFirst().get());
318 System.out.println("range=" + range.getRange());
319
320 }
321
322 }
323
324 @Test
325 public void rangeOfLocatieMeubel() {
326 IRI iri = IRI.create(getBaseName() + "#Locatie_meubel");
327 ontology.axioms(AxiomType.DATA_PROPERTY_RANGE)
328 .filter(range -> iri.equals(
329 range.getProperty().components().findFirst().get()))
330 .forEach(range -> System.out.println(range.getRange()));
331
332 }
333
334 private String getCommentAnnotation(OWLDataProperty odp) {
335 return ontology.annotationAssertionAxioms(odp.getIRI()).findFirst()
336 .get().getAnnotation().annotationValue().toString();
337
338 }
339
340 @Test
341 public void getAllPropertiesOfAllClasses() {
342
343 Set<OWLClass> classes;
344 Set<OWLObjectProperty> prop;
345 Set<OWLDataProperty> dataProp;
346 Set<OWLNamedIndividual> individuals;
347
348 classes = ontology.getClassesInSignature();
349 prop = ontology.getObjectPropertiesInSignature();
350 dataProp = ontology.getDataPropertiesInSignature();
351 individuals = ontology.getIndividualsInSignature();
352
353 System.out.println("Classes");
354 System.out.println("--------------------------------");
355 for (OWLClass cls : classes) {
356 System.out.println("+: " + cls.getIRI().getShortForm());
357
358 System.out.println(" \tObject Property Domain");
359 for (OWLObjectPropertyDomainAxiom op : ontology
360 .getAxioms(AxiomType.OBJECT_PROPERTY_DOMAIN)) {
361 if (op.getDomain().equals(cls)) {
362 for (OWLObjectProperty oop : op
363 .getObjectPropertiesInSignature()) {
364 System.out.println(
365 "\t\t +: " + oop.getIRI().getShortForm());
366 }
367 }
368 }
369
370 System.out.println(" \tData Property Domain");
371 for (OWLDataPropertyDomainAxiom dp : ontology
372 .getAxioms(AxiomType.DATA_PROPERTY_DOMAIN)) {
373 if (dp.getDomain().equals(cls)) {
374 for (OWLDataProperty odp : dp
375 .getDataPropertiesInSignature()) {
376 System.out.println(
377 "\t\t +: " + odp.getIRI().getShortForm());
378 }
379 }
380 }
381
382 }
383
384 }
385
386 @Test
387 public void getSubclassesOfApparaatNotWorking() {
388 IRI iri = IRI.create(
389 "<http://www.owl-ontologies.com/Ontology1444730995.owl#Apparaat");
390 OWLClass apparaat = manager.getOWLDataFactory().getOWLClass(iri);
391 System.out.println("apparaat class=" + apparaat);
392
393 OWLReasonerFactory reasonerFactory = new StructuralReasonerFactory();
394 OWLReasoner reasoner = reasonerFactory.createReasoner(ontology);
395
396 System.out.println(
397 "subclasses:" + reasoner.getSubClasses(apparaat, false));
398 }
399
400 @Test
401 public void getInstancesOfApparaatNotWorking() {
402 IRI iri = IRI.create(
403 "<http://www.owl-ontologies.com/Ontology1444730995.owl#Apparaat");
404 OWLClass apparaat = manager.getOWLDataFactory().getOWLClass(iri);
405
406 // reasoner seems not powerful enough?
407 OWLReasonerFactory reasonerFactory = new StructuralReasonerFactory();
408 OWLReasoner reasoner = reasonerFactory.createReasoner(ontology);
409
410 System.out.println("instances:" + reasoner.getInstances(apparaat));
411 }
412
413 @Test
414 public void getInstancesOfApparaat() {
415 IRI iri = IRI.create(
416 "http://www.owl-ontologies.com/Ontology1444730995.owl#Apparaat");
417 OWLClass apparaat = manager.getOWLDataFactory().getOWLClass(iri);
418 System.out.println("Subclasses of " + apparaat);
419
420 boolean hasSubclasses = false;
421 List<OWLSubClassOfAxiom> axioms = ontology.axioms(AxiomType.SUBCLASS_OF)
422 .filter(aa -> aa.getSuperClass().equals(apparaat))
423 .collect(Collectors.toList());
424
425 System.out.println(axioms);
426 assertFalse(axioms.isEmpty());
427
428 }
429
430 @Test
431 public void getInstancesOfShortApparaat() {
432 IRI iri = IRI.create(getBaseName() + "#Apparaat");
433 OWLClass apparaat = manager.getOWLDataFactory().getOWLClass(iri);
434 System.out.println("Subclasses of " + apparaat);
435
436 boolean hasSubclasses = false;
437 List<OWLSubClassOfAxiom> axioms = ontology.axioms(AxiomType.SUBCLASS_OF)
438 .filter(aa -> aa.getSuperClass().equals(apparaat))
439 .collect(Collectors.toList());
440
441 System.out.println(axioms);
442 assertFalse(axioms.isEmpty());
443 }
444
445 private String getBaseName() {
446 return ontology.getOntologyID().getOntologyIRI().get().toString();
447 }
448
449 @Test
450 public void getAllFormulasTest() {
451 System.out.println("Total axioms:" + ontology.getAxiomCount());
452 System.out.println(
453 "axioms:" + ontology.axioms().collect(Collectors.toList()));
454 assertTrue(ontology.getAxiomCount() > 100);
455 }
456
457 @Test
458 public void addAxiomTest() {
459 int naxioms = ontology.getAxiomCount();
460 OWLDataFactory df = manager.getOWLDataFactory();
461 IRI geneSetIRI = IRI.create("prefix:fdsfds");
462 OWLLiteral literal = df.getOWLLiteral(12);
463 OWLAxiom ax = df.getOWLAnnotationAssertionAxiom(df.getRDFSLabel(),
464 geneSetIRI, literal);
465 ontology.add(ax);
466 assertEquals(naxioms + 1, ontology.getAxiomCount());
467 }
468
469 @Test
470 public void toStringIdTest() {
471 OWLClass apparaatStatus = factory
472 .getOWLClass(IRI.create(base + "#Apparaat"));
473 System.out.println(apparaatStatus.toStringID());
474 System.out.println(apparaatStatus.getIRI().getShortForm());
475 }
476
477}
Note: See TracBrowser for help on using the repository browser.