Debian Codegraph @

net/sf/saxon/value/Cardinality.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.value;
9
10 import net.sf.saxon.expr.*;
11
12 /**
13 * This class contains static methods to manipulate the cardinality
14 * property of a type.
15 * Cardinality of expressions is denoted by one of the values ONE_OR_MORE, ZERO_OR_MORE,
16 * ZERO_OR_ONE, EXACTLY_ONE, or EMPTY. These are combinations of the three bit-significant
17 * values ALLOWS_ZERO, ALLOWS_ONE, and ALLOWS_MANY.
18 */
19
20 public final class Cardinality {
21
22 /**
23 * Private constructor: no instances allowed
24 */
25
26 private Cardinality() {
27 }
28
29 /**
30 * Determine whether multiple occurrences are allowed
31 *
32 * @param cardinality the cardinality of a sequence
33 * @return true if the cardinality allows the sequence to contain more than one item
34 */
35
36 public static boolean allowsMany(int cardinality) {
37 return (cardinality & StaticProperty.ALLOWS_MANY) != 0;
38 }
39
40 /**
41 * Determine whether multiple occurrences are not only allowed, but likely.
42 * This returns false for an expression that is the atomization of a singleton
43 * node, since in that case it's quite unusual for the typed value to be a
44 * non-singleton sequence.
45 *
46 * @param expression an expression
47 * @return true if multiple occurrences are not only allowed, but likely. Return
48 * false if multiple occurrences are unlikely, even though they might be allowed.
49 * This is typically the case for the atomized sequence that is obtained by atomizing
50 * a singleton node.
51 */
52
53 public static boolean expectsMany(Expression expression) {
54 if (expression instanceof VariableReference) {
55 Binding b = ((VariableReference) expression).getBinding();
56 if (b instanceof LetExpression) {
57 return expectsMany(((LetExpression) b).getSequence());
58 }
59 }
60 if (expression instanceof Atomizer) {
61 return expectsMany(((Atomizer) expression).getBaseExpression());
62 }
63 if (expression instanceof FilterExpression) {
64 return expectsMany(((FilterExpression) expression).getSelectExpression());
65 }
66 return allowsMany(expression.getCardinality());
67 }
68
69 /**
70 * Determine whether empty sequence is allowed
71 *
72 * @param cardinality the cardinality of a sequence
73 * @return true if the cardinality allows the sequence to be empty
74 */
75
76 public static boolean allowsZero(int cardinality) {
77 return (cardinality & StaticProperty.ALLOWS_ZERO) != 0;
78 }
79
80 /**
81 * Form the union of two cardinalities. The cardinality of the expression "if (c) then e1 else e2"
82 * is the union of the cardinalities of e1 and e2.
83 *
84 * @param c1 a cardinality
85 * @param c2 another cardinality
86 * @return the cardinality that allows both c1 and c2
87 */
88
89 public static int union(int c1, int c2) {
90 int r = c1 | c2;
91 // eliminate disallowed options
92 if (r == (StaticProperty.ALLOWS_MANY | StaticProperty.ALLOWS_ZERO)) {
93 r = StaticProperty.ALLOWS_ZERO_OR_MORE;
94 }
95 return r;
96 }
97
98 /**
99 * Add two cardinalities
100 *
101 * @param c1 the first cardinality
102 * @param c2 the second cardinality
103 * @return the cardinality of a sequence formed by concatenating the sequences whose cardinalities
104 * are c1 and c2
105 */
106
107 public static int sum(int c1, int c2) {
108 int min = min(c1) + min(c2);
109 int max = max(c1) + max(c2);
110 return fromMinAndMax(min, max);
111 }
112
113 static int min(int cardinality) {
114 if (allowsZero(cardinality)) {
115 return 0;
116 } else if (cardinality == StaticProperty.ALLOWS_MANY) {
117 return 2;
118 } else {
119 return 1;
120 }
121 }
122
123 static int max(int cardinality) {
124 if (allowsMany(cardinality)) {
125 return 2;
126 } else if (cardinality == StaticProperty.ALLOWS_ZERO) {
127 return 0;
128 } else {
129 return 1;
130 }
131 }
132
133 static int fromMinAndMax(int min, int max) {
134 boolean zero = min == 0;
135 boolean one = min <= 1 || max <= 1;
136 boolean many = max > 1;
137 return (zero ? StaticProperty.ALLOWS_ZERO : 0) +
138 (one ? StaticProperty.ALLOWS_ONE : 0) +
139 (many ? StaticProperty.ALLOWS_MANY : 0);
140 }
141
142 /**
143 * Test if one cardinality subsumes another. Cardinality c1 subsumes c2 if every option permitted
144 * by c2 is also permitted by c1.
145 *
146 * @param c1 a cardinality
147 * @param c2 another cardinality
148 * @return true if if every option permitted
149 * by c2 is also permitted by c1.
150 */
151
152 public static boolean subsumes(int c1, int c2) {
153 return (c1 | c2) == c1;
154 }
155
156
157 /**
158 * Multiply two cardinalities
159 *
160 * @param c1 the first cardinality
161 * @param c2 the second cardinality
162 * @return the product of the cardinalities, that is, the cardinality of the sequence
163 * "for $x in S1 return S2", where c1 is the cardinality of S1 and c2 is the cardinality of S2
164 */
165
166 public static int multiply(int c1, int c2) {
167 if (c1 == StaticProperty.EMPTY || c2 == StaticProperty.EMPTY) {
168 return StaticProperty.EMPTY;
169 }
170 if (c2 == StaticProperty.EXACTLY_ONE) {
171 return c1;
172 }
173 if (c1 == StaticProperty.EXACTLY_ONE) {
174 return c2;
175 }
176 if (c1 == StaticProperty.ALLOWS_ZERO_OR_ONE && c2 == StaticProperty.ALLOWS_ZERO_OR_ONE) {
177 return StaticProperty.ALLOWS_ZERO_OR_ONE;
178 }
179 if (c1 == StaticProperty.ALLOWS_ONE_OR_MORE && c2 == StaticProperty.ALLOWS_ONE_OR_MORE) {
180 return StaticProperty.ALLOWS_ONE_OR_MORE;
181 }
182 return StaticProperty.ALLOWS_ZERO_OR_MORE;
183 }
184
185 /**
186 * Display the cardinality as a string
187 *
188 * @param cardinality the cardinality value to be displayed
189 * @return the representation as a string, for example "zero or one", "zero or more"
190 */
191
192 public static String toString(int cardinality) {
193 switch (cardinality) {
194 case StaticProperty.ALLOWS_ZERO_OR_ONE:
195 return "zero or one";
196 case StaticProperty.EXACTLY_ONE:
197 return "exactly one";
198 case StaticProperty.ALLOWS_ZERO_OR_MORE:
199 return "zero or more";
200 case StaticProperty.ALLOWS_ONE_OR_MORE:
201 return "one or more";
202 case StaticProperty.EMPTY:
203 return "exactly zero";
204 case StaticProperty.ALLOWS_MANY:
205 return "more than one";
206 default:
207 return "code " + cardinality;
208 }
209 }
210
211 /**
212 * Get the occurrence indicator representing the cardinality
213 *
214 * @param cardinality the cardinality value
215 * @return the occurrence indicator, for example "*", "+", "?", "".
216 */
217
218 /*@NotNull*/
219 public static String getOccurrenceIndicator(int cardinality) {
220 switch (cardinality) {
221 case StaticProperty.ALLOWS_ZERO_OR_ONE:
222 return "?";
223 case StaticProperty.EXACTLY_ONE:
224 return "";
225 case StaticProperty.ALLOWS_ZERO_OR_MORE:
226 return "*";
227 case StaticProperty.ALLOWS_ONE_OR_MORE:
228 return "+";
229 case StaticProperty.ALLOWS_MANY:
230 return "+";
231 case StaticProperty.EMPTY:
232 return "\u00B0";
233 default:
234 return "*";
235 // Covers no bits set (which shouldn't arise) and zero_or_many (which can arise, but is too specific for our purposes)
236 }
237 }
238
239 /**
240 * Get the cardinality code from an occurrence indicator
241 *
242 * @param cardinality the cardinality value
243 * @return the occurrence indicator, for example "*", "+", "?", "".
244 */
245
246 /*@NotNull*/
247 public static int fromOccurrenceIndicator(char cardinality) {
248 switch (cardinality) {
249 case '?':
250 return StaticProperty.ALLOWS_ZERO_OR_ONE;
251 case '1': return StaticProperty.EXACTLY_ONE;
252 case '*': return StaticProperty.ALLOWS_ZERO_OR_MORE;
253 case '+': return StaticProperty.ALLOWS_ONE_OR_MORE;
254 case '\u00B0': return StaticProperty.ALLOWS_ZERO;
255 default:
256 throw new AssertionError();
257 }
258 }
259
260
261 /**
262 * Generate Javascript code to check whether a number satisfies the cardinality property.
263 *
264 * @param card the cardinality value
265 * @return a Javascript function which checks whether a number satisfies the cardinality property.
266 */
267
268 public static String generateJavaScriptChecker(int card) {
269 if (Cardinality.allowsZero(card) && Cardinality.allowsMany(card)) {
270 return "function c() {return true;};";
271 } else if (card == StaticProperty.EXACTLY_ONE) {
272 return "function c(n) {return n==1;};";
273 } else if (card == StaticProperty.EMPTY) {
274 return "function c(n) {return n==0;};";
275 } else if (!Cardinality.allowsZero(card)) {
276 return "function c(n) {return n>=1;};";
277 } else {
278 return "function c(n) {return n<=1;};";
279 }
280 }
281 }
282