Skip to content

Package: Generators

Generators

nameinstructionbranchcomplexitylinemethod
createListOfIdentifiers()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createProperties()
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createProperties(int)
M: 4 C: 58
94%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 0 C: 11
100%
M: 0 C: 1
100%
createReferencedList()
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createReferencedList(int)
M: 4 C: 14
78%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 5
100%
M: 0 C: 1
100%
createSimpleList()
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createSimpleList(int)
M: 4 C: 14
78%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 5
100%
M: 0 C: 1
100%
createSimpleSet()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
createSimpleSet(int)
M: 4 C: 14
78%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 5
100%
M: 0 C: 1
100%
createTypedProperties()
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createTypedProperties(int)
M: 4 C: 70
95%
M: 1 C: 7
88%
M: 1 C: 4
80%
M: 0 C: 14
100%
M: 0 C: 1
100%
createUriTypes()
M: 0 C: 29
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
createUrls()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
generateInstances(Collection, String, int)
M: 4 C: 46
92%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 10
100%
M: 0 C: 1
100%
generateRandomPropertyValue(int, int)
M: 4 C: 53
93%
M: 1 C: 8
89%
M: 1 C: 7
88%
M: 1 C: 8
89%
M: 0 C: 1
100%
generateUri()
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getRandomItem(List)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getTypes()
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
lambda$1(OWLClassA)
M: 6 C: 4
40%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
randomBoolean()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
randomInt()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
randomInt(int)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
randomPositiveInt(int, int)
M: 9 C: 15
63%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 1 C: 6
86%
M: 0 C: 1
100%
static {...}
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

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