use of org.mvel2.templates.CompiledTemplate in project mvel by mvel.
the class TemplateTests method testPluginNode.
public void testPluginNode() {
Map<String, Class<? extends org.mvel2.templates.res.Node>> plugins = new HashMap<String, Class<? extends org.mvel2.templates.res.Node>>();
plugins.put("testNode", TestPluginNode.class);
TemplateCompiler compiler = new TemplateCompiler("Foo:@testNode{}!!", plugins);
CompiledTemplate compiled = compiler.compile();
assertEquals("Foo:THIS_IS_A_TEST!!", TemplateRuntime.execute(compiled));
}
use of org.mvel2.templates.CompiledTemplate in project mvel by mvel.
the class TemplateTests method testMVEL244.
public void testMVEL244() {
Foo244 foo = new Foo244("plop");
String template = "@foreach{val : foo.liste[0].liste} plop @end{}";
CompiledTemplate compiledTemplate = TemplateCompiler.compileTemplate(template);
Map<String, Object> model = new HashMap<String, Object>();
model.put("foo", foo);
System.out.println(TemplateRuntime.execute(compiledTemplate, new ParserContext(), new MapVariableResolverFactory(model)));
}
use of org.mvel2.templates.CompiledTemplate in project mvel by mvel.
the class CompiledNamedIncludeNode method eval.
public Object eval(TemplateRuntime runtime, TemplateOutputStream appender, Object ctx, VariableResolverFactory factory) {
factory = new StackDelimiterResolverFactory(factory);
if (cPreExpression != null) {
MVEL.executeExpression(cPreExpression, ctx, factory);
}
if (next != null) {
String namedTemplate = MVEL.executeExpression(cIncludeExpression, ctx, factory, String.class);
CompiledTemplate ct = runtime.getNamedTemplateRegistry().getNamedTemplate(namedTemplate);
if (ct == null)
throw new TemplateError("named template does not exist: " + namedTemplate);
return next.eval(runtime, appender.append(String.valueOf(TemplateRuntime.execute(ct, ctx, factory, runtime.getNamedTemplateRegistry()))), ctx, factory);
// return next.eval(runtime,
// appender.append(String.valueOf(TemplateRuntime.execute(runtime.getNamedTemplateRegistry().getNamedTemplate(MVEL.executeExpression(cIncludeExpression, ctx, factory, String.class)), ctx, factory))), ctx, factory);
} else {
return appender.append(String.valueOf(TemplateRuntime.execute(runtime.getNamedTemplateRegistry().getNamedTemplate(MVEL.executeExpression(cIncludeExpression, ctx, factory, String.class)), ctx, factory, runtime.getNamedTemplateRegistry())));
}
}
use of org.mvel2.templates.CompiledTemplate in project drools by kiegroup.
the class MiningSegmentation method generateSegmentationRules.
public String generateSegmentationRules() {
StringBuilder builder = new StringBuilder();
loadTemplates(this.multipleModelMethod);
Map<String, Object> templateVars = new HashMap<>();
// "org.kie.pmml.pmml_4_2."+this.getSegmentationId();
String pkgName = this.getOwner().getModelPackageName();
CompiledTemplate ct = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
switch(this.multipleModelMethod) {
case AVERAGE:
break;
case MAJORITY_VOTE:
break;
case MAX:
break;
case MEDIAN:
break;
case MODEL_CHAIN:
List<MiningSegmentTransfer> segmentTransfers = new ArrayList<>();
MiningSegmentTransfer mst = new MiningSegmentTransfer(this.getSegmentationId(), "1", "2");
mst.addResultToRequestMapping("calculatedScore", "calculatedScore");
segmentTransfers.add(mst);
templateVars.put("segmentTransfers", segmentTransfers);
templateVars.put("miningModel", this.getOwner());
templateVars.put("childSegments", this.getMiningSegments());
templateVars.put("packageName", pkgName);
templateVars.put("resultMappings", segmentTransfers);
templateVars.put("ruleUnitClassName", this.getOwner().getRuleUnitClassName());
ct = templates.getNamedTemplate(this.multipleModelMethod.name());
TemplateRuntime.execute(ct, null, new MapVariableResolverFactory(templateVars), baos);
builder.append(new String(baos.toByteArray()));
break;
case SELECT_ALL:
templateVars.put("ruleUnitClassName", this.getOwner().getRuleUnitClassName());
templateVars.put("miningModel", this.getOwner());
templateVars.put("childSegments", this.getMiningSegments());
templateVars.put("packageName", pkgName);
ct = templates.getNamedTemplate(this.multipleModelMethod.name());
TemplateRuntime.execute(ct, null, new MapVariableResolverFactory(templateVars), baos);
builder.append(new String(baos.toByteArray()));
break;
case SELECT_FIRST:
templateVars.put("ruleUnitClassName", this.getOwner().getRuleUnitClassName());
templateVars.put("miningModel", this.getOwner());
templateVars.put("childSegments", this.getMiningSegments());
templateVars.put("packageName", pkgName);
ct = templates.getNamedTemplate(this.multipleModelMethod.name());
TemplateRuntime.execute(ct, null, new MapVariableResolverFactory(templateVars), baos);
builder.append(new String(baos.toByteArray()));
break;
case SUM:
break;
case WEIGHTED_AVERAGE:
break;
case WEIGHTED_MAJORITY_VOTE:
break;
}
return builder.toString();
}
use of org.mvel2.templates.CompiledTemplate in project drools by kiegroup.
the class MiningSegment method generateSegmentRules.
public String generateSegmentRules(String segmentationAgenda, int segmentIndex) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CompiledTemplate template = getLaunchTemplate();
if (template != null) {
Map<String, Object> vars = new HashMap<>();
vars.put("segmentId", getSegmentId());
vars.put("segmentationAgendaId", segmentationAgenda);
vars.put("segmentSalience", new Integer(1000 - segmentIndex));
if (predicateRuleProducer instanceof CompoundSegmentPredicate) {
CompoundSegmentPredicate predProd = (CompoundSegmentPredicate) predicateRuleProducer;
if (predProd.hasSurrogation()) {
vars.put("segmentPredicate", getSurrogationPredicateText(predProd, -1));
} else {
vars.put("segmentPredicate", predProd.getPredicateRule());
}
} else {
vars.put("segmentPredicate", predicateRuleProducer.getPredicateRule());
}
vars.put("miningPojoClass", getOwner().getOwner().getMiningPojoClassName());
TemplateRuntime.execute(template, null, new MapVariableResolverFactory(vars), baos);
}
PMML pmml = new PMML();
pmml.setDataDictionary(this.internalModel.getDataDictionary());
pmml.getAssociationModelsAndBaselineModelsAndClusteringModels().add(this.internalModel.getRawModel());
PMML4Compiler compiler = new PMML4Compiler();
String innerRules = compiler.generateTheory(pmml);
return (new String(baos.toByteArray())).concat(innerRules);
}
Aggregations