-
Notifications
You must be signed in to change notification settings - Fork 14
/
Generators.java
221 lines (193 loc) · 7.76 KB
/
Generators.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* Copyright (C) 2016 Czech Technical University in Prague
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.test.environment;
import cz.cvut.kbss.jopa.test.OWLClassA;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;
/**
* Generators of test data.
*/
public abstract class Generators {
private static final int DEFAULT_SIZE = 10;
private static final Set<String> TYPES = getTypes();
private static final String PROPERTY_URI_BASE = "http://krizik.felk.cvut.cz/ontologies/jopa/attributes#property";
private static final String TYPE_URI_BASE = "http://krizik.felk.cvut.cz/ontologies/jopa/entities#Entity";
private static final Random RANDOM = new Random();
private Generators() {
// Private constructor
}
public static List<OWLClassA> createSimpleList() {
return createSimpleList(randomPositiveInt(DEFAULT_SIZE));
}
public static List<OWLClassA> createSimpleList(int size) {
assert size > 0;
final List<OWLClassA> lst = new ArrayList<>(size);
generateInstances(lst, "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityASimple",
size);
return lst;
}
public static List<OWLClassA> createReferencedList() {
return createReferencedList(randomPositiveInt(DEFAULT_SIZE));
}
public static List<OWLClassA> createReferencedList(int size) {
assert size > 0;
final List<OWLClassA> lst = new ArrayList<>(size);
generateInstances(lst,
"http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityAReferenced", size);
return lst;
}
public static List<URI> createListOfIdentifiers() {
return createSimpleList().stream().map(OWLClassA::getUri).collect(Collectors.toList());
}
public static Set<OWLClassA> createSimpleSet() {
return createSimpleSet(randomPositiveInt(DEFAULT_SIZE));
}
public static Set<OWLClassA> createSimpleSet(int size) {
assert size > 0;
final Set<OWLClassA> set = new HashSet<>(size);
generateInstances(set, "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityASimpleSet",
size);
return set;
}
public static Map<String, Set<String>> createProperties() {
return createProperties(randomPositiveInt(DEFAULT_SIZE));
}
public static Map<String, Set<String>> createProperties(int size) {
assert size > 0;
final Map<String, Set<String>> m = new HashMap<>(size);
int counter = randomInt(1000);
for (int i = 0; i < size; i++) {
final Set<String> value = new HashSet<>(4);
for (int j = 0; j < size; j++) {
value.add("http://krizik.felk.cvut.cz/ontologies/jopa/tests/ObjectPropertyValue_" + j + "_"
+ counter);
}
m.put(PROPERTY_URI_BASE + counter, value);
counter++;
}
return m;
}
public static Map<URI, Set<Object>> createTypedProperties() {
return createTypedProperties(randomPositiveInt(DEFAULT_SIZE));
}
public static Map<URI, Set<Object>> createTypedProperties(int size) {
assert size > 0;
final Map<URI, Set<Object>> props = new HashMap<>(size);
int counter = randomInt(1000);
for (int i = 0; i < size; i++) {
final Set<Object> value = new HashSet<>();
for (int j = 0; j < size; j++) {
// Generate either an individual's URI or random data value. But same type for a property
// (so that the property is either object or data, but not both)
if (counter % 2 == 0) {
value.add(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/Property_" + counter +
"Individual_" + j));
} else {
value.add(generateRandomPropertyValue(j, counter));
}
}
props.put(URI.create(PROPERTY_URI_BASE + counter), value);
counter++;
}
return props;
}
private static Object generateRandomPropertyValue(int valueIndex, int propertyIndex) {
final int random = randomInt(6);
switch (random) {
case 0: // boolean
return valueIndex % 2 == 0;
case 1: // int
return valueIndex;
case 2: // long
return System.currentTimeMillis();
case 3: //double
return ((double) propertyIndex + 1) / (valueIndex + 1);
case 4: // date
return new Date();
case 5: // String
return "TypedProperty_" + propertyIndex + "Value_" + valueIndex;
default:
throw new IllegalArgumentException();
}
}
private static void generateInstances(Collection<OWLClassA> col, String uriBase, int size) {
assert size > 0;
int counter = randomInt(1000);
for (int i = 0; i < size; i++) {
final OWLClassA a = new OWLClassA();
a.setUri(URI.create(uriBase + counter));
a.setStringAttribute("stringAttributeeee" + counter);
counter++;
a.setTypes(TYPES);
col.add(a);
}
}
private static Set<String> getTypes() {
final Set<String> types = new HashSet<>(3);
types.add("http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassA");
types.add("http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassX");
types.add("http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassZ");
return types;
}
public static Set<URL> createUrls() {
return Generators.createSimpleList().stream().map(a -> {
try {
return a.getUri().toURL();
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
}
}).collect(Collectors.toSet());
}
public static int randomInt(int max) {
return RANDOM.nextInt(max);
}
/**
* Gets a random int greater than <b>2</b>.
*
* @param max upper bound (exclusive). Has to be greater than 2.
* @return Random positive integer
*/
public static int randomPositiveInt(int max) {
if (max <= 2) {
throw new IllegalArgumentException("Upper bound has to be greater than 2.");
}
int rand;
do {
rand = RANDOM.nextInt(max);
} while (rand < 2);
return rand;
}
public static boolean randomBoolean() {
return RANDOM.nextBoolean();
}
public static Set<URI> createUriTypes() {
final int count = randomPositiveInt(DEFAULT_SIZE);
final Set<URI> result = new HashSet<>();
for (int i = 0; i < count; i++) {
result.add(URI.create(TYPE_URI_BASE + randomInt(Integer.MAX_VALUE)));
}
return result;
}
/**
* Generates a random URI.
*
* @return Random URI
*/
public static URI generateUri() {
return URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/individuals#instance" +
randomInt(Integer.MAX_VALUE));
}
}