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.InternalFactHandle;
020: import org.drools.common.InternalWorkingMemory;
021: import org.drools.reteoo.ReteTuple;
022: import org.drools.spi.Evaluator;
023: import org.drools.spi.Extractor;
024: import org.drools.spi.FieldExtractor;
025: import org.drools.spi.FieldValue;
026: import org.drools.spi.Restriction;
027:
028: public class LiteralRestriction implements Restriction {
029:
030: private static final long serialVersionUID = 400L;
031:
032: private final FieldValue field;
033:
034: private final Evaluator evaluator;
035:
036: private static final Declaration[] requiredDeclarations = new Declaration[0];
037:
038: private final LiteralContextEntry contextEntry;
039:
040: public LiteralRestriction(final FieldValue field,
041: final Evaluator evaluator,
042: final FieldExtractor fieldExtractor) {
043: this .field = field;
044: this .evaluator = evaluator;
045: this .contextEntry = new LiteralContextEntry(fieldExtractor);
046: }
047:
048: public Evaluator getEvaluator() {
049: return this .evaluator;
050: }
051:
052: public FieldValue getField() {
053: return this .field;
054: }
055:
056: public boolean isAllowed(final Extractor extractor,
057: final Object object,
058: final InternalWorkingMemory workingMemoiry) {
059: return this .evaluator.evaluate(null, extractor, object,
060: this .field);
061: }
062:
063: public boolean isAllowedCachedLeft(final ContextEntry context,
064: final Object object) {
065: return this .evaluator.evaluate(null,
066: ((LiteralContextEntry) context).getFieldExtractor(),
067: object, this .field);
068: }
069:
070: public boolean isAllowedCachedRight(final ReteTuple tuple,
071: final ContextEntry context) {
072: return this .evaluator
073: .evaluate(null, ((LiteralContextEntry) context)
074: .getFieldExtractor(),
075: ((LiteralContextEntry) context).getObject(),
076: this .field);
077: }
078:
079: /**
080: * Literal constraints cannot have required declarations, so always return an empty array.
081: * @return
082: * Return an empty <code>Declaration[]</code>
083: */
084: public Declaration[] getRequiredDeclarations() {
085: return LiteralRestriction.requiredDeclarations;
086: }
087:
088: public String toString() {
089: return "[LiteralRestriction evaluator=" + this .evaluator
090: + " value=" + this .field + "]";
091: }
092:
093: public int hashCode() {
094: final int PRIME = 31;
095: int result = 1;
096: result = PRIME * result + this .evaluator.hashCode();
097: result = PRIME
098: * result
099: + ((this .field.getValue() != null) ? this .field
100: .getValue().hashCode() : 0);
101: return result;
102: }
103:
104: public boolean equals(final Object object) {
105: if (this == object) {
106: return true;
107: }
108: if (object == null
109: || object.getClass() != LiteralRestriction.class) {
110: return false;
111: }
112: final LiteralRestriction other = (LiteralRestriction) object;
113:
114: return this .field.equals(other.field)
115: && this .evaluator.equals(other.evaluator);
116: }
117:
118: public ContextEntry getContextEntry() {
119: return this .contextEntry;
120: }
121:
122: private static class LiteralContextEntry implements ContextEntry {
123: public FieldExtractor extractor;
124: public Object object;
125: public ContextEntry next;
126:
127: public LiteralContextEntry(final FieldExtractor extractor) {
128: this .extractor = extractor;
129: }
130:
131: public FieldExtractor getFieldExtractor() {
132: return this .extractor;
133: }
134:
135: public Object getObject() {
136: return this .object;
137: }
138:
139: public ContextEntry getNext() {
140: return this .next;
141: }
142:
143: public void setNext(final ContextEntry entry) {
144: this .next = entry;
145: }
146:
147: public void updateFromFactHandle(
148: final InternalWorkingMemory workingMemory,
149: final InternalFactHandle handle) {
150: this .object = handle.getObject();
151: }
152:
153: public void updateFromTuple(
154: final InternalWorkingMemory workingMemory,
155: final ReteTuple tuple) {
156: // nothing to do
157: }
158:
159: }
160:
161: }
|