Skip to content

Package: Rdf4jStatement

Rdf4jStatement

nameinstructionbranchcomplexitylinemethod
Rdf4jStatement(StatementExecutor)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
close()
M: 1 C: 9
90%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 4
80%
M: 0 C: 1
100%
closeCurrentResultSet()
M: 0 C: 10
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
determineResult(String)
M: 6 C: 27
82%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 2 C: 4
67%
M: 0 C: 1
100%
disableInference()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
ensureOpen()
M: 5 C: 4
44%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 2
67%
M: 0 C: 1
100%
executeQuery(String)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
executeUpdate(String)
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%
getStatementOntology()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isAskQuery(String)
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%
isInferenceDisabled()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isOpen()
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%
querySpec(String)
M: 0 C: 10
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
setUseBackupOntology()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
setUseTransactionalOntology()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
useBackupOntology()
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
useOntology(Statement.StatementOntology)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
useTransactionalOntology()
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
validateQueryParams(String)
M: 5 C: 7
58%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 3
75%
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.rdf4j.query;
16:
17: import cz.cvut.kbss.ontodriver.ResultSet;
18: import cz.cvut.kbss.ontodriver.Statement;
19: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
20: import cz.cvut.kbss.ontodriver.rdf4j.connector.StatementExecutor;
21: import cz.cvut.kbss.ontodriver.rdf4j.exception.Rdf4jDriverException;
22: import org.eclipse.rdf4j.query.QueryEvaluationException;
23: import org.eclipse.rdf4j.query.QueryLanguage;
24: import org.eclipse.rdf4j.query.TupleQueryResult;
25: import org.eclipse.rdf4j.query.parser.ParsedBooleanQuery;
26: import org.eclipse.rdf4j.query.parser.QueryParserUtil;
27:
28: import java.util.Objects;
29:
30: public class Rdf4jStatement implements Statement {
31:
32: private StatementOntology targetOntology = StatementOntology.TRANSACTIONAL;
33: private boolean inferenceDisabled = false;
34: private final StatementExecutor queryExecutor;
35: private ResultSet resultSet;
36:
37: private boolean open;
38:
39: public Rdf4jStatement(StatementExecutor queryExecutor) {
40: this.queryExecutor = queryExecutor;
41: this.open = true;
42: }
43:
44: @Override
45: public ResultSet executeQuery(String sparql) throws OntoDriverException {
46: ensureOpen();
47: validateQueryParams(sparql);
48: closeCurrentResultSet();
49: this.resultSet = determineResult(sparql);
50: return resultSet;
51: }
52:
53: private ResultSet determineResult(String sparql) throws Rdf4jDriverException {
54:• if (isAskQuery(sparql)) {
55: return new AskResultSet(queryExecutor.executeBooleanQuery(querySpec(sparql)), this);
56: } else {
57: final TupleQueryResult tqr = queryExecutor.executeSelectQuery(querySpec(sparql));
58: try {
59: return new SelectResultSet(tqr, this);
60: } catch (QueryEvaluationException e) {
61: throw new Rdf4jDriverException(e);
62: }
63: }
64: }
65:
66: QuerySpecification querySpec(String sparql) {
67:• return QuerySpecification.query(sparql).includeInference(!inferenceDisabled);
68: }
69:
70: private static boolean isAskQuery(String query) {
71: return QueryParserUtil.parseOperation(QueryLanguage.SPARQL, query, null) instanceof ParsedBooleanQuery;
72: }
73:
74: @Override
75: public void executeUpdate(String sparql) throws OntoDriverException {
76: ensureOpen();
77: validateQueryParams(sparql);
78: closeCurrentResultSet();
79: queryExecutor.executeUpdate(querySpec(sparql));
80: }
81:
82: @Override
83: public void useOntology(StatementOntology ontology) {
84: this.targetOntology = ontology;
85: }
86:
87: @Override
88: public StatementOntology getStatementOntology() {
89: return targetOntology;
90: }
91:
92: private static void validateQueryParams(String sparql) {
93: Objects.requireNonNull(sparql);
94:• if (sparql.isEmpty()) {
95: throw new IllegalArgumentException("Query string cannot be empty.");
96: }
97: }
98:
99: @Override
100: public boolean isOpen() {
101: return open;
102: }
103:
104: @Override
105: public void close() throws OntoDriverException {
106:• if (!open) {
107: return;
108: }
109: this.open = false;
110: closeCurrentResultSet();
111: }
112:
113: private void closeCurrentResultSet() throws OntoDriverException {
114:• if (resultSet != null) {
115: resultSet.close();
116: this.resultSet = null;
117: }
118: }
119:
120: public void setUseTransactionalOntology() {
121: ensureOpen();
122: this.targetOntology = StatementOntology.TRANSACTIONAL;
123: }
124:
125: public boolean useTransactionalOntology() {
126: ensureOpen();
127:• return targetOntology == StatementOntology.TRANSACTIONAL;
128: }
129:
130: public void setUseBackupOntology() {
131: ensureOpen();
132: this.targetOntology = StatementOntology.CENTRAL;
133: }
134:
135: public boolean useBackupOntology() {
136: ensureOpen();
137:• return targetOntology == StatementOntology.CENTRAL;
138: }
139:
140: @Override
141: public void disableInference() {
142: this.inferenceDisabled = true;
143: }
144:
145: @Override
146: public boolean isInferenceDisabled() {
147: return inferenceDisabled;
148: }
149:
150: void ensureOpen() {
151:• if (!open) {
152: throw new IllegalStateException("This statement is closed.");
153: }
154: }
155: }