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.CharVariableContextEntry;
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 CharacterFactory implements EvaluatorFactory {
029:
030: private static final long serialVersionUID = 400L;
031: private static EvaluatorFactory INSTANCE = new CharacterFactory();
032:
033: private CharacterFactory() {
034:
035: }
036:
037: public static EvaluatorFactory getInstance() {
038: if (CharacterFactory.INSTANCE == null) {
039: CharacterFactory.INSTANCE = new CharacterFactory();
040: }
041: return CharacterFactory.INSTANCE;
042: }
043:
044: public Evaluator getEvaluator(final Operator operator) {
045: if (operator == Operator.EQUAL) {
046: return CharacterEqualEvaluator.INSTANCE;
047: } else if (operator == Operator.NOT_EQUAL) {
048: return CharacterNotEqualEvaluator.INSTANCE;
049: } else if (operator == Operator.LESS) {
050: return CharacterLessEvaluator.INSTANCE;
051: } else if (operator == Operator.LESS_OR_EQUAL) {
052: return CharacterLessOrEqualEvaluator.INSTANCE;
053: } else if (operator == Operator.GREATER) {
054: return CharacterGreaterEvaluator.INSTANCE;
055: } else if (operator == Operator.GREATER_OR_EQUAL) {
056: return CharacterGreaterOrEqualEvaluator.INSTANCE;
057: } else if (operator == Operator.MEMBEROF) {
058: return CharacterMemberOfEvaluator.INSTANCE;
059: } else if (operator == Operator.NOTMEMBEROF) {
060: return CharacterNotMemberOfEvaluator.INSTANCE;
061: } else {
062: throw new RuntimeException("Operator '" + operator
063: + "' does not exist for CharacterEvaluator");
064: }
065: }
066:
067: static class CharacterEqualEvaluator extends BaseEvaluator {
068: /**
069: *
070: */
071: private static final long serialVersionUID = 400L;
072: public final static Evaluator INSTANCE = new CharacterEqualEvaluator();
073:
074: private CharacterEqualEvaluator() {
075: super (ValueType.PCHAR_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.getCharValue(workingMemory, object1) == object2
088: .getCharValue();
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().getCharValue(
102: workingMemory, left) == ((CharVariableContextEntry) 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 ((CharVariableContextEntry) context).left == context.extractor
115: .getCharValue(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.getCharValue(workingMemory, object1) == extractor2
128: .getCharValue(workingMemory, object2);
129: }
130:
131: public String toString() {
132: return "Character ==";
133: }
134: }
135:
136: static class CharacterNotEqualEvaluator extends BaseEvaluator {
137: /**
138: *
139: */
140: private static final long serialVersionUID = 400L;
141: public final static Evaluator INSTANCE = new CharacterNotEqualEvaluator();
142:
143: private CharacterNotEqualEvaluator() {
144: super (ValueType.PCHAR_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.getCharValue(workingMemory, object1) != object2
157: .getCharValue();
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().getCharValue(
171: workingMemory, left) != ((CharVariableContextEntry) 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 ((CharVariableContextEntry) context).left != context.extractor
184: .getCharValue(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.getCharValue(workingMemory, object1) != extractor2
197: .getCharValue(workingMemory, object2);
198: }
199:
200: public String toString() {
201: return "Character !=";
202: }
203: }
204:
205: static class CharacterLessEvaluator extends BaseEvaluator {
206: /**
207: *
208: */
209: private static final long serialVersionUID = 400L;
210: public final static Evaluator INSTANCE = new CharacterLessEvaluator();
211:
212: private CharacterLessEvaluator() {
213: super (ValueType.PCHAR_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.getCharValue(workingMemory, object1) < object2
223: .getCharValue();
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 ((CharVariableContextEntry) context).right < context.declaration
233: .getExtractor().getCharValue(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.getCharValue(workingMemory, right) < ((CharVariableContextEntry) context).left;
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: return extractor1.getCharValue(workingMemory, object1) < extractor2
252: .getCharValue(workingMemory, object2);
253: }
254:
255: public String toString() {
256: return "Character <";
257: }
258: }
259:
260: static class CharacterLessOrEqualEvaluator extends BaseEvaluator {
261: /**
262: *
263: */
264: private static final long serialVersionUID = 400L;
265: public final static Evaluator INSTANCE = new CharacterLessOrEqualEvaluator();
266:
267: private CharacterLessOrEqualEvaluator() {
268: super (ValueType.PCHAR_TYPE, Operator.LESS_OR_EQUAL);
269: }
270:
271: public boolean evaluate(InternalWorkingMemory workingMemory,
272: final Extractor extractor, final Object object1,
273: final FieldValue object2) {
274: if (extractor.isNullValue(workingMemory, object1)) {
275: return false;
276: }
277: return extractor.getCharValue(workingMemory, object1) <= object2
278: .getCharValue();
279: }
280:
281: public boolean evaluateCachedRight(
282: InternalWorkingMemory workingMemory,
283: final VariableContextEntry context, final Object left) {
284: if (context.rightNull) {
285: return false;
286: }
287: return ((CharVariableContextEntry) context).right <= context.declaration
288: .getExtractor().getCharValue(workingMemory, left);
289: }
290:
291: public boolean evaluateCachedLeft(
292: InternalWorkingMemory workingMemory,
293: final VariableContextEntry context, final Object right) {
294: if (context.extractor.isNullValue(workingMemory, right)) {
295: return false;
296: }
297: return context.extractor.getCharValue(workingMemory, right) <= ((CharVariableContextEntry) context).left;
298: }
299:
300: public boolean evaluate(InternalWorkingMemory workingMemory,
301: final Extractor extractor1, final Object object1,
302: final Extractor extractor2, final Object object2) {
303: if (extractor1.isNullValue(workingMemory, object1)) {
304: return false;
305: }
306: return extractor1.getCharValue(workingMemory, object1) <= extractor2
307: .getCharValue(workingMemory, object2);
308: }
309:
310: public String toString() {
311: return "Character <=";
312: }
313: }
314:
315: static class CharacterGreaterEvaluator extends BaseEvaluator {
316: /**
317: *
318: */
319: private static final long serialVersionUID = 400L;
320: public final static Evaluator INSTANCE = new CharacterGreaterEvaluator();
321:
322: private CharacterGreaterEvaluator() {
323: super (ValueType.PCHAR_TYPE, Operator.GREATER);
324: }
325:
326: public boolean evaluate(InternalWorkingMemory workingMemory,
327: final Extractor extractor, final Object object1,
328: final FieldValue object2) {
329: if (extractor.isNullValue(workingMemory, object1)) {
330: return false;
331: }
332: return extractor.getCharValue(workingMemory, object1) > object2
333: .getCharValue();
334: }
335:
336: public boolean evaluateCachedRight(
337: InternalWorkingMemory workingMemory,
338: final VariableContextEntry context, final Object left) {
339: if (context.rightNull) {
340: return false;
341: }
342: return ((CharVariableContextEntry) context).right > context.declaration
343: .getExtractor().getCharValue(workingMemory, left);
344: }
345:
346: public boolean evaluateCachedLeft(
347: InternalWorkingMemory workingMemory,
348: final VariableContextEntry context, final Object right) {
349: if (context.extractor.isNullValue(workingMemory, right)) {
350: return false;
351: }
352: return context.extractor.getCharValue(workingMemory, right) > ((CharVariableContextEntry) context).left;
353: }
354:
355: public boolean evaluate(InternalWorkingMemory workingMemory,
356: final Extractor extractor1, final Object object1,
357: final Extractor extractor2, final Object object2) {
358: if (extractor1.isNullValue(workingMemory, object1)) {
359: return false;
360: }
361: return extractor1.getCharValue(workingMemory, object1) > extractor2
362: .getCharValue(workingMemory, object2);
363: }
364:
365: public String toString() {
366: return "Character >";
367: }
368: }
369:
370: static class CharacterGreaterOrEqualEvaluator extends BaseEvaluator {
371: /**
372: *
373: */
374: private static final long serialVersionUID = 400L;
375: private final static Evaluator INSTANCE = new CharacterGreaterOrEqualEvaluator();
376:
377: private CharacterGreaterOrEqualEvaluator() {
378: super (ValueType.PCHAR_TYPE, Operator.GREATER_OR_EQUAL);
379: }
380:
381: public boolean evaluate(InternalWorkingMemory workingMemory,
382: final Extractor extractor, final Object object1,
383: final FieldValue object2) {
384: if (extractor.isNullValue(workingMemory, object1)) {
385: return false;
386: }
387: return extractor.getCharValue(workingMemory, object1) >= object2
388: .getCharValue();
389: }
390:
391: public boolean evaluateCachedRight(
392: InternalWorkingMemory workingMemory,
393: final VariableContextEntry context, final Object left) {
394: if (context.rightNull) {
395: return false;
396: }
397: return ((CharVariableContextEntry) context).right >= context.declaration
398: .getExtractor().getCharValue(workingMemory, left);
399: }
400:
401: public boolean evaluateCachedLeft(
402: InternalWorkingMemory workingMemory,
403: final VariableContextEntry context, final Object right) {
404: if (context.extractor.isNullValue(workingMemory, right)) {
405: return false;
406: }
407: return context.extractor.getCharValue(workingMemory, right) >= ((CharVariableContextEntry) context).left;
408: }
409:
410: public boolean evaluate(InternalWorkingMemory workingMemory,
411: final Extractor extractor1, final Object object1,
412: final Extractor extractor2, final Object object2) {
413: if (extractor1.isNullValue(workingMemory, object1)) {
414: return false;
415: }
416: return extractor1.getCharValue(workingMemory, object1) >= extractor2
417: .getCharValue(workingMemory, object2);
418: }
419:
420: public String toString() {
421: return "Character >=";
422: }
423: }
424:
425: static class CharacterMemberOfEvaluator extends
426: BaseMemberOfEvaluator {
427:
428: private static final long serialVersionUID = 400L;
429: public final static Evaluator INSTANCE = new CharacterMemberOfEvaluator();
430:
431: private CharacterMemberOfEvaluator() {
432: super (ValueType.PCHAR_TYPE, Operator.MEMBEROF);
433: }
434:
435: public String toString() {
436: return "Character memberOf";
437: }
438: }
439:
440: static class CharacterNotMemberOfEvaluator extends
441: BaseNotMemberOfEvaluator {
442:
443: private static final long serialVersionUID = 400L;
444: public final static Evaluator INSTANCE = new CharacterNotMemberOfEvaluator();
445:
446: private CharacterNotMemberOfEvaluator() {
447: super (ValueType.PCHAR_TYPE, Operator.NOTMEMBEROF);
448: }
449:
450: public String toString() {
451: return "Character not memberOf";
452: }
453: }
454:
455: }
|