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);
}
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);
}
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()");
}
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));
}
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")));
}
Aggregations