001: /*
002: * Copyright 2005 JBoss Inc
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.drools.common;
018:
019: import org.drools.reteoo.ReteTuple;
020: import org.drools.rule.Pattern;
021: import org.drools.rule.ContextEntry;
022: import org.drools.rule.Declaration;
023: import org.drools.spi.BetaNodeFieldConstraint;
024:
025: /**
026: * InstanceEqualsConstraint
027: *
028: * Created: 21/06/2006
029: * @author <a href="mailto:tirelli@post.com">Edson Tirelli</a>
030: *
031: * @version $Id$
032: */
033:
034: public class InstanceEqualsConstraint implements
035: BetaNodeFieldConstraint {
036:
037: private static final long serialVersionUID = 400L;
038:
039: private final Declaration[] declarations = new Declaration[0];
040:
041: private Pattern otherPattern;
042:
043: public InstanceEqualsConstraint(final Pattern otherPattern) {
044: this .otherPattern = otherPattern;
045: }
046:
047: public Declaration[] getRequiredDeclarations() {
048: return this .declarations;
049: }
050:
051: public Pattern getOtherPattern() {
052: return this .otherPattern;
053: }
054:
055: public ContextEntry getContextEntry() {
056: return new InstanceEqualsConstraintContextEntry(
057: this .otherPattern);
058: }
059:
060: public boolean isAllowedCachedLeft(final ContextEntry context,
061: final Object object) {
062: return ((InstanceEqualsConstraintContextEntry) context).left == object;
063: }
064:
065: public boolean isAllowedCachedRight(final ReteTuple tuple,
066: final ContextEntry context) {
067: return tuple.get(this .otherPattern.getOffset()).getObject() == ((InstanceEqualsConstraintContextEntry) context).right;
068: }
069:
070: public String toString() {
071: return "[InstanceEqualsConstraint otherPattern="
072: + this .otherPattern + " ]";
073: }
074:
075: public int hashCode() {
076: return this .otherPattern.hashCode();
077: }
078:
079: public boolean equals(final Object object) {
080: if (this == object) {
081: return true;
082: }
083:
084: if (object == null
085: || !(object instanceof InstanceEqualsConstraint)) {
086: return false;
087: }
088:
089: final InstanceEqualsConstraint other = (InstanceEqualsConstraint) object;
090: return this .otherPattern.equals(other.otherPattern);
091: }
092:
093: public static class InstanceEqualsConstraintContextEntry implements
094: ContextEntry {
095:
096: private static final long serialVersionUID = 400L;
097:
098: public Object left;
099: public Object right;
100:
101: private Pattern pattern;
102: private ContextEntry entry;
103:
104: public InstanceEqualsConstraintContextEntry(
105: final Pattern pattern) {
106: this .pattern = pattern;
107: }
108:
109: public ContextEntry getNext() {
110: return this .entry;
111: }
112:
113: public void setNext(final ContextEntry entry) {
114: this .entry = entry;
115: }
116:
117: public void updateFromTuple(
118: final InternalWorkingMemory workingMemory,
119: final ReteTuple tuple) {
120: this .left = tuple.get(this .pattern.getOffset()).getObject();
121: }
122:
123: public void updateFromFactHandle(
124: final InternalWorkingMemory workingMemory,
125: final InternalFactHandle handle) {
126: this.right = handle.getObject();
127: }
128: }
129: }
|