本文整理汇总了Java中org.apache.commons.jexl2.Expression.evaluate方法的典型用法代码示例。如果您正苦于以下问题:Java Expression.evaluate方法的具体用法?Java Expression.evaluate怎么用?Java Expression.evaluate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.jexl2.Expression
的用法示例。
在下文中一共展示了Expression.evaluate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: evaluate
import org.apache.commons.jexl2.Expression; //导入方法依赖的package包/类
public String evaluate(final String expression,
final JexlContext jexlContext) {
String result = "";
if (expression != null
&& !expression.isEmpty() && jexlContext != null) {
try {
Expression jexlExpression =
jexlEngine.createExpression(expression);
Object evaluated = jexlExpression.evaluate(jexlContext);
if (evaluated != null) {
result = evaluated.toString();
}
} catch (JexlException e) {
LOG.error("Invalid jexl expression: " + expression, e);
result = "";
}
} else {
LOG.debug("Expression not provided or invalid context");
}
return result;
}
示例2: testPower
import org.apache.commons.jexl2.Expression; //导入方法依赖的package包/类
@Test
public void testPower() {
JexlEngine engine = ExpressionEvalFactory.getInstance();
Expression expr = engine.createExpression("X^2");
JexlContext context = new MapContext();
// Don't need this really
context.set("A", 20);
context.set("B", 0);
ArrayList<Double> actualList = new ArrayList<>();
for (int i = -20; i < 20; i++) {
context.set("X", i);
Double result = (Double) expr.evaluate(context);
actualList.add(result);
System.out.println(result + ",");
}
Double[] actual = actualList.toArray(new Double[0]);
Double[] expected = {400.0, 361.0, 324.0, 289.0, 256.0, 225.0, 196.0,
169.0, 144.0, 121.0, 100.0, 81.0, 64.0, 49.0, 36.0, 25.0, 16.0,
9.0, 4.0, 1.0, 0.0, 1.0, 4.0, 9.0, 16.0, 25.0, 36.0, 49.0, 64.0,
81.0, 100.0, 121.0, 144.0, 169.0, 196.0, 225.0, 256.0, 289.0,
324.0, 361.0};
Assert.assertArrayEquals("results wrong", expected, actual);
}
示例3: evaluate
import org.apache.commons.jexl2.Expression; //导入方法依赖的package包/类
/**
* <p>
* The condition can be any JEXL boolean expression:
* </p>
* <ul>
* <li><code>obj.field == 'value'</code></li>
* <li><code>obj.field != null && obj.field == 'value'</code></li>
* </ul>
*/
@Override
public boolean evaluate(R request, C context, String condition) {
StopWatchLogger stopWatchLogger = new StopWatchLogger(logger);
try {
stopWatchLogger.start();
String booleanCondition = "if (" + condition + ") {return true;} else {return false;}";
logger.debug("Evaluating JEXL condition: " + booleanCondition);
Expression expression = jexlEngine.createExpression(booleanCondition);
Boolean result = (Boolean) expression.evaluate(populateJexlContext(request, context));
return result.booleanValue();
} finally {
stopWatchLogger.debug("Evaluated JEXL expression");
stopWatchLogger.stop();
}
}
示例4: evaluate
import org.apache.commons.jexl2.Expression; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public Object evaluate(final String expression, final Map<String, ?> values) {
ArgUtils.notEmpty(expression, "expression");
ArgUtils.notNull(values, "values");
if(logger.isDebugEnabled()) {
logger.debug("Evaluating JEXL expression: {}", expression);
}
try {
Expression expr = expressionCache.get(expression);
if (expr == null) {
expr = jexlEngine.createExpression(expression);
expressionCache.put(expression, expr);
}
return expr.evaluate(new MapContext((Map<String, Object>) values));
} catch(Exception ex) {
throw new ExpressionEvaluationException(String.format("Evaluating [%s] script with JEXL failed.", expression), ex);
}
}
示例5: evaluate
import org.apache.commons.jexl2.Expression; //导入方法依赖的package包/类
@Override
public Object evaluate(final String expression, final Map<String, Object> values) {
Objects.requireNonNull(expression, "expression shoud not be null.");
Objects.requireNonNull(values, "values shoud not be null.");
if(logger.isDebugEnabled()) {
logger.debug("Evaluating JEXL expression: {}", expression);
}
try {
Expression expr = expressionCache.get(expression);
if (expr == null) {
expr = jexlEngine.createExpression(expression);
expressionCache.put(expression, expr);
}
return expr.evaluate(new MapContext((Map<String, Object>) values));
} catch(Exception ex) {
throw new ExpressionEvaluationException(String.format("Evaluating [%s] script with JEXL failed.", expression), ex,
expression, values);
}
}
示例6: evaluate
import org.apache.commons.jexl2.Expression; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public Object evaluate(final String expression, final Map<String, ?> values) throws ExpressionEvaluationException {
LOG.debug("Evaluating JEXL expression: {1}", expression);
try {
Expression expr = expressionCache.get(expression);
if (expr == null) {
expr = jexl.createExpression(expression);
expressionCache.put(expression, expr);
}
return expr.evaluate(new MapContext((Map<String, Object>) values));
} catch (final Exception ex) {
throw new ExpressionEvaluationException("Evaluating JEXL expression failed: " + expression, ex);
}
}
示例7: getDataByExpression
import org.apache.commons.jexl2.Expression; //导入方法依赖的package包/类
@Override
public <T> T getDataByExpression(String expression) {
Expression e;
try {
e = jexl.createExpression(expression);
} catch (NullPointerException ex) {
logger.log(Level.SEVERE, String.format("Error evaluating expresion %s", expression), ex);
return null;
}
@SuppressWarnings("unchecked")
T data = (T) e.evaluate(runtimeContext);
return data;
}
示例8: evaluateConditionGuardExpresion
import org.apache.commons.jexl2.Expression; //导入方法依赖的package包/类
@Override
public boolean evaluateConditionGuardExpresion(String expression) {
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "evaluateConditionGuardExpresion({0})", new Object[]{expression});
logger.log(Level.FINEST, "" + this.runtimeContext.get("breadcrumb"));
}
Expression e = jexl.createExpression(expression);
Boolean result = (Boolean) e.evaluate(runtimeContext);
return result;
}
示例9: getDataByExpression
import org.apache.commons.jexl2.Expression; //导入方法依赖的package包/类
@Override
public <T> T getDataByExpression(String expression) {
Expression e;
try {
e = getEngineInstance().createExpression(expression);
} catch (NullPointerException ex) {
logger.log(Level.SEVERE, String.format("Error evaluating expresion %s", expression), ex);
return null;
}
@SuppressWarnings("unchecked")
T data = (T) e.evaluate(this.innerContext);
return data;
}
示例10: testJexl
import org.apache.commons.jexl2.Expression; //导入方法依赖的package包/类
public void testJexl() {
JexlEngine engine = new JexlEngine();
Map<String, Object> values = new HashMap<String, Object>();
values.put("a", 12);
values.put("b", "abcde");
values.put("c", new Test());
values.put("$row", new Row(42));
Expression expression = engine.createExpression("'I see ' + $row.bnode");
Object result = expression.evaluate(new MapContext(values));
System.out.println(" -> " + result);
}
示例11: _filterAgree
import org.apache.commons.jexl2.Expression; //导入方法依赖的package包/类
public static boolean _filterAgree(Mention s, Mention t, Expression expression, JexlContext jexlContext) {
Boolean agree = true;
try {
jexlContext.set("t", t);
agree = (Boolean) expression.evaluate(jexlContext);
} catch (Exception ex) {
System.err.println("Error evaluating jexl expression");
ex.printStackTrace(System.err);
}
return agree;
}
示例12: evaluate
import org.apache.commons.jexl2.Expression; //导入方法依赖的package包/类
private String evaluate(String propExpr, JexlContext jc) {
if (propExpr != null) {
Expression e = jexl.createExpression(propExpr);
return (String) e.evaluate(jc);
} else {
return null;
}
}
示例13: piecewiseFunctionTest
import org.apache.commons.jexl2.Expression; //导入方法依赖的package包/类
@Test
public void piecewiseFunctionTest() {
System.out.println("piecewise function test");
Variable v = new Variable("a", -50, 50, 1);
// FUNCTION S
// FROM < TO A IS 0
// FROM A TO B IS 2*((X-A)/(C-A))^2
// FROM B TO C IS 1 - 2*((X-C)/(C-A))^2
// FROM C TO > IS 1
PiecewiseFunction s = new PiecewiseFunction("S", "x");
s.addParameter("A", "B", "C");
s.add("<", "A", "0");
s.add("A", "B", "2*(((x-A)/(C-A))^2)");
s.add("B", "C", "1 - (2*(((x-C)/(C-A))^2))");
s.add("C", ">", "1");
FunctionCall sCall = new FunctionCall(s);
sCall.bindParameter(-30).bindParameter(-20).bindParameter(0);
Member nb = v.addMember("NB", sCall);
// FUNCTION PI
// FROM < TO B-A IS 0
// FROM B-A TO B-(A/2) IS 2*((X-2(B-A))/A)^2
// FROM B-(A/2) TO B+(A/2) IS 1 - 2*((X-B)/A)^2
// FROM B+(A/2) TO B+A IS 2*((X-(B+A))/A)^2
// FROM B+A TO > IS 0
PiecewiseFunction pi = new PiecewiseFunction("PI", "x");
pi.addParameter("A", "B");
pi.add("<", "B-A", "0");
pi.add("B-A", "B-(A/2)", "2*(((x-(B-A))/A)^2)");
pi.add("B-(A/2)", "B+(A/2)", "1 - (2*(((x-B)/A)^2))");
pi.add("B+(A/2)", "B+A", "2*(((x-(B+A))/A)^2)");
pi.add("B+A", ">", "0");
FunctionCall piCall = new FunctionCall(pi);
piCall.bindParameter(20).bindParameter(0);
Member ns = v.addMember("NS", piCall);
// FUZZY Z MEMBERS -10,0 0,1 10,0
Member z = v.addMember("Z").add(-10, 0).add(0, 1).add(10, 0);
// FUZZY PS MEMBERS 0,0 15,1 30,0
Member ps = v.addMember("PS").add(0, 0).add(15, 1).add(30, 0);
// FUZZY PB MEMBERS 20,0 30,1 50,1
Member pb = v.addMember("PB").add(20, 0).add(30, 1).add(50, 1);
v.calculateFuzzySpace();
dumpNormalized(nb, ns, z, ps, pb);
// apply hedge
JexlEngine engine = ExpressionEvalFactory.getInstance();
Expression expr = engine.createExpression("x^2");
JexlContext context = new MapContext();
for (int i = 0; i < nb.normalized().size(); i++) {
Number n = nb.normalized().get(i);
context.set("x", n.doubleValue() / 255.);
Number nhedge = 255. * (Double) expr.evaluate(context);
System.out.println(i + " " + n + " -> " + nhedge.intValue());
}
}
示例14: evaluateExpression
import org.apache.commons.jexl2.Expression; //导入方法依赖的package包/类
private Object evaluateExpression(String expressionName, String expression, String bandName, String pattern, GroupCache gc) throws QueryException {
Object value = null;
Expression e = jexl.createExpression(expression);
// create context with all variables, parameters and columns
// make sure to replace spaces in column names (as in designer expression evaluator)
JexlContext checkContext = new MapContext();
for (Variable var : VariableFactory.getVariables()) {
if (((this instanceof RtfExporter) || (this instanceof XlsExporter) || (this instanceof XlsxExporter)) && Variable.PAGE_NO_VARIABLE.equals(var.getName())) {
// RtfPageNumber must be added in RtfExporter -> let the variable as it is
checkContext.set("$V_" + var.getName(), "$V_" + var.getName());
} else if ( (this instanceof PdfExporter) && Variable.TOTAL_PAGE_NO_VARIABLE.equals(var.getName()) ) {
// compute total page no inside PdfExporter
checkContext.set("$V_" + var.getName(), "$V_" + var.getName());
} else {
checkContext.set("$V_" + var.getName(), getValue(var, bandName));
}
}
for (String paramName : bean.getParametersBean().getParamValues().keySet()) {
Object obj = bean.getParametersBean().getParamValues().get(paramName);
if (obj instanceof IdName) {
obj = ((IdName)obj).toString();
}
checkContext.set("$P_" + paramName, obj);
}
// expresions outside detail or group bands do not contain columns
if (expression.contains("$C") ) {
for (int k = 0, size = getResult().getColumnCount(); k < size; k++) {
String columnName = getResult().getColumnName(k);
String col = columnName.replaceAll("\\s", SPACE_REPLACEMENT);
checkContext.set("$C_" + col, getResult().nextValue(columnName));
}
}
// ony expressions in footers or headers can contain functions
if (expression.contains("$F")) {
for (String f : bean.getReportLayout().getFunctions()) {
FunctionCache fc = findFunctionCache(f, bandName);
Double fv = new Double(0);
if (fc != null) {
fv = (Double)fc.getFunction().getComputedValue();
}
checkContext.set("$F_" + f,fv);
}
// expressions in headers
if (needsFirstCrossing() && !(this instanceof FirstCrossingExporter)) {
for (FunctionBandElement fbe : ReportUtil.getFunctionsFromExpression(expression)) {
value = getFunctionTemplate(gc, fbe, false);
String template = (String)value;
if (templatesValues.containsKey(value)) {
value = templatesValues.get(value);
}
checkContext.set("$F_" + fbe.getFunction()+"_" + fbe.getColumn(),value);
}
}
}
try {
value = e.evaluate(checkContext);
if (value instanceof String) {
I18nLanguage lang = I18nUtil.getLanguageByName(bean.getReportLayout(), bean.getLanguage());
value = StringUtil.getI18nString((String)value, lang);
}
} catch (JexlException ex) {
ex.printStackTrace();
LOG.error(ex.getMessage(), ex);
}
return value;
}
示例15: keyReleased
import org.apache.commons.jexl2.Expression; //导入方法依赖的package包/类
public void keyReleased(KeyEvent ke){
String txt = text.getText();
// We have a formula, if the string starts with "="
if (txt.startsWith("=")) {
String formula;
if (txt.contains(";")) {
formula = txt.substring(1, txt.indexOf(";"));
Map<String, Object> functions = new HashMap<>();
functions.put("math", Math.class);
JexlEngine jexl = new JexlEngine();
jexl.setLenient(false);
jexl.setFunctions(functions);
try {
Expression expr = jexl.createExpression(formula);
Object result = expr.evaluate(new MapContext());
text.setText("");
text.setMessage(formula + "=" + result + "");
result = null;
} catch (JexlException e) {
text.setText("");
text.setMessage("Invalid expression: " + formula);
}
}
return;
}
if (txt.length() > 1) {
filterPositionTitle.setSearchText(txt);
viewer.getControl().setRedraw(false);
viewer.refresh();
viewer.getControl().setRedraw(true);
} else {
filterPositionTitle.setSearchText(null);
viewer.getControl().setRedraw(false);
viewer.refresh();
viewer.getControl().setRedraw(true);
}
}