List of usage examples for org.apache.commons.jexl3 JexlEngine createExpression
public final JexlExpression createExpression(String expression)
From source file:com.epam.dlab.core.parser.ConditionEvaluate.java
/** Instantiate the engine to evaluate condition. * @param columnNames the list of column names. * @param condition condition for filtering data. * @throws InitializationException/*from ww w. j a va 2s .c o m*/ */ public ConditionEvaluate(List<String> columnNames, String condition) throws InitializationException { //Replace : to . in column names List<String> colNames = new ArrayList<>(columnNames.size()); for (int i = 0; i < columnNames.size(); i++) { String name = columnNames.get(i); if (name.indexOf(':') > -1 && condition.indexOf(name) > -1) { String newName = StringUtils.replaceChars(name, ':', '.'); colNames.add(newName); condition = StringUtils.replace(condition, name, newName); } else { colNames.add(name); } } try { JexlEngine engine = new JexlBuilder().strict(true).silent(false).debug(true).create(); expression = (Script) engine.createExpression(condition); jexlContext = new MapContext(); } catch (Exception e) { throw new InitializationException( "Cannot initialize JEXL engine for condition: " + condition + ". " + e.getLocalizedMessage(), e); } // Create mapping of columns for evaluations. List<String> names = new ArrayList<>(); List<Integer> indexes = new ArrayList<>(); for (List<String> variableList : expression.getVariables()) { String columnName = StringUtils.join(variableList, '.'); int index = getColumnIndex(colNames, columnName); if (index == -1) { throw new InitializationException("Unknow source column name \"" + columnName + "\" in condition: " + expression.getSourceText() + ". Known column names: " + StringUtils.join(columnNames, ", ") + "."); } names.add(columnName); indexes.add(index); } this.columnNames = new String[names.size()]; this.columnIndexes = new int[indexes.size()]; for (int i = 0; i < indexes.size(); i++) { this.columnNames[i] = names.get(i); this.columnIndexes[i] = indexes.get(i); } }