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