Skip to contentMethod: doStep()
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:
18: import cz.cvut.kbss.ontodriver.owlapi.AxiomAdapter;
19: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
20: import cz.cvut.kbss.ontodriver.owlapi.util.MutableAddAxiom;
21: import cz.cvut.kbss.ontodriver.owlapi.util.MutableRemoveAxiom;
22: import cz.cvut.kbss.ontodriver.descriptor.ListDescriptor;
23: import cz.cvut.kbss.ontodriver.model.Assertion;
24: import cz.cvut.kbss.ontodriver.model.Axiom;
25: import cz.cvut.kbss.ontodriver.model.NamedResource;
26: import org.semanticweb.owlapi.model.*;
27: import org.semanticweb.owlapi.search.EntitySearcher;
28:
29: import java.util.ArrayList;
30: import java.util.Collection;
31: import java.util.List;
32: import java.util.NoSuchElementException;
33:
34: class SimpleListIterator extends OwlapiListIterator {
35:
36: final OWLObjectProperty hasNextProperty;
37:
38: OWLObjectProperty previousProperty;
39: OWLObjectProperty currentProperty;
40: OWLNamedIndividual previousNode;
41: OWLNamedIndividual currentNode;
42: Collection<? extends OWLIndividual> next;
43:
44: private final OWLOntology ontology;
45: private final OWLDataFactory dataFactory;
46:
47: final AxiomAdapter axiomAdapter;
48:
49:
50: SimpleListIterator(ListDescriptor descriptor, OntologySnapshot snapshot, AxiomAdapter axiomAdapter) {
51: this.ontology = snapshot.getOntology();
52: this.dataFactory = snapshot.getDataFactory();
53: this.previousProperty = dataFactory
54: .getOWLObjectProperty(IRI.create(descriptor.getListProperty().getIdentifier()));
55: this.hasNextProperty = dataFactory.getOWLObjectProperty(IRI.create(descriptor.getNextNode().getIdentifier()));
56: this.currentNode = dataFactory.getOWLNamedIndividual(IRI.create(descriptor.getListOwner().getIdentifier()));
57: this.currentProperty = previousProperty;
58: this.axiomAdapter = axiomAdapter;
59: }
60:
61: @Override
62: public boolean hasNext() {
63: return !EntitySearcher.getObjectPropertyValues(currentNode, currentProperty, ontology).isEmpty();
64: }
65:
66: void doStep() {
67: this.next = EntitySearcher.getObjectPropertyValues(currentNode, currentProperty, ontology);
68: this.previousProperty = currentProperty;
69: this.currentProperty = hasNextProperty;
70: this.previousNode = this.currentNode;
71: }
72:
73: @Override
74: public Axiom<NamedResource> next() {
75: final NamedResource value = nextValue();
76: final NamedResource subject = NamedResource.create(previousNode.getIRI().toURI());
77: final Assertion assertion = Assertion.createObjectPropertyAssertion(previousProperty.getIRI().toURI(), false);
78: return axiomAdapter.createAxiom(subject, assertion, value);
79: }
80:
81: @Override
82: NamedResource nextValue() {
83: doStep();
84: if (next.isEmpty()) {
85: throw new NoSuchElementException("There are no more elements.");
86: }
87: checkMaxSuccessors(previousProperty, next);
88: final OWLIndividual item = next.iterator().next();
89: checkIsNamed(item);
90: this.currentNode = item.asOWLNamedIndividual();
91: return getCurrentNode();
92: }
93:
94: @Override
95: NamedResource getCurrentNode() {
96: return NamedResource.create(currentNode.getIRI().toURI());
97: }
98:
99: @Override
100: List<OWLOntologyChange> removeWithoutReconnect() {
101: final List<OWLOntologyChange> changes = new ArrayList<>(2);
102: changes.add(new MutableRemoveAxiom(ontology,
103: dataFactory.getOWLObjectPropertyAssertionAxiom(previousProperty, previousNode, currentNode)));
104: final OWLIndividual nextNode = getNextNode();
105: if (nextNode != null) {
106: changes.add(new MutableRemoveAxiom(ontology,
107: dataFactory.getOWLObjectPropertyAssertionAxiom(currentProperty, currentNode, nextNode)));
108: }
109: return changes;
110: }
111:
112: /**
113: * {@inheritDoc}
114: * <p>
115: * This method also replaces the current node with the new value, so subsequent calls to {@link #getCurrentNode()}
116: * return the new value.
117: *
118: * @param newValue The new value to use
119: * @return The changes to apply
120: */
121: @Override
122: List<OWLOntologyChange> replaceNode(NamedResource newValue) {
123: final List<OWLOntologyChange> changes = new ArrayList<>(2);
124: changes.addAll(removeWithoutReconnect());
125: final OWLNamedIndividual newNode = dataFactory.getOWLNamedIndividual(IRI.create(newValue.getIdentifier()));
126: changes.add(new MutableAddAxiom(ontology,
127: dataFactory.getOWLObjectPropertyAssertionAxiom(previousProperty, previousNode, newNode)));
128: final OWLOntologyChange connectionToNext = connectToNextNode(newNode);
129: if (connectionToNext != null) {
130: changes.add(connectionToNext);
131: }
132: this.currentNode = newNode;
133: return changes;
134: }
135:
136: private OWLOntologyChange connectToNextNode(OWLNamedIndividual newNode) {
137: final OWLIndividual nextNode = getNextNode();
138: if (nextNode == null || nextNode.equals(newNode)) {
139: return null;
140: }
141: return new MutableAddAxiom(ontology,
142: dataFactory.getOWLObjectPropertyAssertionAxiom(currentProperty, newNode, nextNode));
143: }
144:
145: private OWLIndividual getNextNode() {
146: final Collection<OWLIndividual> nextOnes = EntitySearcher
147: .getObjectPropertyValues(currentNode, currentProperty, ontology);
148: if (nextOnes.isEmpty()) {
149: return null;
150: }
151: return nextOnes.iterator().next();
152: }
153: }