Search in sources :

Example 16 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project commons-jexl by apache.

the class SandboxTest method testSetNullKeyAllowed0.

@Test
public void testSetNullKeyAllowed0() throws Exception {
    Arithmetic350 a350 = new Arithmetic350(true);
    JexlEngine jexl = new JexlBuilder().arithmetic(a350).sandbox(new JexlSandbox(true)).create();
    JexlContext jc = new MapContext();
    JexlExpression expression = jexl.createExpression("{null : 'foo'}[null] = 'bar'");
    expression.evaluate(jc);
    Map<?, ?> map = a350.getLastMap();
    Assert.assertEquals("bar", map.get(null));
}
Also used : JexlEngine(org.apache.commons.jexl3.JexlEngine) JexlBuilder(org.apache.commons.jexl3.JexlBuilder) JexlContext(org.apache.commons.jexl3.JexlContext) MapContext(org.apache.commons.jexl3.MapContext) JexlExpression(org.apache.commons.jexl3.JexlExpression) Test(org.junit.Test)

Example 17 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project commons-jexl by apache.

the class SandboxTest method testGetNullKeyAllowed0.

@Test
public void testGetNullKeyAllowed0() throws Exception {
    JexlEngine jexl = new JexlBuilder().sandbox(new JexlSandbox(true)).create();
    JexlExpression expression = jexl.createExpression("{null : 'foo'}[null]");
    Object o = expression.evaluate(null);
    Assert.assertEquals("foo", o);
}
Also used : JexlEngine(org.apache.commons.jexl3.JexlEngine) JexlBuilder(org.apache.commons.jexl3.JexlBuilder) JexlExpression(org.apache.commons.jexl3.JexlExpression) Test(org.junit.Test)

Example 18 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project dotwebstack-framework by dotwebstack.

the class JexlHelper method evaluateExpression.

public <T> Optional<T> evaluateExpression(String expressionString, JexlContext context, Class<T> clazz) {
    JexlExpression expression = this.engine.createExpression(expressionString);
    Object evaluated = expression.evaluate(context);
    if (evaluated == null) {
        return Optional.empty();
    } else if (!clazz.isInstance(evaluated)) {
        throw illegalArgumentException("Jexl evaluateDirectiveArgument type mismatch: expected[{}], but was [{}].", clazz, evaluated.getClass());
    } else {
        return Optional.of(cast(clazz, evaluated));
    }
}
Also used : JexlExpression(org.apache.commons.jexl3.JexlExpression)

Example 19 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project Qualitis by WeBankFinTech.

the class JexlUtil method executeExpression.

public static Object executeExpression(String jexlExpression, Map<String, Object> map) {
    JexlExpression expression = jexlEngine.createExpression(jexlExpression);
    JexlContext context = new MapContext();
    if (map != null && !map.isEmpty()) {
        map.forEach(context::set);
    }
    return expression.evaluate(context);
}
Also used : JexlContext(org.apache.commons.jexl3.JexlContext) JexlExpression(org.apache.commons.jexl3.JexlExpression) MapContext(org.apache.commons.jexl3.MapContext)

Example 20 with JexlExpression

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

the class PredicateEvaluatorTest method evaluate.

@Test
public void evaluate() {
    ImmutableAssessmentContextValue assessmentValue = ImmutableAssessmentContextValue.builder().ratingCode("A").ratingName("aRating").ratingExternalId("A_RATING").ratingComment("a rating description").build();
    ImmutableSurveyQuestionResponseContextValue surveyValue = ImmutableSurveyQuestionResponseContextValue.builder().value("yes").comment("a survey comment").build();
    ImmutableContextVariable<Object> assessmentVar = ImmutableContextVariable.builder().name("assessmentVar").value(assessmentValue).entityRef(EntityReference.mkRef(EntityKind.APPLICATION, 1L)).build();
    ImmutableContextVariable<Object> surveyVar = ImmutableContextVariable.builder().name("surveyVar").value(surveyValue).entityRef(EntityReference.mkRef(EntityKind.APPLICATION, 1L)).build();
    Set<ContextVariable<?>> contextVariables = asSet(assessmentVar, surveyVar);
    JexlBuilder builder = new JexlBuilder();
    JexlEngine jexl = builder.create();
    JexlExpression pred1Expr = jexl.createExpression("assessmentVar.ratingCode == 'A'");
    JexlExpression pred2Expr = jexl.createExpression("assessmentVar.ratingCode == 'C' || surveyVar.value == 'yes'");
    JexlExpression pred3Expr = jexl.createExpression("assessmentVar.ratingCode == 'C'");
    JexlExpression invalidExpr = jexl.createExpression("foo?.bar");
    MapContext ctx = createContext(contextVariables);
    assertTrue(evaluatePredicate(pred1Expr, ctx), "pred1Expr should evaluate true, rating equals A");
    assertTrue(evaluatePredicate(pred2Expr, ctx), "pred2Expr should evaluate true, survey value equals 'yes");
    assertFalse(evaluatePredicate(pred3Expr, ctx), "pred3Expr should evaluate false, rating equals A");
    assertFalse(evaluatePredicate(invalidExpr, ctx), "Invalid expressions should return false");
    Object evaluate = invalidExpr.evaluate(ctx);
    System.out.println(evaluate);
}
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)42 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