-
Notifications
You must be signed in to change notification settings - Fork 14
/
Generators.java
167 lines (146 loc) · 5.78 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
/**
* 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.environment.utils;
import cz.cvut.kbss.jopa.environment.OWLClassA;
import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;
/**
* Generators of test data.
*
* @author ledvima1
*/
public abstract class Generators {
public static final int DEFAULT_SIZE = 5;
private static final Set<String> TYPES = generateTypes(DEFAULT_SIZE);
private static final Random RAND = new Random();
private Generators() {
// Private constructor
}
public static int randomInt(int max) {
return RAND.nextInt(max);
}
/**
* Gets a random integer greater than one.
*
* @param max Upper bound
* @return Random integer
*/
public static int randomPositiveInt(int max) {
assert max > 1;
int res;
do {
res = RAND.nextInt(max);
} while (res <= 1);
return res;
}
public static boolean randomBoolean() {
return RAND.nextBoolean();
}
public static URI createIndividualIdentifier() {
return URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/individuals#instance-" +
randomInt(Integer.MAX_VALUE));
}
public static URI createPropertyIdentifier() {
return URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#property-" +
randomInt(Integer.MAX_VALUE));
}
public static List<OWLClassA> createReferencedList(int size) {
assert size > 0;
return generateInstances("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityAReferenced", size);
}
public static Map<String, Set<String>> generateStringProperties() {
return generateStringProperties(DEFAULT_SIZE, DEFAULT_SIZE);
}
public static Map<String, Set<String>> generateStringProperties(int propCount, int valCount) {
int valueCounter = 0;
final Map<String, Set<String>> properties = new HashMap<>(propCount);
for (int i = 0; i < propCount; i++) {
final Set<String> values = new HashSet<>(valCount);
properties.put("http://krizik.felk.cvut.cz/ontologies/jopa#property_" + i, values);
boolean objectProperty = i % 2 != 0;
for (int j = 0; j < valCount; j++) {
if (objectProperty) {
values.add("http://krizik.felk.cvut.cz/ontologies/jopa#value_" + valueCounter++);
} else {
values.add(j % 2 != 0 ? "value" + valueCounter++ : Integer.toString(valueCounter++));
}
}
}
return properties;
}
public static Map<URI, Set<Object>> generateTypedProperties() {
return generateTypedProperties(DEFAULT_SIZE, DEFAULT_SIZE);
}
public static Map<URI, Set<Object>> generateTypedProperties(int propCount, int valCount) {
int valueCounter = 0;
final Map<URI, Set<Object>> properties = new HashMap<>(propCount);
for (int i = 0; i < propCount; i++) {
final Set<Object> values = new HashSet<>(valCount);
properties.put(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#property_" + i), values);
boolean objectProperty = i % 2 != 0;
for (int j = 0; j < valCount; j++) {
if (objectProperty) {
values.add(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#value_" + valueCounter++));
} else {
values.add(generateDataPropertyValue(j));
}
}
}
return properties;
}
private static Object generateDataPropertyValue(int counter) {
final int index = counter % 5;
switch (index) {
case 0:
return "StringValue_" + counter;
case 1:
return true;
case 2:
return counter;
case 3:
return (double) counter;
case 4:
return new Date();
default:
return null;
}
}
public static List<OWLClassA> generateInstances(int size) {
return generateInstances("http://krizik.felk.cvut.cz/ontologies/jopa/tests/instance_", size);
}
public static List<OWLClassA> generateInstances(String uriBase, int size) {
assert size > 0;
int counter = randomInt(1000);
final List<OWLClassA> lst = new ArrayList<>(size);
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);
lst.add(a);
}
return lst;
}
public static Set<String> generateTypes(int count) {
final Set<String> types = new HashSet<>(count);
for (int i = 0; i < count; i++) {
types.add("http://krizik.felk.cvut.cz/ontologies/jopa#type_" + i);
}
return types;
}
public static Set<URI> generateUriTypes(int count) {
return generateTypes(count).stream().map(URI::create).collect(Collectors.toSet());
}
}