Reputation: 86747
I'm looking for a solution to represent boolean algebra.
Let's say I have a lot of String objects that I'm displaying to the user. He can now multi-select them (make an && AND
connection between them), and also make || OR
connections. The final expression should also be shown to the user.
So, eg if he selects String1, String2, String3
I want to present String1 && String2 && String3
. And maybe lateron the user should be able to build expressions like (String1 || String 2) && String 3
.
My question is: how could I track reference on these boolean selections between the string objects? Of course I could just create a String expression; and always append what he selected. But what if he deselects an object? Then I would have to parse the whole string an revaluate it.
I'm searching for something to keep a AND/OR
reference between the objects and then having a method to build the String final expression based on these.
Any ideas are welcome!
Upvotes: 2
Views: 2260
Reputation: 7540
You can do it by Lexical Analysis. There are many lexical generator for java like: JavaCC , JLex etc.
Or you can use something like pre/post -fix calculator or stack calculator for parenthesis balancing and to maintain the hierarchy, originally it is done by traversing through a "tree" and in that calculator in place of "1"-"9" numbers (which comes as string) you have to match "true" or "false" and in place of "+", "-" binary operator you have to match and operate "and" and "or"!
I have made a Boolean Algebra Parser as I have committed in my previous comment, I think it works, you are also welcome to fork and contribute: https://github.com/sadaf2605/Java-Boolean-Algebra-Parser
code:
public class BooleanAlgebra {
static final String CONST_and="And";
static final String CONST_or="Or";
static final String CONST_true="True";
static final String CONST_false="False";
public static void main(String[] args) {
System.out.println(booleanAlgebra("False")==false);
System.out.println(booleanAlgebra("True")==true);
System.out.println(booleanAlgebra("False And False")==false);
System.out.println(booleanAlgebra("True And False")==false);
System.out.println(booleanAlgebra("True And True")==true);
System.out.println(booleanAlgebra("False And True")==false);
System.out.println(booleanAlgebra("(False And True)")==false);
System.out.println(booleanAlgebra("True Or (False And True)")==true);
System.out.println(booleanAlgebra("(True And False) And (False And True) Or True")==true);
System.out.println(booleanAlgebra("( (True And False) Or True )")==true);
System.out.println(booleanAlgebra("( False Or (True And (False Or True)) Or True )")==true);
}
static boolean booleanAlgebra(String str){
return TrueFalse(str, false, "Or");
}
static boolean TrueFalse(String s,boolean b, String op){
boolean btemp=false;
s=s.replaceAll(" ", "");
while(!s.isEmpty()){
if(s.startsWith(CONST_true)){
btemp=true;
s=s.substring(4);
}else if(s.startsWith(CONST_false)){
btemp=false;
s=s.substring(5);
}else if(s.startsWith(CONST_and)){
op=CONST_and;
s=s.substring(3);
}else if(s.startsWith(CONST_or)){
op=CONST_or;
s=s.substring(2);
}else if(s.startsWith("(")){
int end=s.indexOf(")");
if(end>0){
b=TrueFalse(s.substring(1, end>-1?end:1),b,op);
s=s.substring(end);
}
}else{
s=s.substring(1);
}
if (op.equals(CONST_and)){
b=b&&btemp;
}else if(op.equals(CONST_or)){
b=b||btemp;
}
}
return b;
}
}
Upvotes: 2
Reputation: 272497
It sounds like you just need a tree structure. Every inner node represents an operator (&&
, ||
, etc.), and every leaf node represents a term.
See also: abstract syntax tree
Upvotes: 2