Debian Codegraph @

net/sf/saxon/expr/accum/AccumulatorData.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.expr.accum;
9
10 import net.sf.saxon.Controller;
11 import net.sf.saxon.expr.Expression;
12 import net.sf.saxon.expr.XPathContext;
13 import net.sf.saxon.expr.XPathContextMajor;
14 import net.sf.saxon.expr.instruct.SlotManager;
15 import net.sf.saxon.expr.parser.Evaluator;
16 import net.sf.saxon.om.*;
17 import net.sf.saxon.trans.Err;
18 import net.sf.saxon.trans.XPathException;
19 import net.sf.saxon.trans.rules.Rule;
20 import net.sf.saxon.tree.iter.AxisIterator;
21 import net.sf.saxon.tree.iter.ManualIterator;
22 import net.sf.saxon.tree.util.Navigator;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 /**
28 * Holds the values of an accumulator function for one non-streamed document
29 */
30 public class AccumulatorData implements IAccumulatorData {
31
32 private Accumulator accumulator;
33 private List<DataPoint> values = new ArrayList<>();
34 private boolean building = false;
35
36 public AccumulatorData(Accumulator acc) {
37 this.accumulator = acc;
38 }
39
40 /**
41 * Get the associated accumulator
42 * @return the accumulator
43 */
44
45 public Accumulator getAccumulator() {
46 return accumulator;
47 }
48
49 /**
50 * Build a data structure containing the values of the accumulator for each node in a document.
51 * The data structure holds the value for all nodes where the value changes; the value for other
52 * nodes is obtained by interpolation
53 *
54 * @param doc the root of the tree for which the accumulator is to be evaluated
55 * @param context the dynamic evaluation context
56 * @throws XPathException if a dynamic error occurs while evaluating the accumulator
57 */
58
59 public void buildIndex(NodeInfo doc, XPathContext context) throws XPathException {
60 //System.err.println("Building accData " + this);
61 if (building) {
62 throw new XPathException("Accumulator " + accumulator.getAccumulatorName().getDisplayName() +
63 " requires access to its own value", "XTDE3400");
64 }
65 building = true;
66 Expression initialValue = accumulator.getInitialValueExpression();
67 XPathContextMajor c2 = context.newContext();
68 SlotManager sf = accumulator.getSlotManagerForInitialValueExpression();
69 Sequence[] slots = new Sequence[sf.getNumberOfVariables()];
70 c2.setStackFrame(sf, slots);
71 c2.setCurrentIterator(new ManualIterator<>(doc));
72 Sequence<?> val = initialValue.iterate(c2).materialize();
73 values.add(new DataPoint(new Visit(doc, false), val));
74 val = visit(doc, val, c2);
75 values.add(new DataPoint(new Visit(doc, true), val));
76 ((ArrayList) values).trimToSize();
77 building = false;
78 //diagnosticPrint();
79 }
80
81 /*
82 * Diagnostic output of the entire data structure
83 */
84
85 // public void diagnosticPrint() {
86 // for (DataPoint dp : values) {
87 // System.err.println((dp.visit.isPostDescent ? "B:" : "A:") + Navigator.getPath(dp.visit.node) + " = " + dp.value);
88 // }
89 // }
90
91 /**
92 * Recursive routine to evaluate the accumulator for a given node, before and after
93 * visiting its descendants
94 *
95 * @param node the node to be visited
96 * @param value the value of the accumulator before visiting this node
97 * @param context the dynamic evaluation context
98 * @return the value of the accumulator after visiting this node
99 * @throws XPathException if a dynamic evaluation error occurs
100 */
101
102 @SuppressWarnings({"InfiniteRecursion"}) //Spurious warning from IntelliJ
103 private Sequence<?> visit(NodeInfo node, Sequence<?> value, XPathContext context) throws XPathException {
104 ((ManualIterator)context.getCurrentIterator()).setContextItem(node);
105 Rule rule = accumulator.getPreDescentRules().getRule(node, context);
106 if (rule != null) {
107 value = processRule(rule, node, false, value, context);
108 if (accumulator.isTracing()) {
109 context.getConfiguration().getLogger().info(accumulator.getAccumulatorName().getDisplayName() +
110 " BEFORE " + Navigator.getPath(node) + ": " + Err.depictSequence(value));
111 }
112 }
113 AxisIterator iter = node.iterateAxis(AxisInfo.CHILD);
114 NodeInfo kid;
115 while ((kid = iter.next()) != null) {
116 value = visit(kid, value, context);
117 }
118 ((ManualIterator) context.getCurrentIterator()).setContextItem(node);
119 rule = accumulator.getPostDescentRules().getRule(node, context);
120 if (rule != null) {
121 value = processRule(rule, node, true, value, context);
122 if (accumulator.isTracing()) {
123 context.getConfiguration().getLogger().info(accumulator.getAccumulatorName().getDisplayName() +
124 " AFTER " + Navigator.getPath(node) + ": " + Err.depictSequence(value));
125 }
126 }
127 return value;
128 }
129
130 /**
131 * Apply an accumulator rule
132 *
133 * @param rule the rule to apply
134 * @param node the node that was matched
135 * @param isPostDescent false for the pre-descent visit to a node, true for the post-descent visit
136 * @param value the value of the accumulator before applying the rule
137 * @param context the dynamic evaluation context
138 * @return the value of the accumulator after applying the rule
139 * @throws XPathException if a dynamic error occurs during the evaluation
140 */
141
142 private Sequence<?> processRule(Rule rule, NodeInfo node, boolean isPostDescent, Sequence<?> value, XPathContext context) throws XPathException {
143 AccumulatorRule target = (AccumulatorRule) rule.getAction();
144 Expression delta = target.getNewValueExpression();
145 XPathContextMajor c2 = context.newCleanContext();
146 final Controller controller = c2.getController();
147 assert controller != null;
148 ManualIterator<NodeInfo> initialNode = new ManualIterator<>(node);
149 c2.setCurrentIterator(initialNode);
150 c2.openStackFrame(target.getStackFrameMap());
151 c2.setLocalVariable(0, value);
152 c2.setCurrentComponent(accumulator.getDeclaringComponent());
153 c2.setTemporaryOutputState(StandardNames.XSL_ACCUMULATOR_RULE);
154 value = Evaluator.EAGER_SEQUENCE.evaluate(delta, c2);
155 //System.err.println("Node " + ((TinyNodeImpl) node).getNodeNumber() + " : " + value);
156 values.add(new DataPoint(new Visit(node, isPostDescent), value));
157 return value;
158 }
159
160 /**
161 * Get the value of the accumulator for a given node
162 *
163 * @param node the node in question
164 * @param postDescent false if the pre-descent value of the accumulator is required;
165 * false if the post-descent value is wanted.
166 * @return the value of the accumulator for this node
167 */
168
169 // public Sequence getValue(NodeInfo node, boolean postDescent) {
170 // Visit visit = new Visit(node, postDescent);
171 // return search(0, values.size(), visit);
172 // }
173
174 public Sequence<?> getValue(NodeInfo node, boolean postDescent) {
175 Visit visit = new Visit(node, postDescent);
176 return search(0, values.size(), visit);
177 //System.err.println("Searched " + values.size() + " " + ((TinyNodeImpl) visit.node).getNodeNumber() + " : " + seq);
178 }
179
180 /**
181 * Recursive binary chop search for the value applicable to a given node
182 *
183 * @param start the start index of the search
184 * @param end the end index of the search
185 * @param sought the visit being sought
186 * @return the value associated with this node
187 */
188
189 private Sequence<?> search(int start, int end, Visit sought) {
190 //System.err.println("-- Search " + start + ".." + end);
191 if (start == end) {
192 // sometimes we want the value for the visit we've found, sometimes for the previous visit
193 int rel = sought.compareTo(values.get(start).visit);
194 if (rel < 0 /*|| (rel == 0 && sought.isPostDescent)*/) {
195 return values.get(start - 1).value;
196 } else {
197 return values.get(start).value;
198 }
199 }
200 int mid = (start + end) / 2;
201 if (sought.compareTo(values.get(mid).visit) <= 0) {
202 return search(start, mid, sought);
203 } else {
204 return search(mid + 1, end, sought);
205 }
206
207 // 9.6:
208 // int mid = (start + end) / 2;
209 // if (sought.compareTo(values.get(mid).visit) <= 0) {
210 // return search(start, mid, sought);
211 // } else {
212 // return search(mid + 1, end, sought);
213 // }
214 }
215
216 /**
217 * Class representing one of the two visits to a node during a tree-walk
218 */
219
220 private static class Visit implements Comparable<Visit> {
221 public NodeInfo node;
222 public boolean isPostDescent;
223
224 public Visit(NodeInfo node, boolean isPostDescent) {
225 this.node = node;
226 this.isPostDescent = isPostDescent;
227 }
228
229 /**
230 * Compare the order of two node visits.
231 *
232 * @param other the other node visit
233 * @return -1 if this visit is earlier, 0 if they are the same visit, +1 if this visit is later
234 */
235
236 public int compareTo(Visit other) {
237 int relation = Navigator.comparePosition(node, other.node);
238 switch (relation) {
239 case AxisInfo.SELF:
240 if (isPostDescent == other.isPostDescent) {
241 return 0;
242 } else {
243 return isPostDescent ? +1 : -1;
244 }
245 case AxisInfo.PRECEDING:
246 return -1;
247 case AxisInfo.FOLLOWING:
248 return +1;
249 case AxisInfo.ANCESTOR:
250 return isPostDescent ? +1 : -1;
251 case AxisInfo.DESCENDANT:
252 return other.isPostDescent ? -1 : +1;
253 default:
254 throw new IllegalStateException();
255 }
256 }
257 }
258
259 /**
260 * Class representing a value of the accumulator immediately after a particular visit to a node.
261 */
262
263 private static class DataPoint {
264 public Visit visit;
265 public Sequence<?> value;
266
267 public DataPoint(Visit visit, Sequence<?> value) {
268 this.visit = visit;
269 this.value = value;
270 }
271 }
272
273 }