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:
17: package org.drools.rule.builder.dialect.mvel;
18:
19: import java.io.Serializable;
20: import java.util.HashMap;
21: import java.util.List;
22: import java.util.Map;
23:
24: import org.drools.base.mvel.DroolsMVELFactory;
25: import org.drools.base.mvel.MVELReturnValueExpression;
26: import org.drools.compiler.Dialect;
27: import org.drools.lang.descr.ReturnValueRestrictionDescr;
28: import org.drools.rule.Declaration;
29: import org.drools.rule.ReturnValueRestriction;
30: import org.drools.rule.builder.ReturnValueBuilder;
31: import org.drools.rule.builder.RuleBuildContext;
32: import org.mvel.ExpressionCompiler;
33: import org.mvel.MVEL;
34: import org.mvel.ParserContext;
35:
36: /**
37: * @author etirelli
38: *
39: */
40: public class MVELReturnValueBuilder implements ReturnValueBuilder {
41:
42: public void build(
43: final RuleBuildContext context,
44: final List[] usedIdentifiers,
45: final Declaration[] previousDeclarations,
46: final Declaration[] localDeclarations,
47: final ReturnValueRestriction returnValueRestriction,
48: final ReturnValueRestrictionDescr returnValueRestrictionDescr) {
49:
50: Map previousMap = new HashMap();
51: for (int i = 0, length = previousDeclarations.length; i < length; i++) {
52: previousMap.put(previousDeclarations[i].getIdentifier(),
53: previousDeclarations[i]);
54: }
55:
56: Map localMap = new HashMap();
57: for (int i = 0, length = localDeclarations.length; i < length; i++) {
58: localMap.put(localDeclarations[i].getIdentifier(),
59: localDeclarations[i]);
60: }
61:
62: final DroolsMVELFactory factory = new DroolsMVELFactory(
63: previousMap, localMap, context.getPkg().getGlobals());
64: factory.setNextFactory(((MVELDialect) context.getDialect())
65: .getStaticMethodImportResolverFactory());
66:
67: Dialect.AnalysisResult analysis = context.getDialect()
68: .analyzeExpression(context,
69: returnValueRestrictionDescr,
70: returnValueRestrictionDescr.getContent());
71:
72: final Serializable expr = ((MVELDialect) context.getDialect())
73: .compile((String) returnValueRestrictionDescr
74: .getContent(), analysis, null, null, context);
75:
76: returnValueRestriction
77: .setReturnValueExpression(new MVELReturnValueExpression(
78: expr, factory));
79: }
80:
81: }
|