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.BooleanVariableContextEntry;
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 BooleanFactory implements EvaluatorFactory {
029:
030: private static final long serialVersionUID = 400L;
031: private static EvaluatorFactory INSTANCE = new BooleanFactory();
032:
033: private BooleanFactory() {
034:
035: }
036:
037: public static EvaluatorFactory getInstance() {
038: if (BooleanFactory.INSTANCE == null) {
039: BooleanFactory.INSTANCE = new BooleanFactory();
040: }
041: return BooleanFactory.INSTANCE;
042: }
043:
044: public Evaluator getEvaluator(final Operator operator) {
045: if (operator == Operator.EQUAL) {
046: return BooleanEqualEvaluator.INSTANCE;
047: } else if (operator == Operator.NOT_EQUAL) {
048: return BooleanNotEqualEvaluator.INSTANCE;
049: } else if (operator == Operator.MEMBEROF) {
050: return BooleanMemberOfEvaluator.INSTANCE;
051: } else if (operator == Operator.NOTMEMBEROF) {
052: return BooleanNotMemberOfEvaluator.INSTANCE;
053: } else {
054: throw new RuntimeException("Operator '" + operator
055: + "' does not exist for BooleanEvaluator");
056: }
057: }
058:
059: static class BooleanEqualEvaluator extends BaseEvaluator {
060: /**
061: *
062: */
063: private static final long serialVersionUID = 400L;
064: private final static Evaluator INSTANCE = new BooleanEqualEvaluator();
065:
066: private BooleanEqualEvaluator() {
067: super (ValueType.PBOOLEAN_TYPE, Operator.EQUAL);
068: }
069:
070: public boolean evaluate(InternalWorkingMemory workingMemory,
071: final Extractor extractor, final Object object1,
072: final FieldValue object2) {
073: if (extractor.isNullValue(workingMemory, object1)) {
074: return object2.isNull();
075: } else if (object2.isNull()) {
076: return false;
077: }
078:
079: return extractor.getBooleanValue(workingMemory, object1) == object2
080: .getBooleanValue();
081: }
082:
083: public boolean evaluateCachedRight(
084: InternalWorkingMemory workingMemory,
085: final VariableContextEntry context, final Object left) {
086: if (context.declaration.getExtractor().isNullValue(
087: workingMemory, left)) {
088: return context.isRightNull();
089: } else if (context.isRightNull()) {
090: return false;
091: }
092:
093: return context.declaration.getExtractor().getBooleanValue(
094: workingMemory, left) == ((BooleanVariableContextEntry) context).right;
095: }
096:
097: public boolean evaluateCachedLeft(
098: InternalWorkingMemory workingMemory,
099: final VariableContextEntry context, final Object object2) {
100: if (context.extractor.isNullValue(workingMemory, object2)) {
101: return context.isLeftNull();
102: } else if (context.isLeftNull()) {
103: return false;
104: }
105:
106: return context.extractor.getBooleanValue(workingMemory,
107: object2) == ((BooleanVariableContextEntry) context).left;
108: }
109:
110: public boolean evaluate(InternalWorkingMemory workingMemory,
111: final Extractor extractor1, final Object object1,
112: final Extractor extractor2, final Object object2) {
113: if (extractor1.isNullValue(workingMemory, object1)) {
114: return extractor2.isNullValue(workingMemory, object2);
115: } else if (extractor2.isNullValue(workingMemory, object2)) {
116: return false;
117: }
118:
119: return extractor1.getBooleanValue(workingMemory, object1) == extractor2
120: .getBooleanValue(workingMemory, object2);
121: }
122:
123: public String toString() {
124: return "Boolean ==";
125: }
126:
127: }
128:
129: static class BooleanNotEqualEvaluator extends BaseEvaluator {
130: /**
131: *
132: */
133: private static final long serialVersionUID = 400L;
134: public final static Evaluator INSTANCE = new BooleanNotEqualEvaluator();
135:
136: private BooleanNotEqualEvaluator() {
137: super (ValueType.PBOOLEAN_TYPE, Operator.NOT_EQUAL);
138: }
139:
140: public boolean evaluate(InternalWorkingMemory workingMemory,
141: final Extractor extractor, final Object object1,
142: final FieldValue object2) {
143: if (extractor.isNullValue(workingMemory, object1)) {
144: return !object2.isNull();
145: } else if (object2.isNull()) {
146: return true;
147: }
148:
149: return extractor.getBooleanValue(workingMemory, object1) != object2
150: .getBooleanValue();
151: }
152:
153: public boolean evaluateCachedRight(
154: InternalWorkingMemory workingMemory,
155: final VariableContextEntry context, final Object left) {
156: if (context.declaration.getExtractor().isNullValue(
157: workingMemory, left)) {
158: return !context.isRightNull();
159: } else if (context.isRightNull()) {
160: return true;
161: }
162: return context.declaration.getExtractor().getBooleanValue(
163: workingMemory, left) != ((BooleanVariableContextEntry) context).right;
164: }
165:
166: public boolean evaluateCachedLeft(
167: InternalWorkingMemory workingMemory,
168: final VariableContextEntry context, final Object object2) {
169: if (context.extractor.isNullValue(workingMemory, object2)) {
170: return !context.isLeftNull();
171: } else if (context.isLeftNull()) {
172: return true;
173: }
174:
175: return context.extractor.getBooleanValue(workingMemory,
176: object2) != ((BooleanVariableContextEntry) context).left;
177: }
178:
179: public boolean evaluate(InternalWorkingMemory workingMemory,
180: final Extractor extractor1, final Object object1,
181: final Extractor extractor2, final Object object2) {
182: if (extractor1.isNullValue(workingMemory, object1)) {
183: return !extractor2.isNullValue(workingMemory, object2);
184: } else if (extractor2.isNullValue(workingMemory, object2)) {
185: return true;
186: }
187:
188: return extractor1.getBooleanValue(workingMemory, object1) != extractor1
189: .getBooleanValue(workingMemory, object2);
190: }
191:
192: public String toString() {
193: return "Boolean !=";
194: }
195: }
196:
197: static class BooleanMemberOfEvaluator extends BaseMemberOfEvaluator {
198:
199: private static final long serialVersionUID = 400L;
200: public final static Evaluator INSTANCE = new BooleanMemberOfEvaluator();
201:
202: private BooleanMemberOfEvaluator() {
203: super (ValueType.PBOOLEAN_TYPE, Operator.MEMBEROF);
204: }
205:
206: public String toString() {
207: return "Boolean memberOf";
208: }
209: }
210:
211: static class BooleanNotMemberOfEvaluator extends
212: BaseNotMemberOfEvaluator {
213:
214: private static final long serialVersionUID = 400L;
215: public final static Evaluator INSTANCE = new BooleanNotMemberOfEvaluator();
216:
217: private BooleanNotMemberOfEvaluator() {
218: super (ValueType.PBOOLEAN_TYPE, Operator.NOTMEMBEROF);
219: }
220:
221: public String toString() {
222: return "Boolean not memberOf";
223: }
224: }
225:
226: }
|