001: package org.drools.rule;
002:
003: /*
004: * $Id: Declaration.java,v 1.1 2005/07/26 01:06:31 mproctor Exp $
005: *
006: * Copyright 2001-2003 (C) The Werken Company. All Rights Reserved.
007: *
008: * Redistribution and use of this software and associated documentation
009: * ("Software"), with or without modification, are permitted provided that the
010: * following conditions are met:
011: *
012: * 1. Redistributions of source code must retain copyright statements and
013: * notices. Redistributions must also contain a copy of this document.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright notice,
016: * this list of conditions and the following disclaimer in the documentation
017: * and/or other materials provided with the distribution.
018: *
019: * 3. The name "drools" must not be used to endorse or promote products derived
020: * from this Software without prior written permission of The Werken Company.
021: * For written permission, please contact bob@werken.com.
022: *
023: * 4. Products derived from this Software may not be called "drools" nor may
024: * "drools" appear in their names without prior written permission of The Werken
025: * Company. "drools" is a trademark of The Werken Company.
026: *
027: * 5. Due credit should be given to The Werken Company. (http://werken.com/)
028: *
029: * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS''
030: * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
031: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
032: * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE
033: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
034: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
035: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
036: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
037: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
039: * POSSIBILITY OF SUCH DAMAGE.
040: *
041: */
042:
043: import java.io.Serializable;
044: import java.lang.reflect.Method;
045: import java.util.Collection;
046: import java.util.Iterator;
047:
048: import org.drools.RuntimeDroolsException;
049: import org.drools.base.ShadowProxy;
050: import org.drools.base.ValueType;
051: import org.drools.common.InternalWorkingMemory;
052: import org.drools.spi.Extractor;
053:
054: /*
055: * Copyright 2005 JBoss Inc
056: *
057: * Licensed under the Apache License, Version 2.0 (the "License");
058: * you may not use this file except in compliance with the License.
059: * You may obtain a copy of the License at
060: *
061: * http://www.apache.org/licenses/LICENSE-2.0
062: *
063: * Unless required by applicable law or agreed to in writing, software
064: * distributed under the License is distributed on an "AS IS" BASIS,
065: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
066: * See the License for the specific language governing permissions and
067: * limitations under the License.
068: */
069:
070: /**
071: * @author <a href="mailto:mark.proctor@jboss.com">Mark Proctor</a>
072: * @author <a href="mailto:bob@werken.com">Bob McWhirter</a>
073: * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris </a>
074: *
075: */
076: public class Declaration implements Serializable {
077: // ------------------------------------------------------------
078: // Instance members
079: // ------------------------------------------------------------
080:
081: /**
082: *
083: */
084: private static final long serialVersionUID = 400L;
085:
086: /** The identifier for the variable. */
087: private final String identifier;
088:
089: private final Extractor extractor;
090:
091: private Pattern pattern;
092:
093: private final boolean internalFact;
094:
095: // ------------------------------------------------------------
096: // Constructors
097: // ------------------------------------------------------------
098:
099: /**
100: * Construct.
101: *
102: * @param identifier
103: * The name of the variable.
104: * @param objectType
105: * The type of this variable declaration.
106: * @param order
107: * The index within a rule.
108: */
109: public Declaration(final String identifier,
110: final Extractor extractor, final Pattern pattern) {
111: this (identifier, extractor, pattern, false);
112: }
113:
114: /**
115: * Construct.
116: *
117: * @param identifier
118: * The name of the variable.
119: * @param objectType
120: * The type of this variable declaration.
121: * @param order
122: * The index within a rule.
123: * @param internalFact
124: * True if this is an internal fact created by the engine, like a collection result
125: * of a collect CE
126: */
127: public Declaration(final String identifier,
128: final Extractor extractor, final Pattern pattern,
129: final boolean internalFact) {
130: this .identifier = identifier;
131: this .extractor = extractor;
132: this .pattern = pattern;
133: this .internalFact = internalFact;
134: }
135:
136: // ------------------------------------------------------------
137: // Instance methods
138: // ------------------------------------------------------------
139:
140: /**
141: * Retrieve the variable's identifier.
142: *
143: * @return The variable's identifier.
144: */
145: public String getIdentifier() {
146: return this .identifier;
147: }
148:
149: /**
150: * Retrieve the <code>ValueType</code>.
151: *
152: * @return The ValueType.
153: */
154: public ValueType getValueType() {
155: return this .extractor.getValueType();
156: }
157:
158: /**
159: * Returns the index of the pattern
160: *
161: * @return the pattern
162: */
163: public Pattern getPattern() {
164: return this .pattern;
165: }
166:
167: public void setPattern(final Pattern pattern) {
168: this .pattern = pattern;
169: }
170:
171: /**
172: * Returns true if this declaration is a pattern declaration
173: * @return
174: */
175: public boolean isPatternDeclaration() {
176: return this .pattern != null
177: && this .pattern.getDeclaration() == this ;
178: }
179:
180: /**
181: * Returns the Extractor expression
182: *
183: * @return
184: */
185: public Extractor getExtractor() {
186: return this .extractor;
187: }
188:
189: public Object getValue(InternalWorkingMemory workingMemory,
190: final Object object) {
191: return this .extractor.getValue(workingMemory, object);
192: }
193:
194: public Object getNonShadowedValue(
195: InternalWorkingMemory workingMemory, final Object object) {
196: Object result = this .extractor.getValue(workingMemory, object);
197: if (this .isInternalFact() && result instanceof Collection) {
198: try {
199: Collection newCol = (Collection) result.getClass()
200: .newInstance();
201: for (Iterator it = ((Collection) result).iterator(); it
202: .hasNext();) {
203: Object element = it.next();
204: newCol
205: .add((element instanceof ShadowProxy) ? ((ShadowProxy) element)
206: .getShadowedObject()
207: : element);
208: }
209: return newCol;
210: } catch (InstantiationException e) {
211: // nothing we can do, so just return the resulting object
212: } catch (IllegalAccessException e) {
213: // TODO Auto-generated catch block
214: }
215: }
216: return result;
217: }
218:
219: public char getCharValue(InternalWorkingMemory workingMemory,
220: final Object object) {
221: return this .extractor.getCharValue(workingMemory, object);
222: }
223:
224: public int getIntValue(InternalWorkingMemory workingMemory,
225: final Object object) {
226: return this .extractor.getIntValue(workingMemory, object);
227: }
228:
229: public byte getByteValue(InternalWorkingMemory workingMemory,
230: final Object object) {
231: return this .extractor.getByteValue(workingMemory, object);
232: }
233:
234: public short getShortValue(InternalWorkingMemory workingMemory,
235: final Object object) {
236: return this .extractor.getShortValue(workingMemory, object);
237: }
238:
239: public long getLongValue(InternalWorkingMemory workingMemory,
240: final Object object) {
241: return this .extractor.getLongValue(workingMemory, object);
242: }
243:
244: public float getFloatValue(InternalWorkingMemory workingMemory,
245: final Object object) {
246: return this .extractor.getFloatValue(workingMemory, object);
247: }
248:
249: public double getDoubleValue(InternalWorkingMemory workingMemory,
250: final Object object) {
251: return this .extractor.getDoubleValue(workingMemory, object);
252: }
253:
254: public boolean getBooleanValue(InternalWorkingMemory workingMemory,
255: final Object object) {
256: return this .extractor.getBooleanValue(workingMemory, object);
257: }
258:
259: public int getHashCode(InternalWorkingMemory workingMemory,
260: final Object object) {
261: return this .extractor.getHashCode(workingMemory, object);
262: }
263:
264: public boolean isGlobal() {
265: return this .extractor.isGlobal();
266: }
267:
268: public Method getNativeReadMethod() {
269: if (this .isPatternDeclaration() && this .isInternalFact()) {
270: try {
271: return this .getClass().getDeclaredMethod(
272: "getNonShadowedValue",
273: new Class[] { InternalWorkingMemory.class,
274: Object.class });
275: } catch (final Exception e) {
276: throw new RuntimeDroolsException(
277: "This is a bug. Please report to development team: "
278: + e.getMessage(), e);
279: }
280: }
281: return this .extractor.getNativeReadMethod();
282: }
283:
284: // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
285:
286: public String toString() {
287: return "[Declaration: type=" + this .extractor.getValueType()
288: + " identifier=" + this .identifier + "]";
289: }
290:
291: public int hashCode() {
292: final int PRIME = 31;
293: int result = 1;
294: result = PRIME * this .pattern.getOffset();
295: result = PRIME * this .extractor.hashCode();
296: result = PRIME * this .identifier.hashCode();
297: return result;
298: }
299:
300: public boolean equals(final Object object) {
301: if (this == object) {
302: return true;
303: }
304:
305: if (object == null || getClass() != object.getClass()) {
306: return false;
307: }
308:
309: final Declaration other = (Declaration) object;
310:
311: return this .pattern.getOffset() == other.pattern.getOffset()
312: && this .identifier.equals(other.identifier)
313: && this .extractor.equals(other.extractor);
314: }
315:
316: protected boolean isInternalFact() {
317: return internalFact;
318: }
319:
320: }
|