Debian Codegraph @

net/sf/saxon/trans/rules/Rule.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.trans.rules;
9
10 import net.sf.saxon.expr.XPathContextMajor;
11 import net.sf.saxon.expr.instruct.TemplateRule;
12 import net.sf.saxon.expr.parser.RebindingMap;
13 import net.sf.saxon.om.Item;
14 import net.sf.saxon.pattern.Pattern;
15 import net.sf.saxon.trace.ExpressionPresenter;
16 import net.sf.saxon.trans.XPathException;
17
18 /**
19 * Rule: a template rule, or a strip-space rule used to support the implementation
20 */
21
22 public class Rule {
23 protected Pattern pattern; // The pattern that fires this rule
24 protected RuleTarget action; // The action associated with this rule (usually a Template)
25 protected int precedence; // The import precedence
26 protected int minImportPrecedence;// The minimum import precedence to be considered by xsl:apply-imports
27 protected double priority; // The priority of the rule
28
29 protected Rule next; // The next rule after this one in the chain of rules
30 protected int sequence; // The relative position of this rule, its position in declaration order
31 protected int part; // The relative position of this rule relative to others formed by splitting
32 // on a union pattern
33 private boolean alwaysMatches; // True if the pattern does not need to be tested, because the rule
34 // is on a rule-chain such that the pattern is necessarily satisfied
35 private int rank; // Indicates the relative precedence/priority of a rule within a mode;
36 // used for quick comparison
37
38
39 public Rule() {}
40 /**
41 * Create a Rule.
42 *
43 * @param p the pattern that this rule matches
44 * @param o the object invoked by this rule (usually a Template)
45 * @param prec the precedence of the rule
46 * @param min the minumum import precedence for xsl:apply-imports
47 * @param prio the priority of the rule
48 * @param seq a sequence number for ordering of rules
49 */
50
51 public Rule(/*@NotNull*/ Pattern p, /*@NotNull*/ RuleTarget o, int prec, int min, double prio, int seq, int part) {
52 pattern = p;
53 action = o;
54 precedence = prec;
55 minImportPrecedence = min;
56 priority = prio;
57 next = null;
58 sequence = seq;
59 this.part = part;
60 o.registerRule(this);
61 }
62
63 /**
64 * Copy a rule, including optionally the chain of rules linked to it
65 *
66 * @param r the rule to be copied
67 * @param copyChain true if the whole chain of rules is to be copied
68 */
69
70 protected void copyFrom(Rule r, boolean copyChain) {
71 pattern = r.pattern.copy(new RebindingMap());
72 action = r.action instanceof TemplateRule ? ((TemplateRule) r.action).copy() : r.action;
73 precedence = r.precedence;
74 minImportPrecedence = r.minImportPrecedence;
75 priority = r.priority;
76 sequence = r.sequence;
77 part = r.part;
78 if (r.next == null || !copyChain) {
79 next = null;
80 } else {
81 next = r.next.copy(true);
82 }
83 action.registerRule(this);
84 }
85
86 public Rule copy(boolean copyChain) {
87 Rule r2 = new Rule();
88 r2.copyFrom(this, copyChain);
89 return r2;
90 }
91
92 public int getSequence() {
93 return sequence;
94 }
95
96 public int getPartNumber() {
97 return part;
98 }
99
100 public void setAction(/*@NotNull*/ RuleTarget action) {
101 this.action = action;
102 }
103
104 /*@NotNull*/
105 public RuleTarget getAction() {
106 return action;
107 }
108
109 /*@Nullable*/
110 public Rule getNext() {
111 return next;
112 }
113
114 public void setNext( /*@Nullable*/Rule next) {
115 this.next = next;
116 }
117
118 /*@NotNull*/
119 public Pattern getPattern() {
120 return pattern;
121 }
122
123 public void setPattern(Pattern pattern) {
124 this.pattern = pattern;
125 }
126
127 public int getPrecedence() {
128 return precedence;
129 }
130
131 public int getMinImportPrecedence() {
132 return minImportPrecedence;
133 }
134
135 public double getPriority() {
136 return priority;
137 }
138
139 public void setAlwaysMatches(boolean matches) {
140 alwaysMatches = matches;
141 }
142
143 public boolean isAlwaysMatches() {
144 return alwaysMatches;
145 }
146
147 public void setRank(int rank) {
148 this.rank = rank;
149 }
150
151 public int getRank() {
152 return rank;
153 }
154
155
156 /**
157 * Export this rule
158 * @param out the destination for the export
159 * @param modeStreamable if the mode for this rule is streamable (should be EE only?)
160 */
161 public void export(ExpressionPresenter out, boolean modeStreamable) throws XPathException {
162 RuleTarget target = getAction();
163 TemplateRule template = null;
164 if (target instanceof TemplateRule) {
165 template = (TemplateRule) target;
166 int s = out.startElement("templateRule");
167 out.emitAttribute("prec", getPrecedence() + "");
168 out.emitAttribute("prio", getPriority() + "");
169 out.emitAttribute("seq", getSequence() + "");
170 if (part != 0) {
171 out.emitAttribute("part", "" + part);
172 }
173 out.emitAttribute("rank", "" + getRank());
174 out.emitAttribute("minImp", getMinImportPrecedence() + "");
175 out.emitAttribute("slots", template.getStackFrameMap().getNumberOfVariables() + "");
176 template.explainProperties(out);
177 exportOtherProperties(out);
178 /*int[] preconditionIndexes = getPreconditionIndexes();
179 if (preconditionIndexes != null && preconditionIndexes.length > 0) {
180 boolean first = true;
181 String indexes = "";
182 for (int i : preconditionIndexes) {
183 indexes = indexes + (!first ? " " : "") + i;
184 first = false;
185 }
186 out.emitAttribute("conditions", indexes);
187 }
188 Set<Expression> preconditions = getPreconditions();
189 if (preconditions != null && preconditions.size() > 0) {
190 out.startSubsidiaryElement("c.pre");
191 for (Expression e : preconditions) {
192 e.export(out);
193 }
194 out.endSubsidiaryElement();
195 }*/
196 out.setChildRole("match");
197 getPattern().export(out);
198 if (template.getBody() != null) {
199 out.setChildRole("action");
200 template.getBody().export(out);
201 }
202 int e = out.endElement();
203 if (s != e) {
204 throw new IllegalStateException(
205 "exported expression tree unbalanced in template at line " +
206 (template != null ?
207 template.getLineNumber() + " of " + template.getSystemId() : ""));
208 }
209 } else {
210 target.export(out);
211 }
212 }
213
214 /**
215 * Add other exported properties as required
216 * @param out the export destination
217 */
218 public void exportOtherProperties(ExpressionPresenter out) throws XPathException {}
219
220
221 /**
222 * Rules have an ordering, based on their precedence and priority. This method compares
223 * them using the precomputed rank value.
224 *
225 * @param other Another rule whose ordering rank is to be compared with this one
226 * @return &lt;0 if this rule has lower rank, that is if it has lower precedence or equal
227 * precedence and lower priority. 0 if the two rules have equal precedence and
228 * priority. &gt;0 if this rule has higher rank in precedence/priority order
229 */
230
231 public int compareRank(Rule other) {
232 return rank - other.rank;
233 }
234
235 /**
236 * Rules have an ordering, based on their precedence and priority.
237 *
238 * @param other Another rule whose ordering rank is to be compared with this one
239 * @return &lt;0 if this rule has lower rank, that is if it has lower precedence or equal
240 * precedence and lower priority. 0 if the two rules have equal precedence and
241 * priority. &gt;0 if this rule has higher rank in precedence/priority order
242 */
243
244 public int compareComputedRank(Rule other) {
245 if (precedence == other.precedence) {
246 return Double.compare(priority, other.priority);
247 } else if (precedence < other.precedence) {
248 return -1;
249 } else {
250 return +1;
251 }
252 }
253
254 public boolean matches(Item item, XPathContextMajor context) throws XPathException {
255 return alwaysMatches || pattern.matches(item, context);
256 }
257
258
259 }