Skip to content

Method: nextInternal()

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.sesame;
16:
17: import java.util.ArrayList;
18: import java.util.Collection;
19: import java.util.Collections;
20:
21: import org.openrdf.model.Resource;
22: import org.openrdf.model.Statement;
23: import org.openrdf.model.URI;
24: import org.openrdf.model.ValueFactory;
25:
26: import cz.cvut.kbss.ontodriver.exception.IntegrityConstraintViolatedException;
27: import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
28: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
29: import cz.cvut.kbss.ontodriver.descriptor.ReferencedListDescriptor;
30: import cz.cvut.kbss.ontodriver.model.Axiom;
31: import cz.cvut.kbss.ontodriver.model.NamedResource;
32:
33: class ReferencedListIterator extends AbstractSesameIterator {
34:
35:         private final ReferencedListDescriptor listDescriptor;
36:
37:         private final URI hasContentProperty;
38:
39:         private URI currentProperty;
40:
41:         private Statement currentNode;
42:         private Statement currentContent;
43:         private Collection<Statement> next;
44:
45:         public ReferencedListIterator(ReferencedListDescriptor listDescriptor, Connector connector,
46:                         ValueFactory vf) throws SesameDriverException {
47:                 super(listDescriptor, connector, vf);
48:                 this.listDescriptor = listDescriptor;
49:                 this.hasContentProperty = SesameUtils.toSesameUri(listDescriptor.getNodeContent()
50:                                 .getIdentifier(), vf);
51:                 this.currentProperty = hasListProperty;
52:                 init();
53:         }
54:
55:         private void init() throws SesameDriverException {
56:                 this.next = connector.findStatements(listOwner, hasListProperty, null, includeInferred,
57:                                 context);
58:         }
59:
60:         @Override
61:         public boolean hasNext() throws SesameDriverException {
62:                 return (!next.isEmpty());
63:         }
64:
65:         @Override
66:         public Resource nextNode() throws SesameDriverException {
67:                 nextInternal();
68:                 return (Resource) currentNode.getObject();
69:         }
70:
71:         private void nextInternal() throws SesameDriverException {
72:•                if (!hasNext()) {
73:                         throw new IllegalStateException();
74:                 }
75:                 checkSuccessorMax(next, currentProperty);
76:                 this.currentNode = next.iterator().next();
77:                 this.currentProperty = currentNode.getPredicate();
78:                 checkNodeIsResource(currentNode);
79:                 final Resource elem = (Resource) currentNode.getObject();
80:                 this.currentContent = getNodeContent(elem);
81:                 this.next = connector.findStatements(elem, hasNextProperty, null, includeInferred, context);
82:         }
83:
84:         private Statement getNodeContent(Resource node) throws SesameDriverException {
85:                 final Collection<Statement> elems = connector.findStatements(node, hasContentProperty,
86:                                 null, includeInferred, context);
87:                 checkSuccessorMax(elems, hasContentProperty);
88:                 if (elems.isEmpty()) {
89:                         throw new IntegrityConstraintViolatedException("Node " + node + " has no content.");
90:                 }
91:                 final Statement elem = elems.iterator().next();
92:                 checkNodeIsResource(elem);
93:                 return elem;
94:         }
95:
96:         @Override
97:         public Resource currentContent() throws SesameDriverException {
98:                 assert currentContent.getObject() instanceof Resource;
99:                 return (Resource) currentContent.getObject();
100:         }
101:
102:         @Override
103:         public Axiom<NamedResource> nextAxiom() throws SesameDriverException {
104:                 nextInternal();
105:                 assert currentContent.getObject() instanceof Resource;
106:
107:                 return createAxiom(currentContent.getSubject(), listDescriptor.getNodeContent(),
108:                                 (Resource) currentContent.getObject());
109:         }
110:
111:         @Override
112:         public void remove() throws SesameDriverException {
113:                 assert currentNode.getObject() instanceof Resource;
114:                 final Collection<Statement> toRemove = new ArrayList<>();
115:                 toRemove.add(currentNode);
116:                 toRemove.add(currentContent);
117:                 if (!next.isEmpty()) {
118:                         toRemove.addAll(next);
119:                         final Statement stmt = next.iterator().next();
120:                         checkNodeIsResource(stmt);
121:                         final Resource nextNode = (Resource) stmt.getObject();
122:                         final Statement connectNext = vf.createStatement(currentNode.getSubject(),
123:                                         currentProperty, nextNode, context);
124:                         this.next = Collections.singleton(connectNext);
125:
126:                         this.currentNode = null;
127:                         this.currentContent = null;
128:                         connector.addStatements(next);
129:                 } else {
130:                         next = Collections.emptyList();
131:                 }
132:                 connector.removeStatements(toRemove);
133:         }
134:
135:         @Override
136:         public void replaceCurrentWith(NamedResource newContent) throws SesameDriverException {
137:                 assert currentNode.getObject() instanceof Resource;
138:                 // We just replace the original content statement with new one
139:                 connector.removeStatements(Collections.singleton(currentContent));
140:                 final Resource node = (Resource) currentNode.getObject();
141:                 final Statement stmt = vf.createStatement(node, hasContentProperty,
142:                                 SesameUtils.toSesameUri(newContent.getIdentifier(), vf), context);
143:                 connector.addStatements(Collections.singleton(stmt));
144:         }
145:
146: }