Skip to content

Package: StatementLoader

StatementLoader

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