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