package org.drools.decisiontable.parser;

/*
 * Copyright 2005 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.
 */
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.mvel.MVELTemplateRegistry;
import org.mvel.TemplateInterpreter;
import org.mvel.TemplateRegistry;
/**
 * @author <a href="mailto:stevearoonie@gmail.com">Steven Williams</a>
 * 
 * Generate the rules for a decision table row from a rule template.
 */
public class DefaultGenerator implements Generator {

	private Map ruleTemplates;

	private TemplateRegistry registry = new MVELTemplateRegistry();

	private List rules = new ArrayList();

	public DefaultGenerator(final Map t) {
		ruleTemplates = t;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.drools.decisiontable.parser.Generator#generate(java.lang.String,
	 *      org.drools.decisiontable.parser.Row)
	 */
	public void generate(String templateName, Row row) {
		try {
			String content = getTemplate(templateName);
            Map vars = new HashMap();
			vars.put("row", row);

			for (Iterator it = row.getCells().iterator(); it.hasNext();) {
				Cell cell = (Cell) it.next();
				cell.addValue(vars);
			}
			String drl = (String) TemplateInterpreter.parse( content, null, vars, this.registry ); 
			rules.add(drl);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	private String getTemplate(String templateName) throws IOException {
		String contents = (String) registry.getTemplate( templateName );
		if (contents == null) {
			RuleTemplate template = (RuleTemplate) ruleTemplates
					.get(templateName);
			contents = template.getContents();
            registry.registerTemplate( templateName, contents);
		}
		return contents;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.drools.decisiontable.parser.Generator#getDrl()
	 */
	public String getDrl() {
		StringBuffer sb = new StringBuffer();
		for (Iterator it = rules.iterator(); it.hasNext();) {
			String rule = (String) it.next();
			sb.append(rule).append("\n");
		}
		return sb.toString();
	}

}

@@ -649,16 +649,48 @@
 .%0D%0A */%0D%0A
+import org.mvel.templates.*;%0D%0A%0D%0A
 import j
@@ -847,118 +847,8 @@
 %0D%0A%0D%0A
-import org.mvel.MVELTemplateRegistry;%0D%0Aimport org.mvel.TemplateInterpreter;%0D%0Aimport org.mvel.TemplateRegistry;
 %0D%0A/*
@@ -1134,12 +1134,14 @@
 new 
-MVEL
+Simple
 Temp
@@ -1499,30 +1499,41 @@
 y %7B%0D%0A%09%09%09
-String content
+CompiledTemplate template
  = getTe
@@ -1756,19 +1756,30 @@
 %0D%0A%09%09%09%7D%0D%0A
-%09%09%09
+%0D%0A            
 String d
@@ -1787,65 +1787,63 @@
 l = 
-(
 String
-) TemplateInterpreter.parse( content, null
+.valueOf(TemplateRuntime.execute(template
 , vars, 
 this
@@ -1842,21 +1842,16 @@
 rs, 
-this.
 registry
  ); 
@@ -1850,12 +1850,13 @@
 stry
- ); 
+));%0D%0A
 %0D%0A%09%09
@@ -1955,22 +1955,32 @@
 private 
-String
+CompiledTemplate
  getTemp
@@ -2027,24 +2027,40 @@
 tion %7B%0D%0A
-%09%09String
+        CompiledTemplate
  content
@@ -2062,25 +2062,16 @@
 ntents =
- (String)
  registr
@@ -2075,16 +2075,21 @@
 stry.get
+Named
 Template
@@ -2231,16 +2231,49 @@
 tents = 
+TemplateCompiler.compileTemplate(
 template
@@ -2286,16 +2286,17 @@
 ntents()
+)
 ;%0D%0A     
@@ -2315,16 +2315,16 @@
 try.
-register
+addNamed
 Temp

fix to MVEL calls