Search in sources :

Example 21 with CompiledTemplate

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));
}
Also used : TestPluginNode(org.mvel2.tests.templates.tests.res.TestPluginNode) TemplateCompiler(org.mvel2.templates.TemplateCompiler) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 22 with CompiledTemplate

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)));
}
Also used : MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ParserContext(org.mvel2.ParserContext) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 23 with CompiledTemplate

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())));
    }
}
Also used : StackDelimiterResolverFactory(org.mvel2.integration.impl.StackDelimiterResolverFactory) TemplateError(org.mvel2.templates.TemplateError) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 24 with CompiledTemplate

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();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 25 with CompiledTemplate

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);
}
Also used : HashMap(java.util.HashMap) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) PMML(org.kie.dmg.pmml.pmml_4_2.descr.PMML) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PMML4Compiler(org.kie.pmml.pmml_4_2.PMML4Compiler) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Aggregations

CompiledTemplate (org.mvel2.templates.CompiledTemplate)32 HashMap (java.util.HashMap)8 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)6 Node (org.mvel2.templates.res.Node)5 InputStream (java.io.InputStream)4 SimpleTemplateRegistry (org.mvel2.templates.SimpleTemplateRegistry)4 TemplateRegistry (org.mvel2.templates.TemplateRegistry)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ParserContext (org.mvel2.ParserContext)3 TemplateCompiler (org.mvel2.templates.TemplateCompiler)3 OutputStream (java.io.OutputStream)2 Date (java.util.Date)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 JavaRuleBuilderHelper (org.drools.mvel.java.JavaRuleBuilderHelper)2 ImmutableDefaultFactory (org.mvel2.integration.impl.ImmutableDefaultFactory)2 TemplateError (org.mvel2.templates.TemplateError)2 TemplateRuntime (org.mvel2.templates.TemplateRuntime)2 TestPluginNode (org.mvel2.tests.templates.tests.res.TestPluginNode)2 StringAppender (org.mvel2.util.StringAppender)2 VertxInternal (io.vertx.core.impl.VertxInternal)1