-
Notifications
You must be signed in to change notification settings - Fork 14
/
SnapshotStorageWithInferenceTest.java
254 lines (225 loc) · 11.8 KB
/
SnapshotStorageWithInferenceTest.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
package cz.cvut.kbss.ontodriver.jena.connector;
import cz.cvut.kbss.ontodriver.config.ConfigParam;
import cz.cvut.kbss.ontodriver.config.Configuration;
import cz.cvut.kbss.ontodriver.jena.environment.Generator;
import cz.cvut.kbss.ontodriver.jena.exception.ReasonerInitializationException;
import org.apache.jena.query.Dataset;
import org.apache.jena.query.DatasetFactory;
import org.apache.jena.rdf.model.InfModel;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.reasoner.Reasoner;
import org.apache.jena.reasoner.ReasonerFactory;
import org.apache.jena.reasoner.ValidityReport;
import org.apache.jena.reasoner.rulesys.OWLFBRuleReasonerFactory;
import org.apache.jena.reasoner.rulesys.OWLMiniReasonerFactory;
import org.apache.jena.reasoner.rulesys.RDFSRuleReasonerFactory;
import org.apache.jena.vocabulary.OWL;
import org.apache.jena.vocabulary.RDF;
import org.apache.jena.vocabulary.RDFS;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.lang.reflect.Field;
import static cz.cvut.kbss.ontodriver.jena.connector.StorageTestUtil.*;
import static org.apache.jena.rdf.model.ResourceFactory.createResource;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.isA;
import static org.junit.Assert.*;
public class SnapshotStorageWithInferenceTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
private Configuration configuration;
private SnapshotStorageWithInference storage;
@Before
public void setUp() {
this.configuration = createConfiguration("urn:storage-test");
}
@Test
public void initializationCreatesRDFSReasonerBasedOnConfiguredReasonerFactoryClass() throws Exception {
configuration.setProperty(ConfigParam.REASONER_FACTORY_CLASS, RDFSRuleReasonerFactory.class.getName());
this.storage = new SnapshotStorageWithInference(configuration);
assertTrue(getReasonerFactory() instanceof RDFSRuleReasonerFactory);
}
private ReasonerFactory getReasonerFactory() throws Exception {
final Field rfField = SnapshotStorageWithInference.class.getDeclaredField("reasonerFactory");
rfField.setAccessible(true);
return (ReasonerFactory) rfField.get(storage);
}
@Test
public void initializationThrowsReasonerInitializationExceptionWhenUnknownReasonerFactoryClassIsSpecified() {
configuration
.setProperty(ConfigParam.REASONER_FACTORY_CLASS, "cz.cvut.kbss.ontodriver.jena.UnknownReasonerFactory");
thrown.expect(ReasonerInitializationException.class);
thrown.expectCause(isA(ClassNotFoundException.class));
thrown.expectMessage(containsString("Reasoner factory class"));
thrown.expectMessage(containsString("not found"));
new SnapshotStorageWithInference(configuration);
}
@Test
public void initializationThrowsReasonerInitializationExceptionWhenClassIsNotReasonerFactory() {
configuration.setProperty(ConfigParam.REASONER_FACTORY_CLASS, String.class.getName());
thrown.expect(ReasonerInitializationException.class);
thrown.expectMessage(
containsString("Class " + String.class.getName() + " is not a ReasonerFactory implementation"));
new SnapshotStorageWithInference(configuration);
}
@Test
public void initializationCreatesOWLReasonerBasedOnConfiguredReasonerFactoryClass() throws Exception {
configuration.setProperty(ConfigParam.REASONER_FACTORY_CLASS, OWLFBRuleReasonerFactory.class.getName());
this.storage = new SnapshotStorageWithInference(configuration);
assertTrue(getReasonerFactory() instanceof OWLFBRuleReasonerFactory);
}
@Test
public void getDefaultGraphReturnsInferredDefaultGraph() {
configuration.setProperty(ConfigParam.REASONER_FACTORY_CLASS, RDFSRuleReasonerFactory.class.getName());
this.storage = new SnapshotStorageWithInference(configuration);
storage.initialize();
storage.addCentralData(getDatasetWithDefaultModel());
final Model result = storage.getDefaultGraph();
assertNotNull(result);
assertTrue(result.contains(createResource(SUBJECT), RDF.type, createResource(TYPE_TWO)));
}
private Dataset getDatasetWithDefaultModel() {
final Dataset ds = DatasetFactory.createTxnMem();
ds.setDefaultModel(getModelForInference());
return ds;
}
private Model getModelForInference() {
final Model model = ModelFactory.createDefaultModel();
model.add(statement(TYPE_ONE, RDFS.subClassOf.getURI(), TYPE_TWO));
model.add(statement(SUBJECT, RDF.type.getURI(), TYPE_ONE));
return model;
}
@Test
public void getDefaultGraphReusesInfModelAfterFirstCall() {
configuration.setProperty(ConfigParam.REASONER_FACTORY_CLASS, RDFSRuleReasonerFactory.class.getName());
this.storage = new SnapshotStorageWithInference(configuration);
storage.initialize();
storage.addCentralData(getDatasetWithDefaultModel());
final Model result = storage.getDefaultGraph();
assertEquals(result, storage.getDefaultGraph());
}
@Test
public void getRawDefaultModelReturnsDefaultGraphWithoutInference() {
configuration.setProperty(ConfigParam.REASONER_FACTORY_CLASS, RDFSRuleReasonerFactory.class.getName());
this.storage = new SnapshotStorageWithInference(configuration);
storage.initialize();
storage.addCentralData(getDatasetWithDefaultModel());
final Model result = storage.getRawDefaultGraph();
assertTrue(result.contains(createResource(SUBJECT), RDF.type, createResource(TYPE_ONE)));
assertFalse(result.contains(createResource(SUBJECT), RDF.type, createResource(TYPE_TWO)));
}
@Test
public void getRawDefaultModelReturnsDefaultGraphWithoutInferenceWhenInferredModelExists() {
configuration.setProperty(ConfigParam.REASONER_FACTORY_CLASS, RDFSRuleReasonerFactory.class.getName());
this.storage = new SnapshotStorageWithInference(configuration);
storage.initialize();
storage.addCentralData(getDatasetWithDefaultModel());
assertTrue(storage.getDefaultGraph().contains(createResource(SUBJECT), RDF.type, createResource(TYPE_TWO)));
final Model result = storage.getRawDefaultGraph();
assertFalse(result instanceof InfModel);
assertTrue(result.contains(createResource(SUBJECT), RDF.type, createResource(TYPE_ONE)));
assertFalse(result.contains(createResource(SUBJECT), RDF.type, createResource(TYPE_TWO)));
}
@Test
public void getNamedGraphReturnsModelWithInferences() {
configuration.setProperty(ConfigParam.REASONER_FACTORY_CLASS, RDFSRuleReasonerFactory.class.getName());
this.storage = new SnapshotStorageWithInference(configuration);
storage.initialize();
storage.addCentralData(getDatasetWithDataInNamedGraph());
final Model result = storage.getNamedGraph(NAMED_GRAPH);
assertTrue(result.contains(createResource(SUBJECT), RDF.type, createResource(TYPE_TWO)));
}
private Dataset getDatasetWithDataInNamedGraph() {
final Dataset dataset = DatasetFactory.createTxnMem();
dataset.addNamedModel(NAMED_GRAPH, getModelForInference());
return dataset;
}
@Test
public void getNamedGraphReusesInfModelAfterFirstCall() {
configuration.setProperty(ConfigParam.REASONER_FACTORY_CLASS, RDFSRuleReasonerFactory.class.getName());
this.storage = new SnapshotStorageWithInference(configuration);
storage.initialize();
storage.addCentralData(getDatasetWithDataInNamedGraph());
final Model first = storage.getNamedGraph(NAMED_GRAPH);
assertEquals(first, storage.getNamedGraph(NAMED_GRAPH));
}
@Test
public void getRawNamedGraphReturnsGraphWithoutInference() {
configuration.setProperty(ConfigParam.REASONER_FACTORY_CLASS, RDFSRuleReasonerFactory.class.getName());
this.storage = new SnapshotStorageWithInference(configuration);
storage.initialize();
storage.addCentralData(getDatasetWithDataInNamedGraph());
final Model result = storage.getRawNamedGraph(NAMED_GRAPH);
assertTrue(result.contains(createResource(SUBJECT), RDF.type, createResource(TYPE_ONE)));
assertFalse(result.contains(createResource(SUBJECT), RDF.type, createResource(TYPE_TWO)));
}
@Test
public void getRawNamedGraphReturnsGraphWithoutInferenceWhenInferredAlreadyExists() {
configuration.setProperty(ConfigParam.REASONER_FACTORY_CLASS, RDFSRuleReasonerFactory.class.getName());
this.storage = new SnapshotStorageWithInference(configuration);
storage.initialize();
storage.addCentralData(getDatasetWithDataInNamedGraph());
assertTrue(storage.getNamedGraph(NAMED_GRAPH)
.contains(createResource(SUBJECT), RDF.type, createResource(TYPE_TWO)));
final Model result = storage.getRawNamedGraph(NAMED_GRAPH);
assertTrue(result.contains(createResource(SUBJECT), RDF.type, createResource(TYPE_ONE)));
assertFalse(result.contains(createResource(SUBJECT), RDF.type, createResource(TYPE_TWO)));
}
@Test
public void checkConsistencyReturnsValidityReportForDefaultGraph() {
configuration.setProperty(ConfigParam.REASONER_FACTORY_CLASS, RDFSRuleReasonerFactory.class.getName());
this.storage = new SnapshotStorageWithInference(configuration);
storage.initialize();
storage.addCentralData(getDatasetWithDefaultModel());
final ValidityReport result = storage.checkConsistency();
assertNotNull(result);
assertTrue(result.isValid());
}
@Test
public void checkConsistencyReturnsValidityReportForNamedGraph() {
configuration.setProperty(ConfigParam.REASONER_FACTORY_CLASS, RDFSRuleReasonerFactory.class.getName());
this.storage = new SnapshotStorageWithInference(configuration);
storage.initialize();
storage.addCentralData(getDatasetWithDataInNamedGraph());
final ValidityReport result = storage.checkConsistency(NAMED_GRAPH);
assertNotNull(result);
assertTrue(result.isValid());
}
@Test
public void checkConsistencyReturnsValidityReportForInconsistentDefaultGraph() {
configuration.setProperty(ConfigParam.REASONER_FACTORY_CLASS, OWLMiniReasonerFactory.class.getName());
this.storage = new SnapshotStorageWithInference(configuration);
storage.initialize();
storage.addCentralData(getInconsistentDataset());
final ValidityReport result = storage.checkConsistency();
assertNotNull(result);
assertFalse(result.isValid());
}
private Dataset getInconsistentDataset() {
final Model model = ModelFactory.createOntologyModel();
final Resource clsOne = createResource(Generator.generateUri().toString());
final Resource clsTwo = createResource(Generator.generateUri().toString());
model.add(clsOne, RDF.type, RDFS.Class);
model.add(clsTwo, RDF.type, RDFS.Class);
model.add(clsOne, OWL.disjointWith, clsTwo);
model.add(createResource(SUBJECT), RDF.type, clsOne);
model.add(createResource(SUBJECT), RDF.type, clsTwo);
return DatasetFactory.create(model);
}
@Ignore
@Test
public void supportedConfigurationIsPassedToReasonerOnCreation() {
configuration.setProperty(ConfigParam.REASONER_FACTORY_CLASS, RDFSRuleReasonerFactory.class.getName());
this.storage = new SnapshotStorageWithInference(configuration);
storage.initialize();
storage.addCentralData(getDatasetWithDefaultModel());
final InfModel result = storage.getDefaultGraph();
final Reasoner reasoner = result.getReasoner();
// TODO
}
}