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.BigDecimal;
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 BigDecimalFactory implements EvaluatorFactory {
031:
032: private static final long serialVersionUID = 400L;
033: private static EvaluatorFactory INSTANCE = new BigDecimalFactory();
034:
035: private BigDecimalFactory() {
036:
037: }
038:
039: public static EvaluatorFactory getInstance() {
040: if (BigDecimalFactory.INSTANCE == null) {
041: BigDecimalFactory.INSTANCE = new BigDecimalFactory();
042: }
043: return BigDecimalFactory.INSTANCE;
044: }
045:
046: public Evaluator getEvaluator(final Operator operator) {
047: if (operator == Operator.EQUAL) {
048: return BigDecimalEqualEvaluator.INSTANCE;
049: } else if (operator == Operator.NOT_EQUAL) {
050: return BigDecimalNotEqualEvaluator.INSTANCE;
051: } else if (operator == Operator.LESS) {
052: return BigDecimalLessEvaluator.INSTANCE;
053: } else if (operator == Operator.LESS_OR_EQUAL) {
054: return BigDecimalLessOrEqualEvaluator.INSTANCE;
055: } else if (operator == Operator.GREATER) {
056: return BigDecimalGreaterEvaluator.INSTANCE;
057: } else if (operator == Operator.GREATER_OR_EQUAL) {
058: return BigDecimalGreaterOrEqualEvaluator.INSTANCE;
059: } else if (operator == Operator.MEMBEROF) {
060: return BigDecimalMemberOfEvaluator.INSTANCE;
061: } else if (operator == Operator.NOTMEMBEROF) {
062: return BigDecimalNotMemberOfEvaluator.INSTANCE;
063: } else {
064: throw new RuntimeException("Operator '" + operator
065: + "' does not exist for BigDecimalEvaluator");
066: }
067: }
068:
069: static class BigDecimalEqualEvaluator extends BaseEvaluator {
070: /**
071: *
072: */
073: private static final long serialVersionUID = 400L;
074: public final static Evaluator INSTANCE = new BigDecimalEqualEvaluator();
075:
076: private BigDecimalEqualEvaluator() {
077: super (ValueType.BIG_DECIMAL_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 "BigDecimal ==";
131: }
132:
133: }
134:
135: static class BigDecimalNotEqualEvaluator extends BaseEvaluator {
136: /**
137: *
138: */
139: private static final long serialVersionUID = 400L;
140: public final static Evaluator INSTANCE = new BigDecimalNotEqualEvaluator();
141:
142: private BigDecimalNotEqualEvaluator() {
143: super (ValueType.BIG_DECIMAL_TYPE, Operator.NOT_EQUAL);
144: }
145:
146: public boolean evaluate(InternalWorkingMemory workingMemory,
147: final Extractor extractor, final Object object1,
148: final FieldValue object2) {
149: final Object value1 = extractor.getValue(workingMemory,
150: object1);
151: final Object value2 = object2.getValue();
152: if (value1 == null) {
153: return value2 != null;
154: }
155: return !value1.equals(value2);
156: }
157:
158: public boolean evaluateCachedRight(
159: InternalWorkingMemory workingMemory,
160: final VariableContextEntry context, final Object left) {
161: final Object value = context.declaration.getExtractor()
162: .getValue(workingMemory, left);
163: if (value == null) {
164: return ((ObjectVariableContextEntry) context).right != null;
165: }
166: return !value
167: .equals(((ObjectVariableContextEntry) context).right);
168: }
169:
170: public boolean evaluateCachedLeft(
171: InternalWorkingMemory workingMemory,
172: final VariableContextEntry context, final Object right) {
173: final Object value = context.extractor.getValue(
174: workingMemory, right);
175: if (((ObjectVariableContextEntry) context).left == null) {
176: return value != null;
177: }
178: return !((ObjectVariableContextEntry) context).left
179: .equals(value);
180: }
181:
182: public boolean evaluate(InternalWorkingMemory workingMemory,
183: final Extractor extractor1, final Object object1,
184: final Extractor extractor2, final Object object2) {
185: final Object value1 = extractor1.getValue(workingMemory,
186: object1);
187: final Object value2 = extractor2.getValue(workingMemory,
188: object2);
189: if (value1 == null) {
190: return value2 != null;
191: }
192: return !value1.equals(value2);
193: }
194:
195: public String toString() {
196: return "BigDecimal !=";
197: }
198: }
199:
200: static class BigDecimalLessEvaluator extends BaseEvaluator {
201: /**
202: *
203: */
204: private static final long serialVersionUID = 400L;
205: public final static Evaluator INSTANCE = new BigDecimalLessEvaluator();
206:
207: private BigDecimalLessEvaluator() {
208: super (ValueType.BIG_DECIMAL_TYPE, Operator.LESS);
209: }
210:
211: public boolean evaluate(InternalWorkingMemory workingMemory,
212: final Extractor extractor, final Object object1,
213: final FieldValue object2) {
214: if (extractor.isNullValue(workingMemory, object1)) {
215: return false;
216: }
217: final BigDecimal comp = (BigDecimal) extractor.getValue(
218: workingMemory, object1);
219: return comp.compareTo((BigDecimal) object2.getValue()) < 0;
220: }
221:
222: public boolean evaluateCachedRight(
223: InternalWorkingMemory workingMemory,
224: final VariableContextEntry context, final Object left) {
225: if (context.rightNull) {
226: return false;
227: }
228: final BigDecimal comp = (BigDecimal) ((ObjectVariableContextEntry) context).right;
229: return comp.compareTo((BigDecimal) context.declaration
230: .getExtractor().getValue(workingMemory, left)) < 0;
231: }
232:
233: public boolean evaluateCachedLeft(
234: InternalWorkingMemory workingMemory,
235: final VariableContextEntry context, final Object right) {
236: if (context.extractor.isNullValue(workingMemory, right)) {
237: return false;
238: }
239: final BigDecimal comp = (BigDecimal) context.extractor
240: .getValue(workingMemory, right);
241: return comp
242: .compareTo((BigDecimal) ((ObjectVariableContextEntry) context).left) < 0;
243: }
244:
245: public boolean evaluate(InternalWorkingMemory workingMemory,
246: final Extractor extractor1, final Object object1,
247: final Extractor extractor2, final Object object2) {
248: if (extractor1.isNullValue(workingMemory, object1)) {
249: return false;
250: }
251: final BigDecimal comp = (BigDecimal) extractor1.getValue(
252: workingMemory, object1);
253: return comp.compareTo((BigDecimal) extractor2.getValue(
254: workingMemory, object2)) < 0;
255: }
256:
257: public String toString() {
258: return "BigDecimal <";
259: }
260: }
261:
262: static class BigDecimalLessOrEqualEvaluator extends BaseEvaluator {
263: /**
264: *
265: */
266: private static final long serialVersionUID = 400L;
267: public final static Evaluator INSTANCE = new BigDecimalLessOrEqualEvaluator();
268:
269: private BigDecimalLessOrEqualEvaluator() {
270: super (ValueType.BIG_DECIMAL_TYPE, Operator.LESS_OR_EQUAL);
271: }
272:
273: public boolean evaluate(InternalWorkingMemory workingMemory,
274: final Extractor extractor, final Object object1,
275: final FieldValue object2) {
276: if (extractor.isNullValue(workingMemory, object1)) {
277: return false;
278: }
279: final BigDecimal comp = (BigDecimal) extractor.getValue(
280: workingMemory, object1);
281: return comp.compareTo((BigDecimal) object2.getValue()) <= 0;
282: }
283:
284: public boolean evaluateCachedRight(
285: InternalWorkingMemory workingMemory,
286: final VariableContextEntry context, final Object left) {
287: if (context.rightNull) {
288: return false;
289: }
290: final BigDecimal comp = (BigDecimal) ((ObjectVariableContextEntry) context).right;
291: return comp.compareTo((BigDecimal) context.declaration
292: .getExtractor().getValue(workingMemory, left)) <= 0;
293: }
294:
295: public boolean evaluateCachedLeft(
296: InternalWorkingMemory workingMemory,
297: final VariableContextEntry context, final Object right) {
298: if (context.extractor.isNullValue(workingMemory, right)) {
299: return false;
300: }
301: final BigDecimal comp = (BigDecimal) context.extractor
302: .getValue(workingMemory, right);
303: return comp
304: .compareTo((BigDecimal) ((ObjectVariableContextEntry) context).left) <= 0;
305: }
306:
307: public boolean evaluate(InternalWorkingMemory workingMemory,
308: final Extractor extractor1, final Object object1,
309: final Extractor extractor2, final Object object2) {
310: if (extractor1.isNullValue(workingMemory, object1)) {
311: return false;
312: }
313: final BigDecimal comp = (BigDecimal) extractor1.getValue(
314: workingMemory, object1);
315: return comp.compareTo((BigDecimal) extractor2.getValue(
316: workingMemory, object2)) <= 0;
317: }
318:
319: public String toString() {
320: return "BigDecimal <=";
321: }
322: }
323:
324: static class BigDecimalGreaterEvaluator extends BaseEvaluator {
325: /**
326: *
327: */
328: private static final long serialVersionUID = 400L;
329: public final static Evaluator INSTANCE = new BigDecimalGreaterEvaluator();
330:
331: private BigDecimalGreaterEvaluator() {
332: super (ValueType.BIG_DECIMAL_TYPE, Operator.GREATER);
333: }
334:
335: public boolean evaluate(InternalWorkingMemory workingMemory,
336: final Extractor extractor, final Object object1,
337: final FieldValue object2) {
338: if (extractor.isNullValue(workingMemory, object1)) {
339: return false;
340: }
341: final BigDecimal comp = (BigDecimal) extractor.getValue(
342: workingMemory, object1);
343: return comp.compareTo((BigDecimal) object2.getValue()) > 0;
344: }
345:
346: public boolean evaluateCachedRight(
347: InternalWorkingMemory workingMemory,
348: final VariableContextEntry context, final Object left) {
349: if (context.rightNull) {
350: return false;
351: }
352: final BigDecimal comp = (BigDecimal) ((ObjectVariableContextEntry) context).right;
353: return comp.compareTo((BigDecimal) context.declaration
354: .getExtractor().getValue(workingMemory, left)) > 0;
355: }
356:
357: public boolean evaluateCachedLeft(
358: InternalWorkingMemory workingMemory,
359: final VariableContextEntry context, final Object right) {
360: if (context.extractor.isNullValue(workingMemory, right)) {
361: return false;
362: }
363: final BigDecimal comp = (BigDecimal) context.extractor
364: .getValue(workingMemory, right);
365: return comp
366: .compareTo((BigDecimal) ((ObjectVariableContextEntry) context).left) > 0;
367: }
368:
369: public boolean evaluate(InternalWorkingMemory workingMemory,
370: final Extractor extractor1, final Object object1,
371: final Extractor extractor2, final Object object2) {
372: if (extractor1.isNullValue(workingMemory, object1)) {
373: return false;
374: }
375: final BigDecimal comp = (BigDecimal) extractor1.getValue(
376: workingMemory, object1);
377: return comp.compareTo((BigDecimal) extractor2.getValue(
378: workingMemory, object2)) > 0;
379: }
380:
381: public String toString() {
382: return "BigDecimal >";
383: }
384: }
385:
386: static class BigDecimalGreaterOrEqualEvaluator extends
387: BaseEvaluator {
388: /**
389: *
390: */
391: private static final long serialVersionUID = 400L;
392: private final static Evaluator INSTANCE = new BigDecimalGreaterOrEqualEvaluator();
393:
394: private BigDecimalGreaterOrEqualEvaluator() {
395: super (ValueType.BIG_DECIMAL_TYPE, Operator.GREATER_OR_EQUAL);
396: }
397:
398: public boolean evaluate(InternalWorkingMemory workingMemory,
399: final Extractor extractor, final Object object1,
400: final FieldValue object2) {
401: if (extractor.isNullValue(workingMemory, object1)) {
402: return false;
403: }
404: final BigDecimal comp = (BigDecimal) extractor.getValue(
405: workingMemory, object1);
406: return comp.compareTo((BigDecimal) object2.getValue()) >= 0;
407: }
408:
409: public boolean evaluateCachedRight(
410: InternalWorkingMemory workingMemory,
411: final VariableContextEntry context, final Object left) {
412: if (context.rightNull) {
413: return false;
414: }
415: final BigDecimal comp = (BigDecimal) ((ObjectVariableContextEntry) context).right;
416: return comp.compareTo((BigDecimal) context.declaration
417: .getExtractor().getValue(workingMemory, left)) >= 0;
418: }
419:
420: public boolean evaluateCachedLeft(
421: InternalWorkingMemory workingMemory,
422: final VariableContextEntry context, final Object right) {
423: if (context.extractor.isNullValue(workingMemory, right)) {
424: return false;
425: }
426: final BigDecimal comp = (BigDecimal) context.extractor
427: .getValue(workingMemory, right);
428: return comp
429: .compareTo((BigDecimal) ((ObjectVariableContextEntry) context).left) >= 0;
430: }
431:
432: public boolean evaluate(InternalWorkingMemory workingMemory,
433: final Extractor extractor1, final Object object1,
434: final Extractor extractor2, final Object object2) {
435: if (extractor1.isNullValue(workingMemory, object1)) {
436: return false;
437: }
438: final BigDecimal comp = (BigDecimal) extractor1.getValue(
439: workingMemory, object1);
440: return comp.compareTo((BigDecimal) extractor2.getValue(
441: workingMemory, object2)) >= 0;
442: }
443:
444: public String toString() {
445: return "BigDecimal >=";
446: }
447: }
448:
449: static class BigDecimalMemberOfEvaluator extends
450: BaseMemberOfEvaluator {
451:
452: private static final long serialVersionUID = 400L;
453: public final static Evaluator INSTANCE = new BigDecimalMemberOfEvaluator();
454:
455: private BigDecimalMemberOfEvaluator() {
456: super (ValueType.BIG_DECIMAL_TYPE, Operator.MEMBEROF);
457: }
458:
459: public String toString() {
460: return "BigDecimal memberOf";
461: }
462: }
463:
464: static class BigDecimalNotMemberOfEvaluator extends
465: BaseNotMemberOfEvaluator {
466: /**
467: *
468: */
469: private static final long serialVersionUID = 400L;
470: public final static Evaluator INSTANCE = new BigDecimalNotMemberOfEvaluator();
471:
472: private BigDecimalNotMemberOfEvaluator() {
473: super (ValueType.BIG_DECIMAL_TYPE, Operator.NOTMEMBEROF);
474: }
475:
476: public String toString() {
477: return "BigDecimal not memberOf";
478: }
479: }
480: }
|