01: /*
02: * Copyright 2006 JBoss Inc
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: *
16: * Created on Jun 12, 2007
17: */
18: package org.drools.base.extractors;
19:
20: import java.util.HashMap;
21: import java.util.Iterator;
22: import java.util.Map;
23: import java.util.Set;
24:
25: import org.drools.base.ClassFieldExtractorCache;
26: import org.drools.base.ClassFieldExtractorFactory;
27: import org.drools.base.ValueType;
28: import org.drools.common.InternalWorkingMemory;
29: import org.drools.spi.Extractor;
30: import org.drools.spi.FieldExtractor;
31: import org.mvel.CompiledExpression;
32: import org.mvel.ExpressionCompiler;
33: import org.mvel.MVEL;
34:
35: /**
36: * A class field extractor that uses MVEL engine to extract the actual value for a given
37: * expression. We use MVEL to resolve nested accessor expressions.
38: *
39: * @author etirelli
40: */
41: public class MVELClassFieldExtractor extends
42: BaseObjectClassFieldExtractor {
43:
44: private static final long serialVersionUID = 400L;
45:
46: private CompiledExpression mvelExpression = null;
47: private Map extractors = null;
48: private Map variables = null;
49:
50: public MVELClassFieldExtractor(Class clazz, String fieldName,
51: ClassLoader classLoader) {
52: super (-1, // index
53: Object.class, // fieldType
54: ValueType.determineValueType(Object.class)); // value type
55: this .extractors = new HashMap();
56: this .variables = new HashMap();
57:
58: ExpressionCompiler compiler = new ExpressionCompiler(fieldName);
59: this .mvelExpression = compiler.compile();
60:
61: Set inputs = compiler.getParserContextState().getInputs()
62: .keySet();
63: for (Iterator it = inputs.iterator(); it.hasNext();) {
64: String basefield = (String) it.next();
65:
66: Extractor extr = ClassFieldExtractorCache.getExtractor(
67: clazz, basefield, classLoader);
68: this .extractors.put(basefield, extr);
69: }
70: }
71:
72: /* (non-Javadoc)
73: * @see org.drools.base.extractors.BaseObjectClassFieldExtractor#getValue(java.lang.Object)
74: */
75: public Object getValue(InternalWorkingMemory workingMemory,
76: Object object) {
77: for (Iterator it = this .extractors.entrySet().iterator(); it
78: .hasNext();) {
79: Map.Entry entry = (Map.Entry) it.next();
80: String var = (String) entry.getKey();
81: FieldExtractor extr = (FieldExtractor) entry.getValue();
82:
83: this.variables.put(var, extr
84: .getValue(workingMemory, object));
85: }
86: return MVEL.executeExpression(mvelExpression, this.variables);
87: }
88:
89: }
|