Example #1
0
    def test_is_equivalent_to(self):
        # S -> a* b*
        rsa_1 = RecursiveAutomaton.from_regex(Regex("a* b*"), Symbol("S"))

        # S -> a+ b+
        rsa_2 = RecursiveAutomaton.from_regex(Regex("a a* b b*"), Symbol("S"))

        self.assertNotEqual(rsa_1, rsa_2)
Example #2
0
    def test_from_regex(self):
        # S -> a*
        rsa_2 = RecursiveAutomaton.from_regex(Regex("a*"), Symbol("S"))

        enfa = Regex("a*").to_epsilon_nfa()
        dfa = enfa.minimize()
        box = Box(dfa, Symbol("S"))
        rsa_1 = RecursiveAutomaton({Symbol("S")}, Symbol("S"), {box})

        self.assertEqual(rsa_2, rsa_1)
Example #3
0
def test_intersection_1():
    test_path = os.path.join(os.getcwd(), 'tests/data/test1')

    graph = BMGraph.from_edges_file(os.path.join(test_path, 'graph.txt'))
    regex = BMGraph.from_regex_file(os.path.join(test_path, 'regex.txt'))
    intersection = graph.intersect(regex)

    ans = intersection.to_automaton()
    a = Symbol('a')
    b = Symbol('b')

    assert ans.accepts([])
    assert ans.accepts([a])
    assert ans.accepts([a, a, a])
    assert not ans.accepts([b])
Example #4
0
    def __init__(self,
                 labels: AbstractSet[Symbol] = None,
                 initial_label: Symbol = None,
                 boxes: AbstractSet[Box] = None):

        if labels is not None:
            labels = {to_symbol(x) for x in labels}
        self._labels = labels or set()

        if initial_label is not None:
            initial_label = to_symbol(initial_label)
            if initial_label not in self._labels:
                self._labels.add(initial_label)
        self._initial_label = initial_label or Symbol("")

        self._boxes = dict()
        if boxes is not None:
            for box in boxes:
                self._boxes.update({to_symbol(box.label): box})
                self._labels.add(box.label)

        for label in self._labels:
            box = self.get_box(label)
            if box is None:
                raise ValueError(
                    "RSA must have the same number of labels and DFAs")
Example #5
0
    def __init__(self, enfa: EpsilonNFA = None, label: Symbol = None):
        if enfa is not None:
            enfa = enfa.minimize()
        self._dfa = enfa or EpsilonNFA()

        if label is not None:
            label = to_symbol(label)
        self._label = label or Symbol("")
Example #6
0
    def from_regex(cls, regex: str, initial_label: Symbol):
        """ Create a recursive automaton from regular expression

        Parameters
        -----------
        regex : str
            The regular expression
        initial_label : :class:`~pyformlang.finite_automaton.Symbol`
            A initial label for the recursive automaton

        Returns
        -----------
        rsa : :class:`~pyformlang.rsa.RecursiveAutomaton`
            The new recursive automaton
        """

        initial_label = Symbol(initial_label)
        box = Box(Regex(regex).to_epsilon_nfa(), initial_label)
        return RecursiveAutomaton({initial_label}, initial_label, {box})
Example #7
0
    def test_from_cfg(self):
        # g1: S -> a S b | a b
        rsa1_g1 = RecursiveAutomaton.from_cfg(
            CFG.from_text("S -> a S b | a b"))
        rsa2_g1 = RecursiveAutomaton.from_regex(Regex("a S b | a b"),
                                                Symbol("S"))

        self.assertEqual(rsa1_g1, rsa2_g1)

        # g2: S -> a V b
        #     V -> c S d | c d
        rsa1_g2 = RecursiveAutomaton.from_cfg(
            CFG.from_text("S -> a V b\nV -> c S d | c d"))
        self.assertEqual(rsa1_g2.get_number_of_boxes(), 2)
        self.assertEqual(rsa1_g2.labels, {Symbol("S"), Symbol("V")})

        dfa_S = Regex("a V b").to_epsilon_nfa().minimize()
        self.assertEqual(rsa1_g2.get_box(Symbol("S")), Box(dfa_S, Symbol("S")))

        dfa_V = Regex("c S d | c d").to_epsilon_nfa().minimize()
        self.assertEqual(rsa1_g2.get_box(Symbol("V")), Box(dfa_V, Symbol("V")))
Example #8
0
 def test_add_box(self):
     rsa_1 = RecursiveAutomaton.from_regex(Regex("a* b*"), Symbol("S"))
     new_box = Box(Regex("a*").to_epsilon_nfa().minimize(), Symbol("S"))
     rsa_1.add_box(new_box)
     self.assertEqual(new_box.dfa, rsa_1.get_box(Symbol("S")).dfa)
     self.assertEqual(rsa_1.labels, {Symbol("S")})
Example #9
0
    def test_creation(self):
        # S -> a S b | a b
        enfa = Regex("a S b | a b").to_epsilon_nfa()
        dfa = enfa.minimize()
        box = Box(dfa, Symbol("S"))
        rsa_1 = RecursiveAutomaton({Symbol("S")}, Symbol("S"), {box})

        self.assertEqual(rsa_1.get_number_of_boxes(), 1)
        self.assertEqual(box, rsa_1.get_box(Symbol("S")))
        self.assertEqual(rsa_1.labels, {Symbol("S")})
        self.assertEqual(rsa_1.initial_label, Symbol("S"))

        rsa_2 = RecursiveAutomaton()
        rsa_2.add_box(box)
        rsa_2.change_initial_label(Symbol("S"))

        self.assertEqual(rsa_2, rsa_1)

        # Checking to add a start label
        rsa_3 = RecursiveAutomaton(set(), Symbol("S"), {box})
        self.assertEqual(rsa_3.labels, {Symbol("S")})

        try:
            rsa_4 = RecursiveAutomaton({Symbol("S"), Symbol("v")}, Symbol("S"),
                                       {box})
        except ValueError:
            self.assertEqual(True, True)