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