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