Debian Codegraph @

net/sf/saxon/functions/Exists.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 import net.sf.saxon.value.Cardinality;
20
21
22 /**
23 * Implementation of the fn:exists function
24 */
25 public class Exists extends Aggregate {
26
27 @Override
28 public Expression makeFunctionCall(Expression[] arguments) {
29 return new SystemFunctionCall(this, arguments) {
30
31 public Expression optimize(/*@NotNull*/ ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
32 Expression e2 = super.optimize(visitor, contextInfo);
33 if (e2 != this) {
34 return e2;
35 }
36 // See if we can deduce the answer from the cardinality
37 int c = getArg(0).getCardinality();
38 if (!Cardinality.allowsZero(c)) {
39 return Literal.makeLiteral(BooleanValue.TRUE, e2);
40 } else if (c == StaticProperty.ALLOWS_ZERO) {
41 return Literal.makeLiteral(BooleanValue.FALSE, e2);
42 }
43
44 // Don't sort the argument
45 setArg(0, getArg(0).unordered(false, visitor.isOptimizeForStreaming()));
46
47 // Rewrite
48 // exists(A|B) => exists(A) or exists(B)
49 if (getArg(0) instanceof VennExpression && !visitor.isOptimizeForStreaming()) {
50 VennExpression v = (VennExpression) getArg(0);
51 if (v.getOperator() == Token.UNION) {
52 Expression e0 = SystemFunction.makeCall("exists", getRetainedStaticContext(), v.getLhsExpression());
53 Expression e1 = SystemFunction.makeCall("exists", getRetainedStaticContext(), v.getRhsExpression());
54 return new OrExpression(e0, e1).optimize(visitor, contextInfo);
55 }
56 }
57 return this;
58 }
59
60 @Override
61 public boolean effectiveBooleanValue(XPathContext c) throws XPathException {
62 try {
63 boolean result;
64 SequenceIterator iter = getArg(0).iterate(c);
65 if ((iter.getProperties() & SequenceIterator.LOOKAHEAD) != 0) {
66 result = ((LookaheadIterator) iter).hasNext();
67 } else {
68 result = iter.next() != null;
69 }
70 iter.close();
71 return result;
72 } catch (XPathException e) {
73 e.maybeSetLocation(getLocation());
74 e.maybeSetContext(c);
75 throw e;
76 }
77 }
78
79 @Override
80 public BooleanValue evaluateItem(XPathContext context) throws XPathException {
81 return BooleanValue.get(effectiveBooleanValue(context));
82 }
83
84 @Override
85 public int getNetCost() {
86 return 0;
87 }
88 };
89 }
90
91
92 public Expression makeOptimizedFunctionCall(
93 ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, final Expression... arguments) throws XPathException {
94
95 // See if we can deduce the answer from the cardinality
96 int c = arguments[0].getCardinality();
97 if (c == StaticProperty.ALLOWS_ONE_OR_MORE) {
98 return Literal.makeLiteral(BooleanValue.TRUE, arguments[0]);
99 } else if (c == StaticProperty.ALLOWS_ZERO) {
100 return Literal.makeLiteral(BooleanValue.FALSE, arguments[0]);
101 }
102
103 // Don't sort the argument
104 arguments[0] = arguments[0].unordered(false, visitor.isOptimizeForStreaming());
105
106 // Rewrite
107 // exists(A|B) => exists(A) or exists(B)
108 if (arguments[0] instanceof VennExpression && !visitor.isOptimizeForStreaming()) {
109 VennExpression v = (VennExpression) arguments[0];
110 if (v.getOperator() == Token.UNION) {
111 Expression e0 = SystemFunction.makeCall("exists", getRetainedStaticContext(), v.getLhsExpression());
112 Expression e1 = SystemFunction.makeCall("exists", getRetainedStaticContext(), v.getRhsExpression());
113 return new OrExpression(e0, e1).optimize(visitor, contextInfo);
114 }
115 }
116 return null;
117 }
118
119
120
121 private static boolean exists(SequenceIterator iter) throws XPathException {
122 boolean result;
123 if ((iter.getProperties() & SequenceIterator.LOOKAHEAD) != 0) {
124 result = ((LookaheadIterator) iter).hasNext();
125 } else {
126 result = iter.next() != null;
127 }
128 iter.close();
129 return result;
130 }
131
132 /**
133 * Evaluate the expression
134 *
135 * @param context the dynamic evaluation context
136 * @param arguments the values of the arguments, supplied as SequenceIterators
137 * @return the result of the evaluation, in the form of a SequenceIterator
138 * @throws net.sf.saxon.trans.XPathException
139 * if a dynamic error occurs during the evaluation of the expression
140 */
141 public BooleanValue call(XPathContext context, Sequence[] arguments) throws XPathException {
142 return BooleanValue.get(exists(arguments[0].iterate()));
143 }
144
145 public String getCompilerName() {
146 return "ExistsCompiler";
147 }
148
149 public String getStreamerName() {
150 return "Exists";
151 }
152
153 }
154