source: TraumaOntologies/src/test/java/tudelft/healthpsychology/traumaontologies/OwlFilesTest.java@ 5

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

Added traumaontologies

File size: 5.2 KB
Line 
1package tudelft.healthpsychology.traumaontologies;
2
3import static org.junit.Assert.assertTrue;
4
5import java.io.File;
6import java.io.IOException;
7import java.util.List;
8import java.util.stream.Collectors;
9
10import org.junit.Before;
11import org.junit.Test;
12import org.junit.runner.RunWith;
13import org.junit.runners.Parameterized;
14import org.junit.runners.Parameterized.Parameter;
15import org.junit.runners.Parameterized.Parameters;
16import org.semanticweb.owlapi.apibinding.OWLManager;
17import org.semanticweb.owlapi.model.AxiomType;
18import org.semanticweb.owlapi.model.HasIRI;
19import org.semanticweb.owlapi.model.IRI;
20import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom;
21import org.semanticweb.owlapi.model.OWLClass;
22import org.semanticweb.owlapi.model.OWLDataFactory;
23import org.semanticweb.owlapi.model.OWLDataProperty;
24import org.semanticweb.owlapi.model.OWLOntology;
25import org.semanticweb.owlapi.model.OWLOntologyCreationException;
26import org.semanticweb.owlapi.model.OWLOntologyManager;
27
28import tudelft.utilities.translator.BasicCsvTranslator;
29import tudelft.utilities.translator.TranslationFailedException;
30import uk.ac.manchester.cs.owl.owlapi.OWLLiteralImplString;
31
32/**
33 * Check if the owl files are containing good data.
34 */
35@RunWith(Parameterized.class)
36
37public class OwlFilesTest {
38
39 private final static String[] owlFiles = new String[] {
40 "Child Sexual Abuse.owl", "War.owl", "War Afghanistan.owl",
41 "War Bosnia.owl", "War Libya.owl" };
42
43 @Parameter
44 public String owlFile;
45
46 private OWLOntology ontology;
47 private OWLDataFactory factory;
48 final OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
49
50 private BasicCsvTranslator translator;
51
52 @Parameters
53 public static Object[] data() {
54 return owlFiles;
55 }
56
57 @Before
58 public void before() throws OWLOntologyCreationException, IOException {
59
60 ontology = manager.loadOntologyFromOntologyDocument(
61 new File("src/main/resources/" + owlFile));
62 factory = manager.getOWLDataFactory();
63
64 translator = new BasicCsvTranslator(
65 getClass().getResourceAsStream("/NL.csv"));
66
67 }
68
69 /**
70 * Check that the top node classes are present: Object, Person,
71 * "Geografische_locatie" and "Type_Locatie".
72 */
73 @Test
74 public void testTopClasses() {
75 assertTrue(ontology.containsClassInSignature(getIri("Persoon")));
76 assertTrue(ontology.containsClassInSignature(getIri("Object")));
77 assertTrue(ontology
78 .containsClassInSignature(getIri("Geografische_locatie")));
79 assertTrue(ontology.containsClassInSignature(getIri("Type_Locatie")));
80 }
81
82 private IRI getIri(String name) {
83 return IRI.create(getBaseName() + "#" + name);
84 }
85
86 private String getBaseName() {
87 return ontology.getOntologyID().getOntologyIRI().get().toString();
88 }
89
90 /**
91 * All nodes, fields etc must be translate-able. This is more a Translation
92 * test but this test needs to be done for all owl files therefore this test
93 * is here (not in CsvTranslationFilesTest)
94 *
95 * @throws TranslationFailedException
96 */
97 @Test
98 public void checkAllAnnotationAssertionsTranslateProperly()
99 throws TranslationFailedException {
100 for (OWLAnnotationAssertionAxiom dp : ontology
101 .getAxioms(AxiomType.ANNOTATION_ASSERTION)) {
102 String text = ((OWLLiteralImplString) dp.getValue()).getLiteral();
103 System.out.println(text);
104 try {
105 translator.forward(text);
106 } catch (TranslationFailedException e) {
107 throw new IllegalStateException(
108 "There is a problem with the ontology in axiom " + dp,
109 e);
110 }
111 }
112 }
113
114 @Test
115 public void testGetAllClasses() {
116 System.out.println("\tAnnotation Assertion");
117 ontology.axioms(AxiomType.ANNOTATION_ASSERTION).forEach(dp -> {
118 System.out.println(dp.getSubject() + " " + dp.annotationValue());
119 });
120
121 }
122
123 @Test
124 public void testmax1Annotation() {
125 for (OWLClass clas : ontology.classesInSignature()
126 .collect(Collectors.toSet())) {
127 max1property(clas.getIRI());
128 }
129
130 }
131
132 @Test
133 public void testmax1AnnotationInProperties() {
134 for (OWLDataProperty clas : ontology.dataPropertiesInSignature()
135 .collect(Collectors.toSet())) {
136 max1property(clas.getIRI());
137
138 }
139 }
140
141 /**
142 * Check that there is at most 1 annotation property for the given IRI.
143 *
144 * @param iri the object IRI. Can be a class or functionalproperty iri.
145 */
146 private void max1property(IRI iri) {
147 List<OWLAnnotationAssertionAxiom> annotations = ontology
148 .axioms(AxiomType.ANNOTATION_ASSERTION)
149 .filter(dp -> dp.getSubject().equals(iri))
150 .collect(Collectors.toList());
151 System.out.println(annotations);
152 assertTrue("Object " + iri
153 + " has multiple annotations but can have only 1:"
154 + annotations, annotations.size() <= 1);
155 }
156
157 @Test
158 public void testPropertyLabelsTranslate()
159 throws TranslationFailedException {
160 for (OWLDataProperty clas : ontology.dataPropertiesInSignature()
161 .collect(Collectors.toSet())) {
162 checkIriFragmentTranslates(clas);
163 }
164 }
165
166 @Test
167 public void testClassIDssTranslate() throws TranslationFailedException {
168 for (OWLClass clas : ontology.classesInSignature()
169 .collect(Collectors.toSet())) {
170 checkIriFragmentTranslates(clas);
171
172 }
173
174 }
175
176 private void checkIriFragmentTranslates(HasIRI clas) {
177 try {
178 translator.forward(clas.getIRI().getFragment());
179 } catch (TranslationFailedException e) {
180 throw new IllegalStateException("There is a problem with " + clas,
181 e);
182 }
183 }
184
185}
Note: See TracBrowser for help on using the repository browser.