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();
}
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;
}
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;
}
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();
}
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();
}
Aggregations