Package org.drools.rule.builder.dialect.mvel

Source Code of org.drools.rule.builder.dialect.mvel.MVELAccumulateBuilder

/*
* Copyright 2007 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.drools.rule.builder.dialect.mvel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.drools.base.accumulators.AccumulateFunction;
import org.drools.base.accumulators.MVELAccumulatorFunctionExecutor;
import org.drools.base.mvel.DroolsMVELFactory;
import org.drools.base.mvel.MVELAccumulator;
import org.drools.compiler.Dialect;
import org.drools.lang.descr.AccumulateDescr;
import org.drools.lang.descr.BaseDescr;
import org.drools.lang.descr.PatternDescr;
import org.drools.rule.Accumulate;
import org.drools.rule.Declaration;
import org.drools.rule.Pattern;
import org.drools.rule.RuleConditionElement;
import org.drools.rule.builder.AccumulateBuilder;
import org.drools.rule.builder.PatternBuilder;
import org.drools.rule.builder.RuleBuildContext;
import org.drools.spi.Accumulator;
import org.mvel.MVEL;

/**
* A builder for the java dialect accumulate version
*
* @author etirelli
*/
public class MVELAccumulateBuilder
    implements
    AccumulateBuilder {

    public RuleConditionElement build(final RuleBuildContext context,
                                      final BaseDescr descr) {
        return build( context,
                      descr,
                      null );
    }

    public RuleConditionElement build(final RuleBuildContext context,
                                      final BaseDescr descr,
                                      final Pattern prefixPattern) {

        final AccumulateDescr accumDescr = (AccumulateDescr) descr;

        final PatternBuilder patternBuilder = (PatternBuilder) context.getDialect().getBuilder( PatternDescr.class );

        // create source pattern
        final Pattern sourcePattern = (Pattern) patternBuilder.build( context,
                                                                      accumDescr.getInputPattern() );

        if ( sourcePattern == null ) {
            return null;
        }

        MVELDialect dialect = (MVELDialect) context.getDialect();

        final Declaration[] sourceDeclArr = (Declaration[]) sourcePattern.getOuterDeclarations().values().toArray( new Declaration[0] );

        final DroolsMVELFactory factory = new DroolsMVELFactory( context.getDeclarationResolver().getDeclarations(),
                                                                 sourcePattern.getOuterDeclarations(),
                                                                 context.getPkg().getGlobals() );
        factory.setNextFactory( dialect.getStaticMethodImportResolverFactory() );

        Accumulator accumulator = null;
        Declaration[] declarations = null;

        if ( accumDescr.isExternalFunction() ) {
            // build an external function executor
            final Dialect.AnalysisResult analysis = dialect.analyzeExpression( context,
                                                                               accumDescr,
                                                                               accumDescr.getExpression() );

            int size = analysis.getBoundIdentifiers()[0].size();
            declarations = new Declaration[size];
            for ( int i = 0; i < size; i++ ) {
                declarations[i] = context.getDeclarationResolver().getDeclaration( (String) analysis.getBoundIdentifiers()[0].get( i ) );
            }

            final Serializable expression = dialect.compile( (String) accumDescr.getExpression(),
                                                             analysis,
                                                             null,
                                                             sourcePattern.getOuterDeclarations(),
                                                             context );

            AccumulateFunction function = context.getConfiguration().getAccumulateFunction( accumDescr.getFunctionIdentifier() );

            accumulator = new MVELAccumulatorFunctionExecutor( factory,
                                                               expression,
                                                               function );
        } else {
            // it is a custom accumulate
            final MVELAnalysisResult initCodeAnalysis = (MVELAnalysisResult) dialect.analyzeBlock( context,
                                                                                                   accumDescr,
                                                                                                   accumDescr.getInitCode() );

            final MVELAnalysisResult actionCodeAnalysis = (MVELAnalysisResult) dialect.analyzeBlock( context,
                                                                                                     accumDescr,
                                                                                                     null,
                                                                                                     accumDescr.getActionCode(),
                                                                                                     initCodeAnalysis.getMvelVariables() );
            actionCodeAnalysis.setMvelVariables( initCodeAnalysis.getMvelVariables() );
            final MVELAnalysisResult resultCodeAnalysis = (MVELAnalysisResult) dialect.analyzeExpression( context,
                                                                                                          accumDescr,
                                                                                                          accumDescr.getResultCode(),
                                                                                                          initCodeAnalysis.getMvelVariables() );
            resultCodeAnalysis.setMvelVariables( initCodeAnalysis.getMvelVariables() );

            final List requiredDeclarations = new ArrayList( initCodeAnalysis.getBoundIdentifiers()[0] );
            requiredDeclarations.addAll( actionCodeAnalysis.getBoundIdentifiers()[0] );
            requiredDeclarations.addAll( resultCodeAnalysis.getBoundIdentifiers()[0] );

            if ( accumDescr.getReverseCode() != null ) {
                final Dialect.AnalysisResult reverseCodeAnalysis = context.getDialect().analyzeBlock( context,
                                                                                                      accumDescr,
                                                                                                      accumDescr.getActionCode() );
                requiredDeclarations.addAll( reverseCodeAnalysis.getBoundIdentifiers()[0] );
            }

            declarations = new Declaration[requiredDeclarations.size()];
            for ( int i = 0, size = requiredDeclarations.size(); i < size; i++ ) {
                declarations[i] = context.getDeclarationResolver().getDeclaration( (String) requiredDeclarations.get( i ) );
            }

            final Serializable init = dialect.compile( (String) accumDescr.getInitCode(),
                                                       initCodeAnalysis,
                                                       null,
                                                       sourcePattern.getOuterDeclarations(),
                                                       context );
            final Serializable action = dialect.compile( (String) accumDescr.getActionCode(),
                                                         actionCodeAnalysis,
                                                         null,
                                                         sourcePattern.getOuterDeclarations(),
                                                         context );

            Serializable reverse = null;
            if ( accumDescr.getReverseCode() != null ) {
                reverse = dialect.compile( (String) accumDescr.getReverseCode(),
                                           resultCodeAnalysis,
                                           null,
                                           sourcePattern.getOuterDeclarations(),
                                           context );
            }

            final Serializable result = dialect.compile( (String) accumDescr.getResultCode(),
                                                         resultCodeAnalysis,
                                                         null,
                                                         sourcePattern.getOuterDeclarations(),
                                                         context );

            accumulator = new MVELAccumulator( factory,
                                               init,
                                               action,
                                               reverse,
                                               result );

        }

        final Accumulate accumulate = new Accumulate( sourcePattern,
                                                      declarations,
                                                      sourceDeclArr,
                                                      accumulator );
        return accumulate;
    }

}
TOP

Related Classes of org.drools.rule.builder.dialect.mvel.MVELAccumulateBuilder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.