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 org.drools.RuntimeDroolsException;
020: import org.drools.common.InternalWorkingMemory;
021: import org.drools.reteoo.ReteTuple;
022: import org.drools.rule.ReturnValueRestriction.ReturnValueContextEntry;
023: import org.drools.spi.AlphaNodeFieldConstraint;
024: import org.drools.spi.BetaNodeFieldConstraint;
025: import org.drools.spi.Evaluator;
026: import org.drools.spi.FieldExtractor;
027: import org.drools.spi.ReturnValueExpression;
028:
029: public class ReturnValueConstraint implements BetaNodeFieldConstraint,
030: AlphaNodeFieldConstraint {
031:
032: /**
033: *
034: */
035: private static final long serialVersionUID = 400L;
036:
037: private final FieldExtractor fieldExtractor;
038: private final ReturnValueRestriction restriction;
039:
040: public ReturnValueConstraint(final FieldExtractor fieldExtractor,
041: final ReturnValueRestriction restriction) {
042: this .fieldExtractor = fieldExtractor;
043: this .restriction = restriction;
044: }
045:
046: public Declaration[] getRequiredDeclarations() {
047: return this .restriction.getRequiredDeclarations();
048: }
049:
050: public void setReturnValueExpression(
051: final ReturnValueExpression expression) {
052: this .restriction.setReturnValueExpression(expression);
053: }
054:
055: public ReturnValueExpression getExpression() {
056: return this .restriction.getExpression();
057: }
058:
059: public Evaluator getEvaluator() {
060: return this .restriction.getEvaluator();
061: }
062:
063: public String toString() {
064: return "[ReturnValueConstraint fieldExtractor="
065: + this .fieldExtractor + " evaluator=" + getEvaluator()
066: + " declarations=" + getRequiredDeclarations() + "]";
067: }
068:
069: public int hashCode() {
070: final int PRIME = 31;
071: int result = 1;
072: result = PRIME * result + this .fieldExtractor.hashCode();
073: result = PRIME * result + this .restriction.hashCode();
074: return result;
075: }
076:
077: public boolean equals(final Object object) {
078: if (object == this ) {
079: return true;
080: }
081:
082: if (object == null
083: || object.getClass() != ReturnValueConstraint.class) {
084: return false;
085: }
086:
087: final ReturnValueConstraint other = (ReturnValueConstraint) object;
088:
089: return this .fieldExtractor.equals(other.fieldExtractor)
090: && this .restriction.equals(other.restriction);
091: }
092:
093: public ContextEntry getContextEntry() {
094: return this .restriction.getContextEntry();
095: }
096:
097: public boolean isAllowed(final Object object,
098: final InternalWorkingMemory workingMemory) {
099: try {
100: return this .restriction.isAllowed(this .fieldExtractor,
101: object, null, workingMemory);
102: } catch (final Exception e) {
103: throw new RuntimeDroolsException(
104: "Exception executing ReturnValue constraint "
105: + this .restriction + " : " + e.getMessage(),
106: e);
107: }
108: }
109:
110: public boolean isAllowedCachedLeft(final ContextEntry context,
111: final Object object) {
112: try {
113: final ReturnValueContextEntry ctx = (ReturnValueContextEntry) context;
114: return this .restriction.isAllowed(this .fieldExtractor,
115: object, ctx.getTuple(), ctx.getWorkingMemory());
116: } catch (final Exception e) {
117: throw new RuntimeDroolsException(
118: "Exception executing ReturnValue constraint "
119: + this .restriction + " : " + e.getMessage(),
120: e);
121: }
122: }
123:
124: public boolean isAllowedCachedRight(final ReteTuple tuple,
125: final ContextEntry context) {
126: try {
127: final ReturnValueContextEntry ctx = (ReturnValueContextEntry) context;
128: return this .restriction.isAllowed(this .fieldExtractor, ctx
129: .getObject(), tuple, ctx.getWorkingMemory());
130: } catch (final Exception e) {
131: throw new RuntimeDroolsException(
132: "Exception executing ReturnValue constraint "
133: + this .restriction + " : " + e.getMessage(),
134: e);
135: }
136: }
137:
138: }
|