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