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