Skip to content

Package: StatementLoader

StatementLoader

nameinstructionbranchcomplexitylinemethod
StatementLoader(RuntimeConfiguration, AxiomDescriptor, Connector, Resource, AxiomBuilder)
M: 0 C: 23
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
contextMatches(Assertion, Statement)
M: 0 C: 24
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 5
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%
loadAll(Map)
M: 0 C: 72
100%
M: 0 C: 14
100%
M: 0 C: 8
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
loadAxioms(Map)
M: 0 C: 24
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
loadOneByOne(Collection)
M: 0 C: 65
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 12
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%

Coverage

1: /**
2: * Copyright (C) 2020 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.sesame;
16:
17: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
18: import cz.cvut.kbss.ontodriver.model.Assertion;
19: import cz.cvut.kbss.ontodriver.model.Axiom;
20: import cz.cvut.kbss.ontodriver.sesame.config.RuntimeConfiguration;
21: import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
22: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
23: import cz.cvut.kbss.ontodriver.sesame.util.AxiomBuilder;
24: import cz.cvut.kbss.ontodriver.sesame.util.SesameUtils;
25: import org.eclipse.rdf4j.model.IRI;
26: import org.eclipse.rdf4j.model.Resource;
27: import org.eclipse.rdf4j.model.Statement;
28: import org.eclipse.rdf4j.model.ValueFactory;
29:
30: import java.util.Collection;
31: import java.util.HashSet;
32: import java.util.Map;
33:
34: class StatementLoader {
35:
36: private final AxiomDescriptor descriptor;
37: private final Connector connector;
38: private final Resource subject;
39: private final ValueFactory vf;
40: private final AxiomBuilder axiomBuilder;
41:
42: private final int loadAllThreshold;
43: private boolean loadAll;
44: private boolean includeInferred;
45:
46: StatementLoader(RuntimeConfiguration config, AxiomDescriptor descriptor, Connector connector, Resource subject,
47: AxiomBuilder axiomBuilder) {
48: this.loadAllThreshold = config.getLoadAllThreshold();
49: this.descriptor = descriptor;
50: this.connector = connector;
51: this.vf = connector.getValueFactory();
52: this.subject = subject;
53: this.axiomBuilder = axiomBuilder;
54: }
55:
56: void setIncludeInferred(boolean includeInferred) {
57: this.includeInferred = includeInferred;
58: }
59:
60: Collection<Axiom<?>> loadAxioms(Map<IRI, Assertion> properties)
61: throws SesameDriverException {
62: this.loadAll = properties.containsValue(Assertion.createUnspecifiedPropertyAssertion(includeInferred));
63:• if (properties.size() < loadAllThreshold && !loadAll) {
64: return loadOneByOne(properties.values());
65: } else {
66: return loadAll(properties);
67: }
68: }
69:
70: private Collection<Axiom<?>> loadOneByOne(Collection<Assertion> assertions) throws SesameDriverException {
71: final Collection<Axiom<?>> result = new HashSet<>();
72:• for (Assertion a : assertions) {
73: final IRI context = SesameUtils.toSesameIri(descriptor.getAssertionContext(a), vf);
74: final IRI property = SesameUtils.toSesameIri(a.getIdentifier(), vf);
75:
76: final Collection<Statement> statements;
77: statements = connector.findStatements(subject, property, null, includeInferred, context);
78:• for (Statement s : statements) {
79: final Axiom<?> axiom = axiomBuilder.statementToAxiom(s, a);
80:• if (axiom != null) {
81: result.add(axiom);
82: }
83: }
84: }
85: return result;
86: }
87:
88: private Collection<Axiom<?>> loadAll(Map<IRI, Assertion> properties) throws SesameDriverException {
89: final Collection<Statement> statements = connector.findStatements(subject, null, null, includeInferred);
90: final Collection<Axiom<?>> result = new HashSet<>(statements.size());
91: final Assertion unspecified = Assertion.createUnspecifiedPropertyAssertion(includeInferred);
92:• for (Statement s : statements) {
93:• if (!properties.containsKey(s.getPredicate()) && !loadAll) {
94: continue;
95: }
96: final Assertion a = getAssertion(properties, s);
97:• if (!contextMatches(a, s) && !(loadAll && contextMatches(unspecified, s))) {
98: continue;
99: }
100: final Axiom<?> axiom = axiomBuilder.statementToAxiom(s);
101:• if (axiom != null) {
102: result.add(axiom);
103: }
104: }
105: return result;
106: }
107:
108: private Assertion getAssertion(Map<IRI, Assertion> properties, Statement s) {
109:• if (properties.containsKey(s.getPredicate())) {
110: return properties.get(s.getPredicate());
111: }
112: return Assertion.createUnspecifiedPropertyAssertion(includeInferred);
113: }
114:
115: private boolean contextMatches(Assertion a, Statement s) {
116: final java.net.URI assertionCtx = descriptor.getAssertionContext(a);
117: final Resource statementContext = s.getContext();
118:• if (assertionCtx == null) {
119: // If the assertion should be in default, we don't care about the context of the statement, because
120: // the default is a union of all the contexts
121: return true;
122: }
123:• return statementContext != null && assertionCtx.toString().equals(statementContext.stringValue());
124: }
125: }