Skip to content

Package: SesameAdapter

SesameAdapter

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