猿问

在 JEXL 中获取表达式值

具有以下 JEXL 表达式:

String expression = "myVar >= 12345 && mySecondVar <= 56789";

我可以调用 createScript 和 getVariables 来获取 myVar 和 mySecondVar 作为值,例如:

Set<List<String>> expressionVars = JEXL.createScript(expression).getVariables();

我想知道的是,如果给定相同的表达式,我可以调用一些其他方法来返回这些变量的值。原因是我想验证其中一些值的输入。我检查了文档并使用了 JexlScript 类,但找不到一种优雅的方法。由于 JEXL 已经在解析我的表达式,因此能够检索此信息并且不必手动解析我的表达式来获取此值将是非常棒的。

script.getValue("myVar");正在返回的东西12345


ABOUTYOU
浏览 440回答 2
2回答

阿波罗的战车

使用 JEXL,您可以在包含变量及其值的给定上下文 (JexlContext) 中评估脚本/表达式。JexlContext 公开了 'has' 和 'get' 方法,它们分别检查是否存在并获取变量的值。在您的情况下,您需要找出您的 JexlContext 是(或应该是);从那里,可以直接迭代您的变量(从您的脚本中提取)并检查它们的值(从上下文中)。请参阅: http: //commons.apache.org/proper/commons-jexl/apidocs/org/apache/commons/jexl3/JexlContext.html http://commons.apache.org/proper/commons-jexl/apidocs/org /apache/commons/jexl3/JexlScript.html例如(使用来自https://github.com/apache/commons-jexl的 JEXL 3.2 主干):/**&nbsp;* Collect the global variables of a script and their values from a context.&nbsp;* @param script the script&nbsp;* @param context the context&nbsp;* @return a map keyed by variable name of their contextual values&nbsp;*/Map<String, Object> collectVars(JexlScript script, JexlContext context) {&nbsp; &nbsp; Set<List<String>> sls = script.getVariables();&nbsp; &nbsp; Map<String, Object> vars = new TreeMap<String, Object>();&nbsp; &nbsp; for(List<String> ls : sls) {&nbsp; &nbsp; &nbsp; &nbsp; // build the 'antish' name by concatenating&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder strb = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; for(String s : ls) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (strb.length() > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strb.append('.');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strb.append(s);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; String name = strb.toString();&nbsp; &nbsp; &nbsp; &nbsp; vars.put(name, context.get(name));&nbsp; &nbsp; }&nbsp; &nbsp; return vars;}@Testpublic void testStckOvrflw() throws Exception {&nbsp; &nbsp; JexlEngine jexl = new JexlBuilder().safe(false).create();&nbsp; &nbsp; // a context with some variables&nbsp; &nbsp; JexlContext context = new MapContext();&nbsp; &nbsp; context.set("low", 15000);&nbsp; &nbsp; context.set("high", 50000);&nbsp; &nbsp; context.set("mid", 35000);&nbsp; &nbsp; context.set("limit.low", 15042);&nbsp; &nbsp; context.set("limit.high", 35042);&nbsp; &nbsp; // an expression with 2 variables&nbsp; &nbsp; JexlScript expr = jexl.createScript("low >= 12345 && high <= 56789");&nbsp; &nbsp; // collecting the 2 variables, low and high&nbsp; &nbsp; Map<String, Object> vars = collectVars(expr, context);&nbsp; &nbsp; Assert.assertEquals(2, vars.size());&nbsp; &nbsp; Assert.assertEquals(15000, vars.get("low"));&nbsp; &nbsp; Assert.assertEquals(50000, vars.get("high"));&nbsp; &nbsp; expr = jexl.createScript("limit.low >= 12345 && limit.high <= 56789");&nbsp; &nbsp; vars = collectVars(expr, context);&nbsp; &nbsp; Assert.assertEquals(2, vars.size());&nbsp; &nbsp; Assert.assertEquals(15042, vars.get("limit.low"));&nbsp; &nbsp; Assert.assertEquals(35042, vars.get("limit.high"));}

慕码人8056858

您应该实现自己的上下文:public class ZenContext implements JexlContext {&nbsp; static private final Map<String, Object> reservedVars = new HashMap<String, Object>();&nbsp; private final Map<String, Object> scriptDefinedVars&nbsp; = new HashMap<String, Object>();&nbsp; static {&nbsp; &nbsp; reservedVars.put("math", java.lang.Math.class);&nbsp; &nbsp; reservedVars.put("stats", apache.commons.math3.stats.Stats);&nbsp; &nbsp; // whatever else ...&nbsp; }&nbsp; public boolean has(String name) {&nbsp; &nbsp; if (reservedVars .get(name) != null) return true;&nbsp; &nbsp; return scriptDefinedVars.get(name) != null;&nbsp; }&nbsp; public boolean get (String name) {&nbsp; &nbsp; Object value = null;&nbsp; &nbsp; if ((value = reservedVars .get(name)) != null) return value;&nbsp; &nbsp; return scriptDefinedVars.get(name);&nbsp; }&nbsp; public void set(String name, Object value) {&nbsp; &nbsp; scriptDefinedVars.set(name, value);&nbsp; }&nbsp; public Map<String, Object> getReservedVars () {&nbsp; &nbsp; return reservedVars;&nbsp; }&nbsp; public Map<String, Object> getScriptDefinedVars&nbsp; &nbsp;() {&nbsp; &nbsp; return scriptDefinedVars ;&nbsp; }}这样,您将拥有保留 var 名称的映射,以包含不允许脚本更改其值的对象。例如,可以从脚本设置的单独的变量映射。然后添加这些方法。public Object execute(File scriptFile) {&nbsp; &nbsp; JexlScript script = jexlEngine.createScript(scriptFile);&nbsp; &nbsp; return script.execute(this); // supply this as the context&nbsp;}public Object execute (String scriptText) {&nbsp; &nbsp; JexlScript script = jexlEngine.createScript(scriptText);&nbsp; &nbsp; return script.execute(this); // supply this as the context&nbsp;}您可以修改我在此处编写的 set 方法,以检查映射中的变量,然后再允许它们设置。但是,这不适用于本地脚本变量var greeting = 'The rain in Maine falls plainly insane';因为 local var 依赖于不同的机制,org.apache.commons.jexl3.internal.Scope,使用 getLocalVariables() 方法,它有一个错误。
随时随地看视频慕课网APP

相关分类

Java
我要回答