Skip to content

Package: EpistemicAxiomRemover

EpistemicAxiomRemover

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