在Java中将包含逻辑运算的字符串评估为布尔值 [英] Evaluating a string holding a logic operation as a Boolean in java

查看:314
本文介绍了在Java中将包含逻辑运算的字符串评估为布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将"in1 && in2"评估为布尔值作为测试,但我希望能够评估所有布尔值作为我实际项目的st. in1和in2是具有布尔状态的节点的名称,我得到的是这样的实际表达式,

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是与我要评估的逻辑联系的字符串,curname是在循环中被其boolean(例如"in1")替换的当前节点名称,因此在评估字符串之前,所有节点名称都将被替换,nodes是节点的数组列表,ins是节点数组中输入节点的索引,getState()返回节点布尔值,该方法工作正常,将逻辑字符串的新值设置为"true&& true".

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".

困难的部分是将字符串评估为布尔值.我发现我可以使用javax.script帮助我

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)); 

问题在于,每次我尝试将eval返回的对象强制转换为布尔值并尝试以这种方式显示它时,

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)));

它只返回false.

关于eval的Oracle页面上 ,我看到还有一些其他参数可以传递给eval,我是否缺少其中一个参数?还是完全不是其他参数?

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);
}

输出

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);
    }
}

这篇关于在Java中将包含逻辑运算的字符串评估为布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