Skip to contentMethod: getRemovedStatements()
1: /**
2: * Copyright (C) 2022 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.rdf4j.connector;
16:
17: import org.eclipse.rdf4j.model.*;
18: import org.eclipse.rdf4j.model.impl.LinkedHashModel;
19:
20: import java.util.Collection;
21:
22: /**
23: * Caches local transactional changes to the RDF4J repository model.
24: */
25: class LocalModel {
26:
27: private final Model addedStatements;
28: private final Model removedStatements;
29:
30: enum Contains {
31: TRUE, FALSE, UNKNOWN
32: }
33:
34: LocalModel() {
35: this.addedStatements = new LinkedHashModel();
36: this.removedStatements = new LinkedHashModel();
37: }
38:
39: void enhanceStatements(Collection<Statement> statements, Resource subject, IRI property,
40: Value object, Collection<IRI> context) {
41: final Collection<Statement> added, removed;
42: final IRI[] ctxArray = context.toArray(new IRI[0]);
43: added = addedStatements.filter(subject, property, object, ctxArray);
44: removed = removedStatements.filter(subject, property, object, ctxArray);
45: statements.addAll(added);
46: statements.removeAll(removed);
47: }
48:
49: Contains contains(Resource subject, IRI property, Value object, Collection<IRI> contexts) {
50: final IRI[] ctxArray = contexts.toArray(new IRI[0]);
51: if (addedStatements.contains(subject, property, object, ctxArray)) {
52: return Contains.TRUE;
53: }
54: return removedStatements.contains(subject, property, object, ctxArray) ? Contains.FALSE : Contains.UNKNOWN;
55: }
56:
57: void addStatements(Collection<Statement> statements) {
58: removedStatements.removeAll(statements);
59: addedStatements.addAll(statements);
60: }
61:
62: void removeStatements(Collection<Statement> statements) {
63: addedStatements.removeAll(statements);
64: removedStatements.addAll(statements);
65: }
66:
67: Collection<Statement> getAddedStatements() {
68: return addedStatements;
69: }
70:
71: Collection<Statement> getRemovedStatements() {
72: return removedStatements;
73: }
74: }