def _logic_xor(self, val, fact, dic): """ handle the logic XOR in the condition of the expression """ ival = [int(val[0]), int(val[1])] if ival[0] == td.v_undef and ival[1] == td.v_undef: return td.v_undef if ival[0] == ival[1]: return td.v_false if ival[0] != ival[1] and td.v_undef not in ival: return td.v_true if td.v_true in ival: fact_false = fact[1] if ival[0] is td.v_true else fact[0] if fact_false[0].isupper(): if (fact_false not in [elt for elt in dic if dic[elt][1] is not td.q_unused]): rlt = modify_dict(fact_false, td.v_false, dic, fact_false) else: rlt = modify_dict(fact_false, td.v_undef, dic, fact_false) return td.v_undef if rlt is None else rlt return rlt if rlt is not None else td.v_true return td.v_true return td.v_undef
def _recu_solver(self, dic, r_rpn_cpy, wanted, query, symb): """ recursive function to call the correct operator function """ r_rpn_lst = [] if get_first_index(td.Symbols, r_rpn_cpy) is not -1: r_rpn_lst = self._split_r_rpn(r_rpn_cpy) elif r_rpn_cpy[0].isupper(): ret = modify_dict(r_rpn_cpy[0], wanted, dic, query, symb) cust_ret(ret) if ret is not None else None return get_value_from_dict(r_rpn_cpy[0], dic) func_tbl = { '^': self._logic_xor, '|': self._logic_or, '+': self._logic_and, '!': self._logic_not } if r_rpn_cpy[0] in '^|+!': rlt = func_tbl[r_rpn_cpy[0]](dic, r_rpn_lst, wanted, query, symb) else: rlt = get_value_from_dict(r_rpn_cpy[0], dic) return rlt
def _logic_and(self, dic, r_rpn_lst, wanted, query, symb): """ handle the logic AND in the conclusion of the expression """ val = fact_to_value(r_rpn_lst[1:], dic) if ((wanted is td.v_true and td.v_false in val) or (wanted is td.v_false and val.count(td.v_true) == 2)): if query in r_rpn_lst: not_wanted = (td.v_true if wanted is td.v_false else td.v_false if wanted is td.v_true else td.v_undef) tmp = ''.join(elt for i, elt in enumerate(r_rpn_lst[1:]) if val[i] is not_wanted) bug = set([elt for elt in tmp if elt.isupper()]) ret = modify_dict(bug, td.v_bugged, dic, query, symb) und = set( [elt for elt in tmp if elt.isupper() and elt not in bug]) ret = modify_dict(und, td.v_undef, dic, query, symb) return -2 for i, elt in enumerate(r_rpn_lst[1:]): if len(elt) > 1: other_val = val[0 if i == 1 else 1] to_give = td.v_undef if wanted is td.v_true: to_give = wanted elif wanted is td.v_false and other_val is td.v_true: to_give = td.v_false val[i] = self._recu_solver(dic, elt, to_give, query, symb) else: value = (td.v_true if wanted is td.v_true else td.v_false if wanted is td.v_false and val[0 if i == 1 else 1] is td.v_true else td.v_undef) ret = modify_dict(elt, value, dic, query, symb) cust_ret(ret) if ret is not None else None return (td.v_false if val.count(td.v_false) > 0 else td.v_undef if val.count(td.v_undef) > 0 else td.v_true)
def _logic_or(self, dic, r_rpn_lst, wanted, query, symb): """ handle the logic OR in the conclusion of the expression """ val = fact_to_value(r_rpn_lst[1:], dic) if ((wanted is td.v_false and td.v_true in val) or (wanted is td.v_true and val.count(td.v_false) == 2)): if query in r_rpn_lst: elts = [] for elt in list(r_rpn_lst[1:]): for letter in elt: if letter.isupper(): elts += letter ret = modify_dict(elts, td.v_bugged, dic, query, symb) return -2 i = 1 while i < 3: to_give = td.v_undef if wanted is not td.v_undef: other_val = val[0 if i == 2 else 1] to_give = (td.v_true if wanted is td.v_true and other_val is td.v_false else td.v_false if wanted is td.v_false else td.v_undef) if len(r_rpn_lst[i]) > 1: tmp = self._recu_solver(dic, r_rpn_lst[i], to_give, query, symb) if len(r_rpn_lst[i]) == 1 and r_rpn_lst[i].isupper(): ret = modify_dict(r_rpn_lst[i], to_give, dic, query, symb) cust_ret(ret) if ret is not None else None tmp = dic[r_rpn_lst[i]][0] if tmp != val[i - 1]: val[i - 1] = tmp i = 0 i += 1 return (wanted if val.count(wanted) == 2 or (val.count(wanted) == 1 and wanted is td.v_true) else td.v_undef)
def _logic_not(self, dic, r_rpn_lst, wanted, query, symb): """ handle the logic NOT in the conclusion of the expression """ inv_rlt = self._not(wanted) if wanted < 2 else td.v_undef if len(r_rpn_lst[1]) > 1: val = self._recu_solver(dic, r_rpn_lst[1], inv_rlt, query, symb) val = (td.v_true if val == td.v_false else td.v_false if val == td.v_true else val) elif r_rpn_lst[1].isupper(): if dic[r_rpn_lst[1]][2] >= symb: val = (dic[r_rpn_lst[1]][0] if dic[r_rpn_lst[1]][0] is not td.v_undef else inv_rlt) ret = modify_dict(r_rpn_lst[1], val, dic, query, symb) cust_ret(ret) if ret is not None else None val = (td.v_true if val is td.v_false else td.v_false if val is td.v_true else td.v_undef) else: ret = modify_dict(r_rpn_lst[1], inv_rlt, dic, query, symb) cust_ret(ret) if ret is not None else None val = wanted else: val = r_rpn_lst[1] if (int(val) != int(wanted) and wanted is not td.v_undef and int(val) >= 0 and int(val) is not td.v_undef and (len(r_rpn_lst[1]) > 1 or dic[r_rpn_lst[1]][2] <= symb)): elts = set([elt for elt in ''.join(r_rpn_lst[1]) if elt.isupper()]) ret = modify_dict(elts, td.v_bugged, dic, query, symb) return -2 return val
def _logic_xor(self, dic, r_rpn_lst, wanted, query, symb): """ handle the logic XOR in the conclusion of the expression """ val = fact_to_value(r_rpn_lst[1:], dic) if (-1 not in val and 2 not in val and ((wanted is td.v_true and val[0] == val[1]) or (wanted is td.v_false and val[0] != val[1]))): if query in r_rpn_lst: elts = [] for elt in list(r_rpn_lst[1:]): for letter in elt: if letter.isupper(): elts += letter ret = modify_dict(elts, td.v_bugged, dic, query, symb) return -2 i = 1 while i < 3: to_give = td.v_undef if wanted is not td.v_undef: other_val = val[0 if i == 2 else 1] if other_val == td.v_undef and other_val == -1: to_give = td.v_undef elif wanted == other_val and other_val != -1: to_give = td.v_false elif val.count(-1) == 0 and other_val is not td.v_undef: to_give = td.v_true wanted == (other_val and -1 not in val, val.count(-1) != 2 and -1 not in val) if len(r_rpn_lst[i]) > 1: tmp = self._recu_solver(dic, r_rpn_lst[i], to_give, query, symb) elif r_rpn_lst[i].isupper(): ret = modify_dict(r_rpn_lst[i], to_give, dic, query, symb) cust_ret(ret) if ret is not None else None tmp = to_give if tmp != val[i - 1]: val[i - 1] = tmp i = 0 i += 1 if (val.count(td.v_undef) == 0 and ((wanted is td.v_true and val[0] != val[1]) or (wanted is td.v_false and val[0] == val[1]))): return wanted elif (val.count(td.v_undef) == 0 and ((wanted is td.v_true and val[0] == val[1]) or (wanted is td.v_false and val[0] != val[1]))): elts = set([elt for elt in list(r_rpn_lst[1:]) if elt.isupper()]) ret = modify_dict(elts, td.v_bugged, dic, query, symb) return -2 elif (val.count(td.v_undef) == 1 and len(r_rpn_lst[1]) == 1 and len(r_rpn_lst[2]) == 1): to_give = (td.v_true if (wanted is td.v_true and td.v_false in val) or (wanted is td.v_false and td.v_true in val) else td.v_false) ret = modify_dict(r_rpn_lst[1 + val.index(td.v_undef)], to_give, dic, query, symb) return wanted if ret is None else ret return td.v_undef