Search in sources :

Example 11 with Expression

use of org.kie.dmn.model.v1_1.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 12 with Expression

use of org.kie.dmn.model.v1_1.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)

Example 13 with Expression

use of org.kie.dmn.model.v1_1.Expression in project dhis2-core by dhis2.

the class ExpressionUtils method evaluate.

/**
     * @param expression the expression.
     * @param vars the variables, can be null.
     * @param strict indicates whether to use strict or lenient engine mode.
     * @return the result of the evaluation.
     */
private static Object evaluate(String expression, Map<String, Object> vars, boolean strict) {
    expression = expression.replaceAll(IGNORED_KEYWORDS_REGEX, StringUtils.EMPTY);
    JexlEngine engine = strict ? JEXL_STRICT : JEXL;
    Expression exp = engine.createExpression(expression);
    JexlContext context = vars != null ? new MapContext(vars) : new MapContext();
    return exp.evaluate(context);
}
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 14 with Expression

use of org.kie.dmn.model.v1_1.Expression in project xtext-core by eclipse.

the class Expression_EqualImpl method basicSetLeft.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public NotificationChain basicSetLeft(Expression newLeft, NotificationChain msgs) {
    Expression oldLeft = left;
    left = newLeft;
    if (eNotificationRequired()) {
        ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, Bug385636Package.EXPRESSION_EQUAL__LEFT, oldLeft, newLeft);
        if (msgs == null)
            msgs = notification;
        else
            msgs.add(notification);
    }
    return msgs;
}
Also used : Expression(org.eclipse.xtext.resource.bug385636.Expression) ENotificationImpl(org.eclipse.emf.ecore.impl.ENotificationImpl)

Example 15 with Expression

use of org.kie.dmn.model.v1_1.Expression in project xtext-core by eclipse.

the class Expression_Not_GreaterImpl method basicSetLeft.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public NotificationChain basicSetLeft(Expression newLeft, NotificationChain msgs) {
    Expression oldLeft = left;
    left = newLeft;
    if (eNotificationRequired()) {
        ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, Bug385636Package.EXPRESSION_NOT_GREATER__LEFT, oldLeft, newLeft);
        if (msgs == null)
            msgs = notification;
        else
            msgs.add(notification);
    }
    return msgs;
}
Also used : Expression(org.eclipse.xtext.resource.bug385636.Expression) ENotificationImpl(org.eclipse.emf.ecore.impl.ENotificationImpl)

Aggregations

ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)7 Expression (org.eclipse.xtext.resource.bug385636.Expression)7 Expression (org.apache.commons.jexl2.Expression)6 MapContext (org.apache.commons.jexl2.MapContext)6 JexlContext (org.apache.commons.jexl2.JexlContext)5 Expression (org.kie.dmn.model.v1_1.Expression)5 LiteralExpression (org.kie.dmn.model.v1_1.LiteralExpression)4 HashMap (java.util.HashMap)3 JexlEngine (org.apache.commons.jexl2.JexlEngine)3 DMNExpressionEvaluator (org.kie.dmn.core.api.DMNExpressionEvaluator)3 CompiledExpression (org.kie.dmn.feel.lang.CompiledExpression)3 ArrayList (java.util.ArrayList)2 DMNType (org.kie.dmn.api.core.DMNType)2 BusinessKnowledgeModelNode (org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode)2 DecisionNode (org.kie.dmn.api.core.ast.DecisionNode)2 EvaluatorResult (org.kie.dmn.core.api.EvaluatorResult)2 DMNContextEvaluator (org.kie.dmn.core.ast.DMNContextEvaluator)2 DMNInvocationEvaluator (org.kie.dmn.core.ast.DMNInvocationEvaluator)2 DMNRelationEvaluator (org.kie.dmn.core.ast.DMNRelationEvaluator)2 Binding (org.kie.dmn.model.v1_1.Binding)2