use of org.apache.commons.jexl2.MapContext in project shifu by ShifuML.
the class DataNormalizeWorker method normalizeRecord.
/**
* Normalize the training data record
*
* @param rfs
* - record fields
* @return the data after normalization
*/
private List<Double> normalizeRecord(String[] rfs) {
List<Double> retDouList = new ArrayList<Double>();
if (rfs == null || rfs.length == 0) {
return null;
}
String tag = CommonUtils.trimTag(rfs[this.targetColumnNum]);
boolean isNotSampled = DataSampler.isNotSampled(modelConfig.getPosTags(), modelConfig.getNegTags(), modelConfig.getNormalizeSampleRate(), modelConfig.isNormalizeSampleNegOnly(), tag);
if (isNotSampled) {
return null;
}
JexlContext jc = new MapContext();
Double cutoff = modelConfig.getNormalizeStdDevCutOff();
for (int i = 0; i < rfs.length; i++) {
ColumnConfig config = columnConfigList.get(i);
if (weightExpr != null) {
jc.set(config.getColumnName(), rfs[i]);
}
if (this.targetColumnNum == i) {
if (modelConfig.getPosTags().contains(tag)) {
retDouList.add(Double.valueOf(1));
} else if (modelConfig.getNegTags().contains(tag)) {
retDouList.add(Double.valueOf(0));
} else {
log.error("Invalid data! The target value is not listed - " + tag);
// Return null to skip such record.
return null;
}
} else if (!CommonUtils.isGoodCandidate(config)) {
retDouList.add(null);
} else {
String val = (rfs[i] == null) ? "" : rfs[i];
retDouList.add(Normalizer.normalize(config, val, cutoff, modelConfig.getNormalizeType()));
}
}
double weight = 1.0d;
if (weightExpr != null) {
Object result = weightExpr.evaluate(jc);
if (result instanceof Integer) {
weight = ((Integer) result).doubleValue();
} else if (result instanceof Double) {
weight = ((Double) result).doubleValue();
} else if (result instanceof String) {
// add to parse String data
try {
weight = Double.parseDouble((String) result);
} catch (NumberFormatException e) {
// Not a number, use default
if (System.currentTimeMillis() % 100 == 0) {
log.warn("Weight column type is String and value cannot be parsed with {}, use default 1.0d.", result);
}
weight = 1.0d;
}
}
}
retDouList.add(weight);
return retDouList;
}
use of org.apache.commons.jexl2.MapContext in project newts by OpenNMS.
the class ResultDescriptor method expression.
public ResultDescriptor expression(String label, String expression) {
final JexlEngine je = new JexlEngine();
final Expression expr = je.createExpression(expression);
final String[] labels = getLabels().toArray(new String[0]);
CalculationFunction evaluate = new CalculationFunction() {
private static final long serialVersionUID = -3328049421398096252L;
@Override
public double apply(double... ds) {
JexlContext jc = new MapContext();
for (int i = 0; i < labels.length; i++) {
jc.set(labels[i], ds[i]);
}
return ((Number) expr.evaluate(jc)).doubleValue();
}
};
return calculate(label, evaluate, labels);
}
use of org.apache.commons.jexl2.MapContext in project opennms by OpenNMS.
the class JasperReportService method evaluateToString.
public static String evaluateToString(JasperReport report, JRExpression expression) {
Objects.requireNonNull(report);
Objects.requireNonNull(expression);
SubreportExpressionVisitor visitor = new SubreportExpressionVisitor(report);
String string = visitor.visit(expression);
if (string != null) {
JexlEngine engine = new JexlEngine();
return (String) engine.createExpression(string).evaluate(new MapContext());
}
return null;
}
use of org.apache.commons.jexl2.MapContext in project opennms by OpenNMS.
the class ExpressionConfigWrapper method evaluate.
@Override
public double evaluate(Map<String, Double> values) throws ThresholdExpressionException {
// Add all of the variable values to the script context
Map<String, Object> context = new HashMap<String, Object>();
context.putAll(values);
// To workaround NMS-5019
context.put("datasources", new HashMap<String, Double>(values));
context.put("math", new MathBinding());
double result = Double.NaN;
try {
// Fetch an instance of the JEXL script engine to evaluate the script expression
Object resultObject = new JexlEngine().createExpression(m_expression.getExpression()).evaluate(new MapContext(context));
result = Double.parseDouble(resultObject.toString());
} catch (Throwable e) {
throw new ThresholdExpressionException("Error while evaluating expression " + m_expression.getExpression() + ": " + e.getMessage(), e);
}
return result;
}
use of org.apache.commons.jexl2.MapContext in project jmeter by apache.
the class Jexl2Function method execute.
/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
//$NON-NLS-1$
String str = "";
CompoundVariable var = (CompoundVariable) values[0];
String exp = var.execute();
//$NON-NLS-1$
String varName = "";
if (values.length > 1) {
varName = ((CompoundVariable) values[1]).execute().trim();
}
JMeterContext jmctx = JMeterContextService.getContext();
JMeterVariables vars = jmctx.getVariables();
try {
JexlContext jc = new MapContext();
//$NON-NLS-1$
jc.set("log", log);
//$NON-NLS-1$
jc.set("ctx", jmctx);
//$NON-NLS-1$
jc.set("vars", vars);
//$NON-NLS-1$
jc.set("props", JMeterUtils.getJMeterProperties());
// Previously mis-spelt as theadName
//$NON-NLS-1$
jc.set("threadName", Thread.currentThread().getName());
//$NON-NLS-1$ (may be null)
jc.set("sampler", currentSampler);
//$NON-NLS-1$ (may be null)
jc.set("sampleResult", previousResult);
//$NON-NLS-1$
jc.set("OUT", System.out);
// Now evaluate the script, getting the result
Expression e = getJexlEngine().createExpression(exp);
Object o = e.evaluate(jc);
if (o != null) {
str = o.toString();
}
if (vars != null && varName.length() > 0) {
// vars will be null on TestPlan
vars.put(varName, str);
}
} catch (Exception e) {
log.error("An error occurred while evaluating the expression \"" + exp + "\"\n", e);
}
return str;
}
Aggregations