Debian Codegraph @

net/sf/saxon/functions/Empty.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.functions;
9
10 import net.sf.saxon.expr.*;
11 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
12 import net.sf.saxon.expr.parser.ExpressionVisitor;
13 import net.sf.saxon.expr.parser.Token;
14 import net.sf.saxon.om.Sequence;
15 import net.sf.saxon.om.SequenceIterator;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.tree.iter.LookaheadIterator;
18 import net.sf.saxon.value.BooleanValue;
19
20
21 /**
22 * Implementation of the fn:empty function
23 */
24 public class Empty extends Aggregate {
25
26 public Expression makeOptimizedFunctionCall(
27 ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, final Expression... arguments) throws XPathException {
28
29 // See if we can deduce the answer from the cardinality
30 int c = arguments[0].getCardinality();
31 if (c == StaticProperty.ALLOWS_ONE_OR_MORE) {
32 return Literal.makeLiteral(BooleanValue.FALSE, arguments[0]);
33 } else if (c == StaticProperty.ALLOWS_ZERO) {
34 return Literal.makeLiteral(BooleanValue.TRUE, arguments[0]);
35 }
36
37 // Rewrite
38 // empty(A|B) => empty(A) and empty(B)
39 if (arguments[0] instanceof VennExpression && !visitor.isOptimizeForStreaming()) {
40 VennExpression v = (VennExpression) arguments[0];
41 if (v.getOperator() == Token.UNION) {
42 Expression e0 = SystemFunction.makeCall("empty", getRetainedStaticContext(), v.getLhsExpression());
43 Expression e1 = SystemFunction.makeCall("empty", getRetainedStaticContext(), v.getRhsExpression());
44 return new AndExpression(e0, e1).optimize(visitor, contextInfo);
45 }
46 }
47 return null;
48 }
49
50
51 /**
52 * Evaluate the expression
53 *
54 * @param context the dynamic evaluation context
55 * @param arguments the values of the arguments, supplied as Sequences
56 * @return the result of the evaluation, in the form of a Sequence
57 * @throws net.sf.saxon.trans.XPathException
58 * if a dynamic error occurs during the evaluation of the expression
59 */
60 public BooleanValue call(XPathContext context, Sequence[] arguments) throws XPathException {
61 return BooleanValue.get(empty(arguments[0].iterate()));
62 }
63
64 private static boolean empty(SequenceIterator iter) throws XPathException {
65 boolean result;
66 if ((iter.getProperties() & SequenceIterator.LOOKAHEAD) != 0) {
67 result = !((LookaheadIterator) iter).hasNext();
68 } else {
69 result = iter.next() == null;
70 }
71 iter.close();
72 return result;
73 }
74
75
76 @Override
77 public Expression makeFunctionCall(Expression[] arguments) {
78 return new SystemFunctionCall(this, arguments) {
79
80 /**
81 * Perform optimisation of an expression and its subexpressions.
82 * <p>This method is called after all references to functions and variables have been resolved
83 * to the declaration of the function or variable, and after all type checking has been done.</p>
84 *
85 * @param visitor an expression visitor
86 * @param contextInfo the static type of "." at the point where this expression is invoked.
87 * The parameter is set to null if it is known statically that the context item will be undefined.
88 * If the type of the context item is not known statically, the argument is set to
89 * {@link net.sf.saxon.type.Type#ITEM_TYPE}
90 * @return the original expression, rewritten if appropriate to optimize execution
91 * @throws net.sf.saxon.trans.XPathException if an error is discovered during this phase
92 * (typically a type error)
93 */
94
95 /*@NotNull*/
96 public Expression optimize(/*@NotNull*/ ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
97 Expression e2 = super.optimize(visitor, contextInfo);
98 if (e2 != this) {
99 return e2;
100 }
101 // See if we can deduce the answer from the cardinality
102 int c = getArg(0).getCardinality();
103 if (c == StaticProperty.ALLOWS_ONE_OR_MORE) {
104 return Literal.makeLiteral(BooleanValue.FALSE, e2);
105 } else if (c == StaticProperty.ALLOWS_ZERO) {
106 return Literal.makeLiteral(BooleanValue.TRUE, e2);
107 }
108
109 // Don't sort the argument
110 setArg(0, getArg(0).unordered(false, visitor.isOptimizeForStreaming()));
111
112 // Rewrite
113 // empty(A|B) => empty(A) and empty(B)
114 if (getArg(0) instanceof VennExpression) {
115 VennExpression v = (VennExpression) getArg(0);
116 if (v.getOperator() == Token.UNION && !visitor.isOptimizeForStreaming()) {
117 Expression e0 = SystemFunction.makeCall("empty", getRetainedStaticContext(), v.getLhsExpression());
118 Expression e1 = SystemFunction.makeCall("empty", getRetainedStaticContext(), v.getRhsExpression());
119 return new AndExpression(e0, e1).optimize(visitor, contextInfo);
120 }
121 }
122 return this;
123 }
124
125 /**
126 * Evaluate the function
127 */
128
129 public BooleanValue evaluateItem(XPathContext context) throws XPathException {
130 return BooleanValue.get(effectiveBooleanValue(context));
131 }
132
133 /**
134 * Evaluate the function in a boolean context
135 */
136
137 public boolean effectiveBooleanValue(XPathContext c) throws XPathException {
138 SequenceIterator iter = getArg(0).iterate(c);
139 boolean result;
140 if ((iter.getProperties() & SequenceIterator.LOOKAHEAD) != 0) {
141 result = !((LookaheadIterator) iter).hasNext();
142 } else {
143 result = iter.next() == null;
144 }
145 iter.close();
146 return result;
147 }
148
149 @Override
150 public int getNetCost() {
151 return 0;
152 }
153 };
154 }
155
156 public String getCompilerName() {
157 return "EmptyCompiler";
158 }
159
160 public String getStreamerName() {
161 return "Empty";
162 }
163
164
165 }
166