-
Notifications
You must be signed in to change notification settings - Fork 14
/
EpistemicAxiomRemoverTest.java
353 lines (323 loc) · 18.1 KB
/
EpistemicAxiomRemoverTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/**
* Copyright (C) 2019 Czech Technical University in Prague
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.ontodriver.owlapi;
import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import cz.cvut.kbss.ontodriver.model.Value;
import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
import cz.cvut.kbss.ontodriver.owlapi.environment.TestUtils;
import cz.cvut.kbss.ontodriver.util.Vocabulary;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.search.EntitySearcher;
import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.*;
class EpistemicAxiomRemoverTest {
private static final NamedResource SUBJECT = NamedResource.create("http://krizik.felk.cvut.cz/jopa#Individual");
private OWLOntology ontology;
private OWLOntologyManager manager;
private OWLDataFactory dataFactory;
@Mock
private OwlapiAdapter adapterMock;
private OWLNamedIndividual individual;
private AxiomDescriptor descriptor = new AxiomDescriptor(SUBJECT);
private EpistemicAxiomRemover axiomRemover;
@BeforeEach
void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
final OntologySnapshot snapshot = TestUtils.initRealOntology(null);
this.ontology = spy(snapshot.getOntology());
this.manager = spy(snapshot.getOntologyManager());
this.dataFactory = snapshot.getDataFactory();
final OntologySnapshot snapshotToUse = new OntologySnapshot(ontology, manager, dataFactory, null);
this.axiomRemover = new EpistemicAxiomRemover(adapterMock, snapshotToUse);
this.individual = dataFactory.getOWLNamedIndividual(IRI.create(SUBJECT.getIdentifier()));
}
@Test
void removeDoesNothingWhenNoMatchingValuesAreFound() {
final Assertion clsAssertion = Assertion.createClassAssertion(false);
descriptor.addAssertion(clsAssertion);
axiomRemover.remove(descriptor);
verify(adapterMock, never()).addTransactionalChanges(anyList());
}
@Test
void removeAxiomsByDescriptorWithClassAssertions() {
final Assertion clsAssertion = Assertion.createClassAssertion(false);
descriptor.addAssertion(clsAssertion);
initClassAssertions();
axiomRemover.remove(descriptor);
assertTrue(ontology.classAssertionAxioms(individual).collect(Collectors.toSet()).isEmpty());
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(adapterMock).addTransactionalChanges(captor.capture());
final List<?> changes = captor.getValue();
for (Object change : changes) {
assertTrue(change instanceof RemoveAxiom);
final RemoveAxiom ax = (RemoveAxiom) change;
assertTrue(ax.getAxiom() instanceof OWLClassAssertionAxiom);
}
}
private void initClassAssertions() {
final OWLClass clsOne = dataFactory.getOWLClass(IRI.create("http://krizik.felk.cvut.cz/jopa#OWLClassA"));
final OWLClass clsTwo = dataFactory.getOWLClass(IRI.create("http://krizik.felk.cvut.cz/jopa#OWLClassB"));
manager.addAxiom(ontology, dataFactory.getOWLClassAssertionAxiom(clsOne, individual));
manager.addAxiom(ontology, dataFactory.getOWLClassAssertionAxiom(clsTwo, individual));
}
@Test
void removeAxiomsByDescriptorWithDataPropertyAssertions() {
final Assertion dpAssertion = Assertion
.createDataPropertyAssertion(URI.create("http://krizik.felk.cvut.cz/jopa#dataProperty"), false);
final OWLDataProperty odp = dataFactory.getOWLDataProperty(IRI.create(dpAssertion.getIdentifier()));
descriptor.addAssertion(dpAssertion);
initDataPropertyAssertions(odp);
final int count =
EntitySearcher.getDataPropertyValues(individual, odp, ontology).collect(Collectors.toSet()).size();
axiomRemover.remove(descriptor);
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(adapterMock).addTransactionalChanges(captor.capture());
final List<?> changes = captor.getValue();
assertEquals(count, changes.size());
verifyRemoveAxioms(odp, OWLDataPropertyAssertionAxiom.class, changes);
}
private void initDataPropertyAssertions(OWLDataProperty... dataProperties) {
for (OWLDataProperty dp : dataProperties) {
manager.addAxiom(ontology, dataFactory.getOWLDataPropertyAssertionAxiom(dp, individual, 117));
manager.addAxiom(ontology, dataFactory.getOWLDataPropertyAssertionAxiom(dp, individual, 87));
}
}
private void verifyRemoveAxioms(OWLProperty property, Class<? extends OWLPropertyAssertionAxiom> assertionType,
Collection<?> changes) {
for (Object change : changes) {
assertTrue(change instanceof RemoveAxiom);
final RemoveAxiom ax = (RemoveAxiom) change;
assertTrue(assertionType.isAssignableFrom(ax.getAxiom().getClass()));
final OWLPropertyAssertionAxiom removed = (OWLPropertyAssertionAxiom) ax.getAxiom();
assertEquals(property, removed.getProperty());
}
}
@Test
void removeAxiomsByDescriptorWithObjectPropertyAssertions() {
final Assertion opAssertion = Assertion
.createObjectPropertyAssertion(URI.create("http://krizik.felk.cvut.cz/jopa#objectProperty"), false);
final OWLObjectProperty oop = dataFactory.getOWLObjectProperty(IRI.create(opAssertion.getIdentifier()));
descriptor.addAssertion(opAssertion);
initObjectPropertyAssertions(oop);
final int count =
EntitySearcher.getObjectPropertyValues(individual, oop, ontology).collect(Collectors.toSet()).size();
axiomRemover.remove(descriptor);
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(adapterMock).addTransactionalChanges(captor.capture());
final List<?> changes = captor.getValue();
assertEquals(count, changes.size());
verifyRemoveAxioms(oop, OWLObjectPropertyAssertionAxiom.class, changes);
}
private void initObjectPropertyAssertions(OWLObjectProperty... objectProperties) {
final OWLNamedIndividual value = dataFactory
.getOWLNamedIndividual(IRI.create("http://krizik.felk.cvut.cz/jopa#IndividualTwo"));
for (OWLObjectProperty op : objectProperties) {
manager.addAxiom(ontology, dataFactory.getOWLObjectPropertyAssertionAxiom(op, individual, value));
}
}
@Test
void removeRetainsValuesOfAssertionsNotPresentInDescriptor() {
final Assertion opAssertion = Assertion
.createObjectPropertyAssertion(URI.create("http://krizik.felk.cvut.cz/jopa#objectProperty"), false);
final Assertion opAssertionTwo = Assertion
.createObjectPropertyAssertion(URI.create("http://krizik.felk.cvut.cz/jopa#objectPropertyTwo"), false);
descriptor.addAssertion(opAssertion);
final OWLObjectProperty opOne = dataFactory.getOWLObjectProperty(IRI.create(opAssertion.getIdentifier()));
final OWLObjectProperty opTwo = dataFactory.getOWLObjectProperty(IRI.create(opAssertionTwo.getIdentifier()));
initObjectPropertyAssertions(opOne, opTwo);
final int countTwo =
EntitySearcher.getObjectPropertyValues(individual, opTwo, ontology).collect(Collectors.toSet()).size();
axiomRemover.remove(descriptor);
final int resultCountOne =
EntitySearcher.getObjectPropertyValues(individual, opOne, ontology).collect(Collectors.toSet()).size();
final int resultCountTwo =
EntitySearcher.getObjectPropertyValues(individual, opTwo, ontology).collect(Collectors.toSet()).size();
assertEquals(0, resultCountOne);
assertEquals(countTwo, resultCountTwo);
}
@Test
void removeAxiomsByDescriptorWithAnnotationPropertyAssertions() {
final Assertion apAssertion = Assertion.createAnnotationPropertyAssertion(
URI.create("http://krizik.felk.cvut.cz/jopa#annotationProperty"), false);
final OWLAnnotationProperty oap = dataFactory.getOWLAnnotationProperty(IRI.create(apAssertion.getIdentifier()));
descriptor.addAssertion(apAssertion);
initAnnotationAssertions(oap);
axiomRemover.remove(descriptor);
assertEquals(0,
EntitySearcher.getAnnotationAssertionAxioms(individual.getIRI(), ontology).collect(Collectors.toSet())
.size());
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(adapterMock).addTransactionalChanges(captor.capture());
final List<?> changes = captor.getValue();
verifyAnnotationRemoveAxioms(oap, changes);
}
private void initAnnotationAssertions(OWLAnnotationProperty... annotationProperties) {
for (OWLAnnotationProperty ap : annotationProperties) {
manager.addAxiom(ontology, dataFactory
.getOWLAnnotationAssertionAxiom(ap, individual.getIRI(), dataFactory.getOWLLiteral(117)));
manager.addAxiom(ontology, dataFactory
.getOWLAnnotationAssertionAxiom(ap, individual.getIRI(), dataFactory.getOWLLiteral("String")));
}
}
private void verifyAnnotationRemoveAxioms(OWLProperty property, Collection<?> changes) {
for (Object change : changes) {
assertTrue(change instanceof RemoveAxiom);
final RemoveAxiom ax = (RemoveAxiom) change;
assertTrue(OWLAnnotationAssertionAxiom.class.isAssignableFrom(ax.getAxiom().getClass()));
final OWLAnnotationAssertionAxiom removed = (OWLAnnotationAssertionAxiom) ax.getAxiom();
assertEquals(property, removed.getProperty());
}
}
@Test
void removeCombinationOfDataObjectAndAnnotationPropertiesAndClassAssertion() {
final Set<OWLPropertyExpression> removed = initForCombinedTest();
axiomRemover.remove(descriptor);
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(adapterMock).addTransactionalChanges(captor.capture());
final List<?> changes = captor.getValue();
for (Object change : changes) {
final RemoveAxiom ax = (RemoveAxiom) change;
final OWLPropertyExpression op;
if (!(ax.getAxiom() instanceof OWLClassAssertionAxiom)) {
if (ax.getAxiom() instanceof OWLAnnotationAssertionAxiom) {
op = ((OWLAnnotationAssertionAxiom) ax.getAxiom()).getProperty();
} else {
op = ((OWLPropertyAssertionAxiom) ax.getAxiom()).getProperty();
}
assertTrue(removed.contains(op));
}
}
}
private Set<OWLPropertyExpression> initForCombinedTest() {
final Assertion clsAssertion = Assertion.createClassAssertion(false);
descriptor.addAssertion(clsAssertion);
initClassAssertions();
final Assertion dpAssertion = Assertion
.createDataPropertyAssertion(URI.create("http://krizik.felk.cvut.cz/jopa#dataProperty"), false);
final OWLDataProperty odp = dataFactory.getOWLDataProperty(IRI.create(dpAssertion.getIdentifier()));
descriptor.addAssertion(dpAssertion);
initDataPropertyAssertions(odp);
final Assertion opAssertion = Assertion
.createObjectPropertyAssertion(URI.create("http://krizik.felk.cvut.cz/jopa#objectProperty"), false);
final Assertion opAssertionTwo = Assertion
.createObjectPropertyAssertion(URI.create("http://krizik.felk.cvut.cz/jopa#objectPropertyTwo"), false);
descriptor.addAssertion(opAssertion);
final OWLObjectProperty opOne = dataFactory.getOWLObjectProperty(IRI.create(opAssertion.getIdentifier()));
final OWLObjectProperty opTwo = dataFactory.getOWLObjectProperty(IRI.create(opAssertionTwo.getIdentifier()));
initObjectPropertyAssertions(opOne, opTwo);
final Assertion ap = Assertion.createAnnotationPropertyAssertion(
URI.create("http://krizik.felk.cvut.cz/jopa#annotationProperty"), false);
final OWLAnnotationProperty oap = dataFactory.getOWLAnnotationProperty(IRI.create(ap.getIdentifier()));
descriptor.addAssertion(ap);
initAnnotationAssertions(oap);
return new HashSet<>(Arrays.asList(odp, opOne, oap));
}
@Test
void removeAxiomsRemovesCorrespondingAssertions() {
final Map<Assertion, Set<Value<?>>> toRemove = initForRemoveAxiomsTest();
axiomRemover.removeAxioms(SUBJECT, toRemove);
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(adapterMock).addTransactionalChanges(captor.capture());
final List<?> changes = captor.getValue();
final int toRemoveSize = toRemove.values().stream().mapToInt(Set::size).sum();
assertEquals(toRemoveSize, changes.size());
for (Object change : changes) {
final RemoveAxiom ax = (RemoveAxiom) change;
final Assertion a;
if (ax.getAxiom().annotationPropertiesInSignature().anyMatch(x -> true)) {
a = Assertion.createPropertyAssertion(
ax.getAxiom().annotationPropertiesInSignature().iterator().next().getIRI().toURI(), false);
} else if (ax.getAxiom().dataPropertiesInSignature().anyMatch(x -> true)) {
a = Assertion.createPropertyAssertion(
ax.getAxiom().dataPropertiesInSignature().iterator().next().getIRI().toURI(), false);
} else if (ax.getAxiom().objectPropertiesInSignature().anyMatch(x -> true)) {
a = Assertion.createPropertyAssertion(
ax.getAxiom().objectPropertiesInSignature().iterator().next().getIRI().toURI(), false);
} else {
a = Assertion.createPropertyAssertion(URI.create(Vocabulary.RDF_TYPE), false);
}
assertTrue(toRemove.containsKey(a));
}
}
private Map<Assertion, Set<Value<?>>> initForRemoveAxiomsTest() {
final int valueCount = 5;
final Map<Assertion, Set<Value<?>>> map = new HashMap<>();
final Assertion op = Assertion
.createPropertyAssertion(URI.create("http://krizik.felk.cvut.cz/jopa#objectProperty"), false);
final OWLObjectProperty owlOp = dataFactory.getOWLObjectProperty(IRI.create(op.getIdentifier()));
map.put(op, new HashSet<>());
for (int i = 0; i < valueCount; i++) {
final String strVal = "http://krizik.felk.cvut.cz/jopa#Individual-" + i;
if (i % 2 == 0) {
map.get(op).add(new Value<>(strVal));
}
manager.addAxiom(ontology, dataFactory.getOWLObjectPropertyAssertionAxiom(owlOp, individual,
dataFactory.getOWLNamedIndividual(IRI.create(strVal))));
}
final Assertion dp = Assertion
.createPropertyAssertion(URI.create("http://krizik.felk.cvut.cz/jopa#dataProperty"), false);
final OWLDataProperty owlDp = dataFactory.getOWLDataProperty(IRI.create(dp.getIdentifier()));
map.put(dp, new HashSet<>());
for (int i = 0; i < valueCount; i++) {
final String strVal = "John117";
if (i % 2 == 0) {
map.get(dp).add(new Value<>(strVal));
}
manager.addAxiom(ontology, dataFactory.getOWLDataPropertyAssertionAxiom(owlDp, individual, strVal));
}
final Assertion ap = Assertion
.createPropertyAssertion(URI.create("http://krizik.felk.cvut.cz/jopa#annotationProperty"), false);
final OWLAnnotationProperty owlAp = dataFactory.getOWLAnnotationProperty(IRI.create(ap.getIdentifier()));
map.put(ap, new HashSet<>());
for (int i = 0; i < valueCount; i++) {
final String strVal = "John117";
if (i % 2 == 0) {
map.get(ap).add(new Value<>(strVal));
}
manager.addAxiom(ontology, dataFactory
.getOWLAnnotationAssertionAxiom(owlAp, individual.getIRI(), dataFactory.getOWLLiteral(strVal)));
}
final Assertion ca = Assertion.createPropertyAssertion(URI.create(Vocabulary.RDF_TYPE), false);
map.put(ca, new HashSet<>());
for (int i = 0; i < valueCount; i++) {
final String strVal = "http://krizik.felk.cvut.cz/jopa#OWLClass" + i;
if (i % 2 == 0) {
map.get(ca).add(new Value<>(strVal));
}
manager.addAxiom(ontology,
dataFactory.getOWLClassAssertionAxiom(dataFactory.getOWLClass(IRI.create(strVal)), individual));
}
return map;
}
@Test
void removeAxiomsWithUnknownPropertyDoesNothing() {
final Map<Assertion, Set<Value<?>>> toRemove = new HashMap<>();
final Assertion unknownProperty = Assertion
.createPropertyAssertion(URI.create("http://krizik.felk.cvut.cz/jopa#unknownProperty"), false);
toRemove.put(unknownProperty, Collections.singleton(new Value<>("StringValue")));
axiomRemover.removeAxioms(SUBJECT, toRemove);
verify(manager, never()).applyChange(any(OWLOntologyChange.class));
verify(manager).applyChanges(Collections.emptyList());
}
}