-
Notifications
You must be signed in to change notification settings - Fork 14
/
Generators.java
172 lines (151 loc) · 6.24 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
/**
* 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.
*
* @author ledvima1
*/
public abstract class Generators {
private static final int DEFAULT_SIZE = 5;
private static final Set<String> TYPES = getTypes();
private static final String PROPERTY_URI_BASE = "http://krizik.felk.cvut.cz/ontologies/jopa/attributes#property";
private Generators() {
// Private constructor
}
public static List<OWLClassA> createSimpleList() {
return createSimpleList(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(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 Set<OWLClassA> createSimpleSet() {
return createSimpleSet(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(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 = TestEnvironmentUtils.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(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 = TestEnvironmentUtils.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 = TestEnvironmentUtils.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 = TestEnvironmentUtils.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());
}
}