Skip to content

Package: SelectResultSet

SelectResultSet

nameinstructionbranchcomplexitylinemethod
SelectResultSet(QueryResult, Statement)
M: 0 C: 32
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
ensureState()
M: 0 C: 11
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
ensureValueIsAssignableToClass(Object, Class)
M: 0 C: 23
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
ensureValueIsLiteral(OWLObject)
M: 0 C: 18
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
findColumn(String)
M: 2 C: 36
95%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 1 C: 8
89%
M: 0 C: 1
100%
first()
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
getBoolean(String)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getBoolean(int)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getByte(String)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getByte(int)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getColumnCount()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getCurrentValue(String)
M: 0 C: 51
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
getCurrentValue(int)
M: 0 C: 53
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
getDouble(String)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getDouble(int)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getFloat(String)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getFloat(int)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getInt(String)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getInt(int)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getLiteral(String)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getLiteral(int)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getLong(String)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getLong(int)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getObject(String)
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%
getObject(String, Class)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getObject(int)
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%
getObject(int, Class)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getPrimitiveValue(Class, String)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getPrimitiveValue(Class, int)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getRowIndex()
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%
getShort(String)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getShort(int)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getString(String)
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%
getString(int)
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%
hasNext()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
isBound(String)
M: 0 C: 19
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 2
100%
M: 0 C: 1
100%
isBound(int)
M: 0 C: 21
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 3
100%
M: 0 C: 1
100%
isFirst()
M: 0 C: 9
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
last()
M: 0 C: 9
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
next()
M: 0 C: 23
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
owlObjectToObject(OWLObject)
M: 5 C: 25
83%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 2 C: 6
75%
M: 0 C: 1
100%
owlObjectToType(OWLObject, Class)
M: 18 C: 40
69%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 2 C: 8
80%
M: 0 C: 1
100%
owlValueToString(OWLObject)
M: 3 C: 22
88%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 5
83%
M: 0 C: 1
100%
previous()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
relative(int)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
resolveVariableNamesAndIndexes()
M: 0 C: 42
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
setRowIndex(int)
M: 0 C: 31
100%
M: 0 C: 10
100%
M: 0 C: 6
100%
M: 0 C: 10
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2022 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.ontodriver.owlapi.query;
16:
17: import cz.cvut.kbss.jopa.datatype.DatatypeTransformer;
18: import cz.cvut.kbss.ontodriver.Statement;
19: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
20: import cz.cvut.kbss.ontodriver.exception.VariableNotBoundException;
21: import cz.cvut.kbss.ontodriver.owlapi.exception.BindingValueMismatchException;
22: import cz.cvut.kbss.ontodriver.owlapi.exception.OwlapiDriverException;
23: import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
24: import cz.cvut.kbss.owl2query.model.GroundTerm;
25: import cz.cvut.kbss.owl2query.model.QueryResult;
26: import cz.cvut.kbss.owl2query.model.ResultBinding;
27: import cz.cvut.kbss.owl2query.model.Variable;
28: import org.semanticweb.owlapi.model.OWLEntity;
29: import org.semanticweb.owlapi.model.OWLLiteral;
30: import org.semanticweb.owlapi.model.OWLObject;
31:
32: import java.net.URI;
33: import java.util.*;
34: import java.util.stream.Collectors;
35:
36: import static cz.cvut.kbss.ontodriver.util.ErrorUtils.getNPXMessageSupplier;
37:
38: class SelectResultSet extends AbstractResultSet {
39:
40: private final QueryResult<OWLObject> queryResult;
41: private Iterator<ResultBinding<OWLObject>> iterator;
42:
43: private final Map<String, Variable<OWLObject>> namesToVariables;
44: private final Map<Integer, Variable<OWLObject>> indexesToVariables;
45:
46: private int currentIndex;
47: private ResultBinding<OWLObject> currentRow;
48:
49: public SelectResultSet(QueryResult<OWLObject> queryResult, Statement statement) {
50: super(statement);
51: this.queryResult = queryResult;
52: this.iterator = queryResult.iterator();
53: this.currentIndex = -1;
54: final int bindingSize = queryResult.getResultVars().size();
55: this.namesToVariables = new HashMap<>(bindingSize);
56: this.indexesToVariables = new HashMap<>(bindingSize);
57: resolveVariableNamesAndIndexes();
58: }
59:
60: private void resolveVariableNamesAndIndexes() {
61: Integer i = 0;
62:• for (Variable<OWLObject> v : queryResult.getResultVars()) {
63: namesToVariables.put(v.getName(), v);
64: indexesToVariables.put(i, v);
65: i++;
66: }
67: }
68:
69: @Override
70: public int findColumn(String columnLabel) {
71: ensureOpen();
72: final Variable<OWLObject> v = namesToVariables.get(columnLabel);
73:• if (v == null) {
74: return -1;
75: }
76:• for (Map.Entry<Integer, Variable<OWLObject>> e : indexesToVariables.entrySet()) {
77:• if (e.getValue().equals(v)) {
78: return e.getKey();
79: }
80: }
81: return -1;
82: }
83:
84: @Override
85: public int getColumnCount() {
86: ensureOpen();
87: return namesToVariables.size();
88: }
89:
90: @Override
91: public boolean isBound(int variableIndex) {
92: ensureState();
93:• return indexesToVariables.containsKey(variableIndex) && currentRow
94:• .get(indexesToVariables.get(variableIndex)) != null;
95: }
96:
97: @Override
98: public boolean isBound(String variableName) {
99: ensureState();
100:• return namesToVariables.containsKey(variableName) && currentRow.get(namesToVariables.get(variableName)) != null;
101: }
102:
103: @Override
104: public void first() {
105: ensureOpen();
106: this.currentIndex = -1;
107: this.iterator = queryResult.iterator();
108: next();
109: }
110:
111: @Override
112: public boolean getBoolean(int columnIndex) throws OntoDriverException {
113: return getPrimitiveValue(Boolean.class, columnIndex);
114: }
115:
116: private <T> T getPrimitiveValue(Class<T> cls, int columnIndex) throws OntoDriverException {
117: final Object val = OwlapiUtils.owlLiteralToValue(getLiteral(columnIndex));
118: ensureValueIsAssignableToClass(val, cls);
119: return cls.cast(val);
120: }
121:
122: private static <T> void ensureValueIsAssignableToClass(Object val, Class<T> cls) {
123:• if (!cls.isAssignableFrom(val.getClass())) {
124: throw new BindingValueMismatchException("Value " + val + " cannot be returned as " + cls.getSimpleName());
125: }
126: }
127:
128: private OWLLiteral getLiteral(int columnIndex) throws OntoDriverException {
129: final OWLObject currentValue = getCurrentValue(columnIndex);
130: ensureValueIsLiteral(currentValue);
131: return (OWLLiteral) currentValue;
132: }
133:
134: private static void ensureValueIsLiteral(OWLObject currentValue) {
135:• if (!(currentValue instanceof OWLLiteral)) {
136: throw new BindingValueMismatchException("Value " + currentValue + " is not an OWLLiteral.");
137: }
138: }
139:
140: private OWLObject getCurrentValue(int columnIndex) throws OwlapiDriverException {
141: ensureState();
142:• if (!indexesToVariables.containsKey(columnIndex)) {
143: throw new OwlapiDriverException("No result binding found for index " + columnIndex);
144: }
145: final Variable<OWLObject> v = indexesToVariables.get(columnIndex);
146: final GroundTerm<OWLObject> gt = currentRow.get(v);
147:• if (gt == null) {
148: throw new VariableNotBoundException(
149: "Variable at index " + columnIndex + " not bound in the current result row.");
150: }
151: return gt.getWrappedObject();
152: }
153:
154: private void ensureState() {
155: ensureOpen();
156:• if (currentRow == null) {
157: throw new IllegalStateException("Current row is null.");
158: }
159: }
160:
161: @Override
162: public boolean getBoolean(String columnLabel) throws OntoDriverException {
163: return getPrimitiveValue(Boolean.class, columnLabel);
164: }
165:
166: private <T> T getPrimitiveValue(Class<T> cls, String columnLabel) throws OwlapiDriverException {
167: final Object val = OwlapiUtils.owlLiteralToValue(getLiteral(columnLabel));
168: ensureValueIsAssignableToClass(val, cls);
169: return cls.cast(val);
170: }
171:
172: private OWLLiteral getLiteral(String columnLabel) throws OwlapiDriverException {
173: final OWLObject currentValue = getCurrentValue(columnLabel);
174: ensureValueIsLiteral(currentValue);
175: return (OWLLiteral) currentValue;
176: }
177:
178: private OWLObject getCurrentValue(String columnLabel) throws OwlapiDriverException {
179: ensureState();
180:• if (!namesToVariables.containsKey(columnLabel)) {
181: throw new OwlapiDriverException("No result binding found for label " + columnLabel);
182: }
183: final Variable<OWLObject> v = namesToVariables.get(columnLabel);
184: final GroundTerm<OWLObject> gt = currentRow.get(v);
185:• if (gt == null) {
186: throw new VariableNotBoundException(
187: "Variable \"" + columnLabel + "\" not bound in the current result row.");
188: }
189: return gt.getWrappedObject();
190: }
191:
192: @Override
193: public byte getByte(int columnIndex) throws OntoDriverException {
194: final Number num = getPrimitiveValue(Number.class, columnIndex);
195: return num.byteValue();
196: }
197:
198: @Override
199: public byte getByte(String columnLabel) throws OntoDriverException {
200: final Number num = getPrimitiveValue(Number.class, columnLabel);
201: return num.byteValue();
202: }
203:
204: @Override
205: public double getDouble(int columnIndex) throws OntoDriverException {
206: final Number num = getPrimitiveValue(Number.class, columnIndex);
207: return num.doubleValue();
208: }
209:
210: @Override
211: public double getDouble(String columnLabel) throws OntoDriverException {
212: final Number num = getPrimitiveValue(Number.class, columnLabel);
213: return num.doubleValue();
214: }
215:
216: @Override
217: public float getFloat(int columnIndex) throws OntoDriverException {
218: final Number num = getPrimitiveValue(Number.class, columnIndex);
219: return num.floatValue();
220: }
221:
222: @Override
223: public float getFloat(String columnLabel) throws OntoDriverException {
224: final Number num = getPrimitiveValue(Number.class, columnLabel);
225: return num.floatValue();
226: }
227:
228: @Override
229: public int getInt(int columnIndex) throws OntoDriverException {
230: final Number num = getPrimitiveValue(Number.class, columnIndex);
231: return num.intValue();
232: }
233:
234: @Override
235: public int getInt(String columnLabel) throws OntoDriverException {
236: final Number num = getPrimitiveValue(Number.class, columnLabel);
237: return num.intValue();
238: }
239:
240: @Override
241: public long getLong(int columnIndex) throws OntoDriverException {
242: final Number num = getPrimitiveValue(Number.class, columnIndex);
243: return num.longValue();
244: }
245:
246: @Override
247: public long getLong(String columnLabel) throws OntoDriverException {
248: final Number num = getPrimitiveValue(Number.class, columnLabel);
249: return num.longValue();
250: }
251:
252: @Override
253: public Object getObject(int columnIndex) throws OntoDriverException {
254: return owlObjectToObject(getCurrentValue(columnIndex));
255: }
256:
257: private static Object owlObjectToObject(OWLObject owlValue) {
258:• if (owlValue == null) {
259: return null;
260: }
261:• if (owlValue instanceof OWLLiteral) {
262: return OwlapiUtils.owlLiteralToValue((OWLLiteral) owlValue);
263: }
264: final Set<OWLEntity> sig = owlValue.signature().collect(Collectors.toSet());
265:• if (sig.isEmpty()) {
266: return owlValue.toString();
267: } else {
268: return URI.create(sig.iterator().next().toStringID());
269: }
270: }
271:
272: @Override
273: public Object getObject(String columnLabel) throws OntoDriverException {
274: return owlObjectToObject(getCurrentValue(columnLabel));
275: }
276:
277: @Override
278: public <T> T getObject(int columnIndex, Class<T> cls) throws OntoDriverException {
279: Objects.requireNonNull(cls);
280: return owlObjectToType(getCurrentValue(columnIndex), cls);
281: }
282:
283: private static <T> T owlObjectToType(OWLObject owlValue, Class<T> cls) throws OntoDriverException {
284:• if (cls.isAssignableFrom(owlValue.getClass())) {
285: return cls.cast(owlValue);
286: }
287:• if (owlValue instanceof OWLLiteral) {
288: final Object ob = OwlapiUtils.owlLiteralToValue((OWLLiteral) owlValue);
289: return cls.cast(DatatypeTransformer.transform(ob, cls));
290: } else {
291: final Set<OWLEntity> sig = owlValue.signature().collect(Collectors.toSet());
292:• if (!sig.isEmpty()) {
293: final URI uri = URI.create(sig.iterator().next().toStringID());
294: return cls.cast(DatatypeTransformer.transform(uri, cls));
295: }
296: }
297: throw new OwlapiDriverException("Conversion to type " + cls + " is not supported.");
298: }
299:
300: @Override
301: public <T> T getObject(String columnLabel, Class<T> cls) throws OntoDriverException {
302: Objects.requireNonNull(cls, getNPXMessageSupplier("cls"));
303: return owlObjectToType(getCurrentValue(columnLabel), cls);
304: }
305:
306: @Override
307: public int getRowIndex() {
308: return currentIndex;
309: }
310:
311: @Override
312: public short getShort(int columnIndex) throws OntoDriverException {
313: final Number num = getPrimitiveValue(Number.class, columnIndex);
314: return num.shortValue();
315: }
316:
317: @Override
318: public short getShort(String columnLabel) throws OntoDriverException {
319: final Number num = getPrimitiveValue(Number.class, columnLabel);
320: return num.shortValue();
321: }
322:
323: @Override
324: public String getString(int columnIndex) throws OntoDriverException {
325: return owlValueToString(getCurrentValue(columnIndex));
326: }
327:
328: private static String owlValueToString(OWLObject owlValue) {
329:• if (owlValue instanceof OWLLiteral) {
330: return ((OWLLiteral) owlValue).getLiteral();
331: }
332: final Set<OWLEntity> sig = owlValue.signature().collect(Collectors.toSet());
333:• if (sig.isEmpty()) {
334: return owlValue.toString();
335: } else {
336: return sig.iterator().next().toStringID();
337: }
338: }
339:
340: @Override
341: public String getString(String columnLabel) throws OntoDriverException {
342: return owlValueToString(getCurrentValue(columnLabel));
343: }
344:
345: @Override
346: public boolean isFirst() {
347: ensureOpen();
348:• return currentIndex == 0;
349: }
350:
351: @Override
352: public boolean hasNext() {
353: ensureOpen();
354: return iterator.hasNext();
355: }
356:
357: @Override
358: public void last() {
359: ensureOpen();
360:• while (hasNext()) {
361: next();
362: }
363: }
364:
365: @Override
366: public void next() {
367: ensureOpen();
368:• if (!hasNext()) {
369: throw new NoSuchElementException("The result set has no more rows.");
370: }
371: this.currentRow = iterator.next();
372: currentIndex++;
373: }
374:
375: @Override
376: public void previous() {
377: ensureOpen();
378: relative(-1);
379: }
380:
381: @Override
382: public void relative(int rows) {
383: ensureOpen();
384: setRowIndex(currentIndex + rows);
385: }
386:
387: @Override
388: public void setRowIndex(int rowIndex) {
389: ensureOpen();
390:• if (rowIndex == currentIndex) {
391: return;
392: }
393:• if (rowIndex < 0) {
394: throw new IllegalArgumentException("Cannot set row index to a number less than 0.");
395: }
396:• if (rowIndex < currentIndex) {
397: first();
398: }
399:• while (rowIndex > currentIndex && hasNext()) {
400: next();
401: }
402: }
403: }