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.rule;
018:
019: import java.util.Arrays;
020:
021: import org.drools.common.InternalWorkingMemory;
022: import org.drools.reteoo.ReteTuple;
023: import org.drools.util.ArrayUtils;
024:
025: /**
026: * A class to implement Multi-Field OR constraints, so user can do:
027: *
028: * Person( hair == 'blue' || eyes == 'blue' )
029: *
030: * @author etirelli
031: *
032: */
033: public class OrConstraint extends AbstractCompositeConstraint {
034:
035: private static final long serialVersionUID = 400L;
036:
037: public OrConstraint() {
038: }
039:
040: /**
041: * {@inheritDoc}
042: */
043: public boolean isAllowed(Object object,
044: InternalWorkingMemory workingMemory) {
045: if (this .alphaConstraints.length > 0) {
046: for (int i = 0; i < this .alphaConstraints.length; i++) {
047: if (this .alphaConstraints[i].isAllowed(object,
048: workingMemory)) {
049: return true;
050: }
051: }
052: return false;
053: }
054: return true;
055: }
056:
057: /**
058: * {@inheritDoc}
059: */
060: public boolean isAllowedCachedLeft(ContextEntry context,
061: Object object) {
062: if (this .betaConstraints.length > 0) {
063: for (int i = 0; i < this .betaConstraints.length; i++) {
064: if (this .betaConstraints[i]
065: .isAllowedCachedLeft(
066: ((MultiFieldConstraintContextEntry) context).contexts[i],
067: object)) {
068: return true;
069: }
070: }
071: return false;
072: }
073: return true;
074: }
075:
076: /**
077: * {@inheritDoc}
078: */
079: public boolean isAllowedCachedRight(ReteTuple tuple,
080: ContextEntry context) {
081: if (this .betaConstraints.length > 0) {
082: for (int i = 0; i < this .betaConstraints.length; i++) {
083: if (this .betaConstraints[i]
084: .isAllowedCachedRight(
085: tuple,
086: ((MultiFieldConstraintContextEntry) context).contexts[i])) {
087: return true;
088: }
089: }
090: return false;
091: }
092: return true;
093: }
094:
095: public int hashCode() {
096: final int PRIME = 31;
097: int result = 3; // to differentiate from AND constraint
098: result = PRIME * result
099: + ArrayUtils.hashCode(this .alphaConstraints);
100: result = PRIME * result
101: + ArrayUtils.hashCode(this .betaConstraints);
102: result = PRIME * result
103: + ArrayUtils.hashCode(this .requiredDeclarations);
104: return result;
105: }
106:
107: public boolean equals(final Object object) {
108: if (this == object) {
109: return true;
110: }
111: if (object == null || object.getClass() != OrConstraint.class) {
112: return false;
113: }
114: final OrConstraint other = (OrConstraint) object;
115:
116: return Arrays.equals(this.alphaConstraints,
117: other.alphaConstraints)
118: && Arrays.equals(this.betaConstraints,
119: other.betaConstraints)
120: && Arrays.equals(this.requiredDeclarations,
121: other.requiredDeclarations);
122: }
123:
124: }
|