001: package org.drools.base.evaluators;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.math.BigInteger;
020:
021: import org.drools.base.BaseEvaluator;
022: import org.drools.base.ValueType;
023: import org.drools.common.InternalWorkingMemory;
024: import org.drools.rule.VariableRestriction.ObjectVariableContextEntry;
025: import org.drools.rule.VariableRestriction.VariableContextEntry;
026: import org.drools.spi.Evaluator;
027: import org.drools.spi.Extractor;
028: import org.drools.spi.FieldValue;
029:
030: public class BigIntegerFactory implements EvaluatorFactory {
031:
032: private static final long serialVersionUID = 400L;
033: private static EvaluatorFactory INSTANCE = new BigIntegerFactory();
034:
035: private BigIntegerFactory() {
036:
037: }
038:
039: public static EvaluatorFactory getInstance() {
040: if (BigIntegerFactory.INSTANCE == null) {
041: BigIntegerFactory.INSTANCE = new BigIntegerFactory();
042: }
043: return BigIntegerFactory.INSTANCE;
044: }
045:
046: public Evaluator getEvaluator(final Operator operator) {
047: if (operator == Operator.EQUAL) {
048: return BigIntegerEqualEvaluator.INSTANCE;
049: } else if (operator == Operator.NOT_EQUAL) {
050: return BigIntegerNotEqualEvaluator.INSTANCE;
051: } else if (operator == Operator.LESS) {
052: return BigIntegerLessEvaluator.INSTANCE;
053: } else if (operator == Operator.LESS_OR_EQUAL) {
054: return BigIntegerLessOrEqualEvaluator.INSTANCE;
055: } else if (operator == Operator.GREATER) {
056: return BigIntegerGreaterEvaluator.INSTANCE;
057: } else if (operator == Operator.GREATER_OR_EQUAL) {
058: return BigIntegerGreaterOrEqualEvaluator.INSTANCE;
059: } else if (operator == Operator.MEMBEROF) {
060: return BigIntegerMemberOfEvaluator.INSTANCE;
061: } else if (operator == Operator.NOTMEMBEROF) {
062: return BigIntegerNotMemberOfEvaluator.INSTANCE;
063: } else {
064: throw new RuntimeException("Operator '" + operator
065: + "' does not exist for BigIntegerEvaluator");
066: }
067: }
068:
069: static class BigIntegerEqualEvaluator extends BaseEvaluator {
070: /**
071: *
072: */
073: private static final long serialVersionUID = 400L;
074: public final static Evaluator INSTANCE = new BigIntegerEqualEvaluator();
075:
076: private BigIntegerEqualEvaluator() {
077: super (ValueType.BIG_INTEGER_TYPE, Operator.EQUAL);
078: }
079:
080: public boolean evaluate(InternalWorkingMemory workingMemory,
081: final Extractor extractor, final Object object1,
082: final FieldValue object2) {
083: final Object value1 = extractor.getValue(workingMemory,
084: object1);
085: final Object value2 = object2.getValue();
086: if (value1 == null) {
087: return value2 == null;
088: }
089: return value1.equals(value2);
090: }
091:
092: public boolean evaluateCachedRight(
093: InternalWorkingMemory workingMemory,
094: final VariableContextEntry context, final Object left) {
095: final Object value = context.declaration.getExtractor()
096: .getValue(workingMemory, left);
097: if (value == null) {
098: return ((ObjectVariableContextEntry) context).right == null;
099: }
100: return value
101: .equals(((ObjectVariableContextEntry) context).right);
102: }
103:
104: public boolean evaluateCachedLeft(
105: InternalWorkingMemory workingMemory,
106: final VariableContextEntry context, final Object right) {
107: final Object value = context.extractor.getValue(
108: workingMemory, right);
109: if (((ObjectVariableContextEntry) context).left == null) {
110: return value == null;
111: }
112: return ((ObjectVariableContextEntry) context).left
113: .equals(value);
114: }
115:
116: public boolean evaluate(InternalWorkingMemory workingMemory,
117: final Extractor extractor1, final Object object1,
118: final Extractor extractor2, final Object object2) {
119: final Object value1 = extractor1.getValue(workingMemory,
120: object1);
121: final Object value2 = extractor2.getValue(workingMemory,
122: object2);
123: if (value1 == null) {
124: return value2 == null;
125: }
126: return value1.equals(value2);
127: }
128:
129: public String toString() {
130: return "BigInteger ==";
131: }
132: }
133:
134: static class BigIntegerNotEqualEvaluator extends BaseEvaluator {
135: /**
136: *
137: */
138: private static final long serialVersionUID = 400L;
139: public final static Evaluator INSTANCE = new BigIntegerNotEqualEvaluator();
140:
141: private BigIntegerNotEqualEvaluator() {
142: super (ValueType.BIG_INTEGER_TYPE, Operator.NOT_EQUAL);
143: }
144:
145: public boolean evaluate(InternalWorkingMemory workingMemory,
146: final Extractor extractor, final Object object1,
147: final FieldValue object2) {
148: final Object value1 = extractor.getValue(workingMemory,
149: object1);
150: final Object value2 = object2.getValue();
151: if (value1 == null) {
152: return value2 != null;
153: }
154: return !value1.equals(value2);
155: }
156:
157: public boolean evaluateCachedRight(
158: InternalWorkingMemory workingMemory,
159: final VariableContextEntry context, final Object left) {
160: final Object value = context.declaration.getExtractor()
161: .getValue(workingMemory, left);
162: if (value == null) {
163: return ((ObjectVariableContextEntry) context).right != null;
164: }
165: return !value
166: .equals(((ObjectVariableContextEntry) context).right);
167: }
168:
169: public boolean evaluateCachedLeft(
170: InternalWorkingMemory workingMemory,
171: final VariableContextEntry context, final Object right) {
172: final Object value = context.extractor.getValue(
173: workingMemory, right);
174: if (((ObjectVariableContextEntry) context).left == null) {
175: return value != null;
176: }
177: return !((ObjectVariableContextEntry) context).left
178: .equals(value);
179: }
180:
181: public boolean evaluate(InternalWorkingMemory workingMemory,
182: final Extractor extractor1, final Object object1,
183: final Extractor extractor2, final Object object2) {
184: final Object value1 = extractor1.getValue(workingMemory,
185: object1);
186: final Object value2 = extractor2.getValue(workingMemory,
187: object2);
188: if (value1 == null) {
189: return value2 != null;
190: }
191: return !value1.equals(value2);
192: }
193:
194: public String toString() {
195: return "BigInteger !=";
196: }
197: }
198:
199: static class BigIntegerLessEvaluator extends BaseEvaluator {
200: /**
201: *
202: */
203: private static final long serialVersionUID = 400L;
204: public final static Evaluator INSTANCE = new BigIntegerLessEvaluator();
205:
206: private BigIntegerLessEvaluator() {
207: super (ValueType.BIG_INTEGER_TYPE, Operator.LESS);
208: }
209:
210: public boolean evaluate(InternalWorkingMemory workingMemory,
211: final Extractor extractor, final Object object1,
212: final FieldValue object2) {
213: if (extractor.isNullValue(workingMemory, object1)) {
214: return false;
215: }
216: final BigInteger comp = (BigInteger) extractor.getValue(
217: workingMemory, object1);
218: return comp.compareTo((BigInteger) object2.getValue()) < 0;
219: }
220:
221: public boolean evaluateCachedRight(
222: InternalWorkingMemory workingMemory,
223: final VariableContextEntry context, final Object left) {
224: if (context.rightNull) {
225: return false;
226: }
227: final BigInteger comp = (BigInteger) ((ObjectVariableContextEntry) context).right;
228: return comp.compareTo((BigInteger) context.declaration
229: .getExtractor().getValue(workingMemory, left)) < 0;
230: }
231:
232: public boolean evaluateCachedLeft(
233: InternalWorkingMemory workingMemory,
234: final VariableContextEntry context, final Object right) {
235: if (context.extractor.isNullValue(workingMemory, right)) {
236: return false;
237: }
238: final BigInteger comp = (BigInteger) context.extractor
239: .getValue(workingMemory, right);
240: return comp
241: .compareTo((BigInteger) ((ObjectVariableContextEntry) context).left) < 0;
242: }
243:
244: public boolean evaluate(InternalWorkingMemory workingMemory,
245: final Extractor extractor1, final Object object1,
246: final Extractor extractor2, final Object object2) {
247: if (extractor1.isNullValue(workingMemory, object1)) {
248: return false;
249: }
250: final BigInteger comp = (BigInteger) extractor1.getValue(
251: workingMemory, object1);
252: return comp.compareTo((BigInteger) extractor2.getValue(
253: workingMemory, object2)) < 0;
254: }
255:
256: public String toString() {
257: return "BigInteger <";
258: }
259: }
260:
261: static class BigIntegerLessOrEqualEvaluator extends BaseEvaluator {
262: /**
263: *
264: */
265: private static final long serialVersionUID = 400L;
266: public final static Evaluator INSTANCE = new BigIntegerLessOrEqualEvaluator();
267:
268: private BigIntegerLessOrEqualEvaluator() {
269: super (ValueType.BIG_INTEGER_TYPE, Operator.LESS_OR_EQUAL);
270: }
271:
272: public boolean evaluate(InternalWorkingMemory workingMemory,
273: final Extractor extractor, final Object object1,
274: final FieldValue object2) {
275: if (extractor.isNullValue(workingMemory, object1)) {
276: return false;
277: }
278: final BigInteger comp = (BigInteger) extractor.getValue(
279: workingMemory, object1);
280: return comp.compareTo((BigInteger) object2.getValue()) <= 0;
281: }
282:
283: public boolean evaluateCachedRight(
284: InternalWorkingMemory workingMemory,
285: final VariableContextEntry context, final Object left) {
286: if (context.rightNull) {
287: return false;
288: }
289: final BigInteger comp = (BigInteger) ((ObjectVariableContextEntry) context).right;
290: return comp.compareTo((BigInteger) context.declaration
291: .getExtractor().getValue(workingMemory, left)) <= 0;
292: }
293:
294: public boolean evaluateCachedLeft(
295: InternalWorkingMemory workingMemory,
296: final VariableContextEntry context, final Object right) {
297: if (context.extractor.isNullValue(workingMemory, right)) {
298: return false;
299: }
300: final BigInteger comp = (BigInteger) context.extractor
301: .getValue(workingMemory, right);
302: return comp
303: .compareTo((BigInteger) ((ObjectVariableContextEntry) context).left) <= 0;
304: }
305:
306: public boolean evaluate(InternalWorkingMemory workingMemory,
307: final Extractor extractor1, final Object object1,
308: final Extractor extractor2, final Object object2) {
309: if (extractor1.isNullValue(workingMemory, object1)) {
310: return false;
311: }
312: final BigInteger comp = (BigInteger) extractor1.getValue(
313: workingMemory, object1);
314: return comp.compareTo((BigInteger) extractor2.getValue(
315: workingMemory, object2)) <= 0;
316: }
317:
318: public String toString() {
319: return "BigInteger <=";
320: }
321: }
322:
323: static class BigIntegerGreaterEvaluator extends BaseEvaluator {
324: /**
325: *
326: */
327: private static final long serialVersionUID = 400L;
328: public final static Evaluator INSTANCE = new BigIntegerGreaterEvaluator();
329:
330: private BigIntegerGreaterEvaluator() {
331: super (ValueType.BIG_INTEGER_TYPE, Operator.GREATER);
332: }
333:
334: public boolean evaluate(InternalWorkingMemory workingMemory,
335: final Extractor extractor, final Object object1,
336: final FieldValue object2) {
337: if (extractor.isNullValue(workingMemory, object1)) {
338: return false;
339: }
340: final BigInteger comp = (BigInteger) extractor.getValue(
341: workingMemory, object1);
342: return comp.compareTo((BigInteger) object2.getValue()) > 0;
343: }
344:
345: public boolean evaluateCachedRight(
346: InternalWorkingMemory workingMemory,
347: final VariableContextEntry context, final Object left) {
348: if (context.rightNull) {
349: return false;
350: }
351: final BigInteger comp = (BigInteger) ((ObjectVariableContextEntry) context).right;
352: return comp.compareTo((BigInteger) context.declaration
353: .getExtractor().getValue(workingMemory, left)) > 0;
354: }
355:
356: public boolean evaluateCachedLeft(
357: InternalWorkingMemory workingMemory,
358: final VariableContextEntry context, final Object right) {
359: if (context.extractor.isNullValue(workingMemory, right)) {
360: return false;
361: }
362: final BigInteger comp = (BigInteger) context.extractor
363: .getValue(workingMemory, right);
364: return comp
365: .compareTo((BigInteger) ((ObjectVariableContextEntry) context).left) > 0;
366: }
367:
368: public boolean evaluate(InternalWorkingMemory workingMemory,
369: final Extractor extractor1, final Object object1,
370: final Extractor extractor2, final Object object2) {
371: if (extractor1.isNullValue(workingMemory, object1)) {
372: return false;
373: }
374: final BigInteger comp = (BigInteger) extractor1.getValue(
375: workingMemory, object1);
376: return comp.compareTo((BigInteger) extractor2.getValue(
377: workingMemory, object2)) > 0;
378: }
379:
380: public String toString() {
381: return "BigInteger >";
382: }
383: }
384:
385: static class BigIntegerGreaterOrEqualEvaluator extends
386: BaseEvaluator {
387: /**
388: *
389: */
390: private static final long serialVersionUID = 400L;
391: private final static Evaluator INSTANCE = new BigIntegerGreaterOrEqualEvaluator();
392:
393: private BigIntegerGreaterOrEqualEvaluator() {
394: super (ValueType.BIG_INTEGER_TYPE, Operator.GREATER_OR_EQUAL);
395: }
396:
397: public boolean evaluate(InternalWorkingMemory workingMemory,
398: final Extractor extractor, final Object object1,
399: final FieldValue object2) {
400: if (extractor.isNullValue(workingMemory, object1)) {
401: return false;
402: }
403: final BigInteger comp = (BigInteger) extractor.getValue(
404: workingMemory, object1);
405: return comp.compareTo((BigInteger) object2.getValue()) >= 0;
406: }
407:
408: public boolean evaluateCachedRight(
409: InternalWorkingMemory workingMemory,
410: final VariableContextEntry context, final Object left) {
411: if (context.rightNull) {
412: return false;
413: }
414: final BigInteger comp = (BigInteger) ((ObjectVariableContextEntry) context).right;
415: return comp.compareTo((BigInteger) context.declaration
416: .getExtractor().getValue(workingMemory, left)) >= 0;
417: }
418:
419: public boolean evaluateCachedLeft(
420: InternalWorkingMemory workingMemory,
421: final VariableContextEntry context, final Object right) {
422: if (context.extractor.isNullValue(workingMemory, right)) {
423: return false;
424: }
425: final BigInteger comp = (BigInteger) context.extractor
426: .getValue(workingMemory, right);
427: return comp
428: .compareTo((BigInteger) ((ObjectVariableContextEntry) context).left) >= 0;
429: }
430:
431: public boolean evaluate(InternalWorkingMemory workingMemory,
432: final Extractor extractor1, final Object object1,
433: final Extractor extractor2, final Object object2) {
434: if (extractor1.isNullValue(workingMemory, object1)) {
435: return false;
436: }
437: final BigInteger comp = (BigInteger) extractor1.getValue(
438: workingMemory, object1);
439: return comp.compareTo((BigInteger) extractor2.getValue(
440: workingMemory, object2)) >= 0;
441: }
442:
443: public String toString() {
444: return "BigInteger >=";
445: }
446: }
447:
448: static class BigIntegerMemberOfEvaluator extends
449: BaseMemberOfEvaluator {
450:
451: private static final long serialVersionUID = 400L;
452: public final static Evaluator INSTANCE = new BigIntegerMemberOfEvaluator();
453:
454: private BigIntegerMemberOfEvaluator() {
455: super (ValueType.BIG_INTEGER_TYPE, Operator.MEMBEROF);
456: }
457:
458: public String toString() {
459: return "BigInteger memberOf";
460: }
461: }
462:
463: static class BigIntegerNotMemberOfEvaluator extends
464: BaseNotMemberOfEvaluator {
465: /**
466: *
467: */
468: private static final long serialVersionUID = 400L;
469: public final static Evaluator INSTANCE = new BigIntegerNotMemberOfEvaluator();
470:
471: private BigIntegerNotMemberOfEvaluator() {
472: super (ValueType.BIG_INTEGER_TYPE, Operator.NOTMEMBEROF);
473: }
474:
475: public String toString() {
476: return "BigInteger not memberOf";
477: }
478: }
479:
480: }
|