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.ObjectVariableContextEntry;
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: /**
029: * This is the misc "bucket" evaluator factory for objects.
030: * It is fairly limited in operations,
031: * and what operations are available are dependent on the exact type.
032: *
033: * @author Michael Neale
034: */
035: public class FactTemplateFactory implements EvaluatorFactory {
036:
037: private static final long serialVersionUID = 400L;
038: private static EvaluatorFactory INSTANCE = new FactTemplateFactory();
039:
040: private FactTemplateFactory() {
041:
042: }
043:
044: public static EvaluatorFactory getInstance() {
045: if (FactTemplateFactory.INSTANCE == null) {
046: FactTemplateFactory.INSTANCE = new FactTemplateFactory();
047: }
048: return FactTemplateFactory.INSTANCE;
049: }
050:
051: public Evaluator getEvaluator(final Operator operator) {
052: if (operator == Operator.EQUAL) {
053: return FactTemplateEqualEvaluator.INSTANCE;
054: } else if (operator == Operator.NOT_EQUAL) {
055: return FactTemplateNotEqualEvaluator.INSTANCE;
056: } else {
057: throw new RuntimeException("Operator '" + operator
058: + "' does not exist for FactTemplateEvaluator");
059: }
060: }
061:
062: static class FactTemplateEqualEvaluator extends BaseEvaluator {
063: /**
064: *
065: */
066: private static final long serialVersionUID = 400L;
067: public final static Evaluator INSTANCE = new FactTemplateEqualEvaluator();
068:
069: private FactTemplateEqualEvaluator() {
070: super (ValueType.FACTTEMPLATE_TYPE, Operator.EQUAL);
071: }
072:
073: public boolean evaluate(InternalWorkingMemory workingMemory,
074: final Extractor extractor, final Object object1,
075: final FieldValue object2) {
076: final Object value1 = extractor.getValue(workingMemory,
077: object1);
078: final Object value2 = object2.getValue();
079: if (value1 == null) {
080: return value2 == null;
081: }
082: return value1.equals(value2);
083: }
084:
085: public boolean evaluateCachedRight(
086: InternalWorkingMemory workingMemory,
087: final VariableContextEntry context, final Object left) {
088: final Object value = context.declaration.getExtractor()
089: .getValue(workingMemory, left);
090: if (value == null) {
091: return ((ObjectVariableContextEntry) context).right == null;
092: }
093: return value
094: .equals(((ObjectVariableContextEntry) context).right);
095: }
096:
097: public boolean evaluateCachedLeft(
098: InternalWorkingMemory workingMemory,
099: final VariableContextEntry context, final Object right) {
100: final Object value = context.extractor.getValue(
101: workingMemory, right);
102: if (((ObjectVariableContextEntry) context).left == null) {
103: return value == null;
104: }
105: return ((ObjectVariableContextEntry) context).left
106: .equals(value);
107: }
108:
109: public boolean evaluate(InternalWorkingMemory workingMemory,
110: final Extractor extractor1, final Object object1,
111: final Extractor extractor2, final Object object2) {
112: final Object value1 = extractor1.getValue(workingMemory,
113: object1);
114: final Object value2 = extractor2.getValue(workingMemory,
115: object2);
116: if (value1 == null) {
117: return value2 == null;
118: }
119: return value1.equals(value2);
120: }
121:
122: public String toString() {
123: return "FactTemplate ==";
124: }
125:
126: }
127:
128: static class FactTemplateNotEqualEvaluator extends BaseEvaluator {
129: /**
130: *
131: */
132: private static final long serialVersionUID = 400L;
133: public final static Evaluator INSTANCE = new FactTemplateNotEqualEvaluator();
134:
135: private FactTemplateNotEqualEvaluator() {
136: super (ValueType.FACTTEMPLATE_TYPE, Operator.NOT_EQUAL);
137: }
138:
139: public boolean evaluate(InternalWorkingMemory workingMemory,
140: final Extractor extractor, final Object object1,
141: final FieldValue object2) {
142: final Object value1 = extractor.getValue(workingMemory,
143: object1);
144: final Object value2 = object2.getValue();
145: if (value1 == null) {
146: return value2 != null;
147: }
148: return !value1.equals(value2);
149: }
150:
151: public boolean evaluateCachedRight(
152: InternalWorkingMemory workingMemory,
153: final VariableContextEntry context, final Object left) {
154: final Object value = context.declaration.getExtractor()
155: .getValue(workingMemory, left);
156: if (value == null) {
157: return ((ObjectVariableContextEntry) context).right != null;
158: }
159: return !value
160: .equals(((ObjectVariableContextEntry) context).right);
161: }
162:
163: public boolean evaluateCachedLeft(
164: InternalWorkingMemory workingMemory,
165: final VariableContextEntry context, final Object right) {
166: final Object value = context.extractor.getValue(
167: workingMemory, right);
168: if (((ObjectVariableContextEntry) context).left == null) {
169: return value != null;
170: }
171: return !((ObjectVariableContextEntry) context).left
172: .equals(value);
173: }
174:
175: public boolean evaluate(InternalWorkingMemory workingMemory,
176: final Extractor extractor1, final Object object1,
177: final Extractor extractor2, final Object object2) {
178: final Object value1 = extractor1.getValue(workingMemory,
179: object1);
180: final Object value2 = extractor2.getValue(workingMemory,
181: object2);
182: if (value1 == null) {
183: return value2 != null;
184: }
185: return !value1.equals(value2);
186: }
187:
188: public String toString() {
189: return "FactTemplate !=";
190: }
191: }
192: }
|