Skip to content

Method: createUrls()

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<OWLClassA> createRDFCollection(int size) {
86: assert size > 0;
87: final List<OWLClassA> lst = new ArrayList<>(size);
88: generateInstances(lst,
89: "http://krizik.felk.cvut.cz/ontologies/jopa/tests/rdf-collection-item", size);
90: return lst;
91: }
92:
93: public static List<URI> createListOfIdentifiers() {
94: return createSimpleList().stream().map(OWLClassA::getUri).collect(Collectors.toList());
95: }
96:
97: public static Set<OWLClassA> createSimpleSet() {
98: return createSimpleSet(randomPositiveInt(DEFAULT_MIN, DEFAULT_SIZE));
99: }
100:
101: public static Set<OWLClassA> createSimpleSet(int size) {
102: assert size > 0;
103: final Set<OWLClassA> set = new HashSet<>(size);
104: generateInstances(set, "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityASimpleSet",
105: size);
106: return set;
107: }
108:
109: public static Map<String, Set<String>> createProperties() {
110: return createProperties(randomPositiveInt(DEFAULT_MIN, DEFAULT_SIZE));
111: }
112:
113: public static Map<String, Set<String>> createProperties(int size) {
114: assert size > 0;
115: final Map<String, Set<String>> m = new HashMap<>(size);
116: int counter = randomInt(1000);
117: for (int i = 0; i < size; i++) {
118: final Set<String> value = new HashSet<>(4);
119: for (int j = 0; j < size; j++) {
120: value.add("http://krizik.felk.cvut.cz/ontologies/jopa/tests/ObjectPropertyValue_" + j + "_"
121: + counter);
122: }
123: m.put(PROPERTY_URI_BASE + counter, value);
124: counter++;
125:
126: }
127: return m;
128: }
129:
130: public static Map<URI, Set<Object>> createTypedProperties() {
131: return createTypedProperties(randomPositiveInt(DEFAULT_MIN, DEFAULT_SIZE));
132: }
133:
134: public static Map<URI, Set<Object>> createTypedProperties(int size) {
135: assert size > 0;
136: final Map<URI, Set<Object>> props = new HashMap<>(size);
137: int counter = randomInt(1000);
138: for (int i = 0; i < size; i++) {
139: final Set<Object> value = new HashSet<>();
140: for (int j = 0; j < size; j++) {
141: // Generate either an individual's URI or random data value. But same type for a property
142: // (so that the property is either object or data, but not both)
143: if (counter % 2 == 0) {
144: value.add(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/Property_" + counter +
145: "Individual_" + j));
146: } else {
147: value.add(generateRandomPropertyValue(j, counter));
148: }
149: }
150: props.put(URI.create(PROPERTY_URI_BASE + counter), value);
151: counter++;
152: }
153: return props;
154: }
155:
156: private static Object generateRandomPropertyValue(int valueIndex, int propertyIndex) {
157: final int random = randomInt(10);
158: switch (random) {
159: case 0: // boolean
160: return valueIndex % 2 == 0;
161: case 1: // int
162: return valueIndex;
163: case 2: // long
164: return System.currentTimeMillis();
165: case 3: //double
166: return ((double) propertyIndex + 1) / (valueIndex + 1);
167: case 4: // datetime
168: // Generate date rounded to milliseconds to prevent issues with time rounding
169: return OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS);
170: case 5:
171: return OffsetTime.now().truncatedTo(ChronoUnit.MILLIS);
172: case 6:
173: return LocalDate.now();
174: case 7: // String
175: return "TypedProperty_" + propertyIndex + "Value_" + valueIndex;
176: case 8:
177: return BigInteger.valueOf(valueIndex);
178: case 9:
179: return BigDecimal.valueOf(Math.PI);
180: default:
181: throw new IllegalArgumentException();
182: }
183: }
184:
185: private static void generateInstances(Collection<OWLClassA> col, String uriBase, int size) {
186: assert size > 0;
187: int counter = randomInt(1000);
188: for (int i = 0; i < size; i++) {
189: final OWLClassA a = new OWLClassA();
190: a.setUri(URI.create(uriBase + counter));
191: a.setStringAttribute("stringAttributeeee" + counter);
192: counter++;
193: a.setTypes(TYPES);
194: col.add(a);
195: }
196: }
197:
198: private static Set<String> getTypes() {
199: final Set<String> types = new HashSet<>(3);
200: types.add(Vocabulary.CLASS_IRI_BASE + "OWLClassDF");
201: types.add(Vocabulary.CLASS_IRI_BASE + "OWLClassDFF");
202: types.add(Vocabulary.CLASS_IRI_BASE + "OWLClassDFFF");
203: return types;
204: }
205:
206: public static Set<URL> createUrls() {
207: return Generators.createSimpleList().stream().map(a -> {
208: try {
209: return a.getUri().toURL();
210: } catch (MalformedURLException e) {
211: throw new IllegalArgumentException(e);
212: }
213: }).collect(Collectors.toSet());
214: }
215:
216: public static int randomInt() {
217: return RANDOM.nextInt();
218: }
219:
220: public static int randomInt(int max) {
221: return RANDOM.nextInt(max);
222: }
223:
224: /**
225: * Gets a random int between {@code min} and {@code max}.
226: *
227: *
228: * @param min lower bound (inclusive)
229: * @param max upper bound (exclusive)
230: * @return Random positive integer
231: */
232: public static int randomPositiveInt(int min, int max) {
233: assert min >= 0;
234: if (max <= min) {
235: throw new IllegalArgumentException("Upper bound has to be greater than the lower bound.");
236: }
237: int rand;
238: do {
239: rand = RANDOM.nextInt(max);
240: } while (rand < min);
241: return rand;
242: }
243:
244: public static boolean randomBoolean() {
245: return RANDOM.nextBoolean();
246: }
247:
248: public static Set<URI> createUriTypes() {
249: final int count = randomPositiveInt(DEFAULT_MIN, DEFAULT_SIZE);
250: final Set<URI> result = new HashSet<>();
251: for (int i = 0; i < count; i++) {
252: result.add(URI.create(TYPE_URI_BASE + randomInt(Integer.MAX_VALUE)));
253: }
254: return result;
255: }
256:
257: /**
258: * Generates a random URI.
259: *
260: * @return Random URI
261: */
262: public static URI generateUri() {
263: return URI.create(Vocabulary.INDIVIDUAL_IRI_BASE + randomInt(Integer.MAX_VALUE));
264: }
265:
266: /**
267: * Gets random item from the specified list.
268: * @param items List of items
269: * @return Random item from the list
270: */
271: public static <T> T getRandomItem(List<T> items) {
272: return items.get(randomPositiveInt(0, items.size()));
273: }
274:
275: public static Random getRandomGenerator() {
276: return RANDOM;
277: }
278: }