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.common.InternalWorkingMemory;
020: import org.drools.reteoo.ReteTuple;
021: import org.drools.spi.AlphaNodeFieldConstraint;
022: import org.drools.spi.BetaNodeFieldConstraint;
023: import org.drools.spi.Evaluator;
024: import org.drools.spi.FieldExtractor;
025:
026: public class VariableConstraint implements AlphaNodeFieldConstraint,
027: BetaNodeFieldConstraint {
028:
029: private static final long serialVersionUID = 400L;
030:
031: private final FieldExtractor fieldExtractor;
032: private final VariableRestriction restriction;
033:
034: public VariableConstraint(final FieldExtractor fieldExtractor,
035: final Declaration declaration, final Evaluator evaluator) {
036: this .fieldExtractor = fieldExtractor;
037: this .restriction = new VariableRestriction(fieldExtractor,
038: declaration, evaluator);
039: }
040:
041: public VariableConstraint(final FieldExtractor fieldExtractor,
042: final VariableRestriction restriction) {
043: this .fieldExtractor = fieldExtractor;
044: this .restriction = restriction;
045: }
046:
047: public Declaration[] getRequiredDeclarations() {
048: return this .restriction.getRequiredDeclarations();
049: }
050:
051: public FieldExtractor getFieldExtractor() {
052: return this .fieldExtractor;
053: }
054:
055: public Evaluator getEvaluator() {
056: return this .restriction.getEvaluator();
057: }
058:
059: public boolean isAllowed(final Object object,
060: final InternalWorkingMemory workingMemory) {
061: return this .restriction.isAllowed(this .fieldExtractor, object,
062: workingMemory);
063: }
064:
065: public boolean isAllowedCachedLeft(final ContextEntry context,
066: final Object object) {
067: return this .restriction.isAllowedCachedLeft(context, object);
068: }
069:
070: public boolean isAllowedCachedRight(final ReteTuple tuple,
071: final ContextEntry context) {
072: return this .restriction.isAllowedCachedRight(tuple, context);
073: }
074:
075: public String toString() {
076: return "[VariableConstraint fieldExtractor="
077: + this .fieldExtractor + " declaration="
078: + getRequiredDeclarations() + "]";
079: }
080:
081: public ContextEntry getContextEntry() {
082: return this .restriction.getContextEntry();
083: }
084:
085: public int hashCode() {
086: final int PRIME = 31;
087: int result = 1;
088: result = PRIME * result + this .fieldExtractor.hashCode();
089: result = PRIME * result + this .restriction.hashCode();
090: return result;
091: }
092:
093: public boolean equals(final Object object) {
094: if (this == object) {
095: return true;
096: }
097:
098: if (object == null || getClass() != object.getClass()) {
099: return false;
100: }
101:
102: final VariableConstraint other = (VariableConstraint) object;
103:
104: return this.fieldExtractor.equals(other.fieldExtractor)
105: && this.restriction.equals(other.restriction);
106: }
107:
108: }
|