-
Notifications
You must be signed in to change notification settings - Fork 14
/
AxiomSaverTest.java
243 lines (215 loc) · 12.1 KB
/
AxiomSaverTest.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
/**
* Copyright (C) 2016 Czech Technical University in Prague
*
* 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.
*
* 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.AxiomValueDescriptor;
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.owlapi.util.MutableAddAxiom;
import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
import org.junit.Before;
import org.junit.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 java.net.URI;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyList;
import static org.mockito.Mockito.*;
public class AxiomSaverTest {
private static final NamedResource SUBJECT = NamedResource
.create("http://krizik.felk.cvut.cz/ontologies/jopa/Individual");
private static final URI ASSERTION_URI = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/TestAssertion");
@Mock
private OwlapiAdapter adapterMock;
private OWLOntology ontology;
private OWLOntologyManager manager;
private OWLDataFactory dataFactory;
@Mock
private OWLReasoner reasonerMock;
private OWLNamedIndividual individual;
private AxiomValueDescriptor descriptor;
private AxiomSaver axiomSaver;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
final OntologySnapshot snapshot = TestUtils.initRealOntology(reasonerMock);
this.ontology = spy(snapshot.getOntology());
this.manager = spy(snapshot.getOntologyManager());
this.dataFactory = snapshot.getDataFactory();
final OntologySnapshot snapshotToUse = new OntologySnapshot(ontology, manager, dataFactory, reasonerMock);
this.axiomSaver = new AxiomSaver(adapterMock, snapshotToUse);
when(adapterMock.getLanguage()).thenReturn("en");
this.descriptor = new AxiomValueDescriptor(SUBJECT);
this.individual = dataFactory.getOWLNamedIndividual(IRI.create(SUBJECT.getIdentifier()));
}
@Test
public void persistValueWithNullDoesNothing() throws Exception {
final Assertion dpAssertion = Assertion.createDataPropertyAssertion(ASSERTION_URI, false);
final Assertion opAssertion = Assertion.createObjectPropertyAssertion(ASSERTION_URI, false);
final Assertion apAssertion = Assertion.createAnnotationPropertyAssertion(ASSERTION_URI, false);
descriptor.addAssertionValue(dpAssertion, Value.nullValue());
descriptor.addAssertionValue(opAssertion, Value.nullValue());
descriptor.addAssertionValue(apAssertion, Value.nullValue());
axiomSaver.persist(descriptor);
verify(manager, never()).applyChanges(anyList());
}
@Test
public void testPersistIndividualWithDataProperty() throws Exception {
final Assertion dpAssertion = Assertion.createDataPropertyAssertion(
URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#A-dataAttribute"), false);
final Set<Object> values = new HashSet<>();
values.add("StringValue");
values.add(1111);
values.add(3.14159D);
descriptor.addAssertion(dpAssertion);
values.forEach(value -> descriptor.addAssertionValue(dpAssertion, new Value<>(value)));
axiomSaver.persist(descriptor);
verifyDataPropertyValuesInOntology(dpAssertion, values);
verifyTransactionalChangesRecorded();
}
private void verifyTransactionalChangesRecorded() {
final ArgumentCaptor<List> changesCaptor = ArgumentCaptor.forClass(List.class);
verify(adapterMock, atLeastOnce()).addTransactionalChanges(changesCaptor.capture());
final List<?> changes = changesCaptor.getValue();
for (Object ch : changes) {
assertTrue(ch instanceof MutableAddAxiom);
}
}
private void verifyDataPropertyValuesInOntology(Assertion assertion, Set<Object> values) {
final Set<OWLDataPropertyAssertionAxiom> dpAxioms = ontology.getDataPropertyAssertionAxioms(individual);
assertEquals(values.size(), dpAxioms.size());
for (OWLDataPropertyAssertionAxiom axiom : dpAxioms) {
assertEquals(assertion.getIdentifier(), axiom.getProperty().asOWLDataProperty().getIRI().toURI());
final Object value = OwlapiUtils.owlLiteralToValue(axiom.getObject());
assertTrue(values.contains(value));
}
}
@Test
public void testPersistIndividualWithAnnotationProperty() throws Exception {
// This just makes sure that the individual explicitly exists in the ontology
final Assertion apAssertion = Assertion.createAnnotationPropertyAssertion(
URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#A-annotationAttribute"), false);
final Set<Object> values = new HashSet<>();
values.add("StringLabel");
values.add(1111);
values.add(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/apValue"));
descriptor.addAssertion(apAssertion);
values.forEach(value -> descriptor.addAssertionValue(apAssertion, new Value<>(value)));
axiomSaver.persist(descriptor);
verifyAnnotationPropertyValuesInOntology(apAssertion, values);
verifyTransactionalChangesRecorded();
}
private void verifyAnnotationPropertyValuesInOntology(Assertion assertion, Set<Object> values) {
final Set<OWLAnnotationAssertionAxiom> apAxioms = ontology.getAnnotationAssertionAxioms(individual.getIRI());
assertEquals(values.size(), apAxioms.size());
for (OWLAnnotationAssertionAxiom axiom : apAxioms) {
assertEquals(assertion.getIdentifier(), axiom.getProperty().getIRI().toURI());
final OWLAnnotationValue value = axiom.getValue();
if (value.asIRI().isPresent()) {
assertTrue(values.contains(value.asIRI().get().toURI()));
} else {
final Object v = OwlapiUtils.owlLiteralToValue(value.asLiteral().get());
assertTrue(values.contains(v));
}
}
}
@Test
public void testPersistIndividualWithObjectProperty() throws Exception {
final Assertion assertion = Assertion.createObjectPropertyAssertion(
URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#hasA"), false);
descriptor.addAssertion(assertion);
final Set<NamedResource> values = new HashSet<>();
values.add(NamedResource.create("http://krizik.felk.cvut.cz/ontologies/jopa/entityA"));
values.add(NamedResource.create("http://krizik.felk.cvut.cz/ontologies/jopa/entityA2"));
values.add(NamedResource.create("http://krizik.felk.cvut.cz/ontologies/jopa/entityA3"));
values.forEach(value -> descriptor.addAssertionValue(assertion, new Value<>(value)));
axiomSaver.persist(descriptor);
verifyObjectPropertyValuesInOntology(assertion, values);
verifyTransactionalChangesRecorded();
}
private void verifyObjectPropertyValuesInOntology(Assertion assertion, Set<NamedResource> values) {
final Set<OWLObjectPropertyAssertionAxiom> opAxioms = ontology.getObjectPropertyAssertionAxioms(individual);
assertEquals(values.size(), opAxioms.size());
for (OWLObjectPropertyAssertionAxiom axiom : opAxioms) {
assertEquals(assertion.getIdentifier(), axiom.getProperty().asOWLObjectProperty().getIRI().toURI());
final NamedResource target = NamedResource
.create(axiom.getObject().asOWLNamedIndividual().getIRI().toURI());
assertTrue(values.contains(target));
}
}
@Test
public void testPersistIndividualWithUnspecifiedPropertyTypePropertiesExist() throws Exception {
final Assertion opAssertion = Assertion.createPropertyAssertion(
URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#hasA"), false);
descriptor.addAssertion(opAssertion);
final Set<NamedResource> opValues = new HashSet<>();
opValues.add(NamedResource.create("http://krizik.felk.cvut.cz/ontologies/jopa/entityA"));
opValues.add(NamedResource.create("http://krizik.felk.cvut.cz/ontologies/jopa/entityA2"));
opValues.add(NamedResource.create("http://krizik.felk.cvut.cz/ontologies/jopa/entityA3"));
opValues.forEach(value -> descriptor.addAssertionValue(opAssertion, new Value<>(value)));
final Assertion dpAssertion = Assertion.createPropertyAssertion(
URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#A-dataAttribute"), false);
final Set<Object> dpValues = new HashSet<>();
dpValues.add("StringValue");
descriptor.addAssertion(dpAssertion);
dpValues.forEach(value -> descriptor.addAssertionValue(dpAssertion, new Value<>(value)));
// Make sure that the assertions exist in the ontology so that the saver is able to determine the property type
addDataPropertyAssertion(dpAssertion.getIdentifier());
addObjectPropertyAssertion(opAssertion.getIdentifier());
axiomSaver.persist(descriptor);
verifyDataPropertyValuesInOntology(dpAssertion, dpValues);
verifyObjectPropertyValuesInOntology(opAssertion, opValues);
verifyTransactionalChangesRecorded();
}
private void addDataPropertyAssertion(URI property) {
final OWLDataProperty dataProperty = dataFactory.getOWLDataProperty(IRI.create(property));
final OWLIndividual ind = dataFactory.getOWLAnonymousIndividual();
final AddAxiom ax = new AddAxiom(ontology,
dataFactory.getOWLDataPropertyAssertionAxiom(dataProperty, ind, dataFactory.getOWLLiteral(true)));
manager.applyChange(ax);
}
private void addObjectPropertyAssertion(URI property) {
final OWLObjectProperty objectProperty = dataFactory.getOWLObjectProperty(IRI.create(property));
final OWLIndividual subject = dataFactory.getOWLAnonymousIndividual();
final OWLIndividual target = dataFactory.getOWLAnonymousIndividual();
final AddAxiom ax = new AddAxiom(ontology,
dataFactory.getOWLObjectPropertyAssertionAxiom(objectProperty, subject, target));
manager.applyChange(ax);
}
@Test
public void testPersistIndividualWithUnspecifiedPropertyTypePropertiesUnknown() throws Exception {
final Assertion opAssertion = Assertion.createPropertyAssertion(
URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#hasA"), false);
descriptor.addAssertion(opAssertion);
final Set<NamedResource> opValues = new HashSet<>();
opValues.add(NamedResource.create("http://krizik.felk.cvut.cz/ontologies/jopa/entityA"));
opValues.add(NamedResource.create("http://krizik.felk.cvut.cz/ontologies/jopa/entityA2"));
opValues.add(NamedResource.create("http://krizik.felk.cvut.cz/ontologies/jopa/entityA3"));
opValues.add(NamedResource.create("non-uri-value"));
opValues.forEach(value -> descriptor.addAssertionValue(opAssertion, new Value<>(value)));
axiomSaver.persist(descriptor);
verifyObjectPropertyValuesInOntology(opAssertion, opValues);
verifyTransactionalChangesRecorded();
}
}