001: package org.drools.rule;
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 java.util.Collections;
020: import java.util.Map;
021:
022: import org.drools.RuntimeDroolsException;
023: import org.drools.WorkingMemory;
024: import org.drools.spi.EvalExpression;
025: import org.drools.spi.Tuple;
026:
027: public class EvalCondition extends ConditionalElement {
028: /**
029: *
030: */
031: private static final long serialVersionUID = 400L;
032:
033: private EvalExpression expression;
034:
035: private final Declaration[] requiredDeclarations;
036:
037: private static final Declaration[] EMPTY_DECLARATIONS = new Declaration[0];
038:
039: public EvalCondition(final Declaration[] requiredDeclarations) {
040: this (null, requiredDeclarations);
041: }
042:
043: public EvalCondition(final EvalExpression eval,
044: final Declaration[] requiredDeclarations) {
045:
046: this .expression = eval;
047:
048: if (requiredDeclarations == null) {
049: this .requiredDeclarations = EvalCondition.EMPTY_DECLARATIONS;
050: } else {
051: this .requiredDeclarations = requiredDeclarations;
052: }
053: }
054:
055: public EvalExpression getEvalExpression() {
056: return this .expression;
057: }
058:
059: public void setEvalExpression(final EvalExpression expression) {
060: this .expression = expression;
061: }
062:
063: public Declaration[] getRequiredDeclarations() {
064: return this .requiredDeclarations;
065: }
066:
067: public boolean isAllowed(final Tuple tuple,
068: final WorkingMemory workingMemory) {
069: try {
070: return this .expression.evaluate(tuple,
071: this .requiredDeclarations, workingMemory);
072: } catch (final Exception e) {
073: throw new RuntimeDroolsException(e);
074: }
075: }
076:
077: public Object clone() {
078: final EvalCondition eval = new EvalCondition(this .expression,
079: this .requiredDeclarations);
080: return eval;
081: }
082:
083: public int hashCode() {
084: return this .expression.hashCode();
085: }
086:
087: public boolean equals(final Object object) {
088: if (object == this ) {
089: return true;
090: }
091:
092: if (object == null || object.getClass() != EvalCondition.class) {
093: return false;
094: }
095:
096: final EvalCondition other = (EvalCondition) object;
097:
098: if (this .requiredDeclarations.length != other.requiredDeclarations.length) {
099: return false;
100: }
101:
102: for (int i = 0, length = this .requiredDeclarations.length; i < length; i++) {
103: if (this .requiredDeclarations[i].getPattern().getOffset() != other.requiredDeclarations[i]
104: .getPattern().getOffset()) {
105: return false;
106: }
107:
108: if (!this .requiredDeclarations[i].getExtractor().equals(
109: other.requiredDeclarations[i].getExtractor())) {
110: return false;
111: }
112: }
113:
114: return this .expression.equals(other.expression);
115: }
116:
117: public Map getInnerDeclarations() {
118: return Collections.EMPTY_MAP;
119: }
120:
121: public Map getOuterDeclarations() {
122: return Collections.EMPTY_MAP;
123: }
124:
125: /**
126: * @inheritDoc
127: */
128: public Declaration resolveDeclaration(final String identifier) {
129: return null;
130: }
131:
132: };
|