01: /**
02: *
03: */package org.drools.base.evaluators;
04:
05: import java.util.Collection;
06:
07: import org.drools.base.BaseEvaluator;
08: import org.drools.base.ValueType;
09: import org.drools.common.InternalWorkingMemory;
10: import org.drools.rule.VariableRestriction.ObjectVariableContextEntry;
11: import org.drools.rule.VariableRestriction.VariableContextEntry;
12: import org.drools.spi.Extractor;
13: import org.drools.spi.FieldValue;
14:
15: /**
16: * This is a base class for Not MemberOf Evaluators
17: *
18: * @author etirelli
19: *
20: */
21: public abstract class BaseNotMemberOfEvaluator extends BaseEvaluator {
22:
23: public BaseNotMemberOfEvaluator(ValueType type, Operator operator) {
24: super (type, operator);
25: }
26:
27: public boolean evaluate(InternalWorkingMemory workingMemory,
28: final Extractor extractor, final Object object1,
29: final FieldValue object2) {
30: if (object2.isNull()) {
31: return false;
32: } else if (!object2.isCollectionField()) {
33: throw new ClassCastException(
34: "Can't check if an attribute is not member of an object of class "
35: + object2.getValue().getClass());
36: }
37: final Collection col = (Collection) object2.getValue();
38: final Object value = extractor.getValue(workingMemory, object1);
39: return !col.contains(value);
40: }
41:
42: public boolean evaluateCachedRight(
43: InternalWorkingMemory workingMemory,
44: final VariableContextEntry context, final Object left) {
45: final Object object = context.declaration.getExtractor()
46: .getValue(workingMemory, left);
47: if (object == null) {
48: return false;
49: } else if (!(object instanceof Collection)) {
50: throw new ClassCastException(
51: "Can't check if an attribute is not member of an object of class "
52: + object.getClass());
53: }
54: final Collection col = (Collection) object;
55: final Object value = ((ObjectVariableContextEntry) context).right;
56: return !col.contains(value);
57: }
58:
59: public boolean evaluateCachedLeft(
60: InternalWorkingMemory workingMemory,
61: final VariableContextEntry context, final Object right) {
62: final Object object = ((ObjectVariableContextEntry) context).left;
63: if (object == null) {
64: return false;
65: } else if (!(object instanceof Collection)) {
66: throw new ClassCastException(
67: "Can't check if an attribute is not member of an object of class "
68: + object.getClass());
69: }
70: final Collection col = (Collection) object;
71: final Object value = context.extractor.getValue(workingMemory,
72: right);
73: return !col.contains(value);
74: }
75:
76: public boolean evaluate(InternalWorkingMemory workingMemory,
77: final Extractor extractor1, final Object object1,
78: final Extractor extractor2, final Object object2) {
79: final Object object = extractor2.getValue(workingMemory,
80: object2);
81: if (object == null) {
82: return false;
83: } else if (!(object instanceof Collection)) {
84: throw new ClassCastException(
85: "Can't check if an attribute is not member of an object of class "
86: + object.getClass());
87: }
88: final Collection col = (Collection) object;
89: final Object value = extractor1
90: .getValue(workingMemory, object1);
91: return !col.contains(value);
92: }
93:
94: public abstract String toString();
95:
96: }
|