use of org.mvel2.integration.impl.MapVariableResolverFactory in project drools by kiegroup.
the class AccumulateTemplateTest method testInvokerGenerationSinglePattern.
@Test
public void testInvokerGenerationSinglePattern() {
final String className = "accumulate0";
final String[] declarationTypes = new String[] { "String", "int" };
final Declaration[] declarations = new Declaration[] { new Declaration("name", store.getReader(Person.class, "name"), null), new Declaration("age", store.getReader(Person.class, "age"), null) };
final Declaration[] inner = new Declaration[] { new Declaration("cheese", new PatternExtractor(new ClassObjectType(Cheese.class)), null), new Declaration("price", store.getReader(Cheese.class, "price"), null) };
final String[] globals = new String[] { "aGlobal", "anotherGlobal" };
final List globalTypes = Arrays.asList(new String[] { "String", "String" });
final Map map = new HashMap();
map.put("className", StringUtils.ucFirst(className));
map.put("instanceName", className);
map.put("package", "org.drools");
map.put("ruleClassName", "Rule0");
map.put("invokerClassName", "Rule0" + StringUtils.ucFirst(className) + "Invoker");
map.put("declarations", declarations);
map.put("declarationTypes", declarationTypes);
map.put("globals", globals);
map.put("globalTypes", globalTypes);
map.put("innerDeclarations", inner);
map.put("attributes", new Attribute[] { new Attribute("int", "x") });
map.put("initCode", "x = 0;");
map.put("actionCode", "x += 1;");
map.put("reverseCode", "");
map.put("resultCode", "x + 10");
map.put("supportsReverse", "false");
map.put("resultType", Integer.class);
map.put("hashCode", new Integer(10));
map.put("isMultiPattern", Boolean.FALSE);
TemplateRegistry registry = getInvokerTemplateRegistry();
Object method = TemplateRuntime.execute(registry.getNamedTemplate("accumulateInvoker"), null, new MapVariableResolverFactory(map), registry);
// System.out.println( method );
}
use of org.mvel2.integration.impl.MapVariableResolverFactory in project drools by kiegroup.
the class JavaRuleBuilderHelper method generateInvokerTemplate.
private static void generateInvokerTemplate(final String invokerTemplate, final RuleBuildContext context, final String className, final Map vars, final Object invokerLookup, final BaseDescr descrLookup) {
TemplateRegistry registry = getInvokerTemplateRegistry(context.getKnowledgeBuilder().getRootClassLoader());
final String invokerClassName = context.getPkg().getName() + "." + context.getRuleDescr().getClassName() + StringUtils.ucFirst(className) + "Invoker";
context.addInvoker(invokerClassName, (String) TemplateRuntime.execute(registry.getNamedTemplate(invokerTemplate), null, new MapVariableResolverFactory(vars), registry));
context.addInvokerLookup(invokerClassName, invokerLookup);
context.addDescrLookups(invokerClassName, descrLookup);
}
use of org.mvel2.integration.impl.MapVariableResolverFactory in project drools by kiegroup.
the class AbstractModel method getMappedOutputPojo.
public Map.Entry<String, String> getMappedOutputPojo() {
Map<String, String> result = new HashMap<>();
if (!templateRegistry.contains(getOutputPojoTemplateName())) {
this.addOutputTemplateToRegistry(templateRegistry);
}
List<PMMLOutputField> dataFields = this.getOutputFields();
Map<String, Object> vars = new HashMap<>();
String className = this.getOutputPojoClassName();
vars.put("pmmlPackageName", PMML_JAVA_PACKAGE_NAME);
vars.put("className", className);
vars.put("imports", new ArrayList<>());
vars.put("dataFields", dataFields);
vars.put("modelName", this.getModelId());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
TemplateRuntime.execute(templateRegistry.getNamedTemplate(this.getOutputPojoTemplateName()), null, new MapVariableResolverFactory(vars), baos);
} catch (TemplateError te) {
return null;
} catch (TemplateRuntimeError tre) {
// need to figure out logging here
return null;
}
result.put(className, new String(baos.toByteArray()));
return result.entrySet().iterator().next();
}
use of org.mvel2.integration.impl.MapVariableResolverFactory in project drools by kiegroup.
the class SimpleSetSegmentPredicate method getPredicateRule.
@Override
public String getPredicateRule() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CompiledTemplate ct = getTemplate();
if (ct != null) {
Map<String, Object> vars = new HashMap<>();
vars.put("fieldName", this.getValueFieldName());
vars.put("operator", getOperatorText());
vars.put("setType", setType);
vars.put("values", getValueObjects());
TemplateRuntime.execute(ct, null, new MapVariableResolverFactory(vars), baos);
}
return new String(baos.toByteArray());
}
use of org.mvel2.integration.impl.MapVariableResolverFactory in project mvel by mvel.
the class ForNode method getReducedValueAccelerated.
public Object getReducedValueAccelerated(Object ctx, Object thisValue, VariableResolverFactory factory) {
VariableResolverFactory ctxFactory = indexAlloc ? factory : new MapVariableResolverFactory(new HashMap<String, Object>(1), factory);
Object v;
for (initializer.getValue(ctx, thisValue, ctxFactory); (Boolean) condition.getValue(ctx, thisValue, ctxFactory); after.getValue(ctx, thisValue, ctxFactory)) {
v = compiledBlock.getValue(ctx, thisValue, ctxFactory);
if (ctxFactory.tiltFlag())
return v;
}
return null;
}
Aggregations