Skip to contentMethod: createDataPropertyList()
1: /*
2: * JOPA
3: * Copyright (C) 2023 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.test.environment;
19:
20: import cz.cvut.kbss.jopa.test.OWLClassA;
21: import cz.cvut.kbss.jopa.test.Vocabulary;
22:
23: import java.math.BigDecimal;
24: import java.math.BigInteger;
25: import java.net.MalformedURLException;
26: import java.net.URI;
27: import java.net.URL;
28: import java.time.LocalDate;
29: import java.time.OffsetDateTime;
30: import java.time.OffsetTime;
31: import java.time.temporal.ChronoUnit;
32: import java.util.*;
33: import java.util.stream.Collectors;
34:
35: /**
36: * Generators of test data.
37: */
38: public abstract class Generators {
39:
40: private static final int DEFAULT_MIN = 5;
41: private static final int DEFAULT_SIZE = 10;
42: private static final Set<String> TYPES = getTypes();
43:
44: private static final String PROPERTY_URI_BASE = "http://krizik.felk.cvut.cz/ontologies/jopa/attributes#property";
45: private static final String TYPE_URI_BASE = "http://krizik.felk.cvut.cz/ontologies/jopa/entities#Entity";
46:
47: private static final Random RANDOM = new Random();
48:
49: private Generators() {
50: // Private constructor
51: }
52:
53: public static List<OWLClassA> createSimpleList() {
54: return createSimpleList(randomPositiveInt(DEFAULT_MIN, DEFAULT_SIZE));
55: }
56:
57: public static List<OWLClassA> createSimpleList(int size) {
58: assert size > 0;
59: final List<OWLClassA> lst = new ArrayList<>(size);
60: generateInstances(lst, "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityASimple",
61: size);
62: return lst;
63: }
64:
65: public static List<OWLClassA> createReferencedList() {
66: return createReferencedList(randomPositiveInt(DEFAULT_MIN, DEFAULT_SIZE));
67: }
68:
69: public static List<OWLClassA> createReferencedList(int size) {
70: assert size > 0;
71: final List<OWLClassA> lst = new ArrayList<>(size);
72: generateInstances(lst,
73: "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityAReferenced", size);
74: return lst;
75: }
76:
77: public static List<LocalDate> createDataPropertyList() {
78: final List<LocalDate> list = new ArrayList<>(DEFAULT_SIZE);
79:• for (int i = DEFAULT_SIZE; i >= 0; i--) {
80: list.add(LocalDate.now().minusDays(i));
81: }
82: return list;
83: }
84:
85: public static List<URI> createListOfIdentifiers() {
86: return createSimpleList().stream().map(OWLClassA::getUri).collect(Collectors.toList());
87: }
88:
89: public static Set<OWLClassA> createSimpleSet() {
90: return createSimpleSet(randomPositiveInt(DEFAULT_MIN, DEFAULT_SIZE));
91: }
92:
93: public static Set<OWLClassA> createSimpleSet(int size) {
94: assert size > 0;
95: final Set<OWLClassA> set = new HashSet<>(size);
96: generateInstances(set, "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityASimpleSet",
97: size);
98: return set;
99: }
100:
101: public static Map<String, Set<String>> createProperties() {
102: return createProperties(randomPositiveInt(DEFAULT_MIN, DEFAULT_SIZE));
103: }
104:
105: public static Map<String, Set<String>> createProperties(int size) {
106: assert size > 0;
107: final Map<String, Set<String>> m = new HashMap<>(size);
108: int counter = randomInt(1000);
109: for (int i = 0; i < size; i++) {
110: final Set<String> value = new HashSet<>(4);
111: for (int j = 0; j < size; j++) {
112: value.add("http://krizik.felk.cvut.cz/ontologies/jopa/tests/ObjectPropertyValue_" + j + "_"
113: + counter);
114: }
115: m.put(PROPERTY_URI_BASE + counter, value);
116: counter++;
117:
118: }
119: return m;
120: }
121:
122: public static Map<URI, Set<Object>> createTypedProperties() {
123: return createTypedProperties(randomPositiveInt(DEFAULT_MIN, DEFAULT_SIZE));
124: }
125:
126: public static Map<URI, Set<Object>> createTypedProperties(int size) {
127: assert size > 0;
128: final Map<URI, Set<Object>> props = new HashMap<>(size);
129: int counter = randomInt(1000);
130: for (int i = 0; i < size; i++) {
131: final Set<Object> value = new HashSet<>();
132: for (int j = 0; j < size; j++) {
133: // Generate either an individual's URI or random data value. But same type for a property
134: // (so that the property is either object or data, but not both)
135: if (counter % 2 == 0) {
136: value.add(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/Property_" + counter +
137: "Individual_" + j));
138: } else {
139: value.add(generateRandomPropertyValue(j, counter));
140: }
141: }
142: props.put(URI.create(PROPERTY_URI_BASE + counter), value);
143: counter++;
144: }
145: return props;
146: }
147:
148: private static Object generateRandomPropertyValue(int valueIndex, int propertyIndex) {
149: final int random = randomInt(10);
150: switch (random) {
151: case 0: // boolean
152: return valueIndex % 2 == 0;
153: case 1: // int
154: return valueIndex;
155: case 2: // long
156: return System.currentTimeMillis();
157: case 3: //double
158: return ((double) propertyIndex + 1) / (valueIndex + 1);
159: case 4: // datetime
160: // Generate date rounded to milliseconds to prevent issues with time rounding
161: return OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS);
162: case 5:
163: return OffsetTime.now().truncatedTo(ChronoUnit.MILLIS);
164: case 6:
165: return LocalDate.now();
166: case 7: // String
167: return "TypedProperty_" + propertyIndex + "Value_" + valueIndex;
168: case 8:
169: return BigInteger.valueOf(valueIndex);
170: case 9:
171: return BigDecimal.valueOf(Math.PI);
172: default:
173: throw new IllegalArgumentException();
174: }
175: }
176:
177: private static void generateInstances(Collection<OWLClassA> col, String uriBase, int size) {
178: assert size > 0;
179: int counter = randomInt(1000);
180: for (int i = 0; i < size; i++) {
181: final OWLClassA a = new OWLClassA();
182: a.setUri(URI.create(uriBase + counter));
183: a.setStringAttribute("stringAttributeeee" + counter);
184: counter++;
185: a.setTypes(TYPES);
186: col.add(a);
187: }
188: }
189:
190: private static Set<String> getTypes() {
191: final Set<String> types = new HashSet<>(3);
192: types.add(Vocabulary.CLASS_IRI_BASE + "OWLClassDF");
193: types.add(Vocabulary.CLASS_IRI_BASE + "OWLClassDFF");
194: types.add(Vocabulary.CLASS_IRI_BASE + "OWLClassDFFF");
195: return types;
196: }
197:
198: public static Set<URL> createUrls() {
199: return Generators.createSimpleList().stream().map(a -> {
200: try {
201: return a.getUri().toURL();
202: } catch (MalformedURLException e) {
203: throw new IllegalArgumentException(e);
204: }
205: }).collect(Collectors.toSet());
206: }
207:
208: public static int randomInt() {
209: return RANDOM.nextInt();
210: }
211:
212: public static int randomInt(int max) {
213: return RANDOM.nextInt(max);
214: }
215:
216: /**
217: * Gets a random int between {@code min} and {@code max}.
218: *
219: *
220: * @param min lower bound (inclusive)
221: * @param max upper bound (exclusive)
222: * @return Random positive integer
223: */
224: public static int randomPositiveInt(int min, int max) {
225: assert min >= 0;
226: if (max <= min) {
227: throw new IllegalArgumentException("Upper bound has to be greater than the lower bound.");
228: }
229: int rand;
230: do {
231: rand = RANDOM.nextInt(max);
232: } while (rand < min);
233: return rand;
234: }
235:
236: public static boolean randomBoolean() {
237: return RANDOM.nextBoolean();
238: }
239:
240: public static Set<URI> createUriTypes() {
241: final int count = randomPositiveInt(DEFAULT_MIN, DEFAULT_SIZE);
242: final Set<URI> result = new HashSet<>();
243: for (int i = 0; i < count; i++) {
244: result.add(URI.create(TYPE_URI_BASE + randomInt(Integer.MAX_VALUE)));
245: }
246: return result;
247: }
248:
249: /**
250: * Generates a random URI.
251: *
252: * @return Random URI
253: */
254: public static URI generateUri() {
255: return URI.create(Vocabulary.INDIVIDUAL_IRI_BASE + randomInt(Integer.MAX_VALUE));
256: }
257:
258: /**
259: * Gets random item from the specified list.
260: * @param items List of items
261: * @return Random item from the list
262: */
263: public static <T> T getRandomItem(List<T> items) {
264: return items.get(randomPositiveInt(0, items.size()));
265: }
266:
267: public static Random getRandomGenerator() {
268: return RANDOM;
269: }
270: }