Search in sources :

Example 6 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 7 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)

Example 8 with JexlExpression

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

the class JexlEngineUtil method evaluateCondition.

public boolean evaluateCondition(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
    validateCondition(trimedJexlExp);
    LOGGER.debug("Evaluating condiitional expression : {}", trimedJexlExp);
    Map<String, Object> localContext = new HashMap<>(functions);
    localContext.putAll(context);
    JexlExpression exp = jexl.createExpression(trimedJexlExp);
    JexlContext jc = new MapContext();
    localContext.entrySet().forEach(e -> jc.set(e.getKey(), e.getValue()));
    // Now evaluate the expression, getting the result
    boolean obj = (boolean) exp.evaluate(jc);
    LOGGER.debug("Evaluated expression : {}, returning object {}", trimedJexlExp, obj);
    return obj;
}
Also used : HashMap(java.util.HashMap) JexlContext(org.apache.commons.jexl3.JexlContext) JexlExpression(org.apache.commons.jexl3.JexlExpression) MapContext(org.apache.commons.jexl3.MapContext)

Example 9 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project com.revolsys.open by revolsys.

the class PhoneNumber method format.

/**
 * Parse a phone number using the regular expression and if it matches the
 * phone number, format it using the specified format otherwise return null.
 *
 * @param phoneNumber The normalized phone number to format.
 * @param regex The regular expression to match phone numbers.
 * @param format The format specification.
 * @return The formatted phone number.
 */
public static String format(final String phoneNumber, final String regex, final String format) {
    if (phoneNumber != null && regex != null && format != null) {
        final Pattern pattern = Pattern.compile(regex);
        final Matcher matcher = pattern.matcher(phoneNumber);
        if (matcher.matches()) {
            final Map values = new HashMap();
            for (int i = 1; i <= matcher.groupCount(); i++) {
                values.put("n" + i, matcher.group(i));
            }
            JexlExpression expression;
            try {
                expression = JexlUtil.newExpression(format);
            } catch (final Exception e) {
                throw new IllegalArgumentException(regex + " is not a valid regular expression: " + e.getMessage());
            }
            final MapContext context = new MapContext(values);
            try {
                return (String) expression.evaluate(context);
            } catch (final Exception e) {
                throw new IllegalArgumentException(format + " is not a valid format: " + e.getMessage());
            }
        }
    }
    return null;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) JexlExpression(org.apache.commons.jexl3.JexlExpression) MapContext(org.apache.commons.jexl3.MapContext) Map(java.util.Map) HashMap(java.util.HashMap)

Example 10 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project com.revolsys.open by revolsys.

the class MenuItem method setUri.

public void setUri(final String uri) {
    if (uri != null) {
        JexlExpression uriExpression = null;
        try {
            uriExpression = JexlUtil.newExpression(uri.replaceAll(" ", "%20"));
        } catch (final Exception e) {
            log.error(e.getMessage(), e);
        }
        if (uriExpression == null) {
            this.uri = uri.replaceAll(" ", "%20");
            this.uriExpression = null;
        } else {
            this.uri = null;
            this.uriExpression = uriExpression;
        }
    } else {
        this.uri = null;
        this.uriExpression = null;
    }
}
Also used : JexlExpression(org.apache.commons.jexl3.JexlExpression)

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