def generate_rules(ex: BooleanFunction) -> [(BooleanFunction, BooleanFunction)]: rule_strings = 'p | p = p'.split("=") left_rule_ex = parse_expr(rule_strings[0]) right_rule_ex = parse_expr(rule_strings[1]) return Rule.apply_rule(ex, left_rule_ex, right_rule_ex)
def find_all_rules_of_expr_str(ex_str: str): ex = parse_expr(ex_str) rules = Rule.generate_rules(ex) ex_list = [] for rule in rules: new_ex = Rule.rule_replace(ex, (rule[0], rule[1]))[0] ex_list.append(new_ex) return rules, ex_list
def exec_expr(self, expr): final_expr = parse.subst_consts(expr, self.defines) try: head, args, kwargs = parse.parse_expr(final_expr) except Exception: raise ValueError('Syntax error') if head is None: return elif head == 'class': self.exec_class(args, kwargs) elif head == 'colormap': self.exec_colormap(args, kwargs) elif head == 'define': self.exec_define(args, kwargs) elif head == 'emoji': self.exec_emoji(args, kwargs) elif head == 'include': self.exec_include(args, kwargs) elif head == 'license': self.exec_license(args, kwargs) elif head == 'palette': self.exec_palette(args, kwargs) else: raise ValueError('Unknown expression type: ' + head)
def logic_simplify_expr_string(ex_str: str): try: ex = parse_expr(ex_str) except Exception: raise return logic_simplify(ex)
def equivalent_expr_string(ex_str_1: str, ex_str_2: str): ex1 = parse_expr(ex_str_1) ex2 = parse_expr(ex_str_2) return equivalent(ex1, ex2)
# parse_expr('~u => ~t'), # } # _conclusion = parse_expr('p') # _resolved, _solution = LogicInference.resolve(_premises, _conclusion) # print(_resolved) # print('===================') # for sol in _solution: # print(sol) # # from sympy.parsing.sympy_parser import * # try: # print(parse_expr('p => q | r')) # except Exception: # print('fail') if __name__ == '__main__': _premises = { parse_expr('r => (s | t)'), parse_expr('(~p | q) => r'), parse_expr('~s & ~u'), parse_expr('~u => ~t'), } _conclusion = parse_expr('p') _resolved, _solution = LogicInference.resolve(_premises, _conclusion) print(_resolved) print('===================') for sol in _solution: print(sol)
return False return True def convert_to_not(_expr: BooleanFunction) -> BooleanFunction: if _expr.func is Not: return _expr.args[0] else: return Not(_expr) def remove_double_not(_expr: BooleanFunction) -> BooleanFunction: if _expr.func is Not and _expr.args[0].func is Not: return _expr.args[0].args[0] else: return _expr def revert_not(_expr: BooleanFunction) -> BooleanFunction: if _expr.func is Not: return _expr.args[0] else: return Not(_expr) if __name__ == '__main__': _ex1 = parse_expr('(p & ~q) | (q & ~p)') _ex2 = parse_expr('(p | q) & (~p | ~q)') print(simpler_equal(_ex2, _ex1))
def generate_rules_from_str(ex_str: str) -> [(BooleanFunction, BooleanFunction)]: ex = parse_expr(ex_str) return Rule.generate_rules(ex)
def convert_string_to_rule(string: str) -> (BooleanFunction, BooleanFunction): rule_strings = string.split("<=>") left_rule_ex = parse_expr(rule_strings[0]) right_rule_ex = parse_expr(rule_strings[1]) return left_rule_ex, right_rule_ex