-
Notifications
You must be signed in to change notification settings - Fork 14
/
MainAxiomLoader.java
104 lines (94 loc) · 4.15 KB
/
MainAxiomLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* Copyright (C) 2019 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.ontodriver.jena;
import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
import cz.cvut.kbss.ontodriver.jena.connector.InferredStorageConnector;
import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.Axiom;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import java.net.URI;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
class MainAxiomLoader {
private final AbstractAxiomLoader inferredLoader;
private final ExplicitAxiomLoader explicitLoader;
MainAxiomLoader(StorageConnector connector, InferredStorageConnector inferredConnector) {
this.explicitLoader = new ExplicitAxiomLoader(connector);
// It is possible that the inferred connector is null - if we are using the read_committed strategy or only snapshot,
// without inference
this.inferredLoader = new InferredAxiomLoader(inferredConnector);
}
/**
* Checks whether the storage contains the specified axiom.
*
* @param axiom Axiom whose existence should be verified
* @param context Context to search, optional
* @return {@code true} if the axiom exists, {@code false} otherwise
*/
boolean contains(Axiom<?> axiom, URI context) {
return axiom.getAssertion().isInferred() ? inferredLoader.contains(axiom, context) :
explicitLoader.contains(axiom, context);
}
/**
* Loads axioms corresponding to the specified descriptor.
*
* @param descriptor Descriptor of axioms to load
* @return Matching axioms
*/
Collection<Axiom<?>> find(AxiomDescriptor descriptor) {
final Map<String, Assertion> asserted = new HashMap<>(descriptor.getAssertions().size());
final Map<String, Assertion> inferred = new HashMap<>(descriptor.getAssertions().size());
mapAssertions(descriptor, asserted, inferred);
final Collection<Axiom<?>> result = explicitLoader.find(descriptor, asserted);
result.addAll(inferredLoader.find(descriptor, inferred));
return result;
}
private static void mapAssertions(AxiomDescriptor descriptor, Map<String, Assertion> asserted,
Map<String, Assertion> inferred) {
for (Assertion a : descriptor.getAssertions()) {
if (a.isInferred()) {
inferred.put(a.getIdentifier().toString(), a);
} else {
asserted.put(a.getIdentifier().toString(), a);
}
}
}
/**
* Loads all asserted property statements with the specified subject.
* <p>
* Note that type assertion statements (those with property {@code rdf:type}) are skipped.
*
* @param subject Statement subject
* @param context Context identifier, optional
* @return Matching statements
*/
Collection<Axiom<?>> find(NamedResource subject, URI context) {
return explicitLoader.find(subject, context);
}
/**
* Loads all property statements with the specified subject, including inferred ones.
* <p>
* Note that type assertion statements (those with property {@code rdf:type}) are skipped.
*
* @param subject Statement subject
* @param context Context identifier, optional
* @return Matching statements
*/
Collection<Axiom<?>> findWithInference(NamedResource subject, URI context) {
return inferredLoader.find(subject, context);
}
}