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