Skip to content

Method: generateTestData(EntityManager)

1: /**
2: * Copyright (C) 2023 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 int ITEM_COUNT = 10;
33:
34: private static final String BASE_A = "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA_";
35: private static final String TYPE_A = "http://krizik.felk.cvut.cz/ontologies/jopa/entities#TypeA";
36: private static final String BASE_B = "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityB_";
37: private static final String BASE_D = "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityD_";
38:
39: private static final URI NULL_CONTEXT = URI.create("http://NullContext");
40:
41: private static Map<Class<?>, List<?>> data;
42: private static final Map<URI, Map<Class<?>, List<?>>> dataByContext = new HashMap<>();
43:
44: private QueryTestEnvironment() {
45: // Private constructor to prevent instantiation
46: }
47:
48: /**
49: * Generates and persists test data into the default context of the
50: * specified entity manager.
51: *
52: * @param em EntityManager
53: */
54: public static void generateTestData(EntityManager em) {
55:• assert em != null;
56: final Map<Class<?>, List<?>> map = generate();
57: LOG.debug("Persisting test data...");
58: persistIntoContext(em, map, null);
59: data = map;
60: }
61:
62: /**
63: * Generates and persists test data into the specified contexts.
64: * <p>
65: * This method distributes the data approximately uniformly into all the
66: * specified contexts.
67: *
68: * @param em EntityManager
69: * @param contexts A collection of target contexts
70: */
71: public static void generateTestData(EntityManager em, Collection<URI> contexts) {
72: assert em != null;
73: assert contexts != null && !contexts.isEmpty();
74: final Map<Class<?>, List<?>> map = generate();
75: LOG.debug("Persisting test data...");
76: final int contextCount = contexts.size();
77: final Map<URI, Map<Class<?>, List<?>>> contextMap = new HashMap<>();
78: for (Entry<Class<?>, List<?>> e : map.entrySet()) {
79: final List<?> dataLst = e.getValue();
80: final int sublistSize = dataLst.size() / contextCount;
81: int sublistStart = 0;
82: for (URI ctx : contexts) {
83: if (!contextMap.containsKey(ctx)) {
84: contextMap.put(ctx, new HashMap<>());
85: }
86: final List<?> sublist = dataLst.subList(sublistStart, sublistStart + sublistSize);
87: contextMap.get(ctx).put(e.getKey(), sublist);
88: sublistStart += sublistSize;
89: }
90: }
91: contextMap.forEach((ctx, lst) -> persistIntoContext(em, lst, ctx));
92: data = map;
93: }
94:
95: private static void persistIntoContext(EntityManager em, Map<Class<?>, List<?>> data, URI context) {
96: final EntityDescriptor desc = new EntityDescriptor(context);
97: try {
98: em.getTransaction().begin();
99: for (List<?> l : data.values()) {
100: for (Object o : l) {
101: em.persist(o, desc);
102: }
103: }
104: em.getTransaction().commit();
105: if (context == null) {
106: context = NULL_CONTEXT;
107: }
108: dataByContext.put(context, data);
109: } catch (RuntimeException e) {
110: if (em.getTransaction().isActive()) {
111: em.getTransaction().rollback();
112: }
113: throw e;
114: }
115: }
116:
117: /**
118: * Get all current test data.
119: *
120: * @return Map of test data
121: */
122: public static Map<Class<?>, List<?>> getData() {
123: return data;
124: }
125:
126: /**
127: * Get a list of test instances of the specified class.
128: *
129: * @param cls The class
130: * @return List of test data of the specified class
131: */
132: @SuppressWarnings("unchecked")
133: public static <T> List<T> getData(Class<T> cls) {
134: assert cls != null;
135: return (List<T>) data.get(cls);
136: }
137:
138: /**
139: * Gets data from the specified context and of the specified type
140: *
141: * @param context Context URI, null is permitted
142: * @param cls Data type
143: * @return List of data or an empty list
144: */
145: @SuppressWarnings("unchecked")
146: public static <T> List<T> getDataByContext(URI context, Class<T> cls) {
147: assert cls != null;
148: if (context == null) {
149: context = NULL_CONTEXT;
150: }
151: if (!dataByContext.containsKey(context)) {
152: return Collections.emptyList();
153: }
154: final Map<Class<?>, List<?>> contextData = dataByContext.get(context);
155: if (!contextData.containsKey(cls)) {
156: return Collections.emptyList();
157: }
158: return (List<T>) contextData.get(cls);
159: }
160:
161: private static Map<Class<?>, List<?>> generate() {
162: LOG.debug("Generating test data...");
163: final Map<Class<?>, List<?>> m = new HashMap<>();
164: final List<OWLClassA> aa = generateOwlClassAInstances();
165: m.put(OWLClassA.class, aa);
166: m.put(OWLClassB.class, generateOwlClassBInstances());
167: m.put(OWLClassD.class, generateOwlClassDInstances(aa));
168: m.put(OWLClassE.class, generateOwlClassEInstances());
169: m.put(OWLClassJ.class, generateOwlClassJInstances(aa));
170: m.put(OWLClassM.class, generateOwlClassMInstances());
171: m.put(OWLClassT.class, generateOwlClassTInstances(aa));
172: return m;
173: }
174:
175: private static List<OWLClassA> generateOwlClassAInstances() {
176: final List<OWLClassA> lst = new ArrayList<>(ITEM_COUNT);
177: int randomNum = Generators.randomInt(1000);
178: for (int i = 0; i < ITEM_COUNT; i++) {
179: final OWLClassA a = new OWLClassA();
180: a.setUri(URI.create(BASE_A + randomNum));
181: a.setStringAttribute("stringAttribute" + randomNum);
182: final Set<String> s = new HashSet<>();
183: s.add(TYPE_A);
184: a.setTypes(s);
185: lst.add(a);
186: randomNum++;
187: }
188: return lst;
189: }
190:
191: private static List<OWLClassB> generateOwlClassBInstances() {
192: final List<OWLClassB> lst = new ArrayList<>(ITEM_COUNT);
193: int randomNum = Generators.randomInt(1000);
194: for (int i = 0; i < ITEM_COUNT; i++) {
195: final OWLClassB b = new OWLClassB();
196: b.setUri(URI.create(BASE_B + randomNum));
197: b.setStringAttribute("strAtt" + randomNum);
198: lst.add(b);
199: randomNum++;
200: }
201: return lst;
202: }
203:
204: private static List<OWLClassD> generateOwlClassDInstances(List<OWLClassA> aList) {
205: final List<OWLClassD> lst = new ArrayList<>(ITEM_COUNT);
206: int randomNum = Generators.randomInt(1000);
207: for (int i = 0; i < ITEM_COUNT; i++) {
208: final OWLClassD d = new OWLClassD();
209: d.setUri(URI.create(BASE_D + randomNum));
210: d.setOwlClassA(aList.get(i));
211: lst.add(d);
212: randomNum++;
213: }
214: return lst;
215: }
216:
217: private static List<OWLClassE> generateOwlClassEInstances() {
218: final List<OWLClassE> lst = new ArrayList<>(ITEM_COUNT);
219: for (int i = 0; i < ITEM_COUNT; i++) {
220: final OWLClassE e = new OWLClassE();
221: // Auto-generated id
222: e.setStringAttribute("eStr" + i);
223: lst.add(e);
224: }
225: return lst;
226: }
227:
228: private static List<OWLClassT> generateOwlClassTInstances(List<OWLClassA> aList) {
229: final List<OWLClassT> lst = new ArrayList<>(ITEM_COUNT);
230: for (int i = 0; i < ITEM_COUNT; i++) {
231: final OWLClassT t = new OWLClassT();
232: t.setIntAttribute(i);
233: t.setName("tInstance " + i);
234: t.setDescription("Description of tInstance" + i);
235: t.setOwlClassA(aList.get(Generators.randomInt(aList.size())));
236: lst.add(t);
237: }
238: return lst;
239: }
240:
241: private static List<OWLClassJ> generateOwlClassJInstances(List<OWLClassA> aList) {
242: final List<OWLClassJ> lst = new ArrayList<>(ITEM_COUNT);
243: for (int i = 0; i < ITEM_COUNT; i++) {
244: final OWLClassJ inst = new OWLClassJ(Generators.generateUri());
245: inst.setOwlClassA(new HashSet<>());
246: for (int j = 0; j < 3; j++) {
247: inst.getOwlClassA().add(Generators.getRandomItem(aList));
248: }
249: lst.add(inst);
250: }
251: return lst;
252: }
253:
254: private static List<OWLClassM> generateOwlClassMInstances() {
255: final List<OWLClassM> lst = new ArrayList<>();
256: for (int i = 0; i < ITEM_COUNT; i++) {
257: final OWLClassM m = new OWLClassM();
258: m.setKey(Vocabulary.C_OWL_CLASS_M + Generators.randomPositiveInt(0, Integer.MAX_VALUE));
259: m.setBooleanAttribute(Generators.randomBoolean());
260: m.setDoubleAttribute(Generators.getRandomGenerator().nextDouble() * 100);
261: m.setFloatAttribute(Generators.getRandomGenerator().nextFloat() * 100);
262: m.setIntAttribute(Generators.randomInt());
263: m.setLongAttribute((long) Generators.randomInt());
264: m.setEnumAttribute(Generators.getRandomItem(Arrays.asList(OWLClassM.Severity.values())));
265: m.setEnumSimpleLiteral(m.getEnumAttribute());
266: m.setOrdinalEnumAttribute(m.getEnumAttribute());
267: lst.add(m);
268: }
269: return lst;
270: }
271: }