Skip to content

Package: EpistemicAxiomRemover

EpistemicAxiomRemover

nameinstructionbranchcomplexitylinemethod
EpistemicAxiomRemover(Connector, ValueFactory)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
lambda$remove$0(URI)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
remove(AbstractAxiomDescriptor)
M: 0 C: 53
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
remove(NamedResource, Map, URI)
M: 11 C: 76
87%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 1 C: 14
93%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.ontodriver.rdf4j;
14:
15: import cz.cvut.kbss.ontodriver.descriptor.AbstractAxiomDescriptor;
16: import cz.cvut.kbss.ontodriver.model.Assertion;
17: import cz.cvut.kbss.ontodriver.model.NamedResource;
18: import cz.cvut.kbss.ontodriver.model.Value;
19: import cz.cvut.kbss.ontodriver.rdf4j.connector.Connector;
20: import cz.cvut.kbss.ontodriver.rdf4j.exception.Rdf4jDriverException;
21: import org.eclipse.rdf4j.model.IRI;
22: import org.eclipse.rdf4j.model.Resource;
23: import org.eclipse.rdf4j.model.Statement;
24: import org.eclipse.rdf4j.model.ValueFactory;
25:
26: import java.util.*;
27: import java.util.stream.Collectors;
28:
29: import static cz.cvut.kbss.ontodriver.rdf4j.util.Rdf4jUtils.toRdf4jIri;
30:
31: /**
32: * This class performs an epistemic remove of axioms described by the axiom descriptor.
33: * <p/>
34: * Epistemic remove means that only information known to the application is deleted. The assertions in the descriptor
35: * represent this information. Thus, only these assertions are removed from the ontology. Note that if the descriptor
36: * contains an unspecified property assertion, all property assertions related to the subject individual are removed
37: * from the property's context.
38: */
39: class EpistemicAxiomRemover {
40:
41: private final Connector connector;
42: private final ValueFactory valueFactory;
43:
44: EpistemicAxiomRemover(Connector connector, ValueFactory valueFactory) {
45: this.connector = connector;
46: this.valueFactory = valueFactory;
47: }
48:
49: void remove(AbstractAxiomDescriptor axiomDescriptor) throws Rdf4jDriverException {
50: final Resource individual = toRdf4jIri(axiomDescriptor.getSubject(), valueFactory);
51: final Collection<Statement> toRemove = new HashSet<>();
52:• for (Assertion a : axiomDescriptor.getAssertions()) {
53: final Set<IRI> contexts = axiomDescriptor.getAssertionContexts(a).stream()
54: .map(uri -> toRdf4jIri(uri, valueFactory))
55: .collect(Collectors.toSet());
56: toRemove.addAll(connector.findStatements(individual,
57: toRdf4jIri(a, valueFactory), null, a.isInferred(),
58: contexts));
59: }
60: connector.removeStatements(toRemove);
61: }
62:
63: void remove(NamedResource individual, Map<Assertion, Set<Value<?>>> values, java.net.URI context)
64: throws Rdf4jDriverException {
65: final IRI repoContext = toRdf4jIri(context, valueFactory);
66: final Resource subject = toRdf4jIri(individual.getIdentifier(), valueFactory);
67: final Collection<Statement> toRemove = new ArrayList<>();
68: final ValueConverter valueConverter = new ValueConverter(valueFactory);
69:• for (Map.Entry<Assertion, Set<Value<?>>> entry : values.entrySet()) {
70: final IRI property = toRdf4jIri(entry.getKey(), valueFactory);
71:• for (Value<?> val : entry.getValue()) {
72: final org.eclipse.rdf4j.model.Value rdf4jValue = valueConverter.toRdf4jValue(entry.getKey(), val);
73:• if (repoContext != null) {
74: toRemove.add(valueFactory.createStatement(subject, property, rdf4jValue, repoContext));
75: } else {
76: toRemove.add(valueFactory.createStatement(subject, property, rdf4jValue));
77: }
78: }
79: }
80: connector.removeStatements(toRemove);
81: }
82: }