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:
21: import org.drools.base.dataproviders.MVELDataProvider;
22: import org.drools.base.mvel.DroolsMVELFactory;
23: import org.drools.compiler.Dialect;
24: import org.drools.compiler.RuleError;
25: import org.drools.lang.descr.AccessorDescr;
26: import org.drools.lang.descr.BaseDescr;
27: import org.drools.lang.descr.FromDescr;
28: import org.drools.rule.From;
29: import org.drools.rule.Pattern;
30: import org.drools.rule.RuleConditionElement;
31: import org.drools.rule.builder.FromBuilder;
32: import org.drools.rule.builder.RuleBuildContext;
33: import org.drools.spi.DataProvider;
34: import org.mvel.ExpressionCompiler;
35: import org.mvel.MVEL;
36: import org.mvel.ParserContext;
37: import org.mvel.integration.impl.ClassImportResolverFactory;
38:
39: /**
40: * A builder for "from" conditional element
41: *
42: * @author etirelli
43: */
44: public class MVELFromBuilder implements FromBuilder {
45:
46: public RuleConditionElement build(final RuleBuildContext context,
47: final BaseDescr descr) {
48: return build(context, descr, null);
49: }
50:
51: public RuleConditionElement build(final RuleBuildContext context,
52: final BaseDescr descr, final Pattern prefixPattern) {
53: final FromDescr fromDescr = (FromDescr) descr;
54:
55: final AccessorDescr accessor = (AccessorDescr) fromDescr
56: .getDataSource();
57: DataProvider dataProvider = null;
58: try {
59: final DroolsMVELFactory factory = new DroolsMVELFactory(
60: context.getDeclarationResolver().getDeclarations(),
61: null, context.getPkg().getGlobals());
62:
63: // This builder is re-usable in other dialects, so specify by name
64: MVELDialect dialect = (MVELDialect) context
65: .getDialect("mvel");
66:
67: factory.setNextFactory(dialect
68: .getStaticMethodImportResolverFactory());
69:
70: String text = (String) accessor.toString();
71: Dialect.AnalysisResult analysis = dialect
72: .analyzeExpression(context, descr, text);
73:
74: final Serializable expr = dialect.compile(text, analysis,
75: null, null, context);
76:
77: dataProvider = new MVELDataProvider(expr, factory);
78: } catch (final Exception e) {
79: context.getErrors().add(
80: new RuleError(context.getRule(), fromDescr, null,
81: "Unable to build expression for 'from' node '"
82: + accessor + "'"));
83: return null;
84: }
85:
86: return new From(dataProvider);
87: }
88: }
|