Skip to content

Method: removeWithoutReconnect()

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.ListDescriptor;
21: import cz.cvut.kbss.ontodriver.model.Assertion;
22: import cz.cvut.kbss.ontodriver.model.Axiom;
23: import cz.cvut.kbss.ontodriver.model.NamedResource;
24: import cz.cvut.kbss.ontodriver.owlapi.AxiomAdapter;
25: import cz.cvut.kbss.ontodriver.owlapi.change.TransactionalChange;
26: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
27: import cz.cvut.kbss.ontodriver.owlapi.exception.ReasonerNotAvailableException;
28: import org.semanticweb.owlapi.model.OWLIndividual;
29: import org.semanticweb.owlapi.model.OWLNamedIndividual;
30: import org.semanticweb.owlapi.reasoner.NodeSet;
31: import org.semanticweb.owlapi.reasoner.OWLReasoner;
32:
33: import java.util.Collections;
34: import java.util.List;
35: import java.util.NoSuchElementException;
36: import java.util.stream.Collectors;
37:
38: /**
39: * This class differs from the way basic SimpleListIterator iterates over the list. Here, we do step before even calling
40: * nextItem in order to avoid consulting the reasoner twice for values of the current node's successors.
41: * <p>
42: * We can do this because we need not worry about remove operations, they're not supported on inferred iterator.
43: * <p>
44: * TODO Check if the inferred iterators' logic is correct, especially for the nextValue method
45: */
46: class InferredSimpleListIterator extends SimpleListIterator {
47:
48: private final OWLReasoner reasoner;
49:
50: InferredSimpleListIterator(ListDescriptor descriptor, OntologySnapshot snapshot,
51: AxiomAdapter axiomAdapter) {
52: super(descriptor, snapshot, axiomAdapter);
53: if (snapshot.getReasoner() == null) {
54: throw new ReasonerNotAvailableException();
55: }
56: this.reasoner = snapshot.getReasoner();
57: }
58:
59: @Override
60: public boolean hasNext() {
61: if (nextItem == null) {
62: doStep();
63: }
64: return !nextItem.isEmpty();
65: }
66:
67: @Override
68: void doStep() {
69: final NodeSet<OWLNamedIndividual> nodeSet = reasoner.getObjectPropertyValues(currentNode, currentProperty);
70: this.nextItem = nodeSet.isEmpty() ? Collections.emptySet() : nodeSet.entities().collect(Collectors.toSet());
71: this.previousProperty = currentProperty;
72: this.currentProperty = hasNextProperty;
73: this.previousNode = this.currentNode;
74: }
75:
76: @Override
77: public Axiom<NamedResource> next() {
78: if (!hasNext()) {
79: throw new NoSuchElementException("There are no more elements.");
80: }
81: checkMaxSuccessors(previousProperty, nextItem);
82: final OWLIndividual item = nextItem.iterator().next(); // We know the individual is named
83: this.currentNode = item.asOWLNamedIndividual();
84: final NamedResource subject = NamedResource.create(previousNode.getIRI().toURI());
85: final NamedResource value = NamedResource.create(currentNode.getIRI().toURI());
86: final Assertion assertion = Assertion.createObjectPropertyAssertion(previousProperty.getIRI().toURI(), false);
87: doStep();
88: return axiomAdapter.createAxiom(subject, assertion, value);
89: }
90:
91: @Override
92: List<TransactionalChange> removeWithoutReconnect() {
93: throw new UnsupportedOperationException();
94: }
95:
96: @Override
97: List<TransactionalChange> replaceNode(NamedResource newValue) {
98: throw new UnsupportedOperationException();
99: }
100: }