Package: MainAxiomLoader
MainAxiomLoader
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
MainAxiomLoader(StorageConnector, InferredStorageConnector) |
|
|
|
|
|
||||||||||||||||||||
contains(Axiom, Set) |
|
|
|
|
|
||||||||||||||||||||
find(AxiomDescriptor) |
|
|
|
|
|
||||||||||||||||||||
isInferred(Axiom, Set) |
|
|
|
|
|
||||||||||||||||||||
mapAssertions(AxiomDescriptor, Map, Map) |
|
|
|
|
|
Coverage
1: /**
2: * Copyright (C) 2023 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;
16:
17: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
18: import cz.cvut.kbss.ontodriver.jena.connector.InferredStorageConnector;
19: import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
20: import cz.cvut.kbss.ontodriver.jena.util.JenaUtils;
21: import cz.cvut.kbss.ontodriver.model.Assertion;
22: import cz.cvut.kbss.ontodriver.model.Axiom;
23: import org.apache.jena.rdf.model.Property;
24: import org.apache.jena.rdf.model.RDFNode;
25: import org.apache.jena.rdf.model.Resource;
26: import org.apache.jena.rdf.model.ResourceFactory;
27:
28: import java.net.URI;
29: import java.util.Collection;
30: import java.util.HashMap;
31: import java.util.Map;
32: import java.util.Set;
33:
34: class MainAxiomLoader {
35:
36: private final AbstractAxiomLoader inferredLoader;
37: private final ExplicitAxiomLoader explicitLoader;
38:
39: MainAxiomLoader(StorageConnector connector, InferredStorageConnector inferredConnector) {
40: this.explicitLoader = new ExplicitAxiomLoader(connector);
41: // It is possible that the inferred connector is null - if we are using the read_committed strategy or only snapshot,
42: // without inference
43: this.inferredLoader = new InferredAxiomLoader(inferredConnector);
44: }
45:
46: /**
47: * Checks whether the storage contains the specified axiom.
48: *
49: * @param axiom Axiom whose existence should be verified
50: * @param contexts Contexts to search, optional (empty indicates the default context)
51: * @return {@code true} if the axiom exists, {@code false} otherwise
52: */
53: boolean contains(Axiom<?> axiom, Set<URI> contexts) {
54:• return axiom.getAssertion().isInferred() ? inferredLoader.contains(axiom, contexts) :
55: explicitLoader.contains(axiom, contexts);
56: }
57:
58: /**
59: * Checks whether the storage inferred the specified axiom.
60: * <p>
61: * Note that given the nature of the Jena API, this method will return {@code false} even if the statement is both
62: * asserted and inferred, as there is no way to easily ask only for inferred statements but both asserted and
63: * inferred statements are returned.
64: * <p>
65: * Also note that if the repository does not contain the statement at all, {@code false} is returned.
66: *
67: * @param axiom Axiom whose inference to check
68: * @param contexts Contexts to search, optional (empty indicates the default context)
69: * @return iff the specified statement is inferred in any of the specified contexts
70: */
71: boolean isInferred(Axiom<?> axiom, Set<URI> contexts) {
72: final Resource subject = ResourceFactory.createResource(axiom.getSubject().getIdentifier().toString());
73: final Property property = ResourceFactory.createProperty(axiom.getAssertion().getIdentifier().toString());
74: final RDFNode object = JenaUtils.valueToRdfNode(axiom.getAssertion(), axiom.getValue());
75:• return inferredLoader.contains(subject, property, object, contexts) && !explicitLoader.contains(subject, property, object, contexts);
76: }
77:
78: /**
79: * Loads axioms corresponding to the specified descriptor.
80: *
81: * @param descriptor Descriptor of axioms to load
82: * @return Matching axioms
83: */
84: Collection<Axiom<?>> find(AxiomDescriptor descriptor) {
85: final Map<String, Assertion> asserted = new HashMap<>(descriptor.getAssertions().size());
86: final Map<String, Assertion> inferred = new HashMap<>(descriptor.getAssertions().size());
87: mapAssertions(descriptor, asserted, inferred);
88: final Collection<Axiom<?>> result = explicitLoader.find(descriptor, asserted);
89: result.addAll(inferredLoader.find(descriptor, inferred));
90: return result;
91: }
92:
93: private static void mapAssertions(AxiomDescriptor descriptor, Map<String, Assertion> asserted,
94: Map<String, Assertion> inferred) {
95:• for (Assertion a : descriptor.getAssertions()) {
96:• if (a.isInferred()) {
97: inferred.put(a.getIdentifier().toString(), a);
98: } else {
99: asserted.put(a.getIdentifier().toString(), a);
100: }
101: }
102: }
103: }