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

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.drools.base.mvel.DroolsMVELFactory;
import org.drools.base.mvel.MVELConsequence;
import org.drools.compiler.Dialect;
import org.drools.compiler.RuleError;
import org.drools.rule.builder.ConsequenceBuilder;
import org.drools.rule.builder.RuleBuildContext;
import org.mvel.Macro;
import org.mvel.MacroProcessor;

public class MVELConsequenceBuilder
    implements
    ConsequenceBuilder {

    //private final Interceptor assertInterceptor;
    //private final Interceptor modifyInterceptor;

    private final Map macros;

    public MVELConsequenceBuilder() {
        macros = new HashMap( 4 );

        macros.put( "insert",
                    new Macro() {
                        public String doMacro() {
                            return "drools.insert";
                        }
                    } );
        
        macros.put( "insertLogical",
                    new Macro() {
                        public String doMacro() {
                            return "drools.insertLogical";
                        }
                    } );        

        macros.put( "modify",
                    new Macro() {
                        public String doMacro() {
                            return "@Modify with";
                        }
                    } );

        macros.put( "update",
                    new Macro() {
                        public String doMacro() {
                            return "drools.update";
                        }
                    } );

        macros.put( "retract",
                    new Macro() {
                        public String doMacro() {
                            return "drools.retract";
                        }
                    } );
    }

    public void build(final RuleBuildContext context) {
        // pushing consequence LHS into the stack for variable resolution
        context.getBuildStack().push( context.getRule().getLhs() );

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


            String text = processMacros( (String) context.getRuleDescr().getConsequence() );

            Dialect.AnalysisResult analysis = dialect.analyzeBlock( context,
                                                                    context.getRuleDescr(),
                                                                    dialect.getInterceptors(),
                                                                    text,
                                                                    null );

            final Serializable expr = dialect.compile( text,
                                                       analysis,
                                                       dialect.getInterceptors(),
                                                       null,
                                                       context );            
            
            final DroolsMVELFactory factory = new DroolsMVELFactory( context.getDeclarationResolver().getDeclarations(),
                                                                     null,
                                                                     context.getPkg().getGlobals(),
                                                                     analysis.getBoundIdentifiers() );                        

            context.getRule().setConsequence( new MVELConsequence( expr,
                                                                   factory ) );
        } catch ( final Exception e ) {
            context.getErrors().add( new RuleError( context.getRule(),
                                                    context.getRuleDescr(),
                                                    null,
                                                    "Unable to build expression for 'consequence' '" + context.getRuleDescr().getConsequence() + "'" ) );
        }
    }

    public String processMacros(String consequence) {
        MacroProcessor macroProcessor = new MacroProcessor();
        macroProcessor.setMacros( macros );
        return macroProcessor.parse( delimitExpressions( consequence ) );
    }

    /**
     * Allows newlines to demarcate expressions, as per MVEL command line.
     * If expression spans multiple lines (ie inside an unbalanced bracket) then
     * it is left alone.
     * Uses character based iteration which is at least an order of magnitude faster then a single
     * simple regex.
     */
    public static String delimitExpressions(String s) {

        StringBuffer result = new StringBuffer();
        char[] cs = s.toCharArray();
        int brace = 0;
        int sqre = 0;
        int crly = 0;
        char lastNonWhite = ';';
        for ( int i = 0; i < cs.length; i++ ) {
            char c = cs[i];
            switch ( c ) {
                case '(' :
                    brace++;
                    break;
                case '{' :
                    crly++;
                    break;
                case '[' :
                    sqre++;
                    break;
                case ')' :
                    brace--;
                    break;
                case '}' :
                    crly--;
                    break;
                case ']' :
                    sqre--;
                    break;
                default :
                    break;
            }
            if ( (brace == 0 && sqre == 0 && crly == 0) && (c == '\n' || c == '\r') ) {
                if ( lastNonWhite != ';' ) {
                    result.append( ';' );
                    lastNonWhite = ';';
                }
            } else if ( !Character.isWhitespace( c ) ) {
                lastNonWhite = c;
            }
            result.append( c );

        }
        return result.toString();
    }

        }


@@ -650,24 +650,31 @@
     private 
+static 
 final Map ma
@@ -675,66 +675,8 @@
 Map 
-macros;%0D%0A%0D%0A    public MVELConsequenceBuilder() %7B%0D%0A        
 macr
@@ -697,14 +697,26 @@
 ap( 
-4
+5
  );%0D%0A
+    static %7B
 %0D%0A  
@@ -936,26 +936,18 @@
   %7D );%0D%0A
-        
 %0D%0A
+
         
@@ -1173,24 +1173,16 @@
     %7D );
-        
 %0D%0A%0D%0A    
@@ -1849,16 +1849,67 @@
     %7D );
+;%0D%0A    %7D%0D%0A%0D%0A    public MVELConsequenceBuilder() %7B%0D%0A
 %0D%0A    %7D%0D
@@ -2202,18 +2202,16 @@
 t();%0D%0A%0D%0A
-%0D%0A
         
@@ -3061,34 +3061,10 @@
 t );
-            %0D%0A            
+%0D%0A
 %0D%0A  
@@ -3466,32 +3466,8 @@
 ) );
-                        
 %0D%0A%0D%0A
@@ -4052,24 +4052,31 @@
 %0A    public 
+static 
 String proce
@@ -6003,17 +6003,7 @@
 %0D%0A%0D%0A
-        
 %7D%0D%0A
-%0D%0A

Made the macro map initialization static. Reformatted code.