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.java;
18:
19: import java.util.List;
20: import java.util.Map;
21:
22: import org.drools.compiler.Dialect;
23: import org.drools.lang.descr.BaseDescr;
24: import org.drools.lang.descr.EvalDescr;
25: import org.drools.rule.Declaration;
26: import org.drools.rule.EvalCondition;
27: import org.drools.rule.Pattern;
28: import org.drools.rule.RuleConditionElement;
29: import org.drools.rule.builder.RuleBuildContext;
30: import org.drools.rule.builder.RuleConditionBuilder;
31:
32: /**
33: * @author etirelli
34: *
35: */
36: public class JavaEvalBuilder extends AbstractJavaBuilder implements
37: RuleConditionBuilder {
38:
39: public RuleConditionElement build(final RuleBuildContext context,
40: final BaseDescr descr) {
41: return build(context, descr, null);
42: }
43:
44: /**
45: * Builds and returns an Eval Conditional Element
46: *
47: * @param context The current build context
48: * @param utils The current build utils instance
49: * @param patternBuilder not used by EvalBuilder
50: * @param descr The Eval Descriptor to build the eval conditional element from
51: *
52: * @return the Eval Conditional Element
53: */
54: public RuleConditionElement build(final RuleBuildContext context,
55: final BaseDescr descr, final Pattern prefixPattern) {
56: // it must be an EvalDescr
57: final EvalDescr evalDescr = (EvalDescr) descr;
58:
59: final String className = "eval" + context.getNextId();
60:
61: evalDescr.setClassMethodName(className);
62:
63: Dialect.AnalysisResult analysis = context.getDialect()
64: .analyzeExpression(context, evalDescr,
65: evalDescr.getContent());
66: final List[] usedIdentifiers = analysis.getBoundIdentifiers();
67:
68: final Declaration[] declarations = new Declaration[usedIdentifiers[0]
69: .size()];
70: for (int i = 0, size = usedIdentifiers[0].size(); i < size; i++) {
71: declarations[i] = context.getDeclarationResolver()
72: .getDeclaration((String) usedIdentifiers[0].get(i));
73: }
74:
75: final EvalCondition eval = new EvalCondition(declarations);
76:
77: final Map map = createVariableContext(className,
78: (String) evalDescr.getContent(), context, declarations,
79: null, (String[]) usedIdentifiers[1]
80: .toArray(new String[usedIdentifiers[1].size()]));
81:
82: generatTemplates("evalMethod", "evalInvoker", context,
83: className, map, eval, descr);
84:
85: return eval;
86: }
87: }
|