001: package org.drools.spi;
002:
003: import java.lang.reflect.Method;
004: import java.util.Collection;
005: import java.util.Iterator;
006:
007: import org.drools.RuntimeDroolsException;
008: import org.drools.base.ClassObjectType;
009: import org.drools.base.ShadowProxy;
010: import org.drools.base.ValueType;
011: import org.drools.common.InternalWorkingMemory;
012: import org.drools.facttemplates.Fact;
013: import org.drools.util.ClassUtils;
014:
015: /*
016: * Copyright 2005 JBoss Inc
017: *
018: * Licensed under the Apache License, Version 2.0 (the "License");
019: * you may not use this file except in compliance with the License.
020: * You may obtain a copy of the License at
021: *
022: * http://www.apache.org/licenses/LICENSE-2.0
023: *
024: * Unless required by applicable law or agreed to in writing, software
025: * distributed under the License is distributed on an "AS IS" BASIS,
026: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
027: * See the License for the specific language governing permissions and
028: * limitations under the License.
029: */
030:
031: public class PatternExtractor implements Extractor {
032:
033: /**
034: *
035: */
036: private static final long serialVersionUID = 400L;
037: private ObjectType objectType;
038:
039: public PatternExtractor(final ObjectType objectType) {
040: this .objectType = objectType;
041: }
042:
043: public Object getValue(InternalWorkingMemory workingMemory,
044: final Object object) {
045: // need to use instanceof because an object may be created in nodes like accumulate and from
046: // where no shadow is applied
047: return (object instanceof ShadowProxy) ? ((ShadowProxy) object)
048: .getShadowedObject() : object;
049: }
050:
051: public ObjectType getObjectType() {
052: return this .objectType;
053: }
054:
055: public Class getExtractToClass() {
056: // @todo : this is a bit nasty, but does the trick
057: if (this .objectType instanceof ClassObjectType) {
058: return ((ClassObjectType) this .objectType).getClassType();
059: } else {
060: return Fact.class;
061: }
062: }
063:
064: public String getExtractToClassName() {
065: Class clazz = null;
066: // @todo : this is a bit nasty, but does the trick
067: if (this .objectType instanceof ClassObjectType) {
068: clazz = ((ClassObjectType) this .objectType).getClassType();
069: } else {
070: clazz = Fact.class;
071: }
072: return ClassUtils.canonicalName(clazz);
073: }
074:
075: public ValueType getValueType() {
076: return this .objectType.getValueType();
077: }
078:
079: public boolean getBooleanValue(InternalWorkingMemory workingMemory,
080: final Object object) {
081: if (this .objectType.getValueType().isBoolean()) {
082: return ((Boolean) object).booleanValue();
083: }
084: throw new RuntimeDroolsException(
085: "Conversion to boolean not supported for type: "
086: + object.getClass());
087: }
088:
089: public byte getByteValue(InternalWorkingMemory workingMemory,
090: final Object object) {
091: if (this .objectType.getValueType().isNumber()) {
092: return ((Number) object).byteValue();
093: }
094: throw new RuntimeDroolsException(
095: "Conversion to byte not supported for type: "
096: + object.getClass());
097: }
098:
099: public char getCharValue(InternalWorkingMemory workingMemory,
100: final Object object) {
101: if (this .objectType.getValueType().isChar()) {
102: return ((Character) object).charValue();
103: }
104: throw new RuntimeDroolsException(
105: "Conversion to char not supported for type: "
106: + object.getClass());
107: }
108:
109: public double getDoubleValue(InternalWorkingMemory workingMemory,
110: final Object object) {
111: if (this .objectType.getValueType().isNumber()) {
112: return ((Number) object).doubleValue();
113: }
114: throw new RuntimeDroolsException(
115: "Conversion to double not supported for type: "
116: + object.getClass());
117: }
118:
119: public float getFloatValue(InternalWorkingMemory workingMemory,
120: final Object object) {
121: if (this .objectType.getValueType().isNumber()) {
122: return ((Number) object).floatValue();
123: }
124: throw new RuntimeDroolsException(
125: "Conversion to float not supported for type: "
126: + object.getClass());
127: }
128:
129: public int getIntValue(InternalWorkingMemory workingMemory,
130: final Object object) {
131: if (this .objectType.getValueType().isNumber()) {
132: return ((Number) object).intValue();
133: }
134: throw new RuntimeDroolsException(
135: "Conversion to int not supported for type: "
136: + object.getClass());
137: }
138:
139: public long getLongValue(InternalWorkingMemory workingMemory,
140: final Object object) {
141: if (this .objectType.getValueType().isNumber()) {
142: return ((Number) object).longValue();
143: }
144: throw new RuntimeDroolsException(
145: "Conversion to long not supported for type: "
146: + object.getClass());
147: }
148:
149: public short getShortValue(InternalWorkingMemory workingMemory,
150: final Object object) {
151: if (this .objectType.getValueType().isNumber()) {
152: return ((Number) object).shortValue();
153: }
154: throw new RuntimeDroolsException(
155: "Conversion to short not supported for type: "
156: + object.getClass());
157: }
158:
159: public Method getNativeReadMethod() {
160: try {
161: return this .getClass().getDeclaredMethod(
162: "getValue",
163: new Class[] { InternalWorkingMemory.class,
164: Object.class });
165: } catch (final Exception e) {
166: throw new RuntimeDroolsException(
167: "This is a bug. Please report to development team: "
168: + e.getMessage(), e);
169: }
170: }
171:
172: public boolean isNullValue(InternalWorkingMemory workingMemory,
173: final Object object) {
174: return getValue(workingMemory, object) == null;
175: }
176:
177: public int getHashCode(InternalWorkingMemory workingMemory,
178: final Object object) {
179: return getValue(workingMemory, object).hashCode();
180: }
181:
182: public int hashCode() {
183: return this .objectType.hashCode();
184: }
185:
186: public boolean equals(final Object obj) {
187: if (this == obj) {
188: return true;
189: }
190: if (!(obj instanceof PatternExtractor)) {
191: return false;
192: }
193: final PatternExtractor other = (PatternExtractor) obj;
194: return this .objectType.equals(other.objectType);
195: }
196:
197: public boolean isGlobal() {
198: return false;
199: }
200: }
|