I am obtaining a boolean expression in the form of ( (A & B) | (C & ~D) )
from the frontend. I need to convert this into a boolean expression that can be solved by PyEDA. There are two steps to be done to write a boolean expression in PyEDA.
- Create boolean variables A,B,C,D using
A,B,C,D = map(exprvar, "abcd") - This becomes a problem when number of boolean variables are dynamic
orA = exprvars("a", 4) - solves the dynamic variable issue but need to convert letters in the equation to A[0],A[1]...
. - Write the expression as
( (A & B) | (C & ~D) )
.
The following approach was tried out. boolean_exp represents the boolean expression string and num_variables represent the number of variables in the string.
def parse_boolean_expression(boolean_exp,num_variables):
count = 0
boolean_exp_list = list(boolean_exp)
for index,char in enumerate(boolean_exp_list):
if char.isalpha():
boolean_exp_list[index] = "Z[{}]".format(count)
count += 1
final_bool_exp = "".join(boolean_exp_list)
Z = exprvars("z", num_variables)
expression = final_bool_exp
This approach does not work because the created expression and variables are of type string while the correct types should be <class 'pyeda.boolalg.expr.OrOp'>
for the expression and <class 'pyeda.boolalg.expr.variables'>
for variables.
In the interactive mode we can do the above steps easily, but how can we build this expression in script mode using a boolean expression string with a dynamic number of variables sent from the frontend?