Skip to content

Method: generateInstances(Collection, String, int)

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.environment;
16:
17: import cz.cvut.kbss.jopa.test.OWLClassA;
18:
19: import java.net.MalformedURLException;
20: import java.net.URI;
21: import java.net.URL;
22: import java.util.*;
23: import java.util.stream.Collectors;
24:
25: /**
26: * Generators of test data.
27: */
28: public abstract class Generators {
29:
30: private static final int DEFAULT_SIZE = 10;
31: private static final Set<String> TYPES = getTypes();
32:
33: private static final String PROPERTY_URI_BASE = "http://krizik.felk.cvut.cz/ontologies/jopa/attributes#property";
34: private static final String TYPE_URI_BASE = "http://krizik.felk.cvut.cz/ontologies/jopa/entities#Entity";
35:
36: private static final Random RANDOM = new Random();
37:
38: private Generators() {
39: // Private constructor
40: }
41:
42: public static List<OWLClassA> createSimpleList() {
43: return createSimpleList(randomPositiveInt(DEFAULT_SIZE));
44: }
45:
46: public static List<OWLClassA> createSimpleList(int size) {
47: assert size > 0;
48: final List<OWLClassA> lst = new ArrayList<>(size);
49: generateInstances(lst, "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityASimple",
50: size);
51: return lst;
52: }
53:
54: public static List<OWLClassA> createReferencedList() {
55: return createReferencedList(randomPositiveInt(DEFAULT_SIZE));
56: }
57:
58: public static List<OWLClassA> createReferencedList(int size) {
59: assert size > 0;
60: final List<OWLClassA> lst = new ArrayList<>(size);
61: generateInstances(lst,
62: "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityAReferenced", size);
63: return lst;
64: }
65:
66: public static List<URI> createListOfIdentifiers() {
67: return createSimpleList().stream().map(OWLClassA::getUri).collect(Collectors.toList());
68: }
69:
70: public static Set<OWLClassA> createSimpleSet() {
71: return createSimpleSet(randomPositiveInt(DEFAULT_SIZE));
72: }
73:
74: public static Set<OWLClassA> createSimpleSet(int size) {
75: assert size > 0;
76: final Set<OWLClassA> set = new HashSet<>(size);
77: generateInstances(set, "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityASimpleSet",
78: size);
79: return set;
80: }
81:
82: public static Map<String, Set<String>> createProperties() {
83: return createProperties(randomPositiveInt(DEFAULT_SIZE));
84: }
85:
86: public static Map<String, Set<String>> createProperties(int size) {
87: assert size > 0;
88: final Map<String, Set<String>> m = new HashMap<>(size);
89: int counter = randomInt(1000);
90: for (int i = 0; i < size; i++) {
91: final Set<String> value = new HashSet<>(4);
92: for (int j = 0; j < size; j++) {
93: value.add("http://krizik.felk.cvut.cz/ontologies/jopa/tests/ObjectPropertyValue_" + j + "_"
94: + counter);
95: }
96: m.put(PROPERTY_URI_BASE + counter, value);
97: counter++;
98:
99: }
100: return m;
101: }
102:
103: public static Map<URI, Set<Object>> createTypedProperties() {
104: return createTypedProperties(randomPositiveInt(DEFAULT_SIZE));
105: }
106:
107: public static Map<URI, Set<Object>> createTypedProperties(int size) {
108: assert size > 0;
109: final Map<URI, Set<Object>> props = new HashMap<>(size);
110: int counter = randomInt(1000);
111: for (int i = 0; i < size; i++) {
112: final Set<Object> value = new HashSet<>();
113: for (int j = 0; j < size; j++) {
114: // Generate either an individual's URI or random data value. But same type for a property
115: // (so that the property is either object or data, but not both)
116: if (counter % 2 == 0) {
117: value.add(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/Property_" + counter +
118: "Individual_" + j));
119: } else {
120: value.add(generateRandomPropertyValue(j, counter));
121: }
122: }
123: props.put(URI.create(PROPERTY_URI_BASE + counter), value);
124: counter++;
125: }
126: return props;
127: }
128:
129: private static Object generateRandomPropertyValue(int valueIndex, int propertyIndex) {
130: final int random = randomInt(6);
131: switch (random) {
132: case 0: // boolean
133: return valueIndex % 2 == 0;
134: case 1: // int
135: return valueIndex;
136: case 2: // long
137: return System.currentTimeMillis();
138: case 3: //double
139: return ((double) propertyIndex + 1) / (valueIndex + 1);
140: case 4: // date
141: return new Date();
142: case 5: // String
143: return "TypedProperty_" + propertyIndex + "Value_" + valueIndex;
144: default:
145: throw new IllegalArgumentException();
146: }
147: }
148:
149: private static void generateInstances(Collection<OWLClassA> col, String uriBase, int size) {
150:• assert size > 0;
151: int counter = randomInt(1000);
152:• for (int i = 0; i < size; i++) {
153: final OWLClassA a = new OWLClassA();
154: a.setUri(URI.create(uriBase + counter));
155: a.setStringAttribute("stringAttributeeee" + counter);
156: counter++;
157: a.setTypes(TYPES);
158: col.add(a);
159: }
160: }
161:
162: private static Set<String> getTypes() {
163: final Set<String> types = new HashSet<>(3);
164: types.add("http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassA");
165: types.add("http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassX");
166: types.add("http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassZ");
167: return types;
168: }
169:
170: public static Set<URL> createUrls() {
171: return Generators.createSimpleList().stream().map(a -> {
172: try {
173: return a.getUri().toURL();
174: } catch (MalformedURLException e) {
175: throw new IllegalArgumentException(e);
176: }
177: }).collect(Collectors.toSet());
178: }
179:
180: public static int randomInt() {
181: return RANDOM.nextInt();
182: }
183:
184: public static int randomInt(int max) {
185: return RANDOM.nextInt(max);
186: }
187:
188: /**
189: * Gets a random int greater than <b>2</b>.
190: *
191: * @param max upper bound (exclusive). Has to be greater than 2.
192: * @return Random positive integer
193: */
194: public static int randomPositiveInt(int max) {
195: if (max <= 2) {
196: throw new IllegalArgumentException("Upper bound has to be greater than 2.");
197: }
198: int rand;
199: do {
200: rand = RANDOM.nextInt(max);
201: } while (rand < 2);
202: return rand;
203: }
204:
205: public static boolean randomBoolean() {
206: return RANDOM.nextBoolean();
207: }
208:
209: public static Set<URI> createUriTypes() {
210: final int count = randomPositiveInt(DEFAULT_SIZE);
211: final Set<URI> result = new HashSet<>();
212: for (int i = 0; i < count; i++) {
213: result.add(URI.create(TYPE_URI_BASE + randomInt(Integer.MAX_VALUE)));
214: }
215: return result;
216: }
217:
218: /**
219: * Generates a random URI.
220: *
221: * @return Random URI
222: */
223: public static URI generateUri() {
224: return URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/individuals#instance" +
225: randomInt(Integer.MAX_VALUE));
226: }
227:
228: /**
229: * Gets random item from the specified list.
230: * @param items List of items
231: * @return Random item from the list
232: */
233: public static <T> T getRandomItem(List<T> items) {
234: return items.get(randomInt(items.size()));
235: }
236: }