Skip to contentMethod: hasNext()
1: /*
2: * JOPA
3: * Copyright (C) 2023 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.owlapi.list;
19:
20: import cz.cvut.kbss.ontodriver.descriptor.ReferencedListDescriptor;
21: import cz.cvut.kbss.ontodriver.owlapi.AxiomAdapter;
22: import cz.cvut.kbss.ontodriver.owlapi.change.TransactionalChange;
23: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
24: import cz.cvut.kbss.ontodriver.owlapi.exception.ReasonerNotAvailableException;
25: import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
26: import org.semanticweb.owlapi.model.OWLNamedIndividual;
27: import org.semanticweb.owlapi.reasoner.OWLReasoner;
28:
29: import java.util.Collection;
30: import java.util.Collections;
31: import java.util.List;
32: import java.util.NoSuchElementException;
33: import java.util.stream.Collectors;
34:
35: class InferredReferencedListIterator<T> extends ReferencedListIterator<T> {
36:
37: private OWLNamedIndividual currentNode;
38: private final OWLReasoner reasoner;
39:
40: InferredReferencedListIterator(ReferencedListDescriptor descriptor, OntologySnapshot snapshot,
41: AxiomAdapter axiomAdapter) {
42: super(descriptor, snapshot, axiomAdapter);
43: this.reasoner = snapshot.getReasoner();
44: if (reasoner == null) {
45: throw new ReasonerNotAvailableException();
46: }
47: this.currentNode = OwlapiUtils.getIndividual(descriptor.getListOwner(), snapshot.getDataFactory());
48: }
49:
50: @Override
51: public boolean hasNext() {
52:• if (nextItem == null) {
53: doStep();
54: }
55:• return nextItem != null && !nextItem.isEmpty();
56: }
57:
58: @Override
59: void doStep() {
60: final Collection<OWLNamedIndividual> nextNodes =
61: reasoner.getObjectPropertyValues(currentNode, currentNextNodeProperty).entities()
62: .collect(Collectors.toSet());
63: if (nextNodes.isEmpty()) {
64: this.nextItem = Collections.emptyList();
65: return;
66: }
67: checkMaxSuccessors(currentNextNodeProperty, nextNodes);
68: this.currentNextNodeProperty = hasNextProperty;
69: this.currentNode = nextNodes.iterator().next();
70: this.nextItem = hasContentProperty.isOWLObjectProperty() ? reasoner.getObjectPropertyValues(currentNode, hasContentProperty.asOWLObjectProperty())
71: .entities()
72: .collect(Collectors.toSet())
73: : reasoner.getDataPropertyValues(currentNode, hasContentProperty.asOWLDataProperty());
74: }
75:
76: @Override
77: T nextValue() {
78: if (!hasNext()) {
79: throw new NoSuchElementException("There are no more elements.");
80: }
81: final T result = extractNodeContent();
82: doStep();
83: return result;
84: }
85:
86: @Override
87: List<TransactionalChange> removeWithoutReconnect() {
88: throw new UnsupportedOperationException();
89: }
90:
91: @Override
92: List<TransactionalChange> replaceNode(T newValue) {
93: throw new UnsupportedOperationException();
94: }
95: }