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