001: /*
002: * Copyright 2006 JBoss Inc
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.drools.rule.builder.dialect.mvel;
018:
019: import java.io.Serializable;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.drools.base.mvel.DroolsMVELFactory;
025: import org.drools.base.mvel.MVELEvalExpression;
026: import org.drools.compiler.Dialect;
027: import org.drools.compiler.RuleError;
028: import org.drools.lang.descr.BaseDescr;
029: import org.drools.lang.descr.EvalDescr;
030: import org.drools.rule.Declaration;
031: import org.drools.rule.EvalCondition;
032: import org.drools.rule.Pattern;
033: import org.drools.rule.RuleConditionElement;
034: import org.drools.rule.builder.RuleBuildContext;
035: import org.drools.rule.builder.RuleConditionBuilder;
036: import org.drools.spi.DeclarationScopeResolver;
037: import org.mvel.ExpressionCompiler;
038: import org.mvel.MVEL;
039: import org.mvel.ParserContext;
040: import org.mvel.util.ParseTools;
041:
042: /**
043: * @author etirelli
044: *
045: */
046: public class MVELEvalBuilder implements RuleConditionBuilder {
047:
048: public RuleConditionElement build(final RuleBuildContext context,
049: final BaseDescr descr) {
050: return build(context, descr, null);
051: }
052:
053: /**
054: * Builds and returns an Eval Conditional Element
055: *
056: * @param context The current build context
057: * @param utils The current build utils instance
058: * @param patternBuilder not used by EvalBuilder
059: * @param descr The Eval Descriptor to build the eval conditional element from
060: *
061: * @return the Eval Conditional Element
062: */
063: public RuleConditionElement build(final RuleBuildContext context,
064: final BaseDescr descr, final Pattern prefixPattern) {
065: // it must be an EvalDescr
066: final EvalDescr evalDescr = (EvalDescr) descr;
067:
068: try {
069: final DroolsMVELFactory factory = new DroolsMVELFactory(
070: context.getDeclarationResolver().getDeclarations(),
071: null, context.getPkg().getGlobals());
072: factory.setNextFactory(((MVELDialect) context.getDialect())
073: .getStaticMethodImportResolverFactory());
074:
075: Dialect.AnalysisResult analysis = context.getDialect()
076: .analyzeExpression(context, evalDescr,
077: evalDescr.getContent());
078: final List[] usedIdentifiers = analysis
079: .getBoundIdentifiers();
080:
081: final Declaration[] declarations = new Declaration[usedIdentifiers[0]
082: .size()];
083: for (int i = 0, size = usedIdentifiers[0].size(); i < size; i++) {
084: declarations[i] = context.getDeclarationResolver()
085: .getDeclaration(
086: (String) usedIdentifiers[0].get(i));
087: }
088:
089: final EvalCondition eval = new EvalCondition(declarations);
090:
091: Serializable expr = ((MVELDialect) context.getDialect())
092: .compile((String) evalDescr.getContent(), analysis,
093: null, null, context);
094:
095: eval
096: .setEvalExpression(new MVELEvalExpression(expr,
097: factory));
098:
099: return eval;
100: } catch (final Exception e) {
101: context.getErrors().add(
102: new RuleError(context.getRule(), evalDescr, e,
103: "Unable to build expression for 'eval' node '"
104: + evalDescr.getContent() + "'"));
105: return null;
106: }
107: }
108:
109: }
|