Skip to content

Method: generate()

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