Skip to content

Method: nextAxiom()

1: /*
2: * JOPA
3: * Copyright (C) 2024 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.jena.list;
19:
20: import cz.cvut.kbss.ontodriver.descriptor.SimpleListDescriptor;
21: import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
22: import cz.cvut.kbss.ontodriver.model.*;
23: import org.apache.jena.rdf.model.RDFNode;
24: import org.apache.jena.rdf.model.Resource;
25: import org.apache.jena.rdf.model.Statement;
26:
27: import java.net.URI;
28: import java.util.ArrayList;
29: import java.util.Collections;
30: import java.util.List;
31:
32: import static org.apache.jena.rdf.model.ResourceFactory.createResource;
33: import static org.apache.jena.rdf.model.ResourceFactory.createStatement;
34:
35: class SimpleListIterator extends AbstractListIterator<NamedResource> {
36:
37: SimpleListIterator(SimpleListDescriptor descriptor, StorageConnector connector) {
38: super(descriptor, connector);
39: }
40:
41: @Override
42: Axiom<NamedResource> nextAxiom() {
43: final NamedResource subject = NamedResource.create(currentNode.getURI());
44: final Assertion assertion = Assertion
45:• .createObjectPropertyAssertion(URI.create(first() ? hasListProperty.getURI() : hasNextProperty.getURI()), false);
46: final NamedResource value = nextValue();
47: return new AxiomImpl<>(subject, assertion, new Value<>(value));
48: }
49:
50: @Override
51: NamedResource nextValue() {
52: resolveNextListNode();
53: return NamedResource.create(currentNode.getURI());
54: }
55:
56: /**
57: * Replaces the current node with the specified replacement, connecting it into the list. Original links to the current
58: * element are removed.
59: *
60: * @param replacement The replacement node
61: */
62: @Override
63: void replace(NamedResource replacement) {
64: removeWithoutReconnect();
65: final List<Statement> toAdd = new ArrayList<>(2);
66: final Resource replacementNode = createResource(replacement.getIdentifier().toString());
67: toAdd.add(createStatement(previousNode, index == 0 ? hasListProperty : hasNextProperty, replacementNode));
68: if (hasNext()) {
69: verifySuccessorCount();
70: final RDFNode nextNode = cursor.iterator().next().getObject();
71: remove(currentNode, hasNextProperty, nextNode);
72: if (!nextNode.equals(replacementNode)) {
73: final Statement linkToNext = createStatement(replacementNode, hasNextProperty, nextNode);
74: toAdd.add(linkToNext);
75: this.cursor = Collections.singleton(linkToNext);
76: } else {
77: moveCursor(replacementNode);
78: }
79: }
80: this.currentNode = replacementNode;
81: connector.add(toAdd, context);
82: }
83: }