001: package org.drools.rule.builder.dialect.java;
002:
003: import java.io.InputStreamReader;
004: import java.util.Arrays;
005: import java.util.HashMap;
006: import java.util.List;
007: import java.util.Map;
008:
009: import junit.framework.TestCase;
010:
011: import org.drools.Cheese;
012: import org.drools.Person;
013: import org.drools.base.ClassFieldExtractorCache;
014: import org.drools.base.ClassObjectType;
015: import org.drools.rule.Declaration;
016: import org.drools.spi.PatternExtractor;
017: import org.drools.util.StringUtils;
018: import org.mvel.MVELTemplateRegistry;
019: import org.mvel.TemplateInterpreter;
020: import org.mvel.TemplateRegistry;
021:
022: public class AccumulateTemplateTest extends TestCase {
023:
024: protected void setUp() throws Exception {
025: super .setUp();
026: }
027:
028: protected void tearDown() throws Exception {
029: super .tearDown();
030: }
031:
032: public void testMethodGeneration() {
033: final String className = "accumulate0";
034:
035: final String[] declarationTypes = new String[] { "String",
036: "int" };
037: final Declaration[] declarations = new Declaration[] {
038: new Declaration("name", null, null),
039: new Declaration("age", null, null) };
040: final Declaration[] inner = new Declaration[] {
041: new Declaration("cheese", new PatternExtractor(
042: new ClassObjectType(Cheese.class)), null),
043: new Declaration("price", ClassFieldExtractorCache
044: .getExtractor(Cheese.class, "price", getClass()
045: .getClassLoader()), null) };
046: final String[] globals = new String[] { "aGlobal",
047: "anotherGlobal" };
048: final List globalTypes = Arrays.asList(new String[] { "String",
049: "String" });
050:
051: final Map map = new HashMap();
052:
053: map.put("className", StringUtils.ucFirst(className));
054:
055: map.put("instanceName", className);
056:
057: map.put("package", "org.drools");
058:
059: map.put("ruleClassName", "Rule0");
060:
061: map.put("invokerClassName", "Rule0"
062: + StringUtils.ucFirst(className) + "Invoker");
063:
064: map.put("declarations", declarations);
065:
066: map.put("declarationTypes", declarationTypes);
067:
068: map.put("globals", globals);
069:
070: map.put("globalTypes", globalTypes);
071:
072: map.put("innerDeclarations", inner);
073:
074: map.put("attributes", new String[] { "x" });
075: map.put("attributesTypes", new String[] { "int" });
076: map.put("initCode", "x = 0;");
077: map.put("actionCode", "x += 1;");
078: map.put("reverseCode", "x -= 1;");
079: map.put("resultCode", "x + 10");
080: map.put("supportsReverse", "true");
081:
082: map.put("resultType", Integer.class);
083:
084: map.put("hashCode", new Integer(10));
085:
086: TemplateRegistry registry = getRuleTemplateRegistry();
087: Object method = TemplateInterpreter.parse(registry
088: .getTemplate("accumulateInnerClass"), null, map,
089: registry);
090:
091: //System.out.println( method );
092: }
093:
094: public void testInvokerGeneration() {
095: final String className = "accumulate0";
096:
097: final String[] declarationTypes = new String[] { "String",
098: "int" };
099: final Declaration[] declarations = new Declaration[] {
100: new Declaration("name", ClassFieldExtractorCache
101: .getExtractor(Person.class, "name", getClass()
102: .getClassLoader()), null),
103: new Declaration("age", ClassFieldExtractorCache
104: .getExtractor(Person.class, "age", getClass()
105: .getClassLoader()), null) };
106: final Declaration[] inner = new Declaration[] {
107: new Declaration("cheese", new PatternExtractor(
108: new ClassObjectType(Cheese.class)), null),
109: new Declaration("price", ClassFieldExtractorCache
110: .getExtractor(Cheese.class, "price", getClass()
111: .getClassLoader()), null) };
112: final String[] globals = new String[] { "aGlobal",
113: "anotherGlobal" };
114: final List globalTypes = Arrays.asList(new String[] { "String",
115: "String" });
116:
117: final Map map = new HashMap();
118:
119: map.put("className", StringUtils.ucFirst(className));
120:
121: map.put("instanceName", className);
122:
123: map.put("package", "org.drools");
124:
125: map.put("ruleClassName", "Rule0");
126:
127: map.put("invokerClassName", "Rule0"
128: + StringUtils.ucFirst(className) + "Invoker");
129:
130: map.put("declarations", declarations);
131:
132: map.put("declarationTypes", declarationTypes);
133:
134: map.put("globals", globals);
135:
136: map.put("globalTypes", globalTypes);
137:
138: map.put("innerDeclarations", inner);
139:
140: map.put("attributes", new Attribute[] { new Attribute("int",
141: "x") });
142: map.put("initCode", "x = 0;");
143: map.put("actionCode", "x += 1;");
144: map.put("reverseCode", "");
145: map.put("resultCode", "x + 10");
146:
147: map.put("supportsReverse", "false");
148:
149: map.put("resultType", Integer.class);
150:
151: map.put("hashCode", new Integer(10));
152:
153: TemplateRegistry registry = getInvokerTemplateRegistry();
154: Object method = TemplateInterpreter.parse(registry
155: .getTemplate("accumulateInvoker"), null, map, registry);
156:
157: //System.out.println( method );
158: }
159:
160: private TemplateRegistry getRuleTemplateRegistry() {
161: TemplateRegistry ruleRegistry = new MVELTemplateRegistry();
162: ruleRegistry.registerTemplate(new InputStreamReader(
163: AbstractJavaBuilder.class
164: .getResourceAsStream("javaRule.mvel")));
165:
166: return ruleRegistry;
167: }
168:
169: private TemplateRegistry getInvokerTemplateRegistry() {
170: TemplateRegistry invokerRegistry = new MVELTemplateRegistry();
171: invokerRegistry.registerTemplate(new InputStreamReader(
172: AbstractJavaBuilder.class
173: .getResourceAsStream("javaInvokers.mvel")));
174:
175: return invokerRegistry;
176: }
177:
178: public static class Attribute {
179: private String type;
180: private String name;
181:
182: public Attribute(String type, String name) {
183: this .type = type;
184: this .name = name;
185: }
186:
187: public String getName() {
188: return name;
189: }
190:
191: public void setName(String name) {
192: this .name = name;
193: }
194:
195: public String getType() {
196: return type;
197: }
198:
199: public void setType(String type) {
200: this.type = type;
201: }
202: }
203:
204: }
|