简体   繁体   English

是否可以评估字符串比较的布尔表达式?

[英]Is it possible to evaluate a boolean expression for String comparions?

I will have a String like我会有一个像这样的字符串

('abc' != 'xyz' AND 'thy' = 'thy') OR ('ujy' = 'ujy')

The String will be able to have as many "AND" groups as it wants.字符串将能够拥有任意数量的“AND”组。 There will not be any nested groups within the AND groups. AND 组中不会有任何嵌套组。 All groups will ALWAYS be serparated by an OR.所有组将始终由 OR 分隔。

I can just switch out the AND for && and OR for ||我可以把&&的 AND 和||的 OR 换掉。 . .

What I would like is to pass this String into some type of eval method and output TRUE or FALSE.我想要的是将此字符串传递给某种类型的 eval 方法并输出 TRUE 或 FALSE。

Is there anything out there that can do this?有什么可以做到这一点吗?

You can use the built-in Javascript engine coming with the JDK1.6 to evaluate string containing math expressions.您可以使用JDK1.6 附带的内置Javascript 引擎来评估包含数学表达式的字符串。

You an give a lookup here: ScriptEngine您可以在这里查找: ScriptEngine

Here an example:这里有一个例子:

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class Myclass {
    public static void main(String[] args) {

        try {

            ScriptEngineManager sem = new ScriptEngineManager();
            ScriptEngine se = sem.getEngineByName("JavaScript");
            String myExpression = "('abc' == 'xyz' && 'thy' == 'thy') || ('ujy' == 'ujy')";
            System.out.println(se.eval(myExpression));

        } catch (ScriptException e) {

            System.out.println("Invalid Expression");
            e.printStackTrace();

        }
    }
}

Just remember to replace the following:请记住替换以下内容:

'AND' with '&&', '与 '&&',
'OR' with '||', 'OR' 与 '||',
'=' must be '==' '=' 必须是 '=='

Otherwise it will not accept your expression and will throws a javax.script.ScriptException否则它将不接受您的表达式并抛出javax.script.ScriptException

you can use script package to achieve this like您可以使用script包来实现这一点

    ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");
    String[] vars = {"var1 = 'xyz'", "var2 = 'xyz'"};

    try {
        for (String var : vars) {
            engine.eval(var);
        }
        System.out.println(engine.eval("var1 == var2 "));
    } catch (ScriptException e) {
        e.printStackTrace();
    }

I think you have to parse it and write custom classes for it like this我认为您必须像这样解析它并为它编写自定义类

public interface StringEquals{

    public boolean equals(String s1, String s2);
}

public class Equals implements StringEquals{

    private String mS1;
    private STring mS2;

    public NotEquals(String s1, String s2){
        mS1 = s1;
        mS2 = s2;
    }


    public boolean equals(){
        return mSq1.equals(mS2);
    }
}

public class NotEquals implements StringEquals{

    private String mS1;
    private STring mS2;

    public NotEquals(String s1, String s2){
        mS1 = s1;
        mS2 = s2;
    }

    public boolean equals(){
        return !mS1.equals(mS2);
    }
}

public class AndGroup{

    private List<StringEquals> mStrings;

    public AndGroup(List<StringEquals> list){
        mStrings = list;
    }

    public boolean getResult(){
        for(StringEquals e: mStrings){
            if (!e.equals()){
                return false;
            }
        }
        return true;
    }



and a class to parse it:

public boolean eval(String evalString){
    List<AndGroup> groups = new LinkedList<AndGroup>();
    String[] ands = evalString.split("OR");
    for (String andExp : ands){
        List<StringEquals> list = new LinkedList<StringEquals>();
        String compares = andExp.split("AND");
        for (String comp: compares){
            if (comp.contains("!="){
                String[] notEqual = comp.split("!=");
                list.add(new NotEquals(notEqual[0], notEqual[1]));
            } else {
                 String[] equal = comp.split("=");
                 list.add(new Equals(equal[0], equal[1]);
            }
        }
        groups.add(new AndGroup(list));


    }
    for (AndGroup g: groups){
        if (g.getResult()){
            return true;
        }
    }
    return false;

}

not tested, but it may point you into the right direction未经测试,但它可能会为您指明正确的方向

我认为您可以使用 groovy.util.Eval 尝试 groovy(如果它是您的选择),但首先您应该处理您的字符串并将 AND\OR 替换为 && 和 ||。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM