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