本文整理匯總了Java中org.apache.commons.jexl2.JexlEngine.createExpression方法的典型用法代碼示例。如果您正苦於以下問題:Java JexlEngine.createExpression方法的具體用法?Java JexlEngine.createExpression怎麽用?Java JexlEngine.createExpression使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.jexl2.JexlEngine
的用法示例。
在下文中一共展示了JexlEngine.createExpression方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: setup
import org.apache.commons.jexl2.JexlEngine; //導入方法依賴的package包/類
public void setup(Context context) {
dumpHomepages = context.getConfiguration().getBoolean(HOSTDB_DUMP_HOMEPAGES, false);
dumpHostnames = context.getConfiguration().getBoolean(HOSTDB_DUMP_HOSTNAMES, false);
String expr = context.getConfiguration().get(HOSTDB_FILTER_EXPRESSION);
if (expr != null) {
// Create or retrieve a JexlEngine
JexlEngine jexl = new JexlEngine();
// Dont't be silent and be strict
jexl.setSilent(true);
jexl.setStrict(true);
// Create an expression object
this.expr = jexl.createExpression(expr);
}
}
示例2: isAboveThreshold
import org.apache.commons.jexl2.JexlEngine; //導入方法依賴的package包/類
/**
*
*/
@Override
public boolean isAboveThreshold(MetricTimeSeries timeSeries)
{
Set<Long> timeWindowSet = timeSeries.getTimeWindowSet();
JexlEngine jexl = new JexlEngine();
JexlContext context = new MapContext();
Expression e = jexl.createExpression(this.thresholdExpr);
for(String metricName : metricNames){
long sum = 0;
for (Long timeWindow : timeWindowSet) {
sum += timeSeries.get(timeWindow, metricName).longValue();
}
context.set(metricName, sum);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(metricName + " = " + sum);
}
}
return ((Boolean)e.evaluate(context)).booleanValue();
}
示例3: testPower
import org.apache.commons.jexl2.JexlEngine; //導入方法依賴的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);
}
示例4: _resolveAllFromSameSentence
import org.apache.commons.jexl2.JexlEngine; //導入方法依賴的package包/類
private static void _resolveAllFromSameSentence(Document d, String filter, String comment) {
JexlEngine jexl = new JexlEngine();
jexl.setSilent(JEXL_SILENT);
jexl.setLenient(JEXL_LENIENT);
jexl.setDebug(JEXL_DBG);
Expression expression = jexl.createExpression(filter);
JexlContext jexlContext = new MapContext();
jexlContext.set("Filter", Filter.class);
for (Mention s: d.mentions) {
jexlContext.set("s", s);
List<Mention> tt = MentionBrowser.getAllFromSameSentence(s, expression, jexlContext);
for (Mention t : tt) {
_resolve(d, s, t, comment);
}
}
}
示例5: parseExpression
import org.apache.commons.jexl2.JexlEngine; //導入方法依賴的package包/類
/**
* Parses the given experssion to a Jexl expression. This supports
* date parsing.
*
* @param expr the Jexl expression
* @return parsed Jexl expression or null in case of parse error
*/
public static Expression parseExpression(String expr) {
if (expr == null) return null;
try {
// Translate any date object into a long, dates must be specified as 20-03-2016T00:00:00Z
Matcher matcher = datePattern.matcher(expr);
if (matcher.find()) {
String date = matcher.group();
// Parse the thing and get epoch!
Date parsedDate = DateUtils.parseDateStrictly(date, new String[] {"yyyy-MM-dd'T'HH:mm:ss'Z'"});
long time = parsedDate.getTime();
// Replace in the original expression
expr = expr.replace(date, Long.toString(time));
}
JexlEngine jexl = new JexlEngine();
jexl.setSilent(true);
jexl.setStrict(true);
return jexl.createExpression(expr);
} catch (Exception e) {
LOG.error(e.getMessage());
}
return null;
}
示例6: testJexl
import org.apache.commons.jexl2.JexlEngine; //導入方法依賴的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);
}
示例7: _resolveFirst
import org.apache.commons.jexl2.JexlEngine; //導入方法依賴的package包/類
private static void _resolveFirst(Document d, String filter, String comment) {
JexlEngine jexl = new JexlEngine();
jexl.setSilent(JEXL_SILENT);
jexl.setLenient(JEXL_LENIENT);
jexl.setDebug(JEXL_DBG);
Expression expression = jexl.createExpression(filter);
JexlContext jexlContext = new MapContext();
jexlContext.set("Filter", Filter.class);
Mention t;
for (Mention s: d.mentions) {
jexlContext.set("s", s);
t = MentionBrowser.getFirst(s, expression, jexlContext);
if (t != null) _resolve(d, s, t, comment);
}
}
示例8: _resolveFirstFromSintax
import org.apache.commons.jexl2.JexlEngine; //導入方法依賴的package包/類
private static void _resolveFirstFromSintax(Document d, String filter, String comment) {
JexlEngine jexl = new JexlEngine();
jexl.setSilent(JEXL_SILENT);
jexl.setLenient(JEXL_LENIENT);
jexl.setDebug(JEXL_DBG);
Expression expression = jexl.createExpression(filter);
JexlContext jexlContext = new MapContext();
jexlContext.set("Filter", Filter.class);
Mention t;
for (Mention s: d.mentions) {
jexlContext.set("s", s);
t = MentionBrowser.getFirstFromSintax(s, expression, jexlContext);
if (t != null) _resolve(d, s, t, comment);
}
}
示例9: _resolveFirstFromAll
import org.apache.commons.jexl2.JexlEngine; //導入方法依賴的package包/類
private static void _resolveFirstFromAll(Document d, String filter, String comment) {
JexlEngine jexl = new JexlEngine();
jexl.setSilent(JEXL_SILENT);
jexl.setLenient(JEXL_LENIENT);
jexl.setDebug(JEXL_DBG);
Expression expression = jexl.createExpression(filter);
JexlContext jexlContext = new MapContext();
jexlContext.set("Filter", Filter.class);
Mention t;
for (Mention s: d.mentions) {
jexlContext.set("s", s);
t = MentionBrowser.getFirstFromAll(s, expression, jexlContext);
if (t != null) _resolve(d, s, t, comment);
}
}
示例10: piecewiseFunctionTest
import org.apache.commons.jexl2.JexlEngine; //導入方法依賴的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());
}
}
示例11: keyReleased
import org.apache.commons.jexl2.JexlEngine; //導入方法依賴的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);
}
}
示例12: evaluate
import org.apache.commons.jexl2.JexlEngine; //導入方法依賴的package包/類
/**
* @param expression the expression.
* @param vars the variables, can be null.
* @param strict indicates whether to use strict or lenient engine mode.
* @return the result of the evaluation.
*/
private static Object evaluate( String expression, Map<String, Object> vars, boolean strict )
{
expression = expression.replaceAll( IGNORED_KEYWORDS_REGEX, StringUtils.EMPTY );
JexlEngine engine = strict ? JEXL_STRICT : JEXL;
Expression exp = engine.createExpression( expression );
JexlContext context = vars != null ? new MapContext( vars ) : new MapContext();
return exp.evaluate( context );
}