Home » Java » Is it possible to evaluate a boolean expression for String comparions?

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

Posted by: admin December 28, 2021 Leave a comment

Questions:

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. There will not be any nested groups within the AND groups. All groups will ALWAYS be serparated by an OR.

I can just switch out the AND for && and OR for ||.

What I would like is to pass this String into some type of eval method and output TRUE or FALSE.

Is there anything out there that can do this?

Answers:

You can use the built-in Javascript engine coming with the JDK1.6 to evaluate string containing math expressions.

You an give a lookup here: 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 ‘||’,
‘=’ must be ‘==’

Otherwise it will not accept your expression and will throws a javax.script.ScriptException

###

you can use script package to achieve this like

    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

###

I think you can try groovy (if it’s an option for you) with it’s groovy.util.Eval, but first you should process your string and replace AND\OR with && and ||.