Skip to contentMethod: removeClassAssertionAxioms(OWLNamedIndividual, Set)
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.owlapi;
16:
17: import cz.cvut.kbss.ontodriver.descriptor.AbstractAxiomDescriptor;
18: import cz.cvut.kbss.ontodriver.model.Assertion;
19: import cz.cvut.kbss.ontodriver.model.NamedResource;
20: import cz.cvut.kbss.ontodriver.model.Value;
21: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
22: import cz.cvut.kbss.ontodriver.owlapi.util.MutableRemoveAxiom;
23: import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
24: import org.semanticweb.owlapi.model.*;
25: import org.semanticweb.owlapi.search.EntitySearcher;
26:
27: import java.util.*;
28: import java.util.stream.Collectors;
29: import java.util.stream.Stream;
30:
31: class EpistemicAxiomRemover {
32:
33: private final OwlapiAdapter owlapiAdapter;
34: private final OWLOntology ontology;
35: private final OWLDataFactory dataFactory;
36: private final OntologySnapshot snapshot;
37:
38: EpistemicAxiomRemover(OwlapiAdapter adapter, OntologySnapshot snapshot) {
39: this.owlapiAdapter = adapter;
40: this.snapshot = snapshot;
41: this.ontology = snapshot.getOntology();
42: this.dataFactory = snapshot.getDataFactory();
43: }
44:
45: void remove(AbstractAxiomDescriptor descriptor) {
46: final List<OWLOntologyChange> changes = new ArrayList<>();
47: final OWLNamedIndividual individual = OwlapiUtils.getIndividual(descriptor.getSubject(), dataFactory);
48: for (Assertion a : descriptor.getAssertions()) {
49: switch (a.getType()) {
50: case CLASS:
51: changes.addAll(removeClassAssertionAxioms(individual));
52: break;
53: case DATA_PROPERTY:
54: changes.addAll(removeDataPropertyAssertions(individual, a));
55: break;
56: case OBJECT_PROPERTY:
57: changes.addAll(removeObjectPropertyAssertions(individual, a));
58: break;
59: case ANNOTATION_PROPERTY:
60: changes.addAll(removeAnnotationAssertions(individual, a));
61: break;
62: default:
63: break;
64: }
65: }
66: if (!changes.isEmpty()) {
67: owlapiAdapter.addTransactionalChanges(snapshot.applyChanges(changes));
68: }
69: }
70:
71: private Collection<OWLOntologyChange> removeClassAssertionAxioms(OWLNamedIndividual individual) {
72: return EntitySearcher.getTypes(individual, ontology).map(cls -> new MutableRemoveAxiom(ontology,
73: dataFactory.getOWLClassAssertionAxiom(cls, individual))).collect(Collectors.toList());
74: }
75:
76: private Collection<OWLOntologyChange> removeClassAssertionAxioms(OWLNamedIndividual individual,
77: Set<Value<?>> values) {
78: return values.stream().map(value -> {
79: final OWLClass owlClass = dataFactory.getOWLClass(IRI.create(value.stringValue()));
80: return new MutableRemoveAxiom(ontology, dataFactory.getOWLClassAssertionAxiom(owlClass, individual));
81: }).collect(Collectors.toList());
82: }
83:
84: private Collection<? extends OWLOntologyChange> removeDataPropertyAssertions(OWLNamedIndividual individual,
85: Assertion assertion) {
86: final OWLDataProperty dataProperty = dataFactory.getOWLDataProperty(IRI.create(assertion.getIdentifier()));
87: final Stream<OWLLiteral> values = EntitySearcher.getDataPropertyValues(individual, dataProperty, ontology);
88: return values.map(value -> new MutableRemoveAxiom(ontology,
89: dataFactory.getOWLDataPropertyAssertionAxiom(dataProperty, individual, value)))
90: .collect(Collectors.toList());
91: }
92:
93: private Collection<? extends OWLOntologyChange> removeDataPropertyAssertions(OWLNamedIndividual individual,
94: Assertion assertion,
95: Set<Value<?>> values) {
96: final OWLDataProperty dataProperty = dataFactory.getOWLDataProperty(IRI.create(assertion.getIdentifier()));
97: return values.stream().map(value -> {
98: final OWLLiteral literal = OwlapiUtils
99: .createOWLLiteralFromValue(value.getValue(),
100: OwlapiUtils.getAssertionLanguage(assertion));
101: return new MutableRemoveAxiom(ontology,
102: dataFactory.getOWLDataPropertyAssertionAxiom(dataProperty, individual, literal));
103: }).collect(Collectors.toList());
104: }
105:
106: private Collection<? extends OWLOntologyChange> removeObjectPropertyAssertions(OWLNamedIndividual individual,
107: Assertion assertion) {
108: final OWLObjectProperty objProperty = dataFactory.getOWLObjectProperty(IRI.create(assertion.getIdentifier()));
109: final Stream<OWLIndividual> values = EntitySearcher.getObjectPropertyValues(individual, objProperty, ontology);
110: return values.filter(OWLIndividual::isNamed).map(value -> new MutableRemoveAxiom(ontology,
111: dataFactory.getOWLObjectPropertyAssertionAxiom(objProperty, individual, value)))
112: .collect(Collectors.toList());
113: }
114:
115: private Collection<? extends OWLOntologyChange> removeObjectPropertyAssertions(OWLNamedIndividual individual,
116: IRI assertionId,
117: Set<Value<?>> values) {
118: final OWLObjectProperty objProperty = dataFactory.getOWLObjectProperty(assertionId);
119: return values.stream().map(value -> {
120: final OWLIndividual object = OwlapiUtils
121: .getIndividual(NamedResource.create(value.stringValue()), dataFactory);
122: return new MutableRemoveAxiom(ontology,
123: dataFactory.getOWLObjectPropertyAssertionAxiom(objProperty, individual, object));
124: }).collect(Collectors.toList());
125: }
126:
127: private Collection<? extends OWLOntologyChange> removeAnnotationAssertions(OWLNamedIndividual individual,
128: Assertion assertion) {
129: final OWLAnnotationProperty annProperty = dataFactory
130: .getOWLAnnotationProperty(IRI.create(assertion.getIdentifier()));
131: final Stream<OWLAnnotationAssertionAxiom> values =
132: EntitySearcher.getAnnotationAssertionAxioms(individual.getIRI(), ontology);
133: return values.filter(axiom -> axiom.getProperty().equals(annProperty))
134: .map(value -> new MutableRemoveAxiom(ontology, value)).collect(Collectors.toList());
135: }
136:
137: private Collection<? extends OWLOntologyChange> removeAnnotationAssertions(OWLNamedIndividual individual,
138: Assertion assertion,
139: Set<Value<?>> values) {
140: final OWLAnnotationProperty annProperty = dataFactory
141: .getOWLAnnotationProperty(IRI.create(assertion.getIdentifier()));
142: return values.stream().map(value -> {
143: OWLAnnotationValue av;
144: try {
145: av = IRI.create(value.stringValue());
146: } catch (IllegalArgumentException e) {
147: av = OwlapiUtils.createOWLLiteralFromValue(value.getValue(),
148: OwlapiUtils.getAssertionLanguage(assertion));
149: }
150: return new MutableRemoveAxiom(ontology,
151: dataFactory.getOWLAnnotationAssertionAxiom(annProperty, individual.getIRI(), av));
152: }).collect(Collectors.toList());
153: }
154:
155: void removeAxioms(NamedResource subject, Map<Assertion, Set<Value<?>>> toRemove) {
156: final List<OWLOntologyChange> changes = new ArrayList<>();
157: final OWLNamedIndividual individual = OwlapiUtils.getIndividual(subject, dataFactory);
158: for (Map.Entry<Assertion, Set<Value<?>>> e : toRemove.entrySet()) {
159: final IRI assertionIri = IRI.create(e.getKey().getIdentifier());
160: if (ontology.containsDataPropertyInSignature(assertionIri)) {
161: changes.addAll(removeDataPropertyAssertions(individual, e.getKey(), e.getValue()));
162: } else if (ontology.containsObjectPropertyInSignature(assertionIri)) {
163: changes.addAll(removeObjectPropertyAssertions(individual, assertionIri, e.getValue()));
164: } else if (ontology.containsAnnotationPropertyInSignature(assertionIri)) {
165: changes.addAll(removeAnnotationAssertions(individual, e.getKey(), e.getValue()));
166: } else if (e.getKey().isClassAssertion()) {
167: changes.addAll(removeClassAssertionAxioms(individual, e.getValue()));
168: }
169: // It can happen that the assertionIri is no longer in the ontology, because of the way properties changes
170: // are processed in JOPA - they are compared to the original object, so if multiple property values are removed
171: // in a transaction, they are effectively removed multiple times. Therefore, it can happen that another
172: // property no longer exists in the ontology, because it was removed in the previous modifications during the same
173: // transaction
174: }
175: owlapiAdapter.addTransactionalChanges(snapshot.applyChanges(changes));
176: }
177: }