-
Notifications
You must be signed in to change notification settings - Fork 14
/
JenaStatement.java
117 lines (99 loc) · 3.49 KB
/
JenaStatement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* Copyright (C) 2022 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.ontodriver.jena.query;
import cz.cvut.kbss.ontodriver.ResultSet;
import cz.cvut.kbss.ontodriver.Statement;
import cz.cvut.kbss.ontodriver.jena.connector.StatementExecutor;
import cz.cvut.kbss.ontodriver.jena.exception.JenaDriverException;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QueryParseException;
import java.util.Objects;
public class JenaStatement implements Statement {
private final StatementExecutor executor;
private StatementOntology targetOntology = StatementOntology.CENTRAL;
private boolean open = true;
private AbstractResultSet currentResultSet;
public JenaStatement(StatementExecutor executor) {
this.executor = executor;
}
@Override
public ResultSet executeQuery(String sparql) throws JenaDriverException {
ensureOpen();
final Query query = parseQuery(Objects.requireNonNull(sparql));
closeCurrentResultSet();
final AbstractResultSet resultSet;
if (query.isAskType()) {
resultSet = executor.executeAskQuery(query, targetOntology);
} else {
resultSet = executor.executeSelectQuery(query, targetOntology);
}
resultSet.setStatement(this);
this.currentResultSet = resultSet;
return resultSet;
}
void ensureOpen() {
if (!open) {
throw new IllegalStateException("Statement is closed.");
}
}
private Query parseQuery(String sparql) throws JenaDriverException {
try {
return QueryFactory.create(sparql);
} catch (QueryParseException e) {
throw new JenaDriverException("Unable to parse query " + sparql, e);
}
}
private void closeCurrentResultSet() throws JenaDriverException {
if (currentResultSet != null) {
currentResultSet.close();
this.currentResultSet = null;
}
}
@Override
public void executeUpdate(String sparql) throws JenaDriverException {
ensureOpen();
Objects.requireNonNull(sparql);
closeCurrentResultSet();
executor.executeUpdate(sparql, targetOntology);
}
@Override
public void useOntology(StatementOntology ontology) {
ensureOpen();
this.targetOntology = Objects.requireNonNull(ontology);
}
@Override
public StatementOntology getStatementOntology() {
return targetOntology;
}
@Override
public void close() throws JenaDriverException {
this.open = false;
closeCurrentResultSet();
}
@Override
public void disableInference() {
// TODO
}
@Override
public boolean isInferenceDisabled() {
// TODO
return false;
}
@Override
public boolean isOpen() {
return open;
}
}