Is it possible to evaluate a boolean expression for String comparions?
Solution 1
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
Solution 2
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();
}
Solution 3
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
Related videos on Youtube
Envin
Updated on October 19, 2022Comments
-
Envin over 1 year
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?
-
Luiggi Mendoza over 10 yearsYou could use JavaScript engine to evaluate it.
-
helpermethod over 10 yearsSo the whole expression is stored in a String? An idea would be to split the String into tokens and build an abstract syntax tree out of it. Then, to evaluate the expression, you would call something like
tree.evaluate
. -
Luiggi Mendoza over 10 years@barwnikk I know, and you can use JavaScript engine in Java. Also, a Java valid string is declared using
"
, not'
. -
RyanfaeScotland over 10 yearsCan I just clarify, do you want to pass the entire String "('abc' != 'xyz' AND 'thy' = 'thy) OR ('ujy' = 'ujy')" into a function which will then evaluate it and return true / false?
-
-
Jonathan Drapeau over 10 yearsThat would require him to parse his whole
String
and build thevars
array and conditionsString
. Looks like it would be more complicated than doing it strictly in Java. -
minhas23 over 8 yearsgreat answer and code snippet...ended my 2 days long struggle.. Thanks buddy
-
Hauke almost 6 yearsThe Javascript Engine will be removed in Java 11, so this solution is not really future proof.