0

Possible Duplicate:
Evaluating a math expression given in string form

How can I boolean evaluate a string containing bool expressions? Like:

String userVar[] = {"a = 1", "b = 1", "c = 0"};
String expr = "a & b & c";
boolean result = evaluate(expr); //would evaluate to false

The user should be able to define his own variables (a = 1), and define his own boolean expression (a & b & c). So I will have all expressions only as a string. How can I evaluate them?

Community
  • 1
  • 1
membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • 3
    this may help http://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form – kosa Dec 08 '12 at 17:37

2 Answers2

4

You can use a ScriptEngine as commented by Nambari:

ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
String userVar[] = {"a = 1", "b = 1", "c = 0"};

for (String s : userVar) {
    engine.eval(s);
}

String expr = "a & b & c";
System.out.println(engine.eval(expr));

prints 0.

Also note that the expression is not a boolean expression but a bitwise operation.

assylias
  • 321,522
  • 82
  • 660
  • 783
1

Finally I made code for the solution of the above problem.

public static void main(String[] args) {
        String userVar[] = { "a = 1", "b = 0", "c = 1" };
        String expr = "a & b & c";

        // System.out.println(Boolean.valueOf("true"));
        Map<String, Boolean> booleanMap = getBooleanMap(userVar);
        // System.out.println(booleanMap);
        boolean evaluate = evaluate(booleanMap, expr);
        System.out.println("Final Result = " + evaluate);

    }

    public static boolean evaluate(Map<String, Boolean> operVal, String eq) {
        String[] split = eq.split(" ");
        boolean orgVal = false;
        boolean prevVal = false;
        boolean firstTime = true;
        String lastOpr = "&";
        for (String exp : split) {
            // System.out.println(orgVal + "  " + exp);
            // System.out.println(exp);
            if (exp.equals("&")) {
                lastOpr = "&";
                orgVal = orgVal && prevVal;
            } else if (exp.equals("|")) {
                lastOpr = "|";
                orgVal = orgVal || prevVal;
            } else {
                prevVal = operVal.get(exp);
                if (firstTime) {
                    orgVal = prevVal;
                    firstTime = false;
                }
            }
        }
        if (lastOpr.equals("&")) {
            lastOpr = "&";
            orgVal = orgVal && prevVal;
        } else if (lastOpr.equals("|")) {
            lastOpr = "|";
            orgVal = orgVal || prevVal;
        }

        // System.out.println(orgVal);
        return orgVal;
    }

    public static Map<String, Boolean> getBooleanMap(String[] val) {
        Map<String, Boolean> result = new HashMap<String, Boolean>();

        for (String exp : val) {
            String[] split = exp.split("=");
            // System.out.println(split[0].trim());
            // System.out.println(split[1].trim());
            String opr = split[0].trim();
            int intVal = Integer.valueOf(split[1].trim());
            boolean boolVal = false;

            if (intVal == 1) {
                boolVal = true;
            }

            result.put(opr, boolVal);

        }

        return result;

    }

Test Case - 1

String userVar[] = {"a = 1", "b = 1", "c = 0"};
  • Output Final Result = false

Test Case - 2

String userVar[] = { "a = 1", "b = 1", "c = 1" };
  • Output Final Result = true
Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86