Skip to content

Package: SesameAdapter

SesameAdapter

nameinstructionbranchcomplexitylinemethod
SesameAdapter(Connector, DriverConfiguration)
M: 4 C: 27
87%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 8
100%
M: 0 C: 1
100%
close()
M: 1 C: 10
91%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 4
80%
M: 0 C: 1
100%
commit()
M: 0 C: 14
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
contains(Axiom, Set)
M: 4 C: 57
93%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 11
100%
M: 0 C: 1
100%
find(AxiomDescriptor)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
generateIdentifier(URI)
M: 0 C: 32
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
getConfig()
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%
getConnector()
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%
getContexts()
M: 0 C: 32
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
getQueryExecutor()
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%
getReferencedListHandler()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getSimpleListHandler()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getTypesHandler()
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getValueFactory()
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%
isConsistent(URI)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isIdentifierUnique(URI, URI)
M: 0 C: 19
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
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%
lambda$contains$0(URI)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
persist(AxiomValueDescriptor)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
remove(AxiomDescriptor)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
rollback()
M: 0 C: 14
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
startTransactionIfNotActive()
M: 0 C: 11
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
unwrap(Class)
M: 0 C: 25
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
update(AxiomValueDescriptor)
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.ontodriver.sesame;
14:
15: import cz.cvut.kbss.ontodriver.Closeable;
16: import cz.cvut.kbss.ontodriver.Wrapper;
17: import cz.cvut.kbss.ontodriver.config.DriverConfiguration;
18: import cz.cvut.kbss.ontodriver.descriptor.*;
19: import cz.cvut.kbss.ontodriver.exception.IdentifierGenerationException;
20: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
21: import cz.cvut.kbss.ontodriver.model.Axiom;
22: import cz.cvut.kbss.ontodriver.sesame.config.Constants;
23: import cz.cvut.kbss.ontodriver.sesame.config.RuntimeConfiguration;
24: import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
25: import cz.cvut.kbss.ontodriver.sesame.connector.StatementExecutor;
26: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
27: import cz.cvut.kbss.ontodriver.sesame.util.SesameUtils;
28: import cz.cvut.kbss.ontodriver.util.IdentifierUtils;
29: import cz.cvut.kbss.ontodriver.util.Transaction;
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.*;
37: import java.util.stream.Collectors;
38:
39: import static cz.cvut.kbss.ontodriver.sesame.util.SesameUtils.toSesameIri;
40:
41: class SesameAdapter implements Closeable, Wrapper {
42:
43: /**
44: * Maximum number of attempts to generate a unique identifier
45: */
46: private static final int ID_GENERATION_THRESHOLD = 64;
47:
48: private final Connector connector;
49: private final ValueFactory valueFactory;
50: private final RuntimeConfiguration config;
51: private boolean open;
52: private final Transaction transaction;
53:
54: public SesameAdapter(Connector connector, DriverConfiguration configuration) {
55:• assert connector != null;
56:
57: this.connector = connector;
58: this.valueFactory = connector.getValueFactory();
59: this.config = new RuntimeConfiguration(configuration);
60: this.open = true;
61: this.transaction = new Transaction();
62: }
63:
64: Connector getConnector() {
65: return connector;
66: }
67:
68: ValueFactory getValueFactory() {
69: return valueFactory;
70: }
71:
72: RuntimeConfiguration getConfig() {
73: return config;
74: }
75:
76: @Override
77: public void close() throws OntoDriverException {
78:• if (!open) {
79: return;
80: }
81: try {
82: connector.close();
83: } finally {
84: this.open = false;
85: }
86: }
87:
88: @Override
89: public boolean isOpen() {
90: return open;
91: }
92:
93: void commit() throws SesameDriverException {
94:• if (transaction.isActive()) {
95: transaction.commit();
96: connector.commit();
97: transaction.afterCommit();
98: }
99: }
100:
101: void rollback() throws SesameDriverException {
102:• if (transaction.isActive()) {
103: transaction.rollback();
104: connector.rollback();
105: transaction.afterRollback();
106: }
107: }
108:
109: boolean isConsistent(URI context) {
110: // Sesame currently doesn't support any consistency checking
111: // functionality
112: return true;
113: }
114:
115: List<URI> getContexts() throws SesameDriverException {
116: final List<Resource> contextIds = connector.getContexts();
117: final List<URI> contexts = new ArrayList<>(contextIds.size());
118:• for (Resource res : contextIds) {
119: final URI context = SesameUtils.toJavaUri(res);
120: // We support only named contexts (no blank nodes)
121:• if (context != null) {
122: contexts.add(context);
123: }
124: }
125: return contexts;
126: }
127:
128: URI generateIdentifier(URI classUri) throws SesameDriverException {
129: startTransactionIfNotActive();
130: boolean unique = false;
131: URI id = null;
132: int counter = 0;
133:• while (!unique && counter++ < ID_GENERATION_THRESHOLD) {
134: id = IdentifierUtils.generateIdentifier(classUri);
135: unique = isIdentifierUnique(id, classUri);
136: }
137:• if (!unique) {
138: throw new IdentifierGenerationException("Unable to generate a unique identifier.");
139: }
140: return id;
141:
142: }
143:
144: private void startTransactionIfNotActive() throws SesameDriverException {
145:• if (!transaction.isActive()) {
146: connector.begin();
147: transaction.begin();
148: }
149: }
150:
151: private boolean isIdentifierUnique(URI identifier, URI classUri) throws SesameDriverException {
152:• return !connector.containsStatement(
153: toSesameIri(identifier, valueFactory), RDF.TYPE,
154: toSesameIri(classUri, valueFactory), true, Collections.emptySet());
155: }
156:
157: boolean contains(Axiom<?> axiom, Set<URI> contexts) throws SesameDriverException {
158: startTransactionIfNotActive();
159: Value value;
160:• if (SesameUtils.isResourceIdentifier(axiom.getValue().getValue())) {
161: value = valueFactory.createIRI(axiom.getValue().stringValue());
162: } else {
163: final String lang =
164:• axiom.getAssertion().hasLanguage() ? axiom.getAssertion().getLanguage() : Constants.DEFAULT_LANG;
165: value = SesameUtils.createDataPropertyLiteral(axiom.getValue().getValue(), lang, valueFactory);
166: }
167: return connector.containsStatement(
168: toSesameIri(axiom.getSubject().getIdentifier(), valueFactory),
169: toSesameIri(axiom.getAssertion().getIdentifier(), valueFactory), value,
170: axiom.getAssertion().isInferred(),
171: contexts.stream().map(c -> toSesameIri(c, valueFactory)).collect(Collectors.toSet()));
172:
173: }
174:
175: Collection<Axiom<?>> find(AxiomDescriptor axiomDescriptor) throws SesameDriverException {
176: startTransactionIfNotActive();
177: return new AxiomLoader(connector, valueFactory, config).loadAxioms(axiomDescriptor);
178: }
179:
180: void persist(AxiomValueDescriptor axiomDescriptor) throws SesameDriverException {
181: startTransactionIfNotActive();
182: new AxiomSaver(connector, valueFactory).persistAxioms(axiomDescriptor);
183: }
184:
185: void update(AxiomValueDescriptor axiomDescriptor) throws SesameDriverException {
186: startTransactionIfNotActive();
187: new EpistemicAxiomRemover(connector, valueFactory).remove(axiomDescriptor);
188: new AxiomSaver(connector, valueFactory).persistAxioms(axiomDescriptor);
189: }
190:
191: void remove(AxiomDescriptor axiomDescriptor) throws SesameDriverException {
192: startTransactionIfNotActive();
193: new EpistemicAxiomRemover(connector, valueFactory).remove(axiomDescriptor);
194: }
195:
196: StatementExecutor getQueryExecutor() {
197: return connector;
198: }
199:
200: ListHandler<SimpleListDescriptor, SimpleListValueDescriptor> getSimpleListHandler() throws SesameDriverException {
201: startTransactionIfNotActive();
202: return ListHandler.createForSimpleList(connector, valueFactory);
203: }
204:
205: ListHandler<ReferencedListDescriptor, ReferencedListValueDescriptor> getReferencedListHandler() throws
206: SesameDriverException {
207: startTransactionIfNotActive();
208: return ListHandler.createForReferencedList(connector, valueFactory);
209: }
210:
211: TypesHandler getTypesHandler() throws SesameDriverException {
212: startTransactionIfNotActive();
213: return new TypesHandler(connector, valueFactory);
214: }
215:
216: @Override
217: public <T> T unwrap(Class<T> cls) throws OntoDriverException {
218:• if (cls.isAssignableFrom(this.getClass())) {
219: return cls.cast(this);
220:• } else if (cls.isAssignableFrom(valueFactory.getClass())) {
221: return cls.cast(valueFactory);
222: }
223: return connector.unwrap(cls);
224: }
225: }