Skip to content

Package: LocalModel

LocalModel

nameinstructionbranchcomplexitylinemethod
LocalModel(boolean)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
addStatements(List, String)
M: 0 C: 40
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
addedDefault()
M: 0 C: 15
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
contains(Resource, Property, RDFNode, String)
M: 0 C: 38
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
enhanceStatements(Collection, Resource, Property, RDFNode, Model, Model)
M: 0 C: 25
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
enhanceStatements(Collection, Resource, Property, RDFNode, String)
M: 0 C: 26
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getAdded()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getContexts()
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getRemoved()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$addStatements$0(List, String)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$removeStatements$1(List, String)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
removeStatements(List, String)
M: 0 C: 40
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
removedDefault()
M: 0 C: 15
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
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.jena.connector;
16:
17: import org.apache.jena.query.Dataset;
18: import org.apache.jena.query.DatasetFactory;
19: import org.apache.jena.rdf.model.*;
20:
21: import java.util.*;
22:
23: /**
24: * Tracks transactional changes.
25: */
26: class LocalModel {
27:
28: private final Dataset added;
29: private final Dataset removed;
30:
31: /**
32: * Whether default graph should be treated as union of all graphs.
33: */
34: private final boolean defaultAsUnion;
35:
36: enum Containment {
37: ADDED, REMOVED, UNKNOWN
38: }
39:
40: LocalModel(boolean defaultAsUnion) {
41: this.added = DatasetFactory.create();
42: this.removed = DatasetFactory.create();
43: this.defaultAsUnion = defaultAsUnion;
44: }
45:
46: Collection<Statement> enhanceStatements(Collection<Statement> statements, Resource subject, Property property,
47: RDFNode value, String context) {
48:• if (context != null) {
49: return enhanceStatements(statements, subject, property, value, added.getNamedModel(context),
50: removed.getNamedModel(context));
51: } else {
52: return enhanceStatements(statements, subject, property, value, addedDefault(), removedDefault());
53: }
54: }
55:
56: private Model addedDefault() {
57:• return defaultAsUnion ? added.getUnionModel().union(added.getDefaultModel()) : added.getDefaultModel();
58: }
59:
60: private Model removedDefault() {
61:• return defaultAsUnion ? removed.getUnionModel().union(removed.getDefaultModel()) : removed.getDefaultModel();
62: }
63:
64: private static Collection<Statement> enhanceStatements(Collection<Statement> toEnhance, Resource subject,
65: Property property, RDFNode value, Model addedModel,
66: Model removedModel) {
67: final Set<Statement> statements = new HashSet<>(toEnhance);
68: statements.addAll(addedModel.listStatements(subject, property, value).toList());
69: statements.removeAll(removedModel.listStatements(subject, property, value).toList());
70: return statements;
71: }
72:
73: Containment contains(Resource subject, Property property, RDFNode value, String context) {
74:• final Model removedModel = context != null ? removed.getNamedModel(context) : removedDefault();
75:• final Model addedModel = context != null ? added.getNamedModel(context) : addedDefault();
76:• if (removedModel.contains(subject, property, value)) {
77: return Containment.REMOVED;
78: } else {
79:• return addedModel.contains(subject, property, value) ? Containment.ADDED :
80: Containment.UNKNOWN;
81: }
82: }
83:
84: void addStatements(List<Statement> statements, String context) {
85:• if (context != null) {
86: added.getNamedModel(context).add(statements);
87: removed.getNamedModel(context).remove(statements);
88: } else {
89: added.getDefaultModel().add(statements);
90: removed.getDefaultModel().remove(statements);
91:• if (defaultAsUnion) {
92: removed.listNames().forEachRemaining(n -> removed.getNamedModel(n).remove(statements));
93: }
94: }
95: }
96:
97: void removeStatements(List<Statement> statements, String context) {
98:• if (context != null) {
99: removed.getNamedModel(context).add(statements);
100: added.getNamedModel(context).remove(statements);
101: } else {
102: removed.getDefaultModel().add(statements);
103: added.getDefaultModel().remove(statements);
104:• if (defaultAsUnion) {
105: added.listNames().forEachRemaining(n -> added.getNamedModel(n).remove(statements));
106: }
107: }
108: }
109:
110: Dataset getAdded() {
111: return added;
112: }
113:
114: Dataset getRemoved() {
115: return removed;
116: }
117:
118: List<String> getContexts() {
119: final Iterator<String> it = added.listNames();
120: final List<String> contexts = new ArrayList<>();
121: it.forEachRemaining(contexts::add);
122: return contexts;
123: }
124: }