Debian Codegraph @

net/sf/saxon/expr/sort/ConditionalSorter.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.sort;
9
10 import net.sf.saxon.expr.*;
11 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
12 import net.sf.saxon.expr.parser.ExpressionTool;
13 import net.sf.saxon.expr.parser.ExpressionVisitor;
14 import net.sf.saxon.expr.parser.RebindingMap;
15 import net.sf.saxon.om.SequenceIterator;
16 import net.sf.saxon.trace.ExpressionPresenter;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.type.ItemType;
19 import net.sf.saxon.value.SequenceType;
20
21 /**
22 * An expression that sorts an underlying sequence into document order if some condition is true, or that
23 * returns the sequence "as is" (knowing that it doesn't need sorting) if the condition is false.
24 */
25 public class ConditionalSorter extends Expression {
26
27 private Operand conditionOp;
28 private Operand sorterOp;
29
30 /**
31 * Generalized abstraction of the simplify(), typeCheck(), and optimize() methods, which
32 * in this case are all structurally the same.
33 */
34
35 @FunctionalInterface
36 private interface RewriteAction {
37 // TODO: extend this mechanism to be used by other expression classes.
38 Expression rewrite(Expression e) throws XPathException;
39 }
40
41 private final static OperandRole DOC_SORTER_ROLE =
42 new OperandRole(OperandRole.CONSTRAINED_CLASS, OperandUsage.TRANSMISSION, SequenceType.ANY_SEQUENCE);
43
44 /**
45 * Create a conditional document sorter
46 *
47 * @param condition the conditional expression
48 * @param sorter the sorting expression
49 */
50
51 public ConditionalSorter(Expression condition, DocumentSorter sorter) {
52 conditionOp = new Operand(this, condition, OperandRole.SINGLE_ATOMIC);
53 sorterOp = new Operand(this, sorter, DOC_SORTER_ROLE);
54 adoptChildExpression(condition);
55 adoptChildExpression(sorter);
56 }
57
58 @Override
59 public Iterable<Operand> operands() {
60 return operandList(conditionOp, sorterOp);
61 }
62
63
64 public void setCondition(Expression condition) {
65 conditionOp.setChildExpression(condition);
66 }
67
68 public void setDocumentSorter(DocumentSorter documentSorter) {
69 sorterOp.setChildExpression(documentSorter);
70 }
71
72 /**
73 * Get the condition under which the nodes need to be sorted
74 *
75 * @return the condition (an expression)
76 */
77
78 /*@NotNull*/
79 public Expression getCondition() {
80 return conditionOp.getChildExpression();
81 }
82
83 /**
84 * Get the document sorter, which sorts the nodes if the condition is true
85 *
86 * @return the document sorter
87 */
88
89 /*@NotNull*/
90 public DocumentSorter getDocumentSorter() {
91 return (DocumentSorter)sorterOp.getChildExpression();
92 }
93
94 @Override
95 public Expression simplify() throws XPathException {
96 return rewrite(Expression::simplify);
97 }
98
99 /**
100 * Perform type checking of an expression and its subexpressions. This is the second phase of
101 * static optimization.
102 */
103
104 /*@NotNull*/
105 public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
106 return rewrite(exp -> exp.typeCheck(visitor, contextInfo));
107 }
108
109 /**
110 * Determine the static cardinality of the expression. This establishes how many items
111 * there will be in the result of the expression, at compile time (i.e., without
112 * actually evaluating the result.
113 *
114 * @return one of the values Cardinality.ONE_OR_MORE,
115 * Cardinality.ZERO_OR_MORE, Cardinality.EXACTLY_ONE,
116 * Cardinality.ZERO_OR_ONE, Cardinality.EMPTY. This default
117 * implementation returns ZERO_OR_MORE (which effectively gives no
118 * information).
119 */
120
121 public int getCardinality() {
122 return getDocumentSorter().getCardinality();
123 }
124
125
126 /**
127 * Compute the special properties of this expression. These properties are denoted by a bit-significant
128 * integer, possible values are in class {@link net.sf.saxon.expr.StaticProperty}. The "special" properties are properties
129 * other than cardinality and dependencies, and most of them relate to properties of node sequences, for
130 * example whether the nodes are in document order.
131 *
132 * @return the special properties, as a bit-significant integer
133 */
134
135 protected int computeSpecialProperties() {
136 return getCondition().getSpecialProperties()
137 | StaticProperty.ORDERED_NODESET
138 & ~StaticProperty.REVERSE_DOCUMENT_ORDER;
139 }
140
141 /**
142 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
143 * This method indicates which of these methods is provided directly. The other methods will always be available
144 * indirectly, using an implementation that relies on one of the other methods.
145 *
146 * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or
147 * {@link #PROCESS_METHOD}
148 */
149
150 public int getImplementationMethod() {
151 return ITERATE_METHOD;
152 }
153
154 @Override
155 /*@NotNull*/
156 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
157 return rewrite(exp -> exp.optimize(visitor, contextInfo));
158 }
159
160 private Expression rewrite(RewriteAction rewriter) throws XPathException {
161 Expression base = rewriter.rewrite(getDocumentSorter());
162 if (base instanceof DocumentSorter) {
163 sorterOp.setChildExpression(base);
164 } else {
165 return base;
166 }
167 Expression cond = rewriter.rewrite(getCondition());
168 if (cond instanceof Literal) {
169 boolean b = ((Literal) cond).getValue().effectiveBooleanValue();
170 if (b) {
171 return base;
172 } else {
173 return ((DocumentSorter) base).getBaseExpression();
174 }
175 } else {
176 conditionOp.setChildExpression(cond);
177 return this;
178 }
179 }
180
181 @Override
182 public Expression unordered(boolean retainAllNodes, boolean forStreaming) throws XPathException {
183 Expression base = getDocumentSorter().unordered(retainAllNodes, forStreaming);
184 if (base instanceof DocumentSorter) {
185 return this;
186 } else {
187 return base;
188 }
189 }
190
191 /**
192 * Compute the static cardinality of this expression
193 *
194 * @return the computed cardinality, as one of the values {@link net.sf.saxon.expr.StaticProperty#ALLOWS_ZERO_OR_ONE},
195 * {@link net.sf.saxon.expr.StaticProperty#EXACTLY_ONE}, {@link net.sf.saxon.expr.StaticProperty#ALLOWS_ONE_OR_MORE},
196 * {@link net.sf.saxon.expr.StaticProperty#ALLOWS_ZERO_OR_MORE}
197 */
198
199 protected int computeCardinality() {
200 return StaticProperty.ALLOWS_ZERO_OR_MORE;
201 }
202
203 /**
204 * Copy an expression. This makes a deep copy.
205 *
206 * @return the copy of the original expression
207 * @param rebindings
208 */
209
210 /*@NotNull*/
211 public Expression copy(RebindingMap rebindings) {
212 ConditionalSorter cs = new ConditionalSorter(getCondition().copy(rebindings), (DocumentSorter) getDocumentSorter().copy(rebindings));
213 ExpressionTool.copyLocationInfo(this, cs);
214 return cs;
215 }
216
217 /**
218 * Diagnostic print of expression structure. The abstract expression tree
219 * is written to the supplied output destination.
220 *
221 * @param out the expression presenter used to display the structure
222 */
223
224 public void export(ExpressionPresenter out) throws XPathException {
225 out.startElement("conditionalSort", this);
226 getCondition().export(out);
227 getDocumentSorter().export(out);
228 out.endElement();
229 }
230
231 /**
232 * Determine the data type of the expression, if possible. All expression return
233 * sequences, in general; this method determines the type of the items within the
234 * sequence, assuming that (a) this is known in advance, and (b) it is the same for
235 * all items in the sequence.
236 * <p>This method should always return a result, though it may be the best approximation
237 * that is available at the time.</p>
238 *
239 * @return a value such as Type.STRING, Type.BOOLEAN, Type.NUMBER,
240 * Type.NODE, or Type.ITEM (meaning not known at compile time)
241 */
242
243 /*@NotNull*/
244 public ItemType getItemType() {
245 return getDocumentSorter().getItemType();
246 }
247
248
249 /**
250 * Return an Iterator to iterate over the values of a sequence. The value of every
251 * expression can be regarded as a sequence, so this method is supported for all
252 * expressions. This default implementation handles iteration for expressions that
253 * return singleton values: for non-singleton expressions, the subclass must
254 * provide its own implementation.
255 *
256 * @param context supplies the context for evaluation
257 * @return a SequenceIterator that can be used to iterate over the result
258 * of the expression
259 * @throws net.sf.saxon.trans.XPathException
260 * if any dynamic error occurs evaluating the
261 * expression
262 */
263
264 /*@NotNull*/
265 public SequenceIterator<?> iterate(XPathContext context) throws XPathException {
266 boolean b = getCondition().effectiveBooleanValue(context);
267 if (b) {
268 return getDocumentSorter().iterate(context);
269 } else {
270 return getDocumentSorter().getBaseExpression().iterate(context);
271 }
272 }
273
274 /**
275 * Get a name identifying the kind of expression, in terms meaningful to a user.
276 *
277 * @return a name identifying the kind of expression, in terms meaningful to a user.
278 * The name will always be in the form of a lexical XML QName, and should match the name used
279 * in export() output displaying the expression.
280 */
281 @Override
282 public String getExpressionName() {
283 return "conditionalSort";
284 }
285
286
287
288 }
289