Skip to content

Commit 6be8b33

Browse files
committed
ont-api: add remaining tests for OWLOntology#referencingAxioms(OWLPrimitive) + change test-data (people & travel ontologies), see issue #11 rest
1 parent c4e26e7 commit 6be8b33

File tree

3 files changed

+337
-167
lines changed

3 files changed

+337
-167
lines changed

src/test/java/com/github/owlcs/ontapi/tests/model/ReferencingAxiomsTest.java

+147-24
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414

1515
package com.github.owlcs.ontapi.tests.model;
1616

17-
import com.github.owlcs.ontapi.OWLAdapter;
18-
import com.github.owlcs.ontapi.OntFormat;
19-
import com.github.owlcs.ontapi.OntManagers;
20-
import com.github.owlcs.ontapi.OntologyManager;
17+
import com.github.owlcs.ontapi.*;
2118
import com.github.owlcs.ontapi.utils.FileMap;
2219
import org.junit.Assert;
2320
import org.junit.Test;
@@ -33,6 +30,11 @@
3330
import java.nio.file.Path;
3431
import java.nio.file.Paths;
3532
import java.util.Arrays;
33+
import java.util.Objects;
34+
import java.util.Set;
35+
import java.util.function.Function;
36+
import java.util.stream.Collectors;
37+
import java.util.stream.Stream;
3638

