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