Skip to content

Method: getDataByContext(URI, Class)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.test.query;
16:
17: import cz.cvut.kbss.jopa.model.EntityManager;
18: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
19: import cz.cvut.kbss.jopa.test.*;
20: import cz.cvut.kbss.jopa.test.environment.Generators;
21: import org.slf4j.Logger;
22: import org.slf4j.LoggerFactory;
23:
24: import java.net.URI;
25: import java.util.*;
26: import java.util.Map.Entry;
27:
28: public final class QueryTestEnvironment {
29:
30: private static final Logger LOG = LoggerFactory.getLogger(QueryTestEnvironment.class);
31:
32: private static final String BASE_A = "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA_";
33: private static final String TYPE_A = "http://krizik.felk.cvut.cz/ontologies/jopa/entities#TypeA";
34: private static final String BASE_B = "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityB_";
35: private static final String BASE_D = "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityD_";
36:
37: /**
38: * Default prefixes for SPARQL. </p>
39: *
40: * Currently: owl, rdf, rdfs
41: */
42: public static final String OWL_PREFIX = "PREFIX owl: <http://www.w3.org/2002/07/owl#>";
43: public static final String RDF_PREFIX = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>";
44: public static final String RDFS_PREFIX = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>";
45:
46: /** owl:Thing class */
47: public static final String OWL_THING = "http://www.w3.org/2002/07/owl#Thing";
48:
49: private static final URI NULL_CONTEXT = URI.create("http://NullContext");
50:
51: private static Map<Class<?>, List<?>> data;
52: private static Map<URI, Map<Class<?>, List<?>>> dataByContext = new HashMap<>();
53:
54: private QueryTestEnvironment() {
55: // Private constructor to prevent instantiation
56: }
57:
58: /**
59: * Generates and persists test data into the default context of the
60: * specified entity manager. </p>
61: *
62: * @param em
63: * EntityManager
64: */
65: public static void generateTestData(EntityManager em) {
66: assert em != null;
67: final Map<Class<?>, List<?>> map = generate();
68: LOG.debug("Persisting test data...");
69: persistIntoContext(em, map, null);
70: data = map;
71: }
72:
73: /**
74: * Generates and persists test data into the specified contexts. </p>
75: *
76: * This method distributes the data approximately uniformly into all the
77: * specified contexts.
78: *
79: * @param em
80: * EntityManager
81: * @param contexts
82: * A collection of target contexts
83: */
84: public static void generateTestData(EntityManager em, Collection<URI> contexts) {
85: assert em != null;
86: assert contexts != null && !contexts.isEmpty();
87: final Map<Class<?>, List<?>> map = generate();
88: LOG.debug("Persisting test data...");
89: final int contextCount = contexts.size();
90: final Map<URI, Map<Class<?>, List<?>>> contextMap = new HashMap<>();
91: for (Entry<Class<?>, List<?>> e : map.entrySet()) {
92: final List<?> dataLst = e.getValue();
93: final int sublistSize = dataLst.size() / contextCount;
94: int sublistStart = 0;
95: for (URI ctx : contexts) {
96: if (!contextMap.containsKey(ctx)) {
97: contextMap.put(ctx, new HashMap<>());
98: }
99: final List<?> sublist = dataLst.subList(sublistStart, sublistStart + sublistSize);
100: contextMap.get(ctx).put(e.getKey(), sublist);
101: sublistStart += sublistSize;
102: }
103: }
104: for (URI ctx : contextMap.keySet()) {
105: persistIntoContext(em, contextMap.get(ctx), ctx);
106: }
107: data = map;
108: }
109:
110: private static void persistIntoContext(EntityManager em, Map<Class<?>, List<?>> data,
111: URI context) {
112: final EntityDescriptor desc = new EntityDescriptor(context);
113: em.getTransaction().begin();
114: try {
115: for (List<?> l : data.values()) {
116: for (Object o : l) {
117: em.persist(o, desc);
118: }
119: }
120: em.getTransaction().commit();
121: if (context == null) {
122: context = NULL_CONTEXT;
123: }
124: dataByContext.put(context, data);
125: } catch (RuntimeException e) {
126: if (em.getTransaction().isActive()) {
127: em.getTransaction().rollback();
128: }
129: throw e;
130: }
131: }
132:
133: /**
134: * Get all current test data.
135: *
136: * @return
137: */
138: public static Map<Class<?>, List<?>> getData() {
139: return data;
140: }
141:
142: /**
143: * Get a list of test instances of the specified class.
144: *
145: * @param cls
146: * The class
147: * @return List of test data of the specified class
148: */
149: @SuppressWarnings("unchecked")
150: public static <T> List<T> getData(Class<T> cls) {
151: assert cls != null;
152: return (List<T>) data.get(cls);
153: }
154:
155: /**
156: * Gets all data from the specified context. </p>
157: *
158: * @param context
159: * Context URI, null is permitted
160: * @return Map of all data or an empty map
161: */
162: public static Map<Class<?>, List<?>> getDataByContext(URI context) {
163: if (context == null) {
164: context = NULL_CONTEXT;
165: }
166: if (!dataByContext.containsKey(context)) {
167: return Collections.emptyMap();
168: }
169: return dataByContext.get(context);
170: }
171:
172: /**
173: * Gets data from the specified context and of the specified type
174: *
175: * @param context
176: * Context URI, null is permitted
177: * @param cls
178: * Data type
179: * @return List of data or an empty list
180: */
181: @SuppressWarnings("unchecked")
182: public static <T> List<T> getDataByContext(URI context, Class<T> cls) {
183:• assert cls != null;
184:• if (context == null) {
185: context = NULL_CONTEXT;
186: }
187:• if (!dataByContext.containsKey(context)) {
188: return Collections.emptyList();
189: }
190: final Map<Class<?>, List<?>> contextData = dataByContext.get(context);
191:• if (!contextData.containsKey(cls)) {
192: return Collections.emptyList();
193: }
194: return (List<T>) contextData.get(cls);
195: }
196:
197: private static Map<Class<?>, List<?>> generate() {
198: LOG.debug("Generating test data...");
199: final Map<Class<?>, List<?>> m = new HashMap<>();
200: final int count = 10;
201: final List<OWLClassA> aa = generateOwlClassAInstances(count);
202: m.put(OWLClassA.class, aa);
203: m.put(OWLClassB.class, generateOwlClassBInstances(count));
204: m.put(OWLClassD.class, generateOwlClassDInstances(count, aa));
205: m.put(OWLClassE.class, generateOwlClassEInstances(count));
206: m.put(OWLClassT.class, generateOwlClassTInstances(count, aa));
207: return m;
208: }
209:
210: private static List<OWLClassA> generateOwlClassAInstances(int count) {
211: final List<OWLClassA> lst = new ArrayList<>();
212: int randomNum = Generators.randomInt(1000);
213: for (int i = 0; i < count; i++) {
214: final OWLClassA a = new OWLClassA();
215: a.setUri(URI.create(BASE_A + randomNum));
216: a.setStringAttribute("stringAttribute" + randomNum);
217: final Set<String> s = new HashSet<>();
218: s.add(TYPE_A);
219: a.setTypes(s);
220: lst.add(a);
221: randomNum++;
222: }
223: return lst;
224: }
225:
226: private static List<OWLClassB> generateOwlClassBInstances(int count) {
227: final List<OWLClassB> lst = new ArrayList<>();
228: int randomNum = Generators.randomInt(1000);
229: for (int i = 0; i < count; i++) {
230: final OWLClassB b = new OWLClassB();
231: b.setUri(URI.create(BASE_B + randomNum));
232: b.setStringAttribute("strAtt" + randomNum);
233: lst.add(b);
234: randomNum++;
235: }
236: return lst;
237: }
238:
239: private static List<OWLClassD> generateOwlClassDInstances(int count, List<OWLClassA> aList) {
240: final List<OWLClassD> lst = new ArrayList<>();
241: int randomNum = Generators.randomInt(1000);
242: for (int i = 0; i < count; i++) {
243: final OWLClassD d = new OWLClassD();
244: d.setUri(URI.create(BASE_D + randomNum));
245: d.setOwlClassA(aList.get(i));
246: lst.add(d);
247: randomNum++;
248: }
249: return lst;
250: }
251:
252: private static List<OWLClassE> generateOwlClassEInstances(int count) {
253: final List<OWLClassE> lst = new ArrayList<>();
254: for (int i = 0; i < count; i++) {
255: final OWLClassE e = new OWLClassE();
256: // Auto-generated id
257: e.setStringAttribute("eStr" + i);
258: lst.add(e);
259: }
260: return lst;
261: }
262:
263: private static List<OWLClassT> generateOwlClassTInstances(int count, List<OWLClassA> aList) {
264: final List<OWLClassT> lst = new ArrayList<>();
265: for (int i = 0; i < count; i++) {
266: final OWLClassT t = new OWLClassT();
267: t.setIntAttribute(i);
268: t.setName("tInstance " + i);
269: t.setDescription("Description of tInstance" + i);
270: t.setOwlClassA(aList.get(Generators.randomInt(aList.size())));
271: lst.add(t);
272: }
273: return lst;
274: }
275: }