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 MemberOf Evaluators
17: *
18: * @author etirelli
19: */
20: public abstract class BaseMemberOfEvaluator extends BaseEvaluator {
21:
22: public BaseMemberOfEvaluator(ValueType type, Operator operator) {
23: super (type, operator);
24: }
25:
26: public boolean evaluate(InternalWorkingMemory workingMemory,
27: final Extractor extractor, final Object object1,
28: final FieldValue object2) {
29: if (object2.isNull()) {
30: return false;
31: } else if (!object2.isCollectionField()) {
32: throw new ClassCastException(
33: "Can't check if an attribute is member of an object of class "
34: + object2.getValue().getClass());
35: }
36: final Collection col = (Collection) object2.getValue();
37: final Object value = extractor.getValue(workingMemory, object1);
38: return col.contains(value);
39: }
40:
41: public boolean evaluateCachedRight(
42: InternalWorkingMemory workingMemory,
43: final VariableContextEntry context, final Object left) {
44: final Object object = context.declaration.getExtractor()
45: .getValue(workingMemory, left);
46: if (object == null) {
47: return false;
48: } else if (!(object instanceof Collection)) {
49: throw new ClassCastException(
50: "Can't check if an attribute is member of an object of class "
51: + object.getClass());
52: }
53: final Collection col = (Collection) object;
54: final Object value = ((ObjectVariableContextEntry) context).right;
55: return col.contains(value);
56: }
57:
58: public boolean evaluateCachedLeft(
59: InternalWorkingMemory workingMemory,
60: final VariableContextEntry context, final Object right) {
61: final Object object = ((ObjectVariableContextEntry) context).left;
62: if (object == null) {
63: return false;
64: } else if (!(object instanceof Collection)) {
65: throw new ClassCastException(
66: "Can't check if an attribute is member of an object of class "
67: + object.getClass());
68: }
69: final Collection col = (Collection) object;
70: final Object value = context.extractor.getValue(workingMemory,
71: right);
72: return col.contains(value);
73: }
74:
75: public boolean evaluate(InternalWorkingMemory workingMemory,
76: final Extractor extractor1, final Object object1,
77: final Extractor extractor2, final Object object2) {
78: final Object object = extractor2.getValue(workingMemory,
79: object2);
80: if (object == null) {
81: return false;
82: } else if (!(object instanceof Collection)) {
83: throw new ClassCastException(
84: "Can't check if an attribute is member of an object of class "
85: + object.getClass());
86: }
87: final Collection col = (Collection) object;
88: final Object value = extractor1
89: .getValue(workingMemory, object1);
90: return col.contains(value);
91: }
92:
93: public abstract String toString();
94:
95: }
|