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: 56
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 8
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%
generateIdentifier(URI)
M: 0 C: 81
100%
M: 0 C: 10
100%
M: 0 C: 6
100%
M: 0 C: 15
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%
individualExists(NamedResource, URI)
M: 0 C: 30
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
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: 42
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
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: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
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: *
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.config.ConfigParam;
19: import cz.cvut.kbss.ontodriver.config.Configuration;
20: import cz.cvut.kbss.ontodriver.descriptor.*;
21: import cz.cvut.kbss.ontodriver.exception.IdentifierGenerationException;
22: import cz.cvut.kbss.ontodriver.exception.OWLIndividualExistsException;
23: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
24: import cz.cvut.kbss.ontodriver.model.Axiom;
25: import cz.cvut.kbss.ontodriver.model.NamedResource;
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 org.openrdf.model.Resource;
30: import org.openrdf.model.Statement;
31: import org.openrdf.model.Value;
32: import org.openrdf.model.ValueFactory;
33: import org.openrdf.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: import java.util.Random;
40:
41: class SesameAdapter implements Closeable {
42:
43: /**
44: * Maximum number of attempts to generate a unique identifier
45: */
46: private static final int ID_GENERATION_THRESHOLD = 64;
47: private static final Random RANDOM = new Random();
48:
49: private final Connector connector;
50: private final ValueFactory valueFactory;
51: private final String language;
52: private boolean open;
53: private final Transaction transaction;
54:
55: public SesameAdapter(Connector connector, Configuration configuration) {
56:• assert connector != null;
57:
58: this.connector = connector;
59: this.valueFactory = connector.getValueFactory();
60: this.language = configuration.getProperty(ConfigParam.ONTOLOGY_LANGUAGE);
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: String getLanguage() {
74: return language;
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:• if (classUri.getFragment() != null) {
136: id = URI.create(classUri.toString() + "_instance" + RANDOM.nextInt());
137: } else {
138: String base = classUri.toString();
139:• if (base.endsWith("/")) {
140: id = URI.create(base + "_instance" + RANDOM.nextInt());
141: } else {
142: id = URI.create(base + "#instance" + RANDOM.nextInt());
143: }
144: }
145: unique = isIdentifierUnique(id, classUri);
146: }
147:• if (!unique) {
148: throw new IdentifierGenerationException("Unable to generate a unique identifier.");
149: }
150: return id;
151:
152: }
153:
154: private void startTransactionIfNotActive() throws SesameDriverException {
155:• if (!transaction.isActive()) {
156: connector.begin();
157: transaction.begin();
158: }
159: }
160:
161: private boolean isIdentifierUnique(URI identifier, URI classUri) throws SesameDriverException {
162: final Collection<Statement> stmts = connector.findStatements(
163: SesameUtils.toSesameUri(identifier, valueFactory), RDF.TYPE,
164: SesameUtils.toSesameUri(classUri, valueFactory), true);
165: return stmts.isEmpty();
166: }
167:
168: boolean contains(Axiom<?> axiom, URI context) throws SesameDriverException {
169: startTransactionIfNotActive();
170: Value value;
171:• if (SesameUtils.isResourceIdentifier(axiom.getValue().getValue())) {
172: value = valueFactory.createURI(axiom.getValue().stringValue());
173: } else {
174: value = SesameUtils.createDataPropertyLiteral(axiom.getValue().getValue(), language,
175: valueFactory);
176: }
177: return !(connector.findStatements(
178: SesameUtils.toSesameUri(axiom.getSubject().getIdentifier(), valueFactory),
179: SesameUtils.toSesameUri(axiom.getAssertion().getIdentifier(), valueFactory), value,
180:• true, SesameUtils.toSesameUri(context, valueFactory)).isEmpty());
181: }
182:
183: Collection<Axiom<?>> find(AxiomDescriptor axiomDescriptor) throws SesameDriverException {
184: startTransactionIfNotActive();
185: return new AxiomLoader(connector, valueFactory).loadAxioms(axiomDescriptor);
186: }
187:
188: void persist(AxiomValueDescriptor axiomDescriptor) throws SesameDriverException {
189: startTransactionIfNotActive();
190:• if (individualExists(axiomDescriptor.getSubject(), axiomDescriptor.getSubjectContext())) {
191: throw new SesameDriverException(new OWLIndividualExistsException("An individual with identifier "
192: + axiomDescriptor.getSubject() + " already exists in context "
193: + axiomDescriptor.getSubjectContext()));
194: }
195: new AxiomSaver(connector, valueFactory, language).persistAxioms(axiomDescriptor);
196: }
197:
198: private boolean individualExists(NamedResource subject, URI subjectContext)
199: throws SesameDriverException {
200: final org.openrdf.model.URI sesameSubject = SesameUtils.toSesameUri(
201: subject.getIdentifier(), valueFactory);
202: final Collection<Statement> result = connector.findStatements(sesameSubject, RDF.TYPE,
203: null, true, SesameUtils.toSesameUri(subjectContext, valueFactory));
204:• return !result.isEmpty();
205: }
206:
207: void update(AxiomValueDescriptor axiomDescriptor) throws SesameDriverException {
208: startTransactionIfNotActive();
209: new EpistemicAxiomRemover(connector, valueFactory, language).remove(axiomDescriptor);
210: new AxiomSaver(connector, valueFactory, language).persistAxioms(axiomDescriptor);
211: }
212:
213: void remove(AxiomDescriptor axiomDescriptor) throws SesameDriverException {
214: startTransactionIfNotActive();
215: new EpistemicAxiomRemover(connector, valueFactory, language).remove(axiomDescriptor);
216: }
217:
218: StatementExecutor getQueryExecutor() {
219: return connector;
220: }
221:
222: ListHandler<SimpleListDescriptor, SimpleListValueDescriptor> getSimpleListHandler() {
223: return ListHandler.createForSimpleList(connector, valueFactory);
224: }
225:
226: ListHandler<ReferencedListDescriptor, ReferencedListValueDescriptor> getReferencedListHandler() {
227: return ListHandler.createForReferencedList(connector, valueFactory);
228: }
229:
230: TypesHandler getTypesHandler() {
231: return new TypesHandler(connector, valueFactory);
232: }
233: }