-
Notifications
You must be signed in to change notification settings - Fork 14
/
OwlapiAdapterTest.java
358 lines (317 loc) · 16.8 KB
/
OwlapiAdapterTest.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
354
355
356
357
358
/**
* Copyright (C) 2020 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 com.google.common.collect.Multimap;
import cz.cvut.kbss.ontodriver.Connection;
import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
import cz.cvut.kbss.ontodriver.model.*;
import cz.cvut.kbss.ontodriver.owlapi.connector.Connector;
import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
import cz.cvut.kbss.ontodriver.owlapi.environment.TestUtils;
import cz.cvut.kbss.ontodriver.owlapi.exception.OwlapiDriverException;
import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
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.reasoner.OWLReasoner;
import org.semanticweb.owlapi.search.EntitySearcher;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
class OwlapiAdapterTest {
private static final URI PK = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#EntityA");
private static final NamedResource INDIVIDUAL = NamedResource.create(PK);
@Mock
private Connector connectorMock;
private OntologySnapshot ontologySnapshot;
private OWLOntology ontology;
@Mock
private OWLReasoner reasonerMock;
private OWLDataFactory factory;
private OwlapiAdapter adapter;
@BeforeEach
void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
final OntologySnapshot snapshot = TestUtils.initRealOntology(reasonerMock);
this.ontology = spy(snapshot.getOntology());
this.factory = snapshot.getDataFactory();
this.ontologySnapshot = new OntologySnapshot(ontology, snapshot.getOntologyManager(), factory, reasonerMock);
when(connectorMock.getOntologySnapshot()).thenReturn(ontologySnapshot);
when(connectorMock.getOntologyUri())
.thenReturn(snapshot.getOntology().getOntologyID().getOntologyIRI().get().toURI());
this.adapter = spy(new OwlapiAdapter(connectorMock));
}
@Test
void commitSendsChangesToConnector() throws Exception {
startTransaction();
adapter.addTransactionalChanges(Collections.singletonList(mock(OWLOntologyChange.class)));
adapter.commit();
verify(connectorMock).applyChanges(any());
}
private void startTransaction() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
final Method startTransaction = OwlapiAdapter.class.getDeclaredMethod("startTransactionIfNotActive");
startTransaction.setAccessible(true);
startTransaction.invoke(adapter);
}
@Test
void rollbackCausesChangesToEmpty() throws Exception {
startTransaction();
adapter.addTransactionalChanges(Collections.singletonList(mock(OWLOntologyChange.class)));
adapter.rollback();
adapter.commit();
verify(connectorMock, never()).applyChanges(any());
}
@Test
void testIsConsistentWithCorrectContext() {
final URI uri = getOntologyUri();
when(reasonerMock.isConsistent()).thenReturn(Boolean.TRUE);
assertTrue(adapter.isConsistent(uri));
verify(reasonerMock).isConsistent();
}
private URI getOntologyUri() {
final Optional<IRI> iri = ontology.getOntologyID().getOntologyIRI();
return iri.map(IRI::toURI).orElse(null);
}
@Test
void isConsistentIgnoresContextInfo() {
when(reasonerMock.isConsistent()).thenReturn(Boolean.TRUE);
final URI ctx = URI.create("http://krizik.felk.cvut.cz/differentContext");
assertTrue(adapter.isConsistent(ctx));
verify(reasonerMock).isConsistent();
}
@Test
void testGetContexts() {
final URI uri = getOntologyUri();
final List<URI> res = adapter.getContexts();
assertEquals(1, res.size());
assertEquals(uri, res.get(0));
}
@Test
void testContainsUnknownContext() {
final Axiom<?> axiom = initAxiomForContains(Assertion.AssertionType.CLASS, false);
final URI context = URI.create("http://krizik.felk.cvut.cz/jopa/different");
boolean res = adapter.containsAxiom(axiom, Collections.singleton(context));
assertFalse(res);
}
private Axiom<?> initAxiomForContains(Assertion.AssertionType assType, boolean inferred) {
final NamedResource individual = NamedResource.create(
"http://krizik.felk.cvut.cz/ontologies/jopa#IndividualOne");
Assertion assertion = null;
Value<?> value = null;
switch (assType) {
case CLASS:
assertion = Assertion.createClassAssertion(inferred);
value = new Value<>(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#typeOne"));
break;
case PROPERTY:
case OBJECT_PROPERTY:
assertion = Assertion.createObjectPropertyAssertion(
URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#objectProperty"), inferred);
value = new Value<>(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#IndividualTwo"));
break;
case DATA_PROPERTY:
assertion = Assertion.createDataPropertyAssertion(
URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#dataProperty"), inferred);
value = new Value<>("StringDataPropertyValue");
break;
case ANNOTATION_PROPERTY:
assertion = Assertion.createAnnotationPropertyAssertion(
URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#annotationProperty"), inferred);
value = new Value<>("This is annotation value");
break;
}
return new AxiomImpl<>(individual, assertion, value);
}
private void addAxiomToOntology(Axiom<?> ax) {
final OWLNamedIndividual individual =
factory.getOWLNamedIndividual(IRI.create(ax.getSubject().getIdentifier()));
OWLAxiom owlAxiom;
switch (ax.getAssertion().getType()) {
case CLASS:
final OWLClass cls = factory.getOWLClass(IRI.create(ax.getValue().stringValue()));
owlAxiom = factory.getOWLClassAssertionAxiom(cls, individual);
break;
case OBJECT_PROPERTY:
final OWLNamedIndividual target =
factory.getOWLNamedIndividual(IRI.create(ax.getValue().stringValue()));
final OWLObjectProperty op =
factory.getOWLObjectProperty(IRI.create(ax.getAssertion().getIdentifier()));
owlAxiom = factory.getOWLObjectPropertyAssertionAxiom(op, individual, target);
break;
case DATA_PROPERTY:
final OWLDataProperty dp = factory.getOWLDataProperty(IRI.create(ax.getAssertion().getIdentifier()));
final OWLLiteral value = OwlapiUtils.createOWLLiteralFromValue(ax.getValue().getValue(),
OwlapiUtils.getAssertionLanguage(ax.getAssertion()));
owlAxiom = factory.getOWLDataPropertyAssertionAxiom(dp, individual, value);
break;
case ANNOTATION_PROPERTY:
final OWLAnnotationProperty ap =
factory.getOWLAnnotationProperty(IRI.create(ax.getAssertion().getIdentifier()));
final OWLAnnotationValue val =
OwlapiUtils.createOWLLiteralFromValue(ax.getValue().getValue(),
OwlapiUtils.getAssertionLanguage(ax.getAssertion()));
owlAxiom = factory.getOWLAnnotationAssertionAxiom(ap, individual.getIRI(), val);
break;
default:
throw new IllegalArgumentException(ax.getAssertion().toString());
}
ontologySnapshot.getOntologyManager().addAxiom(ontology, owlAxiom);
}
@Test
void testContainsClassAssertionAxiomNoContext() {
final Axiom<?> axiom = initAxiomForContains(Assertion.AssertionType.CLASS, false);
addAxiomToOntology(axiom);
boolean res = adapter.containsAxiom(axiom, null);
assertTrue(res);
final ArgumentCaptor<OWLAxiom> captor = ArgumentCaptor.forClass(OWLAxiom.class);
verify(ontology).containsAxiom(captor.capture());
final OWLAxiom ax = captor.getValue();
assertTrue(ax.isOfType(AxiomType.CLASS_ASSERTION));
final Set<OWLClass> types = ax.classesInSignature().collect(Collectors.toSet());
assertEquals(1, types.size());
final OWLClass cls = types.iterator().next();
assertEquals(axiom.getValue().getValue(), cls.getIRI().toURI());
}
@Test
void testContainsObjectPropertyAssertionInferred() {
final Axiom<?> axiom = initAxiomForContains(Assertion.AssertionType.OBJECT_PROPERTY, true);
final URI ctx = getOntologyUri();
when(reasonerMock.isEntailed(any(OWLAxiom.class))).thenReturn(Boolean.TRUE);
boolean res = adapter.containsAxiom(axiom, Collections.singleton(ctx));
assertTrue(res);
final ArgumentCaptor<OWLAxiom> captor = ArgumentCaptor.forClass(OWLAxiom.class);
verify(reasonerMock).isEntailed(captor.capture());
final OWLAxiom ax = captor.getValue();
assertTrue(ax.isOfType(AxiomType.OBJECT_PROPERTY_ASSERTION));
final Set<OWLNamedIndividual> individuals = ax.individualsInSignature().collect(Collectors.toSet());
assertEquals(2, individuals.size());
assertTrue(individuals.contains(factory.getOWLNamedIndividual(IRI.create(axiom.getSubject().toString()))));
assertTrue(individuals.contains(factory.getOWLNamedIndividual(IRI.create(axiom.getValue().stringValue()))));
final Set<OWLObjectProperty> objProperties = ax.objectPropertiesInSignature().collect(Collectors.toSet());
assertEquals(1, objProperties.size());
assertEquals(axiom.getAssertion().getIdentifier(), objProperties.iterator().next().getIRI().toURI());
}
@Test
void testContainsDataProperty() {
final Axiom<?> axiom = initAxiomForContains(Assertion.AssertionType.DATA_PROPERTY, false);
addAxiomToOntology(axiom);
boolean res = adapter.containsAxiom(axiom, null);
assertTrue(res);
final ArgumentCaptor<OWLAxiom> captor = ArgumentCaptor.forClass(OWLAxiom.class);
verify(ontology).containsAxiom(captor.capture());
final OWLAxiom ax = captor.getValue();
assertTrue(ax.isOfType(AxiomType.DATA_PROPERTY_ASSERTION));
final Set<OWLDataProperty> dataProperties = ax.dataPropertiesInSignature().collect(Collectors.toSet());
assertEquals(1, dataProperties.size());
assertEquals(axiom.getAssertion().getIdentifier(), dataProperties.iterator().next().getIRI().toURI());
final Set<OWLNamedIndividual> individuals = ax.individualsInSignature().collect(Collectors.toSet());
assertEquals(1, individuals.size());
assertEquals(axiom.getSubject().getIdentifier(), individuals.iterator().next().getIRI().toURI());
assertTrue(ax instanceof OWLDataPropertyAssertionAxiom);
assertEquals(axiom.getValue().stringValue(), ((OWLDataPropertyAssertionAxiom) ax).getObject().getLiteral());
}
@Test
void testContainsAnnotationPropertyInferred() {
final Axiom<?> axiom = initAxiomForContains(Assertion.AssertionType.ANNOTATION_PROPERTY, true);
when(reasonerMock.isEntailed(any(OWLAxiom.class))).thenReturn(Boolean.TRUE);
boolean res = adapter.containsAxiom(axiom, null);
assertTrue(res);
final ArgumentCaptor<OWLAxiom> captor = ArgumentCaptor.forClass(OWLAxiom.class);
verify(reasonerMock).isEntailed(captor.capture());
final OWLAxiom ax = captor.getValue();
assertTrue(ax.isOfType(AxiomType.ANNOTATION_ASSERTION));
final Set<OWLAnnotationProperty> annotationProperties =
ax.annotationPropertiesInSignature().collect(Collectors.toSet());
assertEquals(1, annotationProperties.size());
assertEquals(axiom.getAssertion().getIdentifier(), annotationProperties.iterator().next().getIRI().toURI());
assertTrue(ax instanceof OWLAnnotationAssertionAxiom);
final OWLAnnotationAssertionAxiom assertionAxiom = (OWLAnnotationAssertionAxiom) ax;
assertEquals(axiom.getValue().stringValue(),
assertionAxiom.getAnnotation().getValue().asLiteral().get().getLiteral());
}
@Test
void persistIndividualInMultipleClassesIsLegal() {
final AxiomValueDescriptor descriptorOne = new AxiomValueDescriptor(INDIVIDUAL);
final String typeA = "http://krizik.felk.cvut.cz/typeA";
descriptorOne.addAssertionValue(Assertion.createClassAssertion(false), new Value<>(URI.create(typeA)));
final String propA = "http://krizik.felk.cvut.cz/propertyA";
descriptorOne
.addAssertionValue(Assertion.createDataPropertyAssertion(URI.create(propA), false), new Value<>(false));
adapter.persist(descriptorOne);
adapter.commit();
final AxiomValueDescriptor descriptorTwo = new AxiomValueDescriptor(INDIVIDUAL);
final String typeB = "http://krizik.felk.cvut.cz/typeB";
descriptorTwo.addAssertionValue(Assertion.createClassAssertion(false), new Value<>(URI.create(typeB)));
final String propB = "http://krizik.felk.cvut.cz/propertyB";
final double bValue = 3.1419;
descriptorTwo.addAssertionValue(Assertion.createDataPropertyAssertion(URI.create(propB), false),
new Value<>(bValue));
adapter.persist(descriptorTwo);
final OWLNamedIndividual individual = factory.getOWLNamedIndividual(IRI.create(INDIVIDUAL.getIdentifier()));
final Collection<OWLClassExpression> classes =
EntitySearcher.getTypes(individual, ontology).collect(Collectors.toSet());
assertTrue(classes.contains(factory.getOWLClass(IRI.create(typeA))));
assertTrue(classes.contains(factory.getOWLClass(IRI.create(typeB))));
final Multimap<OWLDataPropertyExpression, OWLLiteral> properties =
EntitySearcher.getDataPropertyValues(individual, ontology);
assertTrue(
properties.containsEntry(factory.getOWLDataProperty(IRI.create(propA)), factory.getOWLLiteral(false)));
assertTrue(
properties.containsEntry(factory.getOWLDataProperty(IRI.create(propB)), factory.getOWLLiteral(bValue)));
}
@Test
void startingTransactionInitializesOntologySnapshotAndExecutorFactory() throws Exception {
final Field executorFactoryField = OwlapiAdapter.class.getDeclaredField("statementExecutorFactory");
executorFactoryField.setAccessible(true);
assertNull(executorFactoryField.get(adapter));
startTransaction();
verify(connectorMock).getOntologySnapshot();
assertNotNull(executorFactoryField.get(adapter));
}
@Test
void transactionCommitClosesTransactionalSnapshot() throws Exception {
startTransaction();
adapter.addTransactionalChanges(Collections.singletonList(mock(OWLOntologyChange.class)));
adapter.commit();
verify(connectorMock).closeSnapshot(ontologySnapshot);
}
@Test
void transactionRollbackClosesTransactionalSnapshot() throws Exception {
startTransaction();
adapter.addTransactionalChanges(Collections.singletonList(mock(OWLOntologyChange.class)));
adapter.rollback();
verify(connectorMock).closeSnapshot(ontologySnapshot);
}
@Test
void unwrapReturnsTheAdapterWhenClassMatches() throws Exception {
assertSame(adapter, adapter.unwrap(OwlapiAdapter.class));
}
@Test
void unwrapReturnsOntologySnapshotWhenOwlOntologyClassIsPassedIn() throws Exception {
assertSame(ontology, adapter.unwrap(OWLOntology.class));
}
@Test
void throwsDriverExceptionWhenUnsupportedClassIsPassedToUnwrap() {
assertThrows(OwlapiDriverException.class, () -> adapter.unwrap(Connection.class));
}
}