java 布尔逻辑表达式,在Java中将包含逻辑运算的字符串评估为布尔值

I am trying to evaluate "in1 && in2" to a Boolean as a test, but i hope to be able to evaluate all booleans as stings for my actual project. in1 and in2 are the names of nodes that have a boolean state, i get the actual expression like so,

logic = logic.replaceAll(curName, (nodes.get(ins.get(j)).getState() ? "true" : "false"));

logic is the string contacting the logic i want evaluated, curname is the current node name being replaced with its boolean("in1" for example) its in a loop so all node names are replaced before the string is evaluated, nodes is an array list of nodes, ins are the indexes of the input nodes in the node array, getState() returns the nodes boolean value this works fine, setting the new value of the logic string to "true && true".

The hard part is evaluating the string as a Boolean. I found that i could use javax.script to help me here. So I implemented it as so

ScriptEngineManager sem = new ScriptEngineManager();

ScriptEngine se = sem.getEngineByName("JavaScript");

nodes.get(outs.get(i)).setState((boolean)se.eval(logic));

The problem is that it evaluates to false every time, When i try and cast the object returned by eval as a Boolean and try to display it like so,

System.out.printLine(String.valueOf((boolean)se.eval(logic)));

it only returns false.

On oracle's page on eval, I see that there are some other parameters that can be passed to eval, am i missing one of them or is it something else entirely?

*Side note, it is not a problem in any of the code i haven't shown here, ive already tested the evaluation with raw booleans rather then a string.

解决方案

No need to modify the script. Just send in the values as variables.

public static void main(String[] args) throws Exception {

test(false, false);

test(false, true );

test(true , false);

test(true , true );

}

private static void test(boolean in1, boolean in2) throws ScriptException {

ScriptEngineManager engineManager = new ScriptEngineManager();

ScriptEngine engine = engineManager.getEngineByName("JavaScript");

Bindings vars = engine.getBindings(ScriptContext.ENGINE_SCOPE);

vars.put("in1", in1);

vars.put("in2", in2);

boolean result = (boolean) engine.eval("in1 && in2");

System.out.println(result);

}

Output

false

false

false

true

You can even pre-compile the script for better performance.

public class Test {

public static void main(String[] args) throws Exception {

Test test = new Test();

System.out.println(test.test(false, false));

System.out.println(test.test(false, true ));

System.out.println(test.test(true , false));

System.out.println(test.test(true , true ));

}

private ScriptEngine jsEngine;

private CompiledScript script;

private Test() throws ScriptException {

this.jsEngine = new ScriptEngineManager().getEngineByName("JavaScript");

this.script = ((Compilable) this.jsEngine).compile("in1 && in2");

}

private boolean test(boolean in1, boolean in2) throws ScriptException {

Bindings vars = this.jsEngine.createBindings();

vars.put("in1", in1);

vars.put("in2", in2);

return (boolean) this.script.eval(vars);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值