Skip to content

Method: loadOneByOne(Collection)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
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: * <p>
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.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 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<IRI, 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 IRI context = SesameUtils.toSesameIri(descriptor.getAssertionContext(a), vf);
71: final IRI property = SesameUtils.toSesameIri(a.getIdentifier(), vf);
72:
73: final Collection<Statement> statements;
74: statements = connector.findStatements(subject, property, null, includeInferred, context);
75:• for (Statement s : statements) {
76: final Axiom<?> axiom = axiomBuilder.statementToAxiom(s, a);
77:• if (axiom != null) {
78: result.add(axiom);
79: }
80: }
81: }
82: return result;
83: }
84:
85: private Collection<Axiom<?>> loadAll(Map<IRI, Assertion> properties) throws SesameDriverException {
86: final Collection<Statement> statements = connector.findStatements(subject, null, null, includeInferred);
87: final Collection<Axiom<?>> result = new HashSet<>(statements.size());
88: final Assertion unspecified = Assertion.createUnspecifiedPropertyAssertion(includeInferred);
89: for (Statement s : statements) {
90: if (!properties.containsKey(s.getPredicate()) && !loadAll) {
91: continue;
92: }
93: final Assertion a = getAssertion(properties, s);
94: if (!contextMatches(a, s) && !(loadAll && contextMatches(unspecified, s))) {
95: continue;
96: }
97: final Axiom<?> axiom = axiomBuilder.statementToAxiom(s);
98: if (axiom != null) {
99: result.add(axiom);
100: }
101: }
102: return result;
103: }
104:
105: private Assertion getAssertion(Map<IRI, Assertion> properties, Statement s) {
106: if (properties.containsKey(s.getPredicate())) {
107: return properties.get(s.getPredicate());
108: }
109: return Assertion.createUnspecifiedPropertyAssertion(includeInferred);
110: }
111:
112: private boolean contextMatches(Assertion a, Statement s) {
113: final java.net.URI assertionCtx = descriptor.getAssertionContext(a);
114: final Resource statementContext = s.getContext();
115: if (assertionCtx == null) {
116: // If the assertion should be in default, we don't care about the context of the statement, because
117: // the default is a union of all the contexts
118: return true;
119: }
120: return statementContext != null && assertionCtx.toString().equals(statementContext.stringValue());
121: }
122: }