01: package org.drools.rule.builder.dialect.mvel;
02:
03: import java.io.Serializable;
04:
05: import org.drools.base.mvel.DroolsMVELFactory;
06: import org.drools.base.mvel.MVELSalienceExpression;
07: import org.drools.compiler.Dialect;
08: import org.drools.compiler.RuleError;
09: import org.drools.rule.builder.RuleBuildContext;
10: import org.drools.rule.builder.SalienceBuilder;
11: import org.mvel.ExpressionCompiler;
12: import org.mvel.MVEL;
13: import org.mvel.ParserContext;
14: import org.mvel.integration.impl.ClassImportResolverFactory;
15:
16: public class MVELSalienceBuilder implements SalienceBuilder {
17:
18: public void build(RuleBuildContext context) {
19: // pushing consequence LHS into the stack for variable resolution
20: context.getBuildStack().push(context.getRule().getLhs());
21:
22: try {
23: final DroolsMVELFactory factory = new DroolsMVELFactory(
24: context.getDeclarationResolver().getDeclarations(),
25: null, context.getPkg().getGlobals());
26: MVELDialect dialect = (MVELDialect) context
27: .getDialect("mvel");
28: // This builder is re-usable in other dialects, so specify by name
29: factory.setNextFactory(dialect
30: .getStaticMethodImportResolverFactory());
31:
32: Dialect.AnalysisResult analysis = dialect
33: .analyzeExpression(context, context.getRuleDescr(),
34: (String) context.getRuleDescr()
35: .getSalience());
36:
37: final Serializable expr = dialect.compile((String) context
38: .getRuleDescr().getSalience(), analysis, null,
39: null, context);
40:
41: MVELSalienceExpression salience = new MVELSalienceExpression(
42: expr, factory);
43:
44: context.getRule().setSalience(salience);
45: } catch (final Exception e) {
46: context.getErrors().add(
47: new RuleError(context.getRule(), context
48: .getRuleDescr(), null,
49: "Unable to build expression for 'salience' node '"
50: + context.getRuleDescr()
51: .getSalience() + "'"));
52: }
53: }
54:
55: }
|