Skip to content

Method: replaceNode(NamedResource)

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.list;
16:
17: import cz.cvut.kbss.ontodriver.owlapi.AxiomAdapter;
18: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
19: import cz.cvut.kbss.ontodriver.owlapi.util.MutableAddAxiom;
20: import cz.cvut.kbss.ontodriver.owlapi.util.MutableRemoveAxiom;
21: import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
22: import cz.cvut.kbss.ontodriver.descriptor.ReferencedListDescriptor;
23: import cz.cvut.kbss.ontodriver.model.Axiom;
24: import cz.cvut.kbss.ontodriver.model.NamedResource;
25: import org.semanticweb.owlapi.model.*;
26: import org.semanticweb.owlapi.search.EntitySearcher;
27:
28: import java.util.*;
29:
30: class ReferencedListIterator extends OwlapiListIterator {
31:
32: final OWLObjectProperty hasNextProperty;
33: final OWLObjectProperty hasContentProperty;
34:
35: final OWLOntology ontology;
36: private final OWLDataFactory dataFactory;
37:
38: final AxiomAdapter axiomAdapter;
39:
40: OWLIndividual previousNode;
41: OWLIndividual currentNode;
42: OWLObjectProperty previousNextNodeProperty;
43: OWLObjectProperty currentNextNodeProperty;
44: Collection<? extends OWLIndividual> next;
45:
46: final ReferencedListDescriptor descriptor;
47:
48: ReferencedListIterator(ReferencedListDescriptor descriptor, OntologySnapshot snapshot,
49: AxiomAdapter axiomAdapter) {
50: this.ontology = snapshot.getOntology();
51: this.dataFactory = snapshot.getDataFactory();
52: this.hasNextProperty = dataFactory.getOWLObjectProperty(IRI.create(descriptor.getNextNode().getIdentifier()));
53: this.hasContentProperty = dataFactory
54: .getOWLObjectProperty(IRI.create(descriptor.getNodeContent().getIdentifier()));
55: this.axiomAdapter = axiomAdapter;
56: this.currentNextNodeProperty = dataFactory
57: .getOWLObjectProperty(IRI.create(descriptor.getListProperty().getIdentifier()));
58: this.previousNextNodeProperty = currentNextNodeProperty;
59: this.currentNode = OwlapiUtils.getIndividual(descriptor.getListOwner(), dataFactory);
60: this.previousNode = currentNode;
61: this.descriptor = descriptor;
62: }
63:
64: @Override
65: public boolean hasNext() {
66: return !EntitySearcher.getObjectPropertyValues(currentNode, currentNextNodeProperty, ontology).isEmpty();
67: }
68:
69: void doStep() {
70: final Collection<OWLIndividual> nextNodes = EntitySearcher
71: .getObjectPropertyValues(currentNode, currentNextNodeProperty, ontology);
72: if (nextNodes.isEmpty()) {
73: this.next = Collections.emptyList();
74: return;
75: }
76: checkMaxSuccessors(currentNextNodeProperty, nextNodes);
77: this.previousNextNodeProperty = currentNextNodeProperty;
78: this.currentNextNodeProperty = hasNextProperty; // This just switches from hasList to hasNext
79: final OWLIndividual node = nextNodes.iterator().next();
80: checkIsNamed(node);
81: this.previousNode = currentNode;
82: this.currentNode = node;
83: this.next = EntitySearcher.getObjectPropertyValues(node, hasContentProperty, ontology);
84: }
85:
86: @Override
87: public Axiom<NamedResource> next() {
88: final NamedResource value = nextValue();
89: return axiomAdapter
90: .createAxiom(NamedResource.create(currentNode.asOWLNamedIndividual().getIRI().toURI()),
91: descriptor.getNodeContent(), value);
92: }
93:
94: @Override
95: NamedResource nextValue() {
96: doStep();
97: if (next.isEmpty()) {
98: throw new NoSuchElementException("There are no more elements.");
99: }
100: checkMaxSuccessors(hasContentProperty, next);
101: final OWLIndividual value = next.iterator().next();
102: checkIsNamed(value);
103: return NamedResource.create(value.asOWLNamedIndividual().getIRI().toURI());
104: }
105:
106: @Override
107: public NamedResource getCurrentNode() {
108: return NamedResource.create(currentNode.asOWLNamedIndividual().getIRI().toURI());
109: }
110:
111: @Override
112: List<OWLOntologyChange> removeWithoutReconnect() {
113: final List<OWLOntologyChange> changes = new ArrayList<>(2);
114: changes.add(new MutableRemoveAxiom(ontology,
115: dataFactory.getOWLObjectPropertyAssertionAxiom(previousNextNodeProperty, previousNode, currentNode)));
116: final OWLIndividual nextNode = getNextNode();
117: if (nextNode != null) {
118: changes.add(new MutableRemoveAxiom(ontology,
119: dataFactory.getOWLObjectPropertyAssertionAxiom(currentNextNodeProperty, currentNode, nextNode)));
120: }
121: return changes;
122: }
123:
124: private OWLIndividual getNextNode() {
125: final Collection<OWLIndividual> nextOnes = EntitySearcher
126: .getObjectPropertyValues(currentNode, currentNextNodeProperty, ontology);
127: if (nextOnes.isEmpty()) {
128: return null;
129: }
130: return nextOnes.iterator().next();
131: }
132:
133: @Override
134: List<OWLOntologyChange> replaceNode(NamedResource newValue) {
135: // We know there is exactly one, because next has to have been called before this method
136: final OWLIndividual originalContent = next.iterator().next();
137: final OWLNamedIndividual newContent = OwlapiUtils.getIndividual(newValue, dataFactory);
138: final List<OWLOntologyChange> changes = new ArrayList<>(2);
139: changes.add(new MutableRemoveAxiom(ontology,
140: dataFactory.getOWLObjectPropertyAssertionAxiom(hasContentProperty, currentNode, originalContent)));
141: changes.add(new MutableAddAxiom(ontology,
142: dataFactory.getOWLObjectPropertyAssertionAxiom(hasContentProperty, currentNode, newContent)));
143: return changes;
144: }
145: }