3739
/**
3840
* Created by @ssz on 08.03.2020.
@@ -52,21 +54,73 @@ public static TestData[] getData() {
5254

5355
@Test
5456
public void testSearchByClass() {
55-
OWLOntology ont = data.load(OntManagers.createONT());
56-
long distinctCount = ont.classesInSignature().flatMap(ont::referencingAxioms).distinct().count();
57-
Assert.assertEquals(data.byClassDistinctCount, distinctCount);
58-
long nonDistinctCount = ont.classesInSignature().flatMap(ont::referencingAxioms).count();
59-
Assert.assertEquals(data.byClassCount, nonDistinctCount);
57+
data.doTest(T.CLS, HasClassesInSignature::classesInSignature);
58+
}
59+
60+
@Test
61+
public void testSearchByLiteral() {
62+
OWLOntology ont = data.load(newManager());
63+
Set<OWLLiteral> literals = ont.axioms().flatMap(x -> OwlObjects.objects(OWLLiteral.class, x))
64+
.collect(Collectors.toSet());
65+
data.getTester(T.LTL).testCounts(ont, x -> literals.stream());
66+
}
67+
68+
@Test
69+
public void testSearchByIRI() {
70+
OWLOntology ont = data.load(newManager());
71+
Set<IRI> iris = ont.signature().map(HasIRI::getIRI).collect(Collectors.toSet());
72+
data.getTester(T.IRI).testCounts(ont, x -> iris.stream());
73+
}
74+
75+
@Test
76+
public void testSearchByAnonymousIndividuals() {
77+
data.doTest(T.ANI, HasAnonymousIndividuals::anonymousIndividuals);
78+
}
79+
80+
@Test
81+
public void testSearchByNamedIndividuals() {
82+
data.doTest(T.NMI, HasIndividualsInSignature::individualsInSignature);
83+
}
84+
85+
@Test
86+
public void testSearchByDatatypes() {
87+
data.doTest(T.DTD, HasDatatypesInSignature::datatypesInSignature);
88+
}
89+
90+
@Test
91+
public void testSearchByObjectProperty() {
92+
data.doTest(T.OBP, HasObjectPropertiesInSignature::objectPropertiesInSignature);
93+
}
94+
95+
@Test
96+
public void testSearchByDatatypeProperty() {
97+
data.doTest(T.DTP, HasDatatypesInSignature::datatypesInSignature);
98+
}
99+
100+
@Test
101+
public void testSearchByAnnotationProperty() {
102+
data.doTest(T.ANP, HasAnnotationPropertiesInSignature::annotationPropertiesInSignature);
103+
}
104+
105+
private static OWLOntologyManager newManager() {
106+
return OntManagers.createONT();
60107
}
61108

62109
enum TestData {
63-
PIZZA("/ontapi/pizza.ttl", 1577, 795),
64-
FAMILY("/ontapi/family.ttl", 342, 236),
65-
PEOPLE("/ontapi/people.ttl", 233, 149),
66-
CAMERA("/ontapi/camera.ttl", 60, 47),
67-
KOALA("/ontapi/koala.ttl", 82, 59),
68-
TRAVEL("/ontapi/travel.ttl", 163, 111),
69-
WINE("/ontapi/wine.ttl", 576, 462) {
110+
PIZZA("/ontapi/pizza.ttl", T.CLS.of(1577, 795), T.LTL.of(120), T.IRI.of(2199, 945), T.ANI.of(),
111+
T.NMI.of(31, 23), T.DTD.of(120), T.OBP.of(231, 223), T.DTP.of(120), T.ANP.of(120)),
112+
FAMILY("/ontapi/family.ttl", T.CLS.of(342, 236), T.LTL.of(521), T.IRI.of(7543, 2845), T.ANI.of(),
113+
T.NMI.of(4214, 2372), T.DTD.of(530), T.OBP.of(1891, 1709), T.DTP.of(530), T.ANP.of(4)),
114+
PEOPLE("/ontapi/people.ttl", T.CLS.of(235, 151), T.LTL.of(201, 196), T.IRI.of(999, 409), T.ANI.of(12, 11),
115+
T.NMI.of(91, 62), T.DTD.of(197, 196), T.OBP.of(76, 65), T.DTP.of(197, 196), T.ANP.of(206, 201)),
116+
CAMERA("/ontapi/camera.ttl", T.CLS.of(60, 47), T.LTL.of(0), T.IRI.of(130, 77), T.ANI.of(),
117+
T.NMI.of(7, 6), T.DTD.of(8), T.OBP.of(27, 25), T.DTP.of(8), T.ANP.of()),
118+
KOALA("/ontapi/koala.ttl", T.CLS.of(82, 59), T.LTL.of(6), T.IRI.of(144, 76), T.ANI.of(),
119+
T.NMI.of(18, 17), T.DTD.of(7), T.OBP.of(24, 23), T.DTP.of(7), T.ANP.of(3)),
120+
TRAVEL("/ontapi/travel.ttl", T.CLS.of(167, 115), T.LTL.of(12), T.IRI.of(321, 177), T.ANI.of(12),
121+
T.NMI.of(61, 51), T.DTD.of(16), T.OBP.of(41, 36), T.DTP.of(16), T.ANP.of(12)),
122+
WINE("/ontapi/wine.ttl", T.CLS.of(576, 462), T.LTL.of(4), T.IRI.of(2127, 911), T.ANI.of(),
123+
T.NMI.of(1080, 744), T.DTD.of(5), T.OBP.of(456, 449), T.DTP.of(5), T.ANP.of(3)) {
70124
@Override
71125
String getName() {
72126
return "http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine";
@@ -77,7 +131,8 @@ public OWLOntology load(OWLOntologyManager manager) {
77131
return load(manager, FOOD, this);
78132
}
79133
},
80-
FOOD("/ontapi/food.ttl", 415, 284) {
134+
FOOD("/ontapi/food.ttl", T.CLS.of(415, 284), T.LTL.of(), T.IRI.of(772, 361), T.ANI.of(),
135+
T.NMI.of(189, 175), T.DTD.of(), T.OBP.of(168, 107), T.DTP.of(), T.ANP.of()) {
81136
@Override
82137
String getName() {
83138
return "http://www.w3.org/TR/2003/PR-owl-guide-20031209/food";
@@ -90,22 +145,31 @@ public OWLOntology load(OWLOntologyManager manager) {
90145
};
91146
private final Path file;
92147
private final OntFormat format;
93-
private final long byClassCount;
94-
private final long byClassDistinctCount;
148+
private final Tester[] expectations;
95149

96-
TestData(String file, long byClassCount, long byClassDistinctCount) {
97-
this(file, OntFormat.TURTLE, byClassCount, byClassDistinctCount);
150+
TestData(String file, Tester... expectations) {
151+
this(file, OntFormat.TURTLE, expectations);
98152
}
99153

100-
TestData(String file, OntFormat format, long byClassCount, long byClassDistinctCount) {
154+
TestData(String file, OntFormat format, Tester... expectations) {
101155
try {
102156
this.file = Paths.get(TestData.class.getResource(file).toURI()).toRealPath();
103157
} catch (IOException | URISyntaxException e) {
104158
throw new ExceptionInInitializerError(e);
105159
}
106-
this.byClassCount = byClassCount;
107-
this.byClassDistinctCount = byClassDistinctCount;
108160
this.format = format;
161+
this.expectations = expectations;
162+
}
163+
164+
public Tester getTester(T type) {
165+
return Arrays.stream(expectations)
166+
.filter(x -> Objects.equals(x.type, type))
167+
.findFirst().orElseThrow(IllegalArgumentException::new);
168+
}
169+
170+
void doTest(T type, Function<OWLOntology, Stream<? extends OWLPrimitive>> getPrimitives) {
171+
OWLOntology ont = load(newManager());
172+
getTester(type).testCounts(ont, getPrimitives);
109173
}
110174

111175
public OWLOntology load(OWLOntologyManager manager) {
@@ -160,5 +224,64 @@ public OWLDocumentFormat getDocumentFormat() {
160224
String getName() {
161225
return name();
162226
}
227+
228+
}
229+
230+
enum T {
231+
CLS,
232+
LTL,
233+
IRI,
234+
ANI,
235+
NMI,
236+
DTD,
237+
OBP,
238+
DTP,
239+
ANP,
240+
;
241+
242+
private Tester of(long sum, long distinct) {
243+
return new Tester(this, sum, distinct);
244+
}
245+
246+
private Tester of(long count) {
247+
return of(count, count);
248+
}
249+
250+
private Tester of() {
251+
return of(0, 0);
252+
}
163253
}
254+
255+
private static class Tester {
256+
private final long distinct;
257+
private final long sum;
258+
private final T type;
259+
260+
private Tester(T type, long sum, long distinct) {
261+
this.type = type;
262+
this.sum = sum;
263+
this.distinct = distinct;
264+
}
265+
266+
long distinctCount(OWLOntology ont, Stream<? extends OWLPrimitive> stream) {
267+
return referencingAxioms(ont, stream).distinct().count();
268+
}
269+
270+
long nonDistinctCount(OWLOntology ont, Stream<? extends OWLPrimitive> stream) {
271+
return referencingAxioms(ont, stream).count();
272+
}
273+
274+
Stream<? extends OWLAxiom> referencingAxioms(OWLOntology ont, Stream<? extends OWLPrimitive> stream) {
275+
return stream.flatMap(ont::referencingAxioms);
276+
}
277+
278+
void testCounts(OWLOntology ont, Function<OWLOntology, Stream<? extends OWLPrimitive>> getPrimitives) {
279+
long d = distinctCount(ont, getPrimitives.apply(ont));
280+
long s = nonDistinctCount(ont, getPrimitives.apply(ont));
281+
String msg = "(" + s + ", " + d + ")";
282+
Assert.assertEquals(msg, distinct, d);
283+
Assert.assertEquals(msg, sum, s);
284+
}
285+
}
286+
164287
}

0 commit comments

Comments
 (0)