Skip to content

Method: getQueryExecutor()

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
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: * <p>
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.sesame;
16:
17: import cz.cvut.kbss.ontodriver.Closeable;
18: import cz.cvut.kbss.ontodriver.Wrapper;
19: import cz.cvut.kbss.ontodriver.config.ConfigParam;
20: import cz.cvut.kbss.ontodriver.config.Configuration;
21: import cz.cvut.kbss.ontodriver.descriptor.*;
22: import cz.cvut.kbss.ontodriver.exception.IdentifierGenerationException;
23: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
24: import cz.cvut.kbss.ontodriver.model.Axiom;
25: import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
26: import cz.cvut.kbss.ontodriver.sesame.connector.StatementExecutor;
27: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
28: import cz.cvut.kbss.ontodriver.sesame.util.SesameUtils;
29: import cz.cvut.kbss.ontodriver.util.IdentifierUtils;
30: import org.eclipse.rdf4j.model.Resource;
31: import org.eclipse.rdf4j.model.Value;
32: import org.eclipse.rdf4j.model.ValueFactory;
33: import org.eclipse.rdf4j.model.vocabulary.RDF;
34:
35: import java.net.URI;
36: import java.util.ArrayList;
37: import java.util.Collection;
38: import java.util.List;
39:
40: class SesameAdapter implements Closeable, Wrapper {
41:
42: /**
43: * Maximum number of attempts to generate a unique identifier
44: */
45: private static final int ID_GENERATION_THRESHOLD = 64;
46:
47: private final Connector connector;
48: private final ValueFactory valueFactory;
49: private final String language;
50: private boolean open;
51: private final Transaction transaction;
52:
53: public SesameAdapter(Connector connector, Configuration configuration) {
54: assert connector != null;
55:
56: this.connector = connector;
57: this.valueFactory = connector.getValueFactory();
58: this.language = configuration.getProperty(ConfigParam.ONTOLOGY_LANGUAGE);
59: this.open = true;
60: this.transaction = new Transaction();
61: }
62:
63: Connector getConnector() {
64: return connector;
65: }
66:
67: ValueFactory getValueFactory() {
68: return valueFactory;
69: }
70:
71: String getLanguage() {
72: return language;
73: }
74:
75: @Override
76: public void close() throws OntoDriverException {
77: if (!open) {
78: return;
79: }
80: try {
81: connector.close();
82: } finally {
83: this.open = false;
84: }
85: }
86:
87: @Override
88: public boolean isOpen() {
89: return open;
90: }
91:
92: void commit() throws SesameDriverException {
93: if (transaction.isActive()) {
94: transaction.commit();
95: connector.commit();
96: transaction.afterCommit();
97: }
98: }
99:
100: void rollback() throws SesameDriverException {
101: if (transaction.isActive()) {
102: transaction.rollback();
103: connector.rollback();
104: transaction.afterRollback();
105: }
106: }
107:
108: boolean isConsistent(URI context) {
109: // Sesame currently doesn't support any consistency checking
110: // functionality
111: return true;
112: }
113:
114: List<URI> getContexts() throws SesameDriverException {
115: final List<Resource> contextIds = connector.getContexts();
116: final List<URI> contexts = new ArrayList<>(contextIds.size());
117: for (Resource res : contextIds) {
118: final URI context = SesameUtils.toJavaUri(res);
119: // We support only named contexts (no blank nodes)
120: if (context != null) {
121: contexts.add(context);
122: }
123: }
124: return contexts;
125: }
126:
127: URI generateIdentifier(URI classUri) throws SesameDriverException {
128: startTransactionIfNotActive();
129: boolean unique = false;
130: URI id = null;
131: int counter = 0;
132: while (!unique && counter++ < ID_GENERATION_THRESHOLD) {
133: id = IdentifierUtils.generateIdentifier(classUri);
134: unique = isIdentifierUnique(id, classUri);
135: }
136: if (!unique) {
137: throw new IdentifierGenerationException("Unable to generate a unique identifier.");
138: }
139: return id;
140:
141: }
142:
143: private void startTransactionIfNotActive() throws SesameDriverException {
144: if (!transaction.isActive()) {
145: connector.begin();
146: transaction.begin();
147: }
148: }
149:
150: private boolean isIdentifierUnique(URI identifier, URI classUri) throws SesameDriverException {
151: return !connector.containsStatement(
152: SesameUtils.toSesameIri(identifier, valueFactory), RDF.TYPE,
153: SesameUtils.toSesameIri(classUri, valueFactory), true);
154: }
155:
156: boolean contains(Axiom<?> axiom, URI context) throws SesameDriverException {
157: startTransactionIfNotActive();
158: Value value;
159: if (SesameUtils.isResourceIdentifier(axiom.getValue().getValue())) {
160: value = valueFactory.createIRI(axiom.getValue().stringValue());
161: } else {
162: value = SesameUtils.createDataPropertyLiteral(axiom.getValue().getValue(), language,
163: valueFactory);
164: }
165: final org.eclipse.rdf4j.model.IRI sesameContext = SesameUtils.toSesameIri(context, valueFactory);
166: return connector.containsStatement(
167: SesameUtils.toSesameIri(axiom.getSubject().getIdentifier(), valueFactory),
168: SesameUtils.toSesameIri(axiom.getAssertion().getIdentifier(), valueFactory), value,
169: axiom.getAssertion().isInferred(), sesameContext);
170:
171: }
172:
173: Collection<Axiom<?>> find(AxiomDescriptor axiomDescriptor) throws SesameDriverException {
174: startTransactionIfNotActive();
175: return new AxiomLoader(connector, valueFactory).loadAxioms(axiomDescriptor);
176: }
177:
178: void persist(AxiomValueDescriptor axiomDescriptor) throws SesameDriverException {
179: startTransactionIfNotActive();
180: new AxiomSaver(connector, valueFactory, language).persistAxioms(axiomDescriptor);
181: }
182:
183: void update(AxiomValueDescriptor axiomDescriptor) throws SesameDriverException {
184: startTransactionIfNotActive();
185: new EpistemicAxiomRemover(connector, valueFactory, language).remove(axiomDescriptor);
186: new AxiomSaver(connector, valueFactory, language).persistAxioms(axiomDescriptor);
187: }
188:
189: void remove(AxiomDescriptor axiomDescriptor) throws SesameDriverException {
190: startTransactionIfNotActive();
191: new EpistemicAxiomRemover(connector, valueFactory, language).remove(axiomDescriptor);
192: }
193:
194: StatementExecutor getQueryExecutor() {
195: return connector;
196: }
197:
198: ListHandler<SimpleListDescriptor, SimpleListValueDescriptor> getSimpleListHandler() throws SesameDriverException {
199: startTransactionIfNotActive();
200: return ListHandler.createForSimpleList(connector, valueFactory);
201: }
202:
203: ListHandler<ReferencedListDescriptor, ReferencedListValueDescriptor> getReferencedListHandler() throws
204: SesameDriverException {
205: startTransactionIfNotActive();
206: return ListHandler.createForReferencedList(connector, valueFactory);
207: }
208:
209: TypesHandler getTypesHandler() throws SesameDriverException {
210: startTransactionIfNotActive();
211: return new TypesHandler(connector, valueFactory);
212: }
213:
214: @Override
215: public <T> T unwrap(Class<T> cls) throws OntoDriverException {
216: if (cls.isAssignableFrom(this.getClass())) {
217: return cls.cast(this);
218: } else if (cls.isAssignableFrom(valueFactory.getClass())) {
219: return cls.cast(valueFactory);
220: }
221: return connector.unwrap(cls);
222: }
223: }