-
Notifications
You must be signed in to change notification settings - Fork 14
/
QueryTestEnvironment.java
267 lines (244 loc) · 9.56 KB
/
QueryTestEnvironment.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
/**
* Copyright (C) 2016 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.jopa.test.query;
import cz.cvut.kbss.jopa.model.EntityManager;
import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
import cz.cvut.kbss.jopa.test.*;
import cz.cvut.kbss.jopa.test.environment.Generators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URI;
import java.util.*;
import java.util.Map.Entry;
public final class QueryTestEnvironment {
private static final Logger LOG = LoggerFactory.getLogger(QueryTestEnvironment.class);
private static final String BASE_A = "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA_";
private static final String TYPE_A = "http://krizik.felk.cvut.cz/ontologies/jopa/entities#TypeA";
private static final String BASE_B = "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityB_";
private static final String BASE_D = "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityD_";
/**
* Default prefixes for SPARQL.
* <p>
* Currently: owl, rdf, rdfs
*/
public static final String OWL_PREFIX = "PREFIX owl: <http://www.w3.org/2002/07/owl#>";
public static final String RDF_PREFIX = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>";
public static final String RDFS_PREFIX = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>";
/**
* owl:Thing class
*/
public static final String OWL_THING = "http://www.w3.org/2002/07/owl#Thing";
private static final URI NULL_CONTEXT = URI.create("http://NullContext");
private static Map<Class<?>, List<?>> data;
private static Map<URI, Map<Class<?>, List<?>>> dataByContext = new HashMap<>();
private QueryTestEnvironment() {
// Private constructor to prevent instantiation
}
/**
* Generates and persists test data into the default context of the
* specified entity manager.
*
* @param em EntityManager
*/
public static void generateTestData(EntityManager em) {
assert em != null;
final Map<Class<?>, List<?>> map = generate();
LOG.debug("Persisting test data...");
persistIntoContext(em, map, null);
data = map;
}
/**
* Generates and persists test data into the specified contexts.
* <p>
* This method distributes the data approximately uniformly into all the
* specified contexts.
*
* @param em EntityManager
* @param contexts A collection of target contexts
*/
public static void generateTestData(EntityManager em, Collection<URI> contexts) {
assert em != null;
assert contexts != null && !contexts.isEmpty();
final Map<Class<?>, List<?>> map = generate();
LOG.debug("Persisting test data...");
final int contextCount = contexts.size();
final Map<URI, Map<Class<?>, List<?>>> contextMap = new HashMap<>();
for (Entry<Class<?>, List<?>> e : map.entrySet()) {
final List<?> dataLst = e.getValue();
final int sublistSize = dataLst.size() / contextCount;
int sublistStart = 0;
for (URI ctx : contexts) {
if (!contextMap.containsKey(ctx)) {
contextMap.put(ctx, new HashMap<>());
}
final List<?> sublist = dataLst.subList(sublistStart, sublistStart + sublistSize);
contextMap.get(ctx).put(e.getKey(), sublist);
sublistStart += sublistSize;
}
}
contextMap.forEach((ctx, lst) -> persistIntoContext(em, lst, ctx));
data = map;
}
private static void persistIntoContext(EntityManager em, Map<Class<?>, List<?>> data, URI context) {
final EntityDescriptor desc = new EntityDescriptor(context);
try {
em.getTransaction().begin();
for (List<?> l : data.values()) {
for (Object o : l) {
em.persist(o, desc);
}
}
em.getTransaction().commit();
if (context == null) {
context = NULL_CONTEXT;
}
dataByContext.put(context, data);
} catch (RuntimeException e) {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
throw e;
}
}
/**
* Get all current test data.
*
* @return Map of test data
*/
public static Map<Class<?>, List<?>> getData() {
return data;
}
/**
* Get a list of test instances of the specified class.
*
* @param cls The class
* @return List of test data of the specified class
*/
@SuppressWarnings("unchecked")
public static <T> List<T> getData(Class<T> cls) {
assert cls != null;
return (List<T>) data.get(cls);
}
/**
* Gets all data from the specified context.
*
* @param context Context URI, null is permitted
* @return Map of all data or an empty map
*/
public static Map<Class<?>, List<?>> getDataByContext(URI context) {
if (context == null) {
context = NULL_CONTEXT;
}
if (!dataByContext.containsKey(context)) {
return Collections.emptyMap();
}
return dataByContext.get(context);
}
/**
* Gets data from the specified context and of the specified type
*
* @param context Context URI, null is permitted
* @param cls Data type
* @return List of data or an empty list
*/
@SuppressWarnings("unchecked")
public static <T> List<T> getDataByContext(URI context, Class<T> cls) {
assert cls != null;
if (context == null) {
context = NULL_CONTEXT;
}
if (!dataByContext.containsKey(context)) {
return Collections.emptyList();
}
final Map<Class<?>, List<?>> contextData = dataByContext.get(context);
if (!contextData.containsKey(cls)) {
return Collections.emptyList();
}
return (List<T>) contextData.get(cls);
}
private static Map<Class<?>, List<?>> generate() {
LOG.debug("Generating test data...");
final Map<Class<?>, List<?>> m = new HashMap<>();
final int count = 10;
final List<OWLClassA> aa = generateOwlClassAInstances(count);
m.put(OWLClassA.class, aa);
m.put(OWLClassB.class, generateOwlClassBInstances(count));
m.put(OWLClassD.class, generateOwlClassDInstances(count, aa));
m.put(OWLClassE.class, generateOwlClassEInstances(count));
m.put(OWLClassT.class, generateOwlClassTInstances(count, aa));
return m;
}
private static List<OWLClassA> generateOwlClassAInstances(int count) {
final List<OWLClassA> lst = new ArrayList<>();
int randomNum = Generators.randomInt(1000);
for (int i = 0; i < count; i++) {
final OWLClassA a = new OWLClassA();
a.setUri(URI.create(BASE_A + randomNum));
a.setStringAttribute("stringAttribute" + randomNum);
final Set<String> s = new HashSet<>();
s.add(TYPE_A);
a.setTypes(s);
lst.add(a);
randomNum++;
}
return lst;
}
private static List<OWLClassB> generateOwlClassBInstances(int count) {
final List<OWLClassB> lst = new ArrayList<>();
int randomNum = Generators.randomInt(1000);
for (int i = 0; i < count; i++) {
final OWLClassB b = new OWLClassB();
b.setUri(URI.create(BASE_B + randomNum));
b.setStringAttribute("strAtt" + randomNum);
lst.add(b);
randomNum++;
}
return lst;
}
private static List<OWLClassD> generateOwlClassDInstances(int count, List<OWLClassA> aList) {
final List<OWLClassD> lst = new ArrayList<>();
int randomNum = Generators.randomInt(1000);
for (int i = 0; i < count; i++) {
final OWLClassD d = new OWLClassD();
d.setUri(URI.create(BASE_D + randomNum));
d.setOwlClassA(aList.get(i));
lst.add(d);
randomNum++;
}
return lst;
}
private static List<OWLClassE> generateOwlClassEInstances(int count) {
final List<OWLClassE> lst = new ArrayList<>();
for (int i = 0; i < count; i++) {
final OWLClassE e = new OWLClassE();
// Auto-generated id
e.setStringAttribute("eStr" + i);
lst.add(e);
}
return lst;
}
private static List<OWLClassT> generateOwlClassTInstances(int count, List<OWLClassA> aList) {
final List<OWLClassT> lst = new ArrayList<>();
for (int i = 0; i < count; i++) {
final OWLClassT t = new OWLClassT();
t.setIntAttribute(i);
t.setName("tInstance " + i);
t.setDescription("Description of tInstance" + i);
t.setOwlClassA(aList.get(Generators.randomInt(aList.size())));
lst.add(t);
}
return lst;
}
}