001: package org.drools.common;
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.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: public class InstanceNotEqualsConstraint implements
026: BetaNodeFieldConstraint {
027:
028: private static final long serialVersionUID = 400L;
029:
030: private static final Declaration[] declarations = new Declaration[0];
031:
032: private Pattern otherPattern;
033:
034: public InstanceNotEqualsConstraint(final Pattern otherPattern) {
035: this .otherPattern = otherPattern;
036: }
037:
038: public Declaration[] getRequiredDeclarations() {
039: return InstanceNotEqualsConstraint.declarations;
040: }
041:
042: public Pattern getOtherPattern() {
043: return this .otherPattern;
044: }
045:
046: public ContextEntry getContextEntry() {
047: return new InstanceNotEqualsConstraintContextEntry(
048: this .otherPattern);
049: }
050:
051: public boolean isAllowed(final ContextEntry entry) {
052: final InstanceNotEqualsConstraintContextEntry context = (InstanceNotEqualsConstraintContextEntry) entry;
053: return context.left != context.right;
054: }
055:
056: public boolean isAllowedCachedLeft(final ContextEntry context,
057: final Object object) {
058: return ((InstanceNotEqualsConstraintContextEntry) context).left != object;
059: }
060:
061: public boolean isAllowedCachedRight(final ReteTuple tuple,
062: final ContextEntry context) {
063: return tuple.get(this .otherPattern.getOffset()).getObject() != ((InstanceNotEqualsConstraintContextEntry) context).right;
064: }
065:
066: public String toString() {
067: return "[InstanceEqualsConstraint otherPattern="
068: + this .otherPattern + " ]";
069: }
070:
071: public int hashCode() {
072: return this .otherPattern.hashCode();
073: }
074:
075: public boolean equals(final Object object) {
076: if (this == object) {
077: return true;
078: }
079:
080: if (object == null
081: || !(object instanceof InstanceNotEqualsConstraint)) {
082: return false;
083: }
084:
085: final InstanceNotEqualsConstraint other = (InstanceNotEqualsConstraint) object;
086: return this .otherPattern.equals(other.otherPattern);
087: }
088:
089: public static class InstanceNotEqualsConstraintContextEntry
090: implements ContextEntry {
091:
092: private static final long serialVersionUID = 400L;
093: public Object left;
094: public Object right;
095:
096: private Pattern pattern;
097: private ContextEntry entry;
098:
099: public InstanceNotEqualsConstraintContextEntry(
100: final Pattern pattern) {
101: this .pattern = pattern;
102: }
103:
104: public ContextEntry getNext() {
105: return this .entry;
106: }
107:
108: public void setNext(final ContextEntry entry) {
109: this .entry = entry;
110: }
111:
112: public void updateFromTuple(
113: final InternalWorkingMemory workingMemory,
114: final ReteTuple tuple) {
115: this .left = tuple.get(this .pattern.getOffset()).getObject();
116: }
117:
118: public void updateFromFactHandle(
119: final InternalWorkingMemory workingMemory,
120: final InternalFactHandle handle) {
121: this.right = handle.getObject();
122:
123: }
124: }
125:
126: }
|