def test_process_re_constraint_1001(self, p1001): constr = p1001.parse_re_constraint(p1001.assertions[1]) re = constr['literal_1'] assert re.is_valid() res_str_1 = "(S+\\+\x19+r+T+\x17+b+R+I+C+\x18+P+/+\x1a)*" res_str_2 = "(\\\x18SCRIPT(S+\\+\x19+r+T+\x17+b+R+I+C+\x18+P+/+\x1a)*" res_re = awalipy.RatExp(res_str_1).mult(awalipy.RatExp(res_str_2)) assert are_re_equivalent(res_re, re)
def create_automata_constraints(constraints: Constraints) -> AutConstraints: """ Parse any constraints-like dictionary into automata constraints. Each entry in `constraints` is process independently and thus can be of a different type (even an automaton). Parameters ---------- constraints: Constraints Dict[str,RE] or Dict[str,Aut] Dictionary var → Union[RE,Aut] (where RE is either string or awalipy.RatExp) Returns ------- AutConstraint The automata representation of constraints """ res = {} for var, const in constraints.items(): if isinstance(const, Aut): res[var] = const continue if isinstance(const, str): const = awalipy.RatExp(const) if not isinstance(const, RE): raise TypeError("constraints must be a regular expression") # We use the Thompson's algorithm as the derivative-term-based one # often explodes with growing alphabet. res[var] = const.thompson().proper().minimal_automaton().trim() return res
def __init__(self, equation: StringEquation, constraints: Dict[str, str]): """ Parameters ---------- equation : StringEquation constraints : dict (equation.vars → aut) """ res = {var: awalipy.RatExp(c) for var, c in constraints.items()} super().__init__(equation, res)
def awalipy_allchar(alphabet: str) -> RE: """ Create awalipy RE for Σ given as a string of characters. Parameters ---------- alphabet: str Returns ------- RE for a+b+c+... """ all_str = '+'.join(alphabet) return awalipy.RatExp(all_str, alphabet=alphabet)
def acb0_constraints(self): alph = "abc" Σ = f"({'+'.join(alph)})" re_constraints = { "u": f"a{Σ}*", "v": f"{Σ}*c{Σ}*", "w": f"{Σ}{Σ}*b", "z": f"{Σ}*" } aut_constr = {} for var, re_str in re_constraints.items(): re = awalipy.RatExp(re_str, alphabet=alph) aut_constr[var] = re.exp_to_aut() return aut_constr
def create_awali_re(self, string): """ Create Awalipy RatExp recognizing `string`. Parameters ---------- string: str string term to be converted to Awali RE. Returns ------- RE Awalipy representation of RE string. """ return awalipy.RatExp(string, alphabet=self.alphabet_str)