Search in sources :

Example 6 with Expression

use of org.apache.commons.jexl2.Expression 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);
}
Also used : JexlEngine(org.apache.commons.jexl2.JexlEngine) Expression(org.apache.commons.jexl2.Expression) JexlContext(org.apache.commons.jexl2.JexlContext) MapContext(org.apache.commons.jexl2.MapContext)

Example 7 with Expression

use of org.apache.commons.jexl2.Expression in project dbeaver by serge-rider.

the class AbstractDescriptor method parseExpression.

public static Expression parseExpression(String exprString) throws DBException {
    synchronized (AbstractDescriptor.class) {
        if (jexlEngine == null) {
            jexlEngine = new JexlEngine(null, null, null, null);
            jexlEngine.setCache(100);
        }
    }
    try {
        return jexlEngine.createExpression(exprString);
    } catch (JexlException e) {
        throw new DBException("Bad expression", e);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) JexlEngine(org.apache.commons.jexl2.JexlEngine) JexlException(org.apache.commons.jexl2.JexlException)

Example 8 with Expression

use of org.apache.commons.jexl2.Expression 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;
}
Also used : JexlEngine(org.apache.commons.jexl2.JexlEngine) MapContext(org.apache.commons.jexl2.MapContext)

Example 9 with Expression

use of org.apache.commons.jexl2.Expression 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;
}
Also used : JexlEngine(org.apache.commons.jexl2.JexlEngine) HashMap(java.util.HashMap) MapContext(org.apache.commons.jexl2.MapContext)

Example 10 with Expression

use of org.apache.commons.jexl2.Expression 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;
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) Expression(org.apache.commons.jexl2.Expression) JexlContext(org.apache.commons.jexl2.JexlContext) MapContext(org.apache.commons.jexl2.MapContext)

Aggregations

MapContext (org.apache.commons.jexl2.MapContext)10 JexlContext (org.apache.commons.jexl2.JexlContext)7 Expression (org.apache.commons.jexl2.Expression)6 JexlEngine (org.apache.commons.jexl2.JexlEngine)6 HashMap (java.util.HashMap)3 JexlException (org.apache.commons.jexl2.JexlException)3 Map (java.util.Map)2 InetAddress (java.net.InetAddress)1 LinkedHashMap (java.util.LinkedHashMap)1 ReadonlyContext (org.apache.commons.jexl2.ReadonlyContext)1 CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)1 JMeterContext (org.apache.jmeter.threads.JMeterContext)1 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)1 DBException (org.jkiss.dbeaver.DBException)1 ParameterMap (org.opennms.core.utils.ParameterMap)1 JmxConnectionManager (org.opennms.netmgt.jmx.connection.JmxConnectionManager)1 JmxServerConnectionException (org.opennms.netmgt.jmx.connection.JmxServerConnectionException)1 JmxServerConnectionWrapper (org.opennms.netmgt.jmx.connection.JmxServerConnectionWrapper)1 DefaultConnectionManager (org.opennms.netmgt.jmx.impl.connection.connectors.DefaultConnectionManager)1 ExpressionException (org.opennms.netmgt.measurements.api.exceptions.ExpressionException)1