-
Notifications
You must be signed in to change notification settings - Fork 14
/
SimpleListIterator.java
149 lines (130 loc) · 5.99 KB
/
SimpleListIterator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* Copyright (C) 2016 Czech Technical University in Prague
* <p>
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.ontodriver.owlapi.list;
import cz.cvut.kbss.ontodriver.owlapi.AxiomAdapter;
import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
import cz.cvut.kbss.ontodriver.owlapi.util.MutableAddAxiom;
import cz.cvut.kbss.ontodriver.owlapi.util.MutableRemoveAxiom;
import cz.cvut.kbss.ontodriver.descriptor.ListDescriptor;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.Axiom;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.search.EntitySearcher;
import java.util.*;
import java.util.stream.Collectors;
class SimpleListIterator extends OwlapiListIterator {
final OWLObjectProperty hasNextProperty;
OWLObjectProperty previousProperty;
OWLObjectProperty currentProperty;
OWLNamedIndividual previousNode;
OWLNamedIndividual currentNode;
Collection<? extends OWLIndividual> next;
private final OWLOntology ontology;
private final OWLDataFactory dataFactory;
final AxiomAdapter axiomAdapter;
SimpleListIterator(ListDescriptor descriptor, OntologySnapshot snapshot, AxiomAdapter axiomAdapter) {
this.ontology = snapshot.getOntology();
this.dataFactory = snapshot.getDataFactory();
this.previousProperty = dataFactory
.getOWLObjectProperty(IRI.create(descriptor.getListProperty().getIdentifier()));
this.hasNextProperty = dataFactory.getOWLObjectProperty(IRI.create(descriptor.getNextNode().getIdentifier()));
this.currentNode = dataFactory.getOWLNamedIndividual(IRI.create(descriptor.getListOwner().getIdentifier()));
this.currentProperty = previousProperty;
this.axiomAdapter = axiomAdapter;
}
@Override
public boolean hasNext() {
return EntitySearcher.getObjectPropertyValues(currentNode, currentProperty, ontology).anyMatch(v -> true);
}
void doStep() {
this.next = EntitySearcher.getObjectPropertyValues(currentNode, currentProperty, ontology)
.collect(Collectors.toSet());
this.previousProperty = currentProperty;
this.currentProperty = hasNextProperty;
this.previousNode = this.currentNode;
}
@Override
public Axiom<NamedResource> next() {
final NamedResource value = nextValue();
final NamedResource subject = NamedResource.create(previousNode.getIRI().toURI());
final Assertion assertion = Assertion.createObjectPropertyAssertion(previousProperty.getIRI().toURI(), false);
return axiomAdapter.createAxiom(subject, assertion, value);
}
@Override
NamedResource nextValue() {
doStep();
if (next.isEmpty()) {
throw new NoSuchElementException("There are no more elements.");
}
checkMaxSuccessors(previousProperty, next);
final OWLIndividual item = next.iterator().next();
checkIsNamed(item);
this.currentNode = item.asOWLNamedIndividual();
return getCurrentNode();
}
@Override
NamedResource getCurrentNode() {
return NamedResource.create(currentNode.getIRI().toURI());
}
@Override
List<OWLOntologyChange> removeWithoutReconnect() {
final List<OWLOntologyChange> changes = new ArrayList<>(2);
changes.add(new MutableRemoveAxiom(ontology,
dataFactory.getOWLObjectPropertyAssertionAxiom(previousProperty, previousNode, currentNode)));
final OWLIndividual nextNode = getNextNode();
if (nextNode != null) {
changes.add(new MutableRemoveAxiom(ontology,
dataFactory.getOWLObjectPropertyAssertionAxiom(currentProperty, currentNode, nextNode)));
}
return changes;
}
/**
* {@inheritDoc}
* <p>
* This method also replaces the current node with the new value, so subsequent calls to {@link #getCurrentNode()}
* return the new value.
*
* @param newValue The new value to use
* @return The changes to apply
*/
@Override
List<OWLOntologyChange> replaceNode(NamedResource newValue) {
final List<OWLOntologyChange> changes = new ArrayList<>(4);
changes.addAll(removeWithoutReconnect());
final OWLNamedIndividual newNode = dataFactory.getOWLNamedIndividual(IRI.create(newValue.getIdentifier()));
changes.add(new MutableAddAxiom(ontology,
dataFactory.getOWLObjectPropertyAssertionAxiom(previousProperty, previousNode, newNode)));
final OWLOntologyChange connectionToNext = connectToNextNode(newNode);
if (connectionToNext != null) {
changes.add(connectionToNext);
}
this.currentNode = newNode;
return changes;
}
private OWLOntologyChange connectToNextNode(OWLNamedIndividual newNode) {
final OWLIndividual nextNode = getNextNode();
if (nextNode == null || nextNode.equals(newNode)) {
return null;
}
return new MutableAddAxiom(ontology,
dataFactory.getOWLObjectPropertyAssertionAxiom(currentProperty, newNode, nextNode));
}
private OWLIndividual getNextNode() {
final Iterator<OWLIndividual> nextOnes =
EntitySearcher.getObjectPropertyValues(currentNode, currentProperty, ontology).iterator();
return nextOnes.hasNext() ? nextOnes.next() : null;
}
}