Esempio n. 1
0
    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)
Esempio n. 2
0
 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
Esempio n. 3
0
 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)
Esempio n. 4
0
def logic_simplify_expr_string(ex_str: str):
    try:
        ex = parse_expr(ex_str)
    except Exception:
        raise
    return logic_simplify(ex)
Esempio n. 5
0
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)
Esempio n. 6
0
#     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)
Esempio n. 7
0
        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))
Esempio n. 8
0
 def generate_rules_from_str(ex_str: str) -> [(BooleanFunction, BooleanFunction)]:
     ex = parse_expr(ex_str)
     return Rule.generate_rules(ex)
Esempio n. 9
0
    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