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 FloatFactory implements EvaluatorFactory {
029:
030: private static final long serialVersionUID = 400L;
031: private static EvaluatorFactory INSTANCE = new FloatFactory();
032:
033: private FloatFactory() {
034:
035: }
036:
037: public static EvaluatorFactory getInstance() {
038: if (FloatFactory.INSTANCE == null) {
039: FloatFactory.INSTANCE = new FloatFactory();
040: }
041: return FloatFactory.INSTANCE;
042: }
043:
044: public Evaluator getEvaluator(final Operator operator) {
045: if (operator == Operator.EQUAL) {
046: return FloatEqualEvaluator.INSTANCE;
047: } else if (operator == Operator.NOT_EQUAL) {
048: return FloatNotEqualEvaluator.INSTANCE;
049: } else if (operator == Operator.LESS) {
050: return FloatLessEvaluator.INSTANCE;
051: } else if (operator == Operator.LESS_OR_EQUAL) {
052: return FloatLessOrEqualEvaluator.INSTANCE;
053: } else if (operator == Operator.GREATER) {
054: return FloatGreaterEvaluator.INSTANCE;
055: } else if (operator == Operator.GREATER_OR_EQUAL) {
056: return FloatGreaterOrEqualEvaluator.INSTANCE;
057: } else if (operator == Operator.MEMBEROF) {
058: return FloatMemberOfEvaluator.INSTANCE;
059: } else if (operator == Operator.NOTMEMBEROF) {
060: return FloatNotMemberOfEvaluator.INSTANCE;
061: } else {
062: throw new RuntimeException("Operator '" + operator
063: + "' does not exist for FloatEvaluator");
064: }
065: }
066:
067: static class FloatEqualEvaluator extends BaseEvaluator {
068:
069: private static final long serialVersionUID = 400L;
070: public final static Evaluator INSTANCE = new FloatEqualEvaluator();
071:
072: private FloatEqualEvaluator() {
073: super (ValueType.PFLOAT_TYPE, Operator.EQUAL);
074: }
075:
076: public boolean evaluate(InternalWorkingMemory workingMemory,
077: final Extractor extractor, final Object object1,
078: final FieldValue object2) {
079: if (extractor.isNullValue(workingMemory, object1)) {
080: return object2.isNull();
081: } else if (object2.isNull()) {
082: return false;
083: }
084:
085: // TODO: we are not handling delta right now... maybe we should
086: return extractor.getFloatValue(workingMemory, object1) == object2
087: .getFloatValue();
088: }
089:
090: public boolean evaluateCachedRight(
091: InternalWorkingMemory workingMemory,
092: final VariableContextEntry context, final Object left) {
093: if (context.declaration.getExtractor().isNullValue(
094: workingMemory, left)) {
095: return context.isRightNull();
096: } else if (context.isRightNull()) {
097: return false;
098: }
099:
100: // TODO: we are not handling delta right now... maybe we should
101: return context.declaration.getExtractor().getFloatValue(
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: .getFloatValue(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.getFloatValue(workingMemory, object1) == extractor2
130: .getFloatValue(workingMemory, object2);
131: }
132:
133: public String toString() {
134: return "Float ==";
135: }
136: }
137:
138: static class FloatNotEqualEvaluator extends BaseEvaluator {
139: /**
140: *
141: */
142: private static final long serialVersionUID = 400L;
143: public final static Evaluator INSTANCE = new FloatNotEqualEvaluator();
144:
145: private FloatNotEqualEvaluator() {
146: super (ValueType.PFLOAT_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.getFloatValue(workingMemory, object1) != object2
160: .getFloatValue();
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().getFloatValue(
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: .getFloatValue(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.getFloatValue(workingMemory, object1) != extractor2
203: .getFloatValue(workingMemory, object2);
204: }
205:
206: public String toString() {
207: return "Float !=";
208: }
209: }
210:
211: static class FloatLessEvaluator extends BaseEvaluator {
212: /**
213: *
214: */
215: private static final long serialVersionUID = 400L;
216: public final static Evaluator INSTANCE = new FloatLessEvaluator();
217:
218: private FloatLessEvaluator() {
219: super (ValueType.PFLOAT_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.getFloatValue(workingMemory, object1) < object2
230: .getFloatValue();
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().getFloatValue(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
252: .getFloatValue(workingMemory, 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.getFloatValue(workingMemory, object1) < extractor2
263: .getFloatValue(workingMemory, object2);
264: }
265:
266: public String toString() {
267: return "Float <";
268: }
269: }
270:
271: static class FloatLessOrEqualEvaluator extends BaseEvaluator {
272: /**
273: *
274: */
275: private static final long serialVersionUID = 400L;
276: public final static Evaluator INSTANCE = new FloatLessOrEqualEvaluator();
277:
278: private FloatLessOrEqualEvaluator() {
279: super (ValueType.PFLOAT_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.getFloatValue(workingMemory, object1) <= object2
290: .getFloatValue();
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().getFloatValue(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
312: .getFloatValue(workingMemory, 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.getFloatValue(workingMemory, object1) <= extractor2
323: .getFloatValue(workingMemory, object2);
324: }
325:
326: public String toString() {
327: return "Float <=";
328: }
329: }
330:
331: static class FloatGreaterEvaluator extends BaseEvaluator {
332: /**
333: *
334: */
335: private static final long serialVersionUID = 400L;
336: public final static Evaluator INSTANCE = new FloatGreaterEvaluator();
337:
338: private FloatGreaterEvaluator() {
339: super (ValueType.PFLOAT_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.getFloatValue(workingMemory, object1) > object2
350: .getFloatValue();
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().getFloatValue(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
372: .getFloatValue(workingMemory, 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.getFloatValue(workingMemory, object1) > extractor2
383: .getFloatValue(workingMemory, object2);
384: }
385:
386: public String toString() {
387: return "Float >";
388: }
389: }
390:
391: static class FloatGreaterOrEqualEvaluator extends BaseEvaluator {
392: /**
393: *
394: */
395: private static final long serialVersionUID = 400L;
396: private final static Evaluator INSTANCE = new FloatGreaterOrEqualEvaluator();
397:
398: private FloatGreaterOrEqualEvaluator() {
399: super (ValueType.PFLOAT_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.getFloatValue(workingMemory, object1) >= object2
410: .getFloatValue();
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().getFloatValue(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
432: .getFloatValue(workingMemory, 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.getFloatValue(workingMemory, object1) >= extractor2
443: .getFloatValue(workingMemory, object2);
444: }
445:
446: public String toString() {
447: return "Float >=";
448: }
449: }
450:
451: static class FloatMemberOfEvaluator extends BaseMemberOfEvaluator {
452:
453: private static final long serialVersionUID = 400L;
454: public final static Evaluator INSTANCE = new FloatMemberOfEvaluator();
455:
456: private FloatMemberOfEvaluator() {
457: super (ValueType.PFLOAT_TYPE, Operator.MEMBEROF);
458: }
459:
460: public String toString() {
461: return "Float memberOf";
462: }
463: }
464:
465: static class FloatNotMemberOfEvaluator extends
466: BaseNotMemberOfEvaluator {
467:
468: private static final long serialVersionUID = 400L;
469: public final static Evaluator INSTANCE = new FloatNotMemberOfEvaluator();
470:
471: private FloatNotMemberOfEvaluator() {
472: super (ValueType.PFLOAT_TYPE, Operator.NOTMEMBEROF);
473: }
474:
475: public String toString() {
476: return "Float not memberOf";
477: }
478: }
479:
480: }
|