Results 1 to 5 of 5
  1. #1
    sirair is offline Member
    Join Date
    Jun 2013
    Posts
    13
    Rep Power
    0

    Default Validate Boolean Expressions

    Hello,

    I am searching for a Java Api which can validate boolean expressions.
    For example:
    This is the rule.... ((A & B) | C) and I have a set of codes that should be validated:
    So the code C should return true and so on....
    Do anybody know any API?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,203
    Rep Power
    12

    Default Re: Validate Boolean Expressions

    Assuming that I understand what you're asking, but why not just use a print statement?


    Java Code:
    boolean res = (true && false) | true);
    System.out.println(res);
    You could also pass the true/false arguments to a function and
    print the return value.

    You might also want to revisit truth tables if you haven't worked with these in a while.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    sirair is offline Member
    Join Date
    Jun 2013
    Posts
    13
    Rep Power
    0

    Default Re: Validate Boolean Expressions

    I have something like this.
    Java Code:
    Map<String, String> codes = new HashMap<>();
    codes.put("CODE_A", "A&B");
    codes.put("CODE_B", "!C");
    So when I validate against "((A & B) | C)" I should get for
    CODE_A -> true
    CODE_B -> false

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,203
    Rep Power
    12

    Default Re: Validate Boolean Expressions

    First, it appears you want a parser to parse the expression and apply the values to the expression. You can either search the web
    for one or write your own. I do not have a recommendation on a particular one to use. You may want to check out the ScriptEngine
    class in the Java API, though I have never used it.

    But I don't follow your example. You can't evaluate the expression against A, B, and C without assigning boolean values of true or false to A, B, and C.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,965
    Rep Power
    7

    Default Re: Validate Boolean Expressions

    I use (and adapted) jeval for that. It's old, but still valid.

    JEval
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Type mismatch: cannot convert String to boolean (or boolean to String)
    By msulli1355@gmail.com in forum New To Java
    Replies: 5
    Last Post: 03-23-2017, 10:35 AM
  2. Replies: 6
    Last Post: 01-16-2014, 01:48 PM
  3. Replies: 5
    Last Post: 07-12-2013, 01:22 AM
  4. Replies: 3
    Last Post: 05-11-2013, 06:27 AM
  5. Replies: 3
    Last Post: 09-13-2011, 12:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •