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.MVELPredicateExpression;
26: import org.drools.compiler.Dialect;
27: import org.drools.compiler.RuleError;
28: import org.drools.lang.descr.PredicateDescr;
29: import org.drools.rule.Declaration;
30: import org.drools.rule.PredicateConstraint;
31: import org.drools.rule.builder.PredicateBuilder;
32: import org.drools.rule.builder.RuleBuildContext;
33: import org.mvel.ExpressionCompiler;
34: import org.mvel.MVEL;
35: import org.mvel.ParserContext;
36:
37: /**
38: * @author etirelli
39: *
40: */
41: public class MVELPredicateBuilder implements PredicateBuilder {
42:
43: public void build(final RuleBuildContext context,
44: final List[] usedIdentifiers,
45: final Declaration[] previousDeclarations,
46: final Declaration[] localDeclarations,
47: final PredicateConstraint predicate,
48: final PredicateDescr predicateDescr) {
49: Map previousMap = new HashMap();
50: for (int i = 0, length = previousDeclarations.length; i < length; i++) {
51: previousMap.put(previousDeclarations[i].getIdentifier(),
52: previousDeclarations[i]);
53: }
54:
55: Map localMap = new HashMap();
56: for (int i = 0, length = localDeclarations.length; i < length; i++) {
57: localMap.put(localDeclarations[i].getIdentifier(),
58: localDeclarations[i]);
59: }
60:
61: try {
62: final DroolsMVELFactory factory = new DroolsMVELFactory(
63: previousMap, localMap, context.getPkg()
64: .getGlobals());
65: factory.setNextFactory(((MVELDialect) context.getDialect())
66: .getStaticMethodImportResolverFactory());
67:
68: Dialect.AnalysisResult analysis = context.getDialect()
69: .analyzeExpression(context, predicateDescr,
70: predicateDescr.getContent());
71:
72: final Serializable expr = ((MVELDialect) context
73: .getDialect()).compile((String) predicateDescr
74: .getContent(), analysis, null, null, context);
75:
76: predicate
77: .setPredicateExpression(new MVELPredicateExpression(
78: expr, factory));
79: } catch (final Exception e) {
80: context.getErrors().add(
81: new RuleError(context.getRule(), predicateDescr, e,
82: "Unable to build expression for 'inline-eval' node '"
83: + predicateDescr.getContent()
84: + "'\n" + e.getMessage()));
85: }
86: }
87:
88: }
|