Skip to content

Package: BasicStorageConnector

BasicStorageConnector

nameinstructionbranchcomplexitylinemethod
BasicStorageConnector(OntologyStorageProperties, Configuration)
M: 0 C: 5
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: 89
95%
M: 3 C: 5
63%
M: 3 C: 2
40%
M: 2 C: 21
91%
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: 16 C: 24
60%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 5
71%
M: 0 C: 1
100%
writeToFile()
M: 16 C: 11
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(OntologyStorageProperties storageProperties, Configuration configuration)
63: throws OwlapiDriverException {
64: super(storageProperties, configuration);
65: }
66:
67: @Override
68: protected void initializeConnector() throws OwlapiDriverException {
69:• if (isOpen()) {
70: return;
71: }
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 {}.", storageProperties.getPhysicalURI());
106: try {
107: this.ontology = ontologyManager.createOntology(IRI.create(storageProperties.getOntologyURI()));
108: ontology.saveOntology(IRI.create(storageProperties.getPhysicalURI()));
109: } catch (OWLOntologyCreationException | OWLOntologyStorageException e) {
110: throw new OwlapiDriverException("Unable to create ontology in " + storageProperties.getPhysicalURI(), e);
111: }
112: }
113:
114: private void initializeReasonerFactory() {
115: final String reasonerFactoryClass = configuration.getProperty(ConfigParam.REASONER_FACTORY_CLASS);
116:• if (reasonerFactoryClass == null) {
117: LOG.warn("Reasoner factory class not found. Reasoner won't be available.");
118: return;
119: }
120: try {
121: this.reasonerFactory = (OWLReasonerFactory) Class.forName(reasonerFactoryClass).newInstance();
122: } catch (InstantiationException | IllegalAccessException e) {
123: final String msg = "Unable to instantiate reasoner factory class " + reasonerFactoryClass;
124: LOG.error(msg);
125: throw new ReasonerNotAvailableException(msg, e);
126: } catch (ClassNotFoundException e) {
127: final String msg = "Reasoner factory class " + reasonerFactoryClass + " not found!";
128: LOG.error(msg);
129: throw new ReasonerNotAvailableException(msg, e);
130: }
131: }
132:
133: @Override
134: public OntologySnapshot getOntologySnapshot() {
135: ensureOpen();
136: READ.lock();
137: try {
138: final OWLOntologyManager m = OWLManager.createOWLOntologyManager();
139: final OWLOntology snapshot = m.copyOntology(ontology, OntologyCopy.DEEP);
140: return new OntologySnapshot(snapshot, m, m.getOWLDataFactory(), getReasoner(snapshot));
141: } catch (OWLOntologyCreationException e) {
142: throw new OntologySnapshotException("Unable to create ontology snapshot.", e);
143: } finally {
144: READ.unlock();
145: }
146: }
147:
148: private OntologySnapshot getLiveOntology() {
149: ensureOpen();
150: return new OntologySnapshot(ontology, ontologyManager, ontologyManager.getOWLDataFactory(), reasoner);
151: }
152:
153: @Override
154: public <R> R executeRead(Function<OntologySnapshot, R> function) {
155: ensureOpen();
156: READ.lock();
157: try {
158: return function.apply(getLiveOntology());
159: } finally {
160: READ.unlock();
161: }
162: }
163:
164: @Override
165: public void executeWrite(Consumer<OntologySnapshot> function) {
166: ensureOpen();
167: WRITE.lock();
168: try {
169: function.accept(getLiveOntology());
170: } finally {
171: WRITE.unlock();
172: }
173: }
174:
175: private OWLReasoner getReasoner(OWLOntology ontology) {
176:• if (reasonerFactory == null) {
177: LOG.warn(
178: "Creating ontology snapshot without reasoner, because reasoner factory class was not specified.");
179: return null;
180: }
181: return reasonerFactory.createReasoner(ontology);
182: }
183:
184: @Override
185: public void applyChanges(List<OWLOntologyChange> changes) {
186: ensureOpen();
187:• assert changes != null;
188: WRITE.lock();
189: try {
190: changes.stream().filter(ch -> ch instanceof MutableAxiomChange)
191: .forEach(ch -> ((MutableAxiomChange) ch).setOntology(ontology));
192: ontologyManager.applyChanges(changes);
193: try {
194: writeToFile();
195: } catch (OntologyStorageException e) {
196: LOG.error("Unable to write out ontology." + e);
197: }
198: } finally {
199: WRITE.unlock();
200: }
201: }
202:
203: @Override
204: public void close() throws OntoDriverException {
205:• if (!isOpen()) {
206: return;
207: }
208: WRITE.lock();
209: try {
210: writeToFile();
211: super.close();
212: } finally {
213: WRITE.unlock();
214: }
215: }
216:
217: private void writeToFile() throws OntologyStorageException {
218: try {
219: ontologyManager.saveOntology(ontology, IRI.create(storageProperties.getPhysicalURI()));
220: } catch (OWLOntologyStorageException e) {
221: throw new OntologyStorageException(
222: "Error when saving ontology to " + storageProperties.getPhysicalURI(), e);
223: }
224: }
225: }