Skip to content

Package: StatementLoader

StatementLoader

nameinstructionbranchcomplexitylinemethod
StatementLoader(Connector, Resource, AxiomBuilder)
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
contextMatches(Set, Statement, Assertion)
M: 0 C: 20
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getAssertion(Map, Statement)
M: 0 C: 15
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
lambda$loadAxioms$1(URI)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$loadOneByOne$0(URI)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
loadAll(AxiomDescriptor, Map)
M: 0 C: 78
100%
M: 0 C: 14
100%
M: 0 C: 8
100%
M: 0 C: 15
100%
M: 0 C: 1
100%
loadAxioms(AxiomDescriptor, Map)
M: 0 C: 25
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
loadAxioms(Set)
M: 0 C: 31
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
loadOneByOne(AxiomDescriptor, Map)
M: 0 C: 70
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
resolveContexts(AxiomDescriptor, Assertion)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
setIncludeInferred(boolean)
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%
setLoadAllThreshold(int)
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%

Coverage

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.ontodriver.rdf4j.loader;
14:
15: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
16: import cz.cvut.kbss.ontodriver.model.Assertion;
17: import cz.cvut.kbss.ontodriver.model.Axiom;
18: import cz.cvut.kbss.ontodriver.rdf4j.config.Constants;
19: import cz.cvut.kbss.ontodriver.rdf4j.connector.Connector;
20: import cz.cvut.kbss.ontodriver.rdf4j.exception.Rdf4jDriverException;
21: import cz.cvut.kbss.ontodriver.rdf4j.util.AxiomBuilder;
22: import cz.cvut.kbss.ontodriver.rdf4j.util.Rdf4jUtils;
23: import org.eclipse.rdf4j.model.IRI;
24: import org.eclipse.rdf4j.model.Resource;
25: import org.eclipse.rdf4j.model.Statement;
26: import org.eclipse.rdf4j.model.ValueFactory;
27:
28: import java.net.URI;
29: import java.util.Collection;
30: import java.util.HashSet;
31: import java.util.Map;
32: import java.util.Set;
33: import java.util.stream.Collectors;
34:
35: public class StatementLoader {
36:
37: private final Connector connector;
38: private final Resource subject;
39: private final ValueFactory vf;
40: private final AxiomBuilder axiomBuilder;
41:
42: private int loadAllThreshold = Constants.DEFAULT_LOAD_ALL_THRESHOLD;
43: private boolean loadAll;
44: boolean includeInferred;
45:
46: public StatementLoader(Connector connector, Resource subject, AxiomBuilder axiomBuilder) {
47: this.connector = connector;
48: this.vf = connector.getValueFactory();
49: this.subject = subject;
50: this.axiomBuilder = axiomBuilder;
51: }
52:
53: public void setLoadAllThreshold(int loadAllThreshold) {
54: this.loadAllThreshold = loadAllThreshold;
55: }
56:
57: public void setIncludeInferred(boolean includeInferred) {
58: this.includeInferred = includeInferred;
59: }
60:
61: public Collection<Axiom<?>> loadAxioms(AxiomDescriptor descriptor,
62: Map<IRI, Assertion> properties) throws Rdf4jDriverException {
63: this.loadAll = properties.containsValue(Assertion.createUnspecifiedPropertyAssertion(includeInferred));
64:• if (properties.size() < loadAllThreshold && !loadAll) {
65: return loadOneByOne(descriptor, properties);
66: } else {
67: return loadAll(descriptor, properties);
68: }
69: }
70:
71: private Collection<Axiom<?>> loadOneByOne(AxiomDescriptor descriptor,
72: Map<IRI, Assertion> assertions) throws Rdf4jDriverException {
73: final Collection<Axiom<?>> result = new HashSet<>();
74:• for (Map.Entry<IRI, Assertion> e : assertions.entrySet()) {
75: final Set<IRI> contexts = resolveContexts(descriptor, e.getValue()).stream()
76: .map(uri -> Rdf4jUtils.toRdf4jIri(uri,
77: vf))
78: .collect(Collectors.toSet());
79:
80: final Collection<Statement> statements;
81: statements = connector.findStatements(subject, e.getKey(), null, includeInferred, contexts);
82:• for (Statement s : statements) {
83: final Axiom<?> axiom = axiomBuilder.statementToAxiom(s, e.getValue());
84:• if (axiom != null) {
85: result.add(axiom);
86: }
87: }
88: }
89: return result;
90: }
91:
92: protected Set<URI> resolveContexts(AxiomDescriptor descriptor, Assertion a) {
93: return descriptor.getAssertionContexts(a);
94: }
95:
96: private Collection<Axiom<?>> loadAll(AxiomDescriptor descriptor,
97: Map<IRI, Assertion> properties) throws Rdf4jDriverException {
98: final Collection<Statement> statements = connector.findStatements(subject, null, null, includeInferred);
99: final Collection<Axiom<?>> result = new HashSet<>(statements.size());
100: final Assertion unspecified = Assertion.createUnspecifiedPropertyAssertion(includeInferred);
101:• for (Statement s : statements) {
102:• if (!properties.containsKey(s.getPredicate()) && !loadAll) {
103: continue;
104: }
105: final Assertion a = getAssertion(properties, s);
106:• if (!contextMatches(descriptor.getAssertionContexts(a), s, a) &&
107:• !(loadAll && contextMatches(descriptor.getAssertionContexts(unspecified), s, a))) {
108: continue;
109: }
110: final Axiom<?> axiom = axiomBuilder.statementToAxiom(s);
111:• if (axiom != null) {
112: result.add(axiom);
113: }
114: }
115: return result;
116: }
117:
118: private Assertion getAssertion(Map<IRI, Assertion> properties, Statement s) {
119:• if (properties.containsKey(s.getPredicate())) {
120: return properties.get(s.getPredicate());
121: }
122: return Assertion.createUnspecifiedPropertyAssertion(includeInferred);
123: }
124:
125: protected boolean contextMatches(Set<URI> assertionCtx, Statement s, Assertion a) {
126:• if (assertionCtx.isEmpty()) {
127: // If the assertion should be in default, we don't care about the context of the statement, because
128: // the default is a union of all the contexts
129: return true;
130: }
131: final Resource statementContext = s.getContext();
132:• return statementContext != null && assertionCtx.contains(URI.create(statementContext.stringValue()));
133: }
134:
135: public Collection<Axiom<?>> loadAxioms(Set<URI> contexts) throws Rdf4jDriverException {
136: final Collection<Statement> statements =
137: connector.findStatements(subject, null, null, includeInferred, contexts.stream()
138: .map(uri -> Rdf4jUtils.toRdf4jIri(
139: uri, vf))
140: .collect(Collectors.toSet()));
141: return statements.stream().map(axiomBuilder::statementToAxiom).collect(Collectors.toSet());
142: }
143: }