Skip to content

Method: getSimpleListHandler()

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