Search in sources :

Example 1 with TemplateRuntimeError

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

Example 2 with TemplateRuntimeError

use of org.mvel2.templates.TemplateRuntimeError in project mvel by mikebrock.

the class ForEachNode method eval.

public Object eval(TemplateRuntime runtime, TemplateOutputStream appender, Object ctx, VariableResolverFactory factory) {
    Iterator[] iters = new Iterator[item.length];
    Object o;
    for (int i = 0; i < iters.length; i++) {
        if ((o = MVEL.eval(expression[i], ctx, factory)) instanceof Iterable) {
            iters[i] = ((Iterable) o).iterator();
        } else if (o instanceof Object[]) {
            iters[i] = new ArrayIterator((Object[]) o);
        } else {
            throw new TemplateRuntimeError("cannot iterate object type: " + o.getClass().getName());
        }
    }
    Map<String, Object> locals = new HashMap<String, Object>();
    MapVariableResolverFactory localFactory = new MapVariableResolverFactory(locals, factory);
    int iterate = iters.length;
    while (true) {
        for (int i = 0; i < iters.length; i++) {
            if (!iters[i].hasNext()) {
                iterate--;
                locals.put(item[i], "");
            } else {
                locals.put(item[i], iters[i].next());
            }
        }
        if (iterate != 0) {
            nestedNode.eval(runtime, appender, ctx, localFactory);
            if (sepExpr != null) {
                for (Iterator it : iters) {
                    if (it.hasNext()) {
                        appender.append(String.valueOf(MVEL.eval(sepExpr, ctx, factory)));
                        break;
                    }
                }
            }
        } else
            break;
    }
    return next != null ? next.eval(runtime, appender, ctx, factory) : null;
}
Also used : TemplateRuntimeError(org.mvel2.templates.TemplateRuntimeError) HashMap(java.util.HashMap) Iterator(java.util.Iterator) ArrayIterator(org.mvel2.templates.util.ArrayIterator) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ArrayIterator(org.mvel2.templates.util.ArrayIterator)

Example 3 with TemplateRuntimeError

use of org.mvel2.templates.TemplateRuntimeError in project mvel by mvel.

the class ForEachNode method eval.

public Object eval(TemplateRuntime runtime, TemplateOutputStream appender, Object ctx, VariableResolverFactory factory) {
    Iterator[] iters = new Iterator[item.length];
    Object o;
    for (int i = 0; i < iters.length; i++) {
        if ((o = MVEL.eval(expression[i], ctx, factory)) instanceof Iterable) {
            iters[i] = ((Iterable) o).iterator();
        } else if (o instanceof Object[]) {
            iters[i] = new ArrayIterator((Object[]) o);
        } else {
            throw new TemplateRuntimeError("cannot iterate object type: " + o.getClass().getName());
        }
    }
    Map<String, Object> locals = new HashMap<String, Object>();
    MapVariableResolverFactory localFactory = new MapVariableResolverFactory(locals, factory);
    int iterate = iters.length;
    while (true) {
        for (int i = 0; i < iters.length; i++) {
            if (!iters[i].hasNext()) {
                iterate--;
                locals.put(item[i], "");
            } else {
                locals.put(item[i], iters[i].next());
            }
        }
        if (iterate != 0) {
            nestedNode.eval(runtime, appender, ctx, localFactory);
            if (sepExpr != null) {
                for (Iterator it : iters) {
                    if (it.hasNext()) {
                        appender.append(String.valueOf(MVEL.eval(sepExpr, ctx, factory)));
                        break;
                    }
                }
            }
        } else
            break;
    }
    return next != null ? next.eval(runtime, appender, ctx, factory) : null;
}
Also used : TemplateRuntimeError(org.mvel2.templates.TemplateRuntimeError) HashMap(java.util.HashMap) Iterator(java.util.Iterator) ArrayIterator(org.mvel2.templates.util.ArrayIterator) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ArrayIterator(org.mvel2.templates.util.ArrayIterator)

Example 4 with TemplateRuntimeError

use of org.mvel2.templates.TemplateRuntimeError in project drools by kiegroup.

the class AbstractModel method getMappedMiningPojo.

public Map.Entry<String, String> getMappedMiningPojo() {
    Map<String, String> result = new HashMap<>();
    if (!templateRegistry.contains(getMiningPojoTemplateName())) {
        this.addMiningTemplateToRegistry(templateRegistry);
    }
    List<PMMLMiningField> dataFields = this.getMiningFields();
    Map<String, Object> vars = new HashMap<>();
    String className = this.getMiningPojoClassName();
    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.getMiningPojoTemplateName()), null, new MapVariableResolverFactory(vars), baos);
    } catch (TemplateRuntimeError tre) {
        // need to figure out logging here
        return null;
    }
    result.put(PMML_JAVA_PACKAGE_NAME + "." + className, new String(baos.toByteArray()));
    return result.entrySet().iterator().next();
}
Also used : TemplateRuntimeError(org.mvel2.templates.TemplateRuntimeError) HashMap(java.util.HashMap) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 5 with TemplateRuntimeError

use of org.mvel2.templates.TemplateRuntimeError in project drools by kiegroup.

the class AbstractModel method getMappedRuleUnit.

@Override
public Map.Entry<String, String> getMappedRuleUnit() {
    Map<String, String> result = new HashMap<>();
    if (!templateRegistry.contains(this.getRuleUnitTemplateName())) {
        this.addRuleUnitTemplateToRegistry(templateRegistry);
    }
    Map<String, Object> vars = new HashMap<>();
    String className = this.getRuleUnitClassName();
    vars.put("pmmlPackageName", this.getModelPackageName());
    vars.put("className", className);
    vars.put("pojoInputClassName", PMMLRequestData.class.getName());
    if (this instanceof Miningmodel) {
        vars.put("miningPojoClassName", this.getMiningPojoClassName());
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        TemplateRuntime.execute(templateRegistry.getNamedTemplate(this.getRuleUnitTemplateName()), null, new MapVariableResolverFactory(vars), baos);
    } catch (TemplateError te) {
        return null;
    } catch (TemplateRuntimeError tre) {
        // need to figure out logging here
        return null;
    }
    result.put(this.getModelPackageName() + "." + className, new String(baos.toByteArray()));
    return result.isEmpty() ? null : result.entrySet().iterator().next();
}
Also used : TemplateRuntimeError(org.mvel2.templates.TemplateRuntimeError) PMMLRequestData(org.kie.api.pmml.PMMLRequestData) HashMap(java.util.HashMap) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TemplateError(org.mvel2.templates.TemplateError)

Aggregations

HashMap (java.util.HashMap)7 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)7 TemplateRuntimeError (org.mvel2.templates.TemplateRuntimeError)7 Iterator (java.util.Iterator)4 ArrayIterator (org.mvel2.templates.util.ArrayIterator)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 TemplateError (org.mvel2.templates.TemplateError)2 CountIterator (org.mvel2.templates.util.CountIterator)2 PMMLRequestData (org.kie.api.pmml.PMMLRequestData)1