Package: SnapshotStorageWithInference
SnapshotStorageWithInference
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
SnapshotStorageWithInference(DriverConfiguration, Map) |
|
|
|
|
|
||||||||||||||||||||
addCentralData(Dataset) |
|
|
|
|
|
||||||||||||||||||||
checkConsistency(String) |
|
|
|
|
|
||||||||||||||||||||
cloneModel(Model) |
|
|
|
|
|
||||||||||||||||||||
createReasoner() |
|
|
|
|
|
||||||||||||||||||||
getDefaultGraph() |
|
|
|
|
|
||||||||||||||||||||
getNamedGraph(String) |
|
|
|
|
|
||||||||||||||||||||
getRawDefaultGraph() |
|
|
|
|
|
||||||||||||||||||||
getRawNamedGraph(String) |
|
|
|
|
|
||||||||||||||||||||
initReasonerFactory(DriverConfiguration) |
|
|
|
|
|
||||||||||||||||||||
lambda$addCentralData$1(Dataset) |
|
|
|
|
|
||||||||||||||||||||
lambda$createReasoner$2(Reasoner, String, String) |
|
|
|
|
|
||||||||||||||||||||
lambda$getNamedGraph$3(String, String) |
|
|
|
|
|
||||||||||||||||||||
lambda$new$0(Map.Entry) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: /**
2: * Copyright (C) 2022 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.jena.connector;
16:
17: import cz.cvut.kbss.ontodriver.config.DriverConfigParam;
18: import cz.cvut.kbss.ontodriver.config.DriverConfiguration;
19: import cz.cvut.kbss.ontodriver.jena.exception.ReasonerInitializationException;
20: import org.apache.jena.query.Dataset;
21: import org.apache.jena.query.DatasetFactory;
22: import org.apache.jena.rdf.model.InfModel;
23: import org.apache.jena.rdf.model.Model;
24: import org.apache.jena.rdf.model.ModelFactory;
25: import org.apache.jena.rdf.model.Property;
26: import org.apache.jena.reasoner.IllegalParameterException;
27: import org.apache.jena.reasoner.Reasoner;
28: import org.apache.jena.reasoner.ReasonerFactory;
29: import org.apache.jena.reasoner.ValidityReport;
30: import org.apache.jena.system.Txn;
31: import org.apache.jena.vocabulary.ReasonerVocabulary;
32:
33: import java.lang.reflect.InvocationTargetException;
34: import java.lang.reflect.Method;
35: import java.util.*;
36: import java.util.stream.Collectors;
37:
38: import static org.apache.jena.rdf.model.ResourceFactory.createProperty;
39:
40: class SnapshotStorageWithInference extends SnapshotStorage {
41:
42: /**
43: * Configuration parameters supported by at least one of the Jena reasoners. Used to pre-filter reasoner config.
44: */
45: private static final Set<String> SUPPORTED_CONFIG = new HashSet<>(Arrays.asList(
46: ReasonerVocabulary.PROPderivationLogging.getURI(),
47: ReasonerVocabulary.PROPenableCMPScan.getURI(),
48: ReasonerVocabulary.PROPenableFunctorFiltering.getURI(),
49: ReasonerVocabulary.PROPenableOWLTranslation.getURI(),
50: ReasonerVocabulary.PROPenableTGCCaching.getURI(),
51: ReasonerVocabulary.PROPruleMode.getURI(),
52: ReasonerVocabulary.PROPruleSet.getURI(),
53: ReasonerVocabulary.PROPsetRDFSLevel.getURI(),
54: ReasonerVocabulary.PROPtraceOn.getURI()
55: ));
56:
57: private final ReasonerFactory reasonerFactory;
58: private final Map<String, String> reasonerConfig;
59:
60: private final Map<String, InfModel> inferredGraphs = new HashMap<>();
61:
62: SnapshotStorageWithInference(DriverConfiguration configuration, Map<String, String> reasonerConfig) {
63: super(configuration);
64: this.reasonerFactory = initReasonerFactory(configuration);
65: this.reasonerConfig = reasonerConfig.entrySet().stream()
66: .filter(e -> SUPPORTED_CONFIG.contains(e.getKey()))
67: .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
68: this.dataset = DatasetFactory.createGeneral();
69: }
70:
71: private static ReasonerFactory initReasonerFactory(DriverConfiguration configuration) {
72: final String factoryClass = configuration.getProperty(DriverConfigParam.REASONER_FACTORY_CLASS, "");
73: LOG.trace("Creating reasoner using reasoner factory class {}.", factoryClass);
74: try {
75: final Class<? extends ReasonerFactory> rfClass =
76: (Class<? extends ReasonerFactory>) Class.forName(factoryClass);
77: final Method instanceMethod = rfClass.getMethod("theInstance");
78: return (ReasonerFactory) instanceMethod.invoke(null);
79: } catch (ClassNotFoundException e) {
80: throw new ReasonerInitializationException("Reasoner factory class " + factoryClass + " not found.", e);
81: } catch (NoSuchMethodException e) {
82: throw new ReasonerInitializationException("Class " + factoryClass +
83: " is not a ReasonerFactory implementation or does not contain static 'theInstance' method.");
84: } catch (IllegalAccessException | InvocationTargetException e) {
85: throw new ReasonerInitializationException(
86: "Unable to instantiate Jena reasoner from factory " + factoryClass, e);
87: }
88: }
89:
90: @Override
91: void addCentralData(Dataset central) {
92: Txn.executeRead(central, () -> {
93: final Iterator<String> it = central.listNames();
94: InfModel clonedModel;
95:• while (it.hasNext()) {
96: final String name = it.next();
97: clonedModel = cloneModel(central.getNamedModel(name));
98: inferredGraphs.put(name, clonedModel);
99: dataset.addNamedModel(name, clonedModel);
100: }
101: clonedModel = cloneModel(central.getDefaultModel());
102: inferredGraphs.put(null, clonedModel);
103: dataset.setDefaultModel(clonedModel);
104: });
105: }
106:
107: private InfModel cloneModel(Model model) {
108: return ModelFactory.createInfModel(createReasoner(), ModelFactory.createDefaultModel().add(model));
109: }
110:
111: @Override
112: public InfModel getDefaultGraph() {
113: return inferredGraphs.get(null);
114: }
115:
116: private Reasoner createReasoner() {
117: final Reasoner reasoner = reasonerFactory.create(null);
118: reasonerConfig.forEach((key, value) -> {
119: final Property prop = createProperty(key);
120: try {
121: reasoner.setParameter(prop, value);
122: } catch (IllegalParameterException ex) {
123: LOG.error("Failed to set property " + prop + " on reasoner.", ex);
124: }
125: });
126: return reasoner;
127: }
128:
129: Model getRawDefaultGraph() {
130:• return inferredGraphs.containsKey(null) ? inferredGraphs.get(null).getRawModel() : dataset.getDefaultModel();
131: }
132:
133: @Override
134: public InfModel getNamedGraph(String context) {
135: return inferredGraphs.computeIfAbsent(context, c -> {
136: // If the context does not exist, we need to create it, so that the default Dataset behavior is preserved
137: final InfModel model = ModelFactory.createInfModel(createReasoner(), ModelFactory.createDefaultModel());
138: dataset.addNamedModel(context, model);
139: return model;
140: });
141: }
142:
143: Model getRawNamedGraph(String context) {
144:• return inferredGraphs.containsKey(context) ? inferredGraphs.get(context).getRawModel() :
145: dataset.getNamedModel(context);
146: }
147:
148: ValidityReport checkConsistency(String context) {
149:• return context != null ? getNamedGraph(context).validate() : getDefaultGraph().validate();
150: }
151: }