Search in sources :

Example 1 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project dbeaver by serge-rider.

the class DBVUtils method parseExpression.

public static JexlExpression parseExpression(String expression) {
    Map<String, Object> nsList = getExpressionNamespaces();
    JexlBuilder jexlBuilder = new JexlBuilder();
    jexlBuilder.cache(100);
    jexlBuilder.namespaces(nsList);
    JexlEngine jexlEngine = jexlBuilder.create();
    return jexlEngine.createExpression(expression);
}
Also used : JexlEngine(org.apache.commons.jexl3.JexlEngine) JexlBuilder(org.apache.commons.jexl3.JexlBuilder)

Example 2 with JexlExpression

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

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)

Example 3 with JexlExpression

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

the class SideEffectTest method canMakeAssessmentRefs.

@Test
public void canMakeAssessmentRefs() {
    JexlBuilder builder = new JexlBuilder();
    TestSideEffectNamespace testNamespace = new TestSideEffectNamespace();
    JexlEngine jexl = builder.namespaces(newHashMap("assessment", new AssessmentSideEffectNamespace(), "test", testNamespace)).create();
    MapContext ctx = new MapContext();
    String messageString = "I have updated something!";
    ctx.set("message", messageString);
    JexlExpression sideEffectExpression = jexl.createExpression("assessment:update('ARCH_REVIEW', 'YES')");
    JexlExpression messageExpression = jexl.createExpression("test:sendMessage(message)");
    messageExpression.evaluate(ctx);
    assertEquals(messageString, testNamespace.lastMessage, "Should be able to use context as part of expression");
    assertEquals(AssessmentSideEffectNamespace.update("ARCH_REVIEW", "YES"), sideEffectExpression.evaluate(ctx), "Should be able to parse survey templateExtId and ratingExtId from update()");
}
Also used : JexlEngine(org.apache.commons.jexl3.JexlEngine) JexlBuilder(org.apache.commons.jexl3.JexlBuilder) MapContext(org.apache.commons.jexl3.MapContext) JexlExpression(org.apache.commons.jexl3.JexlExpression) Test(org.junit.jupiter.api.Test)

Example 4 with JexlExpression

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

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)

Example 5 with JexlExpression

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

the class ReferenceBuilderNamespaceTest method declarationsFromFile.

@Test
public void declarationsFromFile() throws IOException {
    String declarationExpressions = readAsString(getFileResource("decl-test.cfg").getInputStream());
    ReferenceBuilderContext jexlCtx = new ReferenceBuilderContext();
    JexlExpression expr = jexl.createExpression(declarationExpressions);
    expr.evaluate(jexlCtx);
    Set<ContextVariableDeclaration> declarations = jexlCtx.declarations();
    assertEquals(3, declarations.size());
    Optional<ContextVariableDeclaration> x = find(declarations, t -> t.name().equals("x"));
    Optional<ContextVariableDeclaration> y = find(declarations, t -> t.name().equals("y"));
    Optional<ContextVariableDeclaration> longName = find(declarations, t -> t.name().equals("longName"));
    assertTrue(x.isPresent());
    assertTrue(y.isPresent());
    assertTrue(longName.isPresent());
    x.ifPresent(t -> assertEquals(t.ref(), ReferenceBuilderNamespace.assessment("wibble")));
    y.ifPresent(t -> assertEquals(t.ref(), ReferenceBuilderNamespace.surveyQuestionResponse("surv", "q1")));
    longName.ifPresent(t -> assertEquals(t.ref(), ReferenceBuilderNamespace.surveyQuestionResponse("surv", "q2")));
}
Also used : IOUtilities.readAsString(org.finos.waltz.common.IOUtilities.readAsString) JexlExpression(org.apache.commons.jexl3.JexlExpression) Test(org.junit.jupiter.api.Test)

Aggregations

JexlExpression (org.apache.commons.jexl3.JexlExpression)12 JexlBuilder (org.apache.commons.jexl3.JexlBuilder)5 JexlEngine (org.apache.commons.jexl3.JexlEngine)5 Test (org.junit.jupiter.api.Test)5 MapContext (org.apache.commons.jexl3.MapContext)4 DBException (org.jkiss.dbeaver.DBException)4 JexlContext (org.apache.commons.jexl3.JexlContext)3 DBCException (org.jkiss.dbeaver.model.exec.DBCException)2 ResultSetRow (org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow)2 IntrospectionException (java.beans.IntrospectionException)1 LinkedHashMap (java.util.LinkedHashMap)1 JexlException (org.apache.commons.jexl3.JexlException)1 StopWatch (org.apache.nifi.util.StopWatch)1 IOUtilities.readAsString (org.finos.waltz.common.IOUtilities.readAsString)1