Debian Codegraph @

net/sf/saxon/pattern/BooleanExpressionPattern.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.pattern;
9
10 import com.saxonica.ee.stream.Streamability;
11 import com.saxonica.ee.stream.Sweep;
12 import com.saxonica.ee.trans.ContextItemStaticInfoEE;
13 import net.sf.saxon.expr.*;
14 import net.sf.saxon.expr.instruct.SlotManager;
15 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
16 import net.sf.saxon.expr.parser.ExpressionTool;
17 import net.sf.saxon.expr.parser.ExpressionVisitor;
18 import net.sf.saxon.expr.parser.RebindingMap;
19 import net.sf.saxon.om.Item;
20 import net.sf.saxon.trace.ExpressionPresenter;
21 import net.sf.saxon.trans.XPathException;
22 import net.sf.saxon.tree.iter.ManualIterator;
23 import net.sf.saxon.type.AnyItemType;
24 import net.sf.saxon.type.ItemType;
25 import net.sf.saxon.type.UType;
26
27 /**
28 * A BooleanExpressionPattern is a pattern of the form .[ Expr ] introduced in XSLT 3.0. It matches
29 * an item if the expression has an effective boolean value of true() when evaluated with that item
30 * as the singleton focus.
31 *
32 * @author Michael H. Kay
33 */
34
35 public class BooleanExpressionPattern extends Pattern implements PatternWithPredicate {
36
37 private Operand expressionOp;
38 //private Expression expression;
39
40 /**
41 * Create a BooleanExpressionPattern
42 *
43 * @param expression the expression to be evaluated (the expression in the predicate)
44 */
45
46 public BooleanExpressionPattern(Expression expression) {
47 this.expressionOp = new Operand(this, expression, OperandRole.SINGLE_ATOMIC);
48 setPriority(1);
49 }
50
51 @Override
52 public Expression getPredicate() {
53 return expressionOp.getChildExpression();
54 }
55
56 /**
57 * Get the immediate sub-expressions of this expression, with information about the relationship
58 * of each expression to its parent expression. Default implementation
59 * works off the results of iterateSubExpressions()
60 * <p>If the expression is a Callable, then it is required that the order of the operands
61 * returned by this function is the same as the order of arguments supplied to the corresponding
62 * call() method.</p>
63 *
64 * @return an iterator containing the sub-expressions of this expression
65 */
66 @Override
67 public Iterable<Operand> operands() {
68 return expressionOp;
69 }
70
71 /**
72 * Get a UType indicating which kinds of items this Pattern can match.
73 *
74 * @return a UType indicating all the primitive types of item that the pattern can match.
75 */
76 @Override
77 public UType getUType() {
78 if (getPredicate() instanceof InstanceOfExpression) {
79 return ((InstanceOfExpression) getPredicate()).getRequiredItemType().getUType();
80 } else {
81 return UType.ANY;
82 }
83 }
84
85 /**
86 * Allocate slots to any variables used within the pattern
87 *
88 * @param slotManager holds details of the allocated slots
89 * @param nextFree the next slot that is free to be allocated @return the next slot that is free to be allocated
90 */
91
92 public int allocateSlots(SlotManager slotManager, int nextFree) {
93 return ExpressionTool.allocateSlots(getPredicate(), nextFree, slotManager);
94 }
95
96 /**
97 * Type-check the pattern.
98 *
99 * @param visitor the expression visitor
100 * @param contextItemType the type of the context item at the point where the pattern
101 * is defined. Set to null if it is known that the context item is undefined.
102 * @return the optimised Pattern
103 */
104 @Override
105 public Pattern typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
106 ContextItemStaticInfo cit = visitor.getConfiguration().getDefaultContextItemStaticInfo();
107 expressionOp.setChildExpression(getPredicate().typeCheck(visitor, cit));
108 return this;
109 }
110
111 @Override
112 public Pattern optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
113 ContextItemStaticInfo cit = visitor.getConfiguration().getDefaultContextItemStaticInfo();
114 expressionOp.setChildExpression(getPredicate().optimize(visitor, cit));
115 return this;
116 }
117
118 //#ifdefined STREAM
119 @Override
120 public boolean isMotionless() {
121 ContextItemStaticInfoEE cio = (ContextItemStaticInfoEE)getConfiguration().makeContextItemStaticInfo(getItemType(), false);
122 cio.setContextPostureStriding();
123 Streamability.getStreamability(getPredicate(), cio, null);
124 return Streamability.getSweep(getPredicate()) == Sweep.MOTIONLESS;
125 }
126 //#endif
127
128 /**
129 * Determine whether this Pattern matches the given item. This is the main external interface
130 * for matching patterns: it sets current() to the node being tested
131 *
132 * @param item The item to be tested against the Pattern
133 * @param context The context in which the match is to take place. Only relevant if the pattern
134 * uses variables, or contains calls on functions such as document() or key().
135 * @return true if the item matches the Pattern, false otherwise
136 */
137
138 public boolean matches(Item<?> item, XPathContext context) {
139 XPathContext c2 = context.newMinorContext();
140 ManualIterator<Item<?>> iter = new ManualIterator<>(item);
141 c2.setCurrentIterator(iter);
142 c2.setCurrentOutputUri(null);
143 try {
144 return getPredicate().effectiveBooleanValue(c2);
145 } catch (XPathException e) {
146 return false;
147 }
148 }
149
150 /**
151 * Get an Itemtype that all the items matching this pattern must satisfy
152 */
153
154 public ItemType getItemType() {
155 if (getPredicate() instanceof InstanceOfExpression) {
156 InstanceOfExpression ioe = (InstanceOfExpression)getPredicate();
157 if (ioe.getBaseExpression() instanceof ContextItemExpression) {
158 return ioe.getRequiredItemType();
159 }
160 }
161 return AnyItemType.getInstance();
162 }
163
164 /**
165 * Determine the name fingerprint of nodes to which this pattern applies. Used for
166 * optimisation.
167 *
168 * @return A fingerprint that the nodes must match, or -1 if it can match multiple fingerprints
169 */
170
171 public int getFingerprint() {
172 return -1;
173 }
174
175 /**
176 * Display the pattern for diagnostics
177 */
178
179 public String toString() {
180 return ".[" + getPredicate() + "]";
181 }
182
183 /**
184 * Determine whether this pattern is the same as another pattern
185 *
186 * @param other the other object
187 */
188
189 public boolean equals(/*@NotNull*/ Object other) {
190 return (other instanceof BooleanExpressionPattern) &&
191 ((BooleanExpressionPattern) other).getPredicate().isEqual(getPredicate());
192 }
193
194 /**
195 * Hashcode supporting equals()
196 */
197
198 public int computeHashCode() {
199 return 0x7aeffea9 ^ getPredicate().hashCode();
200 }
201
202 /**
203 * Copy a pattern. This makes a deep copy.
204 *
205 * @return the copy of the original pattern
206 * @param rebindings variables that need to be re-bound
207 */
208
209 /*@NotNull*/
210 public Pattern copy(RebindingMap rebindings) {
211 BooleanExpressionPattern n = new BooleanExpressionPattern(getPredicate().copy(rebindings));
212 ExpressionTool.copyLocationInfo(this, n);
213 return n;
214 }
215
216 public void export(ExpressionPresenter presenter) throws XPathException {
217 presenter.startElement("p.booleanExp");
218 /*if (comp != null) {
219 comp.export(presenter);
220 } else { */
221 getPredicate().export(presenter);
222 //}
223 presenter.endElement();
224 }
225
226 }
227