Debian Codegraph @

net/sf/saxon/pattern/VennPattern.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 net.sf.saxon.expr.*;
11 import net.sf.saxon.expr.instruct.SlotManager;
12 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
13 import net.sf.saxon.expr.parser.ExpressionVisitor;
14 import net.sf.saxon.trace.ExpressionPresenter;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.type.ItemType;
17 import net.sf.saxon.value.SequenceType;
18
19 import java.util.HashSet;
20 import java.util.Set;
21
22 /**
23 * Abstract pattern formed as the union, intersection, or difference of two other patterns;
24 * concrete subclasses are used for the different operators
25 */
26
27 public abstract class VennPattern extends Pattern {
28
29 protected Pattern p1, p2;
30
31 /**
32 * Constructor
33 *
34 * @param p1 the left-hand operand
35 * @param p2 the right-hand operand
36 */
37
38 public VennPattern(Pattern p1, Pattern p2) {
39 this.p1 = p1;
40 this.p2 = p2;
41 adoptChildExpression(p1);
42 adoptChildExpression(p2);
43 }
44
45 /**
46 * Get the immediate sub-expressions of this expression, with information about the relationship
47 * of each expression to its parent expression. Default implementation
48 * works off the results of iterateSubExpressions()
49 * <p>If the expression is a Callable, then it is required that the order of the operands
50 * returned by this function is the same as the order of arguments supplied to the corresponding
51 * call() method.</p>
52 *
53 * @return an iterator containing the sub-expressions of this expression
54 */
55 @Override
56 public Iterable<Operand> operands() {
57 return operandList(
58 new Operand(this, p1, OperandRole.SAME_FOCUS_ACTION),
59 new Operand(this, p2, OperandRole.SAME_FOCUS_ACTION));
60 }
61
62 /**
63 * Simplify the pattern: perform any context-independent optimisations
64 *
65 */
66
67 public Pattern simplify() throws XPathException {
68 p1 = p1.simplify();
69 p2 = p2.simplify();
70 return this;
71 }
72
73 /**
74 * Type-check the pattern.
75 * This is only needed for patterns that contain variable references or function calls.
76 *
77 * @return the optimised Pattern
78 */
79
80 public Pattern typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
81 mustBeNodePattern(p1);
82 p1 = p1.typeCheck(visitor, contextItemType);
83 mustBeNodePattern(p2);
84 p2 = p2.typeCheck(visitor, contextItemType);
85 return this;
86 }
87
88 private void mustBeNodePattern(Pattern p) throws XPathException {
89 if (p instanceof NodeTestPattern) {
90 ItemType it = p.getItemType();
91 if (!(it instanceof NodeTest)) {
92 XPathException err = new XPathException("The operands of a union, intersect, or except pattern " +
93 "must be patterns that match nodes", "XPTY0004");
94 err.setIsTypeError(true);
95 throw err;
96 }
97 }
98 }
99
100 /**
101 * Replace any calls on current() by a variable reference bound to the supplied binding
102 */
103 @Override
104 public void bindCurrent(LocalBinding binding) {
105 p1.bindCurrent(binding);
106 p2.bindCurrent(binding);
107 }
108
109 /**
110 * Test whether a pattern is motionless, that is, whether it can be evaluated against a node
111 * without repositioning the input stream. This is a necessary condition for patterns used
112 * as the match pattern of a streamed template rule.
113 *
114 * @return true if the pattern is motionless, that is, if it can be evaluated against a streamed
115 * node without changing the position in the streamed input file
116 */
117 @Override
118 public boolean isMotionless() {
119 return p1.isMotionless() && p2.isMotionless();
120 }
121
122 /**
123 * Allocate slots to any variables used within the pattern
124 *
125 * @param slotManager represents the stack frame on which slots are allocated
126 * @param nextFree the next slot that is free to be allocated @return the next slot that is free to be allocated
127 */
128
129 public int allocateSlots(SlotManager slotManager, int nextFree) {
130 nextFree = p1.allocateSlots(slotManager, nextFree);
131 nextFree = p2.allocateSlots(slotManager, nextFree);
132 return nextFree;
133 }
134
135 /**
136 * Gather the component (non-Venn) patterns of this Venn pattern
137 *
138 * @param set the set into which the components will be added
139 */
140
141 public void gatherComponentPatterns(Set<Pattern> set) {
142 if (p1 instanceof VennPattern) {
143 ((VennPattern) p1).gatherComponentPatterns(set);
144 } else {
145 set.add(p1);
146 }
147 if (p2 instanceof VennPattern) {
148 ((VennPattern) p2).gatherComponentPatterns(set);
149 } else {
150 set.add(p2);
151 }
152 }
153
154
155 /**
156 * Get the dependencies of the pattern. The only possible dependency for a pattern is
157 * on local variables. This is analyzed in those patterns where local variables may appear.
158 *
159 * @return the dependencies, as a bit-significant mask
160 */
161
162 public int getDependencies() {
163 return p1.getDependencies() | p2.getDependencies();
164 }
165
166 /**
167 * Get the LHS of the union
168 *
169 * @return the first operand of the union
170 */
171
172 public Pattern getLHS() {
173 return p1;
174 }
175
176 /**
177 * Get the RHS of the union
178 *
179 * @return the second operand of the union
180 */
181
182 public Pattern getRHS() {
183 return p2;
184 }
185
186 /**
187 * Ask whether the pattern is anchored on a call on current-group()
188 *
189 * @return true if calls on matchesBeneathAnchor should test with all nodes in the
190 * current group as anchor nodes. If false, only the first node in a group is
191 * treated as the anchor node
192 */
193 @Override
194 public boolean matchesCurrentGroup() {
195 return p1.matchesCurrentGroup() || p2.matchesCurrentGroup();
196 }
197
198
199 /**
200 * Determine whether this pattern is the same as another pattern
201 *
202 * @param other the other object
203 */
204
205 public boolean equals(Object other) {
206 if (other instanceof VennPattern) {
207 Set<Pattern> s0 = new HashSet<>(10);
208 gatherComponentPatterns(s0);
209 Set<Pattern> s1 = new HashSet<>(10);
210 ((VennPattern) other).gatherComponentPatterns(s1);
211 return s0.equals(s1);
212 } else {
213 return false;
214 }
215 }
216
217 /**
218 * Hashcode supporting equals()
219 */
220
221 public int computeHashCode() {
222 return 0x9bd723a6 ^ p1.hashCode() ^ p2.hashCode();
223 }
224
225 /**
226 * Get the relevant operator: "union", "intersect", or "except"
227 * @return the operator, as a string
228 */
229
230 protected abstract String getOperatorName();
231
232 /**
233 * Get the original pattern text
234 */
235 @Override
236 public String toString() {
237 return p1 + " " + getOperatorName() + " " + p2;
238 }
239
240 public void export(ExpressionPresenter presenter) throws XPathException{
241 ExpressionPresenter.ExportOptions options = (ExpressionPresenter.ExportOptions) presenter.getOptions();
242 presenter.startElement("p.venn");
243 presenter.emitAttribute("op", getOperatorName());
244 if (options.addStaticType) {
245 SequenceType type = SequenceType.makeSequenceType(getItemType(), StaticProperty.ALLOWS_ONE);
246 presenter.emitAttribute("sType", type.toExportString2());
247 }
248 p1.export(presenter);
249 p2.export(presenter);
250 presenter.endElement();
251 }
252 }