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 StringFactory implements EvaluatorFactory {
036:
037: private static final long serialVersionUID = 400L;
038: private static EvaluatorFactory INSTANCE = new StringFactory();
039:
040: private StringFactory() {
041:
042: }
043:
044: public static EvaluatorFactory getInstance() {
045: if (StringFactory.INSTANCE == null) {
046: StringFactory.INSTANCE = new StringFactory();
047: }
048: return StringFactory.INSTANCE;
049: }
050:
051: public Evaluator getEvaluator(final Operator operator) {
052: if (operator == Operator.EQUAL) {
053: return StringEqualEvaluator.INSTANCE;
054: } else if (operator == Operator.NOT_EQUAL) {
055: return StringNotEqualEvaluator.INSTANCE;
056: } else if (operator == Operator.MATCHES) {
057: return StringMatchesEvaluator.INSTANCE;
058: } else if (operator == Operator.NOT_MATCHES) {
059: return StringNotMatchesEvaluator.INSTANCE;
060: } else if (operator == Operator.MEMBEROF) {
061: return StringMemberOfEvaluator.INSTANCE;
062: } else if (operator == Operator.NOTMEMBEROF) {
063: return StringNotMemberOfEvaluator.INSTANCE;
064: } else {
065: throw new RuntimeException("Operator '" + operator
066: + "' does not exist for StringEvaluator");
067: }
068: }
069:
070: static class StringEqualEvaluator extends BaseEvaluator {
071: /**
072: *
073: */
074: private static final long serialVersionUID = 400L;
075: public final static Evaluator INSTANCE = new StringEqualEvaluator();
076:
077: private StringEqualEvaluator() {
078: super (ValueType.STRING_TYPE, Operator.EQUAL);
079: }
080:
081: public boolean evaluate(InternalWorkingMemory workingMemory,
082: final Extractor extractor, final Object object1,
083: final FieldValue object2) {
084: final Object value1 = extractor.getValue(workingMemory,
085: object1);
086: final Object value2 = object2.getValue();
087: if (value1 == null) {
088: return value2 == null;
089: }
090: return value1.equals(value2);
091: }
092:
093: public boolean evaluateCachedRight(
094: InternalWorkingMemory workingMemory,
095: final VariableContextEntry context, final Object left) {
096: final Object value = context.declaration.getExtractor()
097: .getValue(workingMemory, left);
098: if (value == null) {
099: return ((ObjectVariableContextEntry) context).right == null;
100: }
101: return value
102: .equals(((ObjectVariableContextEntry) context).right);
103: }
104:
105: public boolean evaluateCachedLeft(
106: InternalWorkingMemory workingMemory,
107: final VariableContextEntry context, final Object right) {
108: final Object value = context.extractor.getValue(
109: workingMemory, right);
110: if (((ObjectVariableContextEntry) context).left == null) {
111: return value == null;
112: }
113: return ((ObjectVariableContextEntry) context).left
114: .equals(value);
115: }
116:
117: public boolean evaluate(InternalWorkingMemory workingMemory,
118: final Extractor extractor1, final Object object1,
119: final Extractor extractor2, final Object object2) {
120: final Object value1 = extractor1.getValue(workingMemory,
121: object1);
122: final Object value2 = extractor2.getValue(workingMemory,
123: object2);
124: if (value1 == null) {
125: return value2 == null;
126: }
127: return value1.equals(value2);
128: }
129:
130: public String toString() {
131: return "String ==";
132: }
133:
134: }
135:
136: static class StringNotEqualEvaluator extends BaseEvaluator {
137: /**
138: *
139: */
140: private static final long serialVersionUID = 400L;
141: public final static Evaluator INSTANCE = new StringNotEqualEvaluator();
142:
143: private StringNotEqualEvaluator() {
144: super (ValueType.STRING_TYPE, Operator.NOT_EQUAL);
145: }
146:
147: public boolean evaluate(InternalWorkingMemory workingMemory,
148: final Extractor extractor, final Object object1,
149: final FieldValue object2) {
150: final Object value1 = extractor.getValue(workingMemory,
151: object1);
152: final Object value2 = object2.getValue();
153: if (value1 == null) {
154: return value2 != null;
155: }
156: return !value1.equals(value2);
157: }
158:
159: public boolean evaluateCachedRight(
160: InternalWorkingMemory workingMemory,
161: final VariableContextEntry context, final Object left) {
162: final Object value = context.declaration.getExtractor()
163: .getValue(workingMemory, left);
164: if (value == null) {
165: return ((ObjectVariableContextEntry) context).right != null;
166: }
167: return !value
168: .equals(((ObjectVariableContextEntry) context).right);
169: }
170:
171: public boolean evaluateCachedLeft(
172: InternalWorkingMemory workingMemory,
173: final VariableContextEntry context, final Object right) {
174: final Object value = context.extractor.getValue(
175: workingMemory, right);
176: if (((ObjectVariableContextEntry) context).left == null) {
177: return value != null;
178: }
179: return !((ObjectVariableContextEntry) context).left
180: .equals(value);
181: }
182:
183: public boolean evaluate(InternalWorkingMemory workingMemory,
184: final Extractor extractor1, final Object object1,
185: final Extractor extractor2, final Object object2) {
186: final Object value1 = extractor1.getValue(workingMemory,
187: object1);
188: final Object value2 = extractor2.getValue(workingMemory,
189: object2);
190: if (value1 == null) {
191: return value2 != null;
192: }
193: return !value1.equals(value2);
194: }
195:
196: public String toString() {
197: return "String !=";
198: }
199: }
200:
201: static class StringMatchesEvaluator extends BaseEvaluator {
202: /**
203: *
204: */
205: private static final long serialVersionUID = 400L;
206: public final static Evaluator INSTANCE = new StringMatchesEvaluator();
207:
208: private StringMatchesEvaluator() {
209: super (ValueType.STRING_TYPE, Operator.MATCHES);
210: }
211:
212: public boolean evaluate(InternalWorkingMemory workingMemory,
213: final Extractor extractor, final Object object1,
214: final FieldValue object2) {
215: final String value1 = (String) extractor.getValue(
216: workingMemory, object1);
217: final String value2 = (String) object2.getValue();
218: if (value1 == null) {
219: return false;
220: }
221: return value1.matches(value2);
222: }
223:
224: public boolean evaluateCachedRight(
225: InternalWorkingMemory workingMemory,
226: final VariableContextEntry context, final Object left) {
227: final String value = (String) ((ObjectVariableContextEntry) context).right;
228: if (value == null) {
229: return false;
230: }
231: return value.matches((String) context.declaration
232: .getExtractor().getValue(workingMemory, left));
233: }
234:
235: public boolean evaluateCachedLeft(
236: InternalWorkingMemory workingMemory,
237: final VariableContextEntry context, final Object right) {
238: final String value = (String) context.extractor.getValue(
239: workingMemory, right);
240: if (value == null) {
241: return false;
242: }
243: return value
244: .matches((String) ((ObjectVariableContextEntry) context).left);
245: }
246:
247: public boolean evaluate(InternalWorkingMemory workingMemory,
248: final Extractor extractor1, final Object object1,
249: final Extractor extractor2, final Object object2) {
250: final Object value1 = extractor1.getValue(workingMemory,
251: object1);
252: final Object value2 = extractor2.getValue(workingMemory,
253: object2);
254: if (value1 == null) {
255: return false;
256: }
257: return ((String) value1).matches((String) value2);
258: }
259:
260: public String toString() {
261: return "String matches";
262: }
263: }
264:
265: static class StringNotMatchesEvaluator extends BaseEvaluator {
266: /**
267: *
268: */
269: private static final long serialVersionUID = 400L;
270: public final static Evaluator INSTANCE = new StringNotMatchesEvaluator();
271:
272: private StringNotMatchesEvaluator() {
273: super (ValueType.STRING_TYPE, Operator.NOT_MATCHES);
274: }
275:
276: public boolean evaluate(InternalWorkingMemory workingMemory,
277: final Extractor extractor, final Object object1,
278: final FieldValue object2) {
279: final String value1 = (String) extractor.getValue(
280: workingMemory, object1);
281: final String value2 = (String) object2.getValue();
282: if (value1 == null) {
283: return false;
284: }
285: return !value1.matches(value2);
286: }
287:
288: public boolean evaluateCachedRight(
289: InternalWorkingMemory workingMemory,
290: final VariableContextEntry context, final Object left) {
291: final String value = (String) ((ObjectVariableContextEntry) context).right;
292: if (value == null) {
293: return false;
294: }
295: return !value.matches((String) context.declaration
296: .getExtractor().getValue(workingMemory, left));
297: }
298:
299: public boolean evaluateCachedLeft(
300: InternalWorkingMemory workingMemory,
301: final VariableContextEntry context, final Object right) {
302: final String value = (String) context.extractor.getValue(
303: workingMemory, right);
304: if (value == null) {
305: return false;
306: }
307: return !value
308: .matches((String) ((ObjectVariableContextEntry) context).left);
309: }
310:
311: public boolean evaluate(InternalWorkingMemory workingMemory,
312: final Extractor extractor1, final Object object1,
313: final Extractor extractor2, final Object object2) {
314: final Object value1 = extractor1.getValue(workingMemory,
315: object1);
316: final Object value2 = extractor2.getValue(workingMemory,
317: object2);
318: if (value1 == null) {
319: return false;
320: }
321: return !((String) value1).matches((String) value2);
322: }
323:
324: public String toString() {
325: return "String not matches";
326: }
327: }
328:
329: static class StringMemberOfEvaluator extends BaseMemberOfEvaluator {
330:
331: private static final long serialVersionUID = 400L;
332: public final static Evaluator INSTANCE = new StringMemberOfEvaluator();
333:
334: private StringMemberOfEvaluator() {
335: super (ValueType.STRING_TYPE, Operator.MEMBEROF);
336: }
337:
338: public String toString() {
339: return "String memberOf";
340: }
341: }
342:
343: static class StringNotMemberOfEvaluator extends
344: BaseNotMemberOfEvaluator {
345:
346: private static final long serialVersionUID = 400L;
347: public final static Evaluator INSTANCE = new StringNotMemberOfEvaluator();
348:
349: private StringNotMemberOfEvaluator() {
350: super (ValueType.STRING_TYPE, Operator.NOTMEMBEROF);
351: }
352:
353: public String toString() {
354: return "String not memberOf";
355: }
356: }
357:
358: }
|