Skip to content

Package: BasicStorageConnector

BasicStorageConnector

nameinstructionbranchcomplexitylinemethod
BasicStorageConnector(Configuration)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
applyChanges(List)
M: 15 C: 25
63%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 10
83%
M: 0 C: 1
100%
close()
M: 0 C: 13
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
executeRead(Function)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
executeWrite(Consumer)
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getLiveOntology()
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getOntologySnapshot()
M: 7 C: 27
79%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 6
75%
M: 0 C: 1
100%
getReasoner(OWLOntology)
M: 0 C: 13
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
initializeConnector()
M: 5 C: 87
95%
M: 3 C: 5
63%
M: 3 C: 2
40%
M: 2 C: 22
92%
M: 0 C: 1
100%
initializeReasonerFactory()
M: 40 C: 19
32%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 8 C: 7
47%
M: 0 C: 1
100%
lambda$applyChanges$0(OWLOntologyChange)
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$applyChanges$1(OWLOntologyChange)
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%
setIriMapper()
M: 17 C: 6
26%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 2
67%
M: 0 C: 1
100%
static {...}
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
tryCreatingOntology()
M: 17 C: 27
61%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 3 C: 6
67%
M: 0 C: 1
100%
writeToFile()
M: 17 C: 12
41%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 3 C: 3
50%
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.owlapi.connector;
16:
17: import cz.cvut.kbss.ontodriver.OntologyStorageProperties;
18: import cz.cvut.kbss.ontodriver.config.ConfigParam;
19: import cz.cvut.kbss.ontodriver.config.Configuration;
20: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
21: import cz.cvut.kbss.ontodriver.owlapi.config.OwlapiConfigParam;
22: import cz.cvut.kbss.ontodriver.owlapi.exception.*;
23: import cz.cvut.kbss.ontodriver.owlapi.util.DefaultOntologyIriMapper;
24: import cz.cvut.kbss.ontodriver.owlapi.util.MappingFileParser;
25: import cz.cvut.kbss.ontodriver.owlapi.util.MutableAxiomChange;
26: import org.semanticweb.owlapi.apibinding.OWLManager;
27: import org.semanticweb.owlapi.model.*;
28: import org.semanticweb.owlapi.model.parameters.OntologyCopy;
29: import org.semanticweb.owlapi.reasoner.OWLReasoner;
30: import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;
31: import org.slf4j.Logger;
32: import org.slf4j.LoggerFactory;
33:
34: import java.io.FileNotFoundException;
35: import java.util.List;
36: import java.util.concurrent.locks.Lock;
37: import java.util.concurrent.locks.ReentrantReadWriteLock;
38: import java.util.function.Consumer;
39: import java.util.function.Function;
40:
41: /**
42: * Default file-based storage connector.
43: * <p>
44: * Each call to {@link #getOntologySnapshot()} returns a new snapshot of the current state of the ontology. The changes
45: * are the applied to a shared ontology, which represents the current state of the underlying storage.
46: * <p>
47: * Note: This connector currently does not handle concurrent updates.
48: */
49: public class BasicStorageConnector extends AbstractConnector {
50:
51: private static final Logger LOG = LoggerFactory.getLogger(BasicStorageConnector.class);
52:
53: private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock();
54: private static Lock READ = LOCK.readLock();
55: private static Lock WRITE = LOCK.writeLock();
56:
57: private OWLOntologyManager ontologyManager;
58: private OWLOntology ontology;
59: private OWLReasoner reasoner;
60: private OWLReasonerFactory reasonerFactory;
61:
62: public BasicStorageConnector(Configuration configuration) throws OwlapiDriverException {
63: super(configuration);
64: }
65:
66: @Override
67: protected void initializeConnector() throws OwlapiDriverException {
68:• if (isOpen()) {
69: return;
70: }
71: final OntologyStorageProperties storageProperties = configuration.getStorageProperties();
72: LOG.debug("Loading ontology {} from {}.", storageProperties.getOntologyURI(),
73: storageProperties.getPhysicalURI());
74: this.ontologyManager = OWLManager.createOWLOntologyManager();
75: setIriMapper();
76: try {
77: this.ontology = ontologyManager.loadOntologyFromOntologyDocument(
78: IRI.create(storageProperties.getPhysicalURI()));
79:• if (!ontology.getOntologyID().getOntologyIRI().isPresent() ||
80:• !ontology.getOntologyID().getOntologyIRI().get().equals(
81: IRI.create(storageProperties.getOntologyURI()))) {
82: throw new InvalidOntologyIriException(
83: "Expected ontology with IRI " + storageProperties.getOntologyURI() +
84: " but the loaded ontology has IRI " + ontology.getOntologyID().getOntologyIRI());
85: }
86: } catch (OWLOntologyCreationException e) {
87:• if (e.getCause() instanceof FileNotFoundException) {
88: LOG.trace("Ontology file {} does not exist.", storageProperties.getPhysicalURI());
89: } else {
90: LOG.trace("Unable to load ontology from document.", e);
91: }
92: tryCreatingOntology();
93: }
94: initializeReasonerFactory();
95: this.reasoner = getReasoner(ontology);
96: }
97:
98: private void setIriMapper() {
99:• if (configuration.isSet(OwlapiConfigParam.MAPPING_FILE_LOCATION)) {
100: ontologyManager.getIRIMappers().add(new DefaultOntologyIriMapper(new MappingFileParser(configuration)));
101: }
102: }
103:
104: private void tryCreatingOntology() throws OwlapiDriverException {
105: LOG.trace("Creating new ontology in {}.", configuration.getStorageProperties().getPhysicalURI());
106: try {
107: this.ontology = ontologyManager
108: .createOntology(IRI.create(configuration.getStorageProperties().getOntologyURI()));
109: ontology.saveOntology(IRI.create(configuration.getStorageProperties().getPhysicalURI()));
110: } catch (OWLOntologyCreationException | OWLOntologyStorageException e) {
111: throw new OwlapiDriverException(
112: "Unable to create ontology in " + configuration.getStorageProperties().getPhysicalURI(), e);
113: }
114: }
115:
116: private void initializeReasonerFactory() {
117: final String reasonerFactoryClass = configuration.getProperty(ConfigParam.REASONER_FACTORY_CLASS);
118:• if (reasonerFactoryClass == null) {
119: LOG.warn("Reasoner factory class not found. Reasoner won't be available.");
120: return;
121: }
122: try {
123: this.reasonerFactory = (OWLReasonerFactory) Class.forName(reasonerFactoryClass).newInstance();
124: } catch (InstantiationException | IllegalAccessException e) {
125: final String msg = "Unable to instantiate reasoner factory class " + reasonerFactoryClass;
126: LOG.error(msg);
127: throw new ReasonerNotAvailableException(msg, e);
128: } catch (ClassNotFoundException e) {
129: final String msg = "Reasoner factory class " + reasonerFactoryClass + " not found!";
130: LOG.error(msg);
131: throw new ReasonerNotAvailableException(msg, e);
132: }
133: }
134:
135: @Override
136: public OntologySnapshot getOntologySnapshot() {
137: ensureOpen();
138: READ.lock();
139: try {
140: final OWLOntologyManager m = OWLManager.createOWLOntologyManager();
141: final OWLOntology snapshot = m.copyOntology(ontology, OntologyCopy.DEEP);
142: return new OntologySnapshot(snapshot, m, m.getOWLDataFactory(), getReasoner(snapshot));
143: } catch (OWLOntologyCreationException e) {
144: throw new OntologySnapshotException("Unable to create ontology snapshot.", e);
145: } finally {
146: READ.unlock();
147: }
148: }
149:
150: private OntologySnapshot getLiveOntology() {
151: ensureOpen();
152: return new OntologySnapshot(ontology, ontologyManager, ontologyManager.getOWLDataFactory(), reasoner);
153: }
154:
155: @Override
156: public <R> R executeRead(Function<OntologySnapshot, R> function) {
157: ensureOpen();
158: READ.lock();
159: try {
160: return function.apply(getLiveOntology());
161: } finally {
162: READ.unlock();
163: }
164: }
165:
166: @Override
167: public void executeWrite(Consumer<OntologySnapshot> function) {
168: ensureOpen();
169: WRITE.lock();
170: try {
171: function.accept(getLiveOntology());
172: } finally {
173: WRITE.unlock();
174: }
175: }
176:
177: private OWLReasoner getReasoner(OWLOntology ontology) {
178:• if (reasonerFactory == null) {
179: LOG.warn(
180: "Creating ontology snapshot without reasoner, because reasoner factory class was not specified.");
181: return null;
182: }
183: return reasonerFactory.createReasoner(ontology);
184: }
185:
186: @Override
187: public void applyChanges(List<OWLOntologyChange> changes) {
188: ensureOpen();
189:• assert changes != null;
190: WRITE.lock();
191: try {
192: changes.stream().filter(ch -> ch instanceof MutableAxiomChange)
193: .forEach(ch -> ((MutableAxiomChange) ch).setOntology(ontology));
194: ontologyManager.applyChanges(changes);
195: try {
196: writeToFile();
197: } catch (OntologyStorageException e) {
198: LOG.error("Unable to write out ontology." + e);
199: }
200: } finally {
201: WRITE.unlock();
202: }
203: }
204:
205: @Override
206: public void close() throws OntoDriverException {
207:• if (!isOpen()) {
208: return;
209: }
210: WRITE.lock();
211: try {
212: writeToFile();
213: super.close();
214: } finally {
215: WRITE.unlock();
216: }
217: }
218:
219: private void writeToFile() throws OntologyStorageException {
220: try {
221: ontologyManager.saveOntology(ontology, IRI.create(configuration.getStorageProperties().getPhysicalURI()));
222: } catch (OWLOntologyStorageException e) {
223: throw new OntologyStorageException(
224: "Error when saving ontology to " + configuration.getStorageProperties().getPhysicalURI(), e);
225: }
226: }
227: }