001: package org.drools.rule.builder.dialect.java;
002:
003: import java.io.InputStreamReader;
004: import java.util.ArrayList;
005: import java.util.HashMap;
006: import java.util.List;
007: import java.util.Map;
008:
009: import org.drools.lang.descr.BaseDescr;
010: import org.drools.rule.Declaration;
011: import org.drools.rule.builder.RuleBuildContext;
012: import org.drools.util.StringUtils;
013: import org.mvel.MVELTemplateRegistry;
014: import org.mvel.TemplateInterpreter;
015: import org.mvel.TemplateRegistry;
016:
017: public class AbstractJavaBuilder {
018:
019: protected static final TemplateRegistry RULE_REGISTRY = new MVELTemplateRegistry();
020: protected static final TemplateRegistry INVOKER_REGISTRY = new MVELTemplateRegistry();
021:
022: static {
023: RULE_REGISTRY.registerTemplate(new InputStreamReader(
024: AbstractJavaBuilder.class
025: .getResourceAsStream("javaRule.mvel")));
026: INVOKER_REGISTRY.registerTemplate(new InputStreamReader(
027: AbstractJavaBuilder.class
028: .getResourceAsStream("javaInvokers.mvel")));
029: }
030:
031: public TemplateRegistry getRuleTemplateRegistry() {
032: return RULE_REGISTRY;
033: }
034:
035: public TemplateRegistry getInvokerTemplateRegistry() {
036: return INVOKER_REGISTRY;
037: }
038:
039: public Map createVariableContext(final String className,
040: final String text, final RuleBuildContext context,
041: final Declaration[] declarations,
042: final Declaration[] localDeclarations,
043: final String[] globals) {
044: final Map map = new HashMap();
045:
046: map.put("methodName", className);
047:
048: map.put("package", context.getPkg().getName());
049:
050: map.put("ruleClassName", StringUtils.ucFirst(context
051: .getRuleDescr().getClassName()));
052:
053: map.put("invokerClassName", context.getRuleDescr()
054: .getClassName()
055: + StringUtils.ucFirst(className) + "Invoker");
056:
057: if (text != null) {
058: map.put("text", text);
059:
060: map.put("hashCode", new Integer(text.hashCode()));
061: }
062:
063: final String[] declarationTypes = new String[declarations.length];
064: for (int i = 0, size = declarations.length; i < size; i++) {
065: declarationTypes[i] = ((JavaDialect) context.getDialect())
066: .getTypeFixer().fix(declarations[i]);
067: }
068:
069: map.put("declarations", declarations);
070:
071: map.put("declarationTypes", declarationTypes);
072:
073: if (localDeclarations != null) {
074: final String[] localDeclarationTypes = new String[localDeclarations.length];
075: for (int i = 0, size = localDeclarations.length; i < size; i++) {
076: localDeclarationTypes[i] = ((JavaDialect) context
077: .getDialect()).getTypeFixer().fix(
078: localDeclarations[i]);
079: }
080:
081: map.put("localDeclarations", localDeclarations);
082:
083: map.put("localDeclarationTypes", localDeclarationTypes);
084: }
085:
086: final List globalTypes = new ArrayList(globals.length);
087: for (int i = 0, length = globals.length; i < length; i++) {
088: globalTypes.add(((Class) context.getPkg().getGlobals().get(
089: globals[i])).getName().replace('$', '.'));
090: }
091:
092: map.put("globals", globals);
093:
094: map.put("globalTypes", globalTypes);
095:
096: return map;
097: }
098:
099: public void generatTemplates(final String ruleTemplate,
100: final String invokerTemplate,
101: final RuleBuildContext context, final String className,
102: final Map vars, final Object invokerLookup,
103: final BaseDescr descrLookup) {
104: TemplateRegistry registry = getRuleTemplateRegistry();
105: context.getMethods().add(
106: TemplateInterpreter.parse(registry
107: .getTemplate(ruleTemplate), null, vars,
108: registry));
109:
110: registry = getInvokerTemplateRegistry();
111: final String invokerClassName = context.getPkg().getName()
112: + "." + context.getRuleDescr().getClassName()
113: + StringUtils.ucFirst(className) + "Invoker";
114: context.getInvokers().put(
115: invokerClassName,
116: TemplateInterpreter.parse(registry
117: .getTemplate(invokerTemplate), null, vars,
118: registry));
119:
120: context.getInvokerLookups()
121: .put(invokerClassName, invokerLookup);
122: context.getDescrLookups().put(invokerClassName, descrLookup);
123: }
124: }
|