Search in sources :

Example 46 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project hl7v2-fhir-converter by LinuxForHealth.

the class JexlEngineUtil method evaluate.

public Object evaluate(String jexlExp, Map<String, Object> context) {
    Preconditions.checkArgument(StringUtils.isNotBlank(jexlExp), "jexlExp cannot be blank");
    Preconditions.checkArgument(context != null, "context cannot be null");
    String trimedJexlExp = StringUtils.trim(jexlExp);
    // ensure that expression
    validateExpression(trimedJexlExp);
    LOGGER.debug("Evaluating expression : {}", trimedJexlExp);
    Map<String, Object> localContext = new HashMap<>(functions);
    localContext.putAll(context);
    JexlExpression exp = exprCache.get(trimedJexlExp);
    if (exp == null) {
        exp = jexl.createExpression(trimedJexlExp);
        exprCache.put(trimedJexlExp, exp);
    }
    JexlContext jc = new MapContext();
    localContext.entrySet().forEach(e -> jc.set(e.getKey(), e.getValue()));
    // Now evaluate the expression, getting the result
    try {
        Object obj = exp.evaluate(jc);
        LOGGER.debug("Evaluated expression : {}, returning object {}", trimedJexlExp, obj);
        return obj;
    } catch (JexlException e) {
        throw new DataExtractionException("Exception encountered during JEXL expression evaluation", e);
    }
}
Also used : HashMap(java.util.HashMap) JexlException(org.apache.commons.jexl3.JexlException) JexlContext(org.apache.commons.jexl3.JexlContext) JexlExpression(org.apache.commons.jexl3.JexlExpression) MapContext(org.apache.commons.jexl3.MapContext) DataExtractionException(io.github.linuxforhealth.core.exception.DataExtractionException)

Example 47 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project jxls by jxlsteam.

the class JexlExpressionEvaluator method evaluate.

@Override
public Object evaluate(String expression, Map<String, Object> context) {
    JexlContext jexlContext = new MapContext(context);
    try {
        Map<String, JexlExpression> expressionMap = expressionMapThreadLocal.get();
        JexlExpression jexlExpression = expressionMap.get(expression);
        if (jexlExpression == null) {
            jexlExpression = getJexlEngine().createExpression(expression);
            expressionMap.put(expression, jexlExpression);
        }
        return jexlExpression.evaluate(jexlContext);
    } catch (Exception e) {
        throw new EvaluationException("An error occurred when evaluating expression " + expression, e);
    }
}
Also used : JexlContext(org.apache.commons.jexl3.JexlContext) MapContext(org.apache.commons.jexl3.MapContext) JexlExpression(org.apache.commons.jexl3.JexlExpression)

Example 48 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project jxls by jxlsteam.

the class JexlExpressionEvaluatorNoThreadLocal method evaluate.

@Override
public Object evaluate(String expression, Map<String, Object> context) {
    JexlContext jexlContext = new MapContext(context);
    try {
        JexlExpression jexlExpression = expressionMap.get(expression);
        if (jexlExpression == null) {
            jexlExpression = jexl.createExpression(expression);
            expressionMap.put(expression, jexlExpression);
        }
        return jexlExpression.evaluate(jexlContext);
    } catch (Exception e) {
        throw new EvaluationException("An error occurred when evaluating expression " + expression, e);
    }
}
Also used : JexlContext(org.apache.commons.jexl3.JexlContext) MapContext(org.apache.commons.jexl3.MapContext) JexlExpression(org.apache.commons.jexl3.JexlExpression)

Example 49 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project conga by wcm-io-devops.

the class JexlResolver method resolve.

public Object resolve(String expressionString, Map<String, Object> variables) {
    Map<String, Object> resolvedVariables = resolveMapWithoutCycles(variables);
    try {
        JexlExpression expression = jexl.createExpression(expressionString);
        JexlContext context = new MapContext(resolvedVariables);
        return expression.evaluate(context);
    } catch (JexlException ex) {
        throw new GeneratorException("Unable to parse expression: ${" + expressionString + "}: " + ex.getMessage(), ex);
    }
}
Also used : JexlException(org.apache.commons.jexl3.JexlException) JexlContext(org.apache.commons.jexl3.JexlContext) JexlExpression(org.apache.commons.jexl3.JexlExpression) MapContext(org.apache.commons.jexl3.MapContext) GeneratorException(io.wcm.devops.conga.generator.GeneratorException)

Example 50 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project waltz by finos.

the class ReferenceBuilderNamespaceTest method evalTest.

@Test
public void evalTest() {
    JexlBuilder builder = new JexlBuilder();
    JexlEngine jexl = builder.create();
    JexlExpression expr = jexl.createExpression("x == 'IN_SCOPE' && y == 10");
    MapContext ctx1 = new MapContext();
    ctx1.set("x", "IN_SCOPE");
    ctx1.set("y", 10);
    MapContext ctx2 = new MapContext();
    ctx2.set("x", "IN_SCOPE");
    ctx2.set("y", 12);
    System.out.printf("Result1: [%s]\n", expr.evaluate(ctx1));
    System.out.printf("Result2: [%s]\n", expr.evaluate(ctx2));
}
Also used : JexlEngine(org.apache.commons.jexl3.JexlEngine) JexlBuilder(org.apache.commons.jexl3.JexlBuilder) JexlExpression(org.apache.commons.jexl3.JexlExpression) MapContext(org.apache.commons.jexl3.MapContext) Test(org.junit.jupiter.api.Test)

Aggregations

JexlExpression (org.apache.commons.jexl3.JexlExpression)43 JexlBuilder (org.apache.commons.jexl3.JexlBuilder)18 MapContext (org.apache.commons.jexl3.MapContext)18 JexlEngine (org.apache.commons.jexl3.JexlEngine)17 JexlContext (org.apache.commons.jexl3.JexlContext)13 Test (org.junit.jupiter.api.Test)10 Test (org.junit.Test)9 JexlException (org.apache.commons.jexl3.JexlException)8 HashMap (java.util.HashMap)4 DBException (org.jkiss.dbeaver.DBException)4 IntrospectionException (java.beans.IntrospectionException)2 IOException (java.io.IOException)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 Engine (org.apache.commons.jexl3.internal.Engine)2 IOUtilities.readAsString (org.finos.waltz.common.IOUtilities.readAsString)2 DBCException (org.jkiss.dbeaver.model.exec.DBCException)2 ResultSetRow (org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow)2 ThreadSharedProperties (com.revolsys.collection.map.ThreadSharedProperties)1 DataExtractionException (io.github.linuxforhealth.core.exception.DataExtractionException)1