Esempio n. 1
0
    def setUp(self):
        self.G = ATMGraph({'0'}, '|-', ' ', 'q1', lambda q: '∧'
                          if q == 'qaccept' else '∨')
        for i in range(1, 6):
            self.G.add_vertex('q' + str(i))
        self.G.add_vertex('qaccept')
        self.G.add_edge('q1', 'q1', ('|-', '|-', 'R'))
        self.G.add_edge('q1', 'q2', ('0', ' ', 'R'))
        self.G.add_edge('q2', 'qaccept', (' ', ' ', 'R'))
        self.G.add_edge('q2', 'q2', ('x', 'x', 'R'))
        self.G.add_edge('q2', 'q3', ('0', 'x', 'R'))
        self.G.add_edge('q3', 'q4', ('0', '0', 'R'))
        self.G.add_edge('q3', 'q3', ('x', 'x', 'R'))
        self.G.add_edge('q3', 'q5', (' ', ' ', 'L'))
        self.G.add_edge('q4', 'q4', ('x', 'x', 'R'))
        self.G.add_edge('q4', 'q3', ('0', 'x', 'R'))
        self.G.add_edge('q5', 'q5', ('x', 'x', 'L'))
        self.G.add_edge('q5', 'q5', ('0', '0', 'L'))
        self.G.add_edge('q5', 'q2', (' ', ' ', 'R'))

        self.H = ATMGraph({'0', '1'}, '|-', ' ', 'q1', lambda q: '∧'
                          if q == 'qaccept' else '∨')
        for i in range(1, 5):
            self.H.add_vertex('q' + str(i))
        self.H.add_vertex('qaccept')
        self.H.add_edge('q1', 'q1', ('|-', '|-', 'R'))
        self.H.add_edge('q1', 'q1', ('0', '0', 'R'))
        self.H.add_edge('q1', 'q1', ('1', '1', 'R'))
        self.H.add_edge('q1', 'q2', ('1', '1', 'R'))
        self.H.add_edge('q2', 'q3', ('0', '0', 'R'))
        self.H.add_edge('q2', 'q3', ('1', '1', 'R'))
        self.H.add_edge('q3', 'q4', ('0', '0', 'R'))
        self.H.add_edge('q3', 'q4', ('1', '1', 'R'))
        self.H.add_edge('q4', 'qaccept', (' ', ' ', 'R'))
Esempio n. 2
0
    def setUp(self):
        self.H = ATMGraph({'0', '1'}, '|-', ' ', 'q1', lambda q: '∧' if q == 'qaccept' else '∨')
        for i in range(1, 5):
            self.H.add_vertex('q' + str(i))
        self.H.add_vertex('qaccept')
        self.H.add_edge('q1', 'q1', ('|-', '|-', 'R'))
        self.H.add_edge('q1', 'q1', ('0', '0', 'R'))
        self.H.add_edge('q1', 'q1', ('1', '1', 'R'))
        self.H.add_edge('q1', 'q2', ('1', '1', 'R'))
        self.H.add_edge('q2', 'q3', ('0', '0', 'R'))
        self.H.add_edge('q2', 'q3', ('1', '1', 'R'))
        self.H.add_edge('q3', 'q4', ('0', '0', 'R'))
        self.H.add_edge('q4', 'qaccept', (' ', ' ', 'R'))

        def delta(q, a):
            transitions = {('q1', '|-'): [('q1', '|-', 'R')], ('q1', '0'): [('q1', '0', 'R')],
                           ('q1', '1'): [('q1', '1', 'R'), ('q2', '1', 'R')],
                           ('q2', '0'): [('q3', '0', 'R')], ('q2', '1'): [('q3', '1', 'R')],
                           ('q3', '0'): [('q4', '0', 'R')], ('q3', '1'): [('q4', '1', 'R')],
                           ('q4', ' '): [('qaccept', ' ', 'R')]}
            return [] if (q, a) not in transitions else transitions[(q, a)]

        def t(q):
            if q == 'qaccept':
                return '∧'
            else:
                return '∨'

        self.N = ATM({'q1', 'q2', 'q3', 'q4', 'qaccept'}, {'0', '1'}, {'0', '1', '|-', ' '}, '|-', ' ', delta, 'q1', t)
Esempio n. 3
0
    def setUp(self):
        self.M = Model()
        Q = {'q1,∨', 'q2,∨', 'q3,∨', 'q4,∨', 'q5,∨', 'qaccept,∧'}
        for q in Q:
            self.M.add_state(q)
        Sig = {'0'}
        for a in Sig:
            self.M.add_input_symbol(a)
        self.M.change_left_end('|-')
        self.M.change_blank(' ')
        self.M.change_start('q1')
        delta = {
            ('q1', 'q1'): {'|-->|-,R'},
            ('q1', 'q2'): {'0-> ,R'},
            ('q2', 'qaccept'): {' -> ,R'},
            ('q2', 'q2'): {'x->x,R'},
            ('q2', 'q3'): {'0->x,R'},
            ('q3', 'q4'): {'0->0,R'},
            ('q3', 'q3'): {'x->x,R'},
            ('q3', 'q5'): {' -> ,L'},
            ('q4', 'q4'): {'x->x,R'},
            ('q4', 'q3'): {'0->x,R'},
            ('q5', 'q5'): {'x->x,L', '0->0,L'},
            ('q5', 'q2'): {' -> ,R'}
        }
        for p, q in delta:
            for m in delta[(p, q)]:
                self.M.add_transition(p, q, m)

        self.G = ATMGraph({'0'}, '|-', ' ', 'q1', lambda q: '∧'
                          if q == 'qaccept' else '∨')
        for i in range(1, 6):
            self.G.add_vertex('q' + str(i))
        self.G.add_vertex('qaccept')
        self.G.add_edge('q1', 'q1', ('|-', '|-', 'R'))
        self.G.add_edge('q1', 'q2', ('0', ' ', 'R'))
        self.G.add_edge('q2', 'qaccept', (' ', ' ', 'R'))
        self.G.add_edge('q2', 'q2', ('x', 'x', 'R'))
        self.G.add_edge('q2', 'q3', ('0', 'x', 'R'))
        self.G.add_edge('q3', 'q4', ('0', '0', 'R'))
        self.G.add_edge('q3', 'q3', ('x', 'x', 'R'))
        self.G.add_edge('q3', 'q5', (' ', ' ', 'L'))
        self.G.add_edge('q4', 'q4', ('x', 'x', 'R'))
        self.G.add_edge('q4', 'q3', ('0', 'x', 'R'))
        self.G.add_edge('q5', 'q5', ('x', 'x', 'L'))
        self.G.add_edge('q5', 'q5', ('0', '0', 'L'))
        self.G.add_edge('q5', 'q2', (' ', ' ', 'R'))
Esempio n. 4
0
    def setUp(self):
        self.G = ATMGraph({'0'}, '|-', ' ', 'q1',
                          lambda q: '∧' if q == 'qaccept' else '∨')
        for i in range(1, 6):
            self.G.add_vertex('q' + str(i))
        self.G.add_vertex('qaccept')
        self.G.add_edge('q1', 'q1', ('|-', '|-', 'R'))
        self.G.add_edge('q1', 'q2', ('0', ' ', 'R'))
        self.G.add_edge('q2', 'qaccept', (' ', ' ', 'R'))
        self.G.add_edge('q2', 'q2', ('x', 'x', 'R'))
        self.G.add_edge('q2', 'q3', ('0', 'x', 'R'))
        self.G.add_edge('q3', 'q4', ('0', '0', 'R'))
        self.G.add_edge('q3', 'q3', ('x', 'x', 'R'))
        self.G.add_edge('q3', 'q5', (' ', ' ', 'L'))
        self.G.add_edge('q4', 'q4', ('x', 'x', 'R'))
        self.G.add_edge('q4', 'q3', ('0', 'x', 'R'))
        self.G.add_edge('q5', 'q5', ('x', 'x', 'L'))
        self.G.add_edge('q5', 'q5', ('0', '0', 'L'))
        self.G.add_edge('q5', 'q2', (' ', ' ', 'R'))

        Q = {'q1', 'q2', 'q3', 'q4', 'q5', 'qaccept'}
        Sigma = {'0'}
        Gamma = {' ', 'x', '0', '|-'}

        def delta(q, a):
            transitions = {('q1', '|-'): {('q1', '|-', 'R')}, ('q1', '0'): {('q2', ' ', 'R')},
                ('q2', ' '): {('qaccept', ' ', 'R')}, ('q2', 'x'): {('q2', 'x', 'R')}, ('q2', '0'): {('q3', 'x', 'R')},
                ('q3', '0'): {('q4', '0', 'R')}, ('q3', 'x'): {('q3', 'x', 'R')}, ('q3', ' '): {('q5', ' ', 'L')},
                ('q4', 'x'): {('q4', 'x', 'R')}, ('q4', '0'): {('q3', 'x', 'R')}, ('q5', 'x'): {('q5', 'x', 'L')},
                ('q5', '0'): {('q5', '0', 'L')}, ('q5', ' '): {('q2', ' ', 'R')}}
            return set() if (q, a) not in transitions else transitions[(q, a)]

        def t(q):
            if q == 'qaccept':
                return '∧'
            else:
                return '∨'

        self.M = ATM(Q, Sigma, Gamma, '|-', ' ', delta, 'q1', t)
Esempio n. 5
0
    def to_graph(M):
        G = ATMGraph(M.Sigma, M.left_end, M.blank, M.start, M.t)

        for q in M.Q:
            G.add_vertex(q)

        for q in M.Q:
            for a in M.Gamma:
                for p, b, d in M.delta(q, a):
                    G.add_edge(q, p, (a, b, d))

        return G
Esempio n. 6
0
    def from_graphics(states_and_types, Sigma, left_end, blank, start, delta):
        A = ATMGraph(Sigma, left_end, blank, start, None)
        types = {}
        for s in states_and_types:  # construct states and types
            q, t = s.rsplit(',', 1)
            types[q] = t
            A.add_vertex(q)

        def t(q):
            return types[q]

        A.t = t

        for p, q in delta:  # construct transitions
            for s in delta[(p, q)]:
                a, r = s.split("->", 1)
                b, d = r.rsplit(',', 1)
                A.add_edge(p, q, (a, b, d))

        return A
Esempio n. 7
0
 def __init__(self):
     self.G = ATMGraph(
     )  # the specialized multi-graph representation of the ATM
     self.t = dict()
Esempio n. 8
0
class Model:
    """
    Sets up the Model
    """
    def __init__(self):
        self.G = ATMGraph(
        )  # the specialized multi-graph representation of the ATM
        self.t = dict()

    """
    Simulates the ATM on an input with time and space bounds
    :param x a list representing the input string to the machine
    :param T the time bound 
    :param S the space bound
    :returns time, space, and graphical representation of the proof tree if the computation accepted in the required
    time and space bounds and None otherwise
    """

    def simulate(self, x, T=None, S=None):
        A = ATMConverter.from_graph(self.G)
        tree = AdvancedSimulator.simulate(A, x, T, S)
        if tree is not None:
            return tree.time(), tree.space(), TreeConverter.to_graphics(tree)
        return None

    """
    Constructs the graphical representation of the ATM
    :returns the graphical representation of the ATM as a networkx graph
    """

    def graphical_representation(self):
        return GraphConverter.to_graphics(self.G)

    """
    Changes the blank symbol of the ATM
    :param blank the new blank symbol
    :returns None iff the change was unsuccessful
    """

    def change_blank(self, blank):
        if blank is not None:
            self.G.blank = blank
        return blank

    """
    Changes the left end marker of the ATM
    :param left_end the new left end marker
    :returns None iff the change was unsuccessful
    """

    def change_left_end(self, left_end):
        if left_end is not None:
            self.G.left_end = left_end
        return left_end

    """
    Changes the start state of the ATM
    :param the new start state
    :returns the new start state if the change is successful else None
    """

    def change_start(self, start):
        if self.G.is_vertex(start):
            self.G.start = start
            return start
        return None

    """
    Adds a symbol to Sigma
    :param a the symbol to add
    """

    def add_input_symbol(self, a):
        self.G.Sigma |= {a}

    """
    Removes a symbol from Sigma
    :param a the symbol to be removed
    :returns a if successful and None otherwise
    """

    def remove_input_symbol(self, a):
        if a in self.G.Sigma:
            self.G.Sigma.remove(a)
            return a
        return None

    """
    Adds a state to the ATM with its type
    :param qty the new state concatenated with ',' concatenated with the type
    :returns q if adding the state was successful and None otherwise
    """

    def add_state(self, qty):
        q, ty = qty.rsplit(',', 1)
        if self.G.is_vertex(q) or (ty != '∨' and ty != '∧'):
            return None
        self.G.add_vertex(q)
        self.t[q] = ty
        self.G.t = lambda x: self.t[x]
        return q

    """
    Removes a state from the ATM
    :param q the state to be removed
    :returns q if the state was removed successfully and None otherwise
    """

    def remove_state(self, q):
        if self.G.is_vertex(q):
            self.G.remove_vertex(q)
            del self.t[q]
            return q
        return None

    """
    Adds a transition to the ATM
    :param p the current state
    :param q the next state
    :param abd string of form '(the symbol read)->(the symbol to write on the tape),('L' or 'R')'
    :returns p if the transition was added successfully and None otherwise
    """

    def add_transition(self, p, q, abd):
        if self.G.is_vertex(p) and self.G.is_vertex(q):
            a, bd = abd.split("->", 1)
            b, d = bd.rsplit(',', 1)
            if d == 'L' or d == 'R':
                self.G.add_edge(p, q, (a, b, d))
                return p
        return None

    """
    Removes a transition from the ATM
    :param p the current state
    :param q the next state
    :param abd string of form '(the symbol read)->(the symbol to write on the tape),('L' or 'R')'
    :returns p if the transition was removed successfully and None otherwise
    """

    def remove_transition(self, p, q, abd):
        a, bd = abd.split("->", 1)
        b, d = bd.rsplit(',', 1)
        if self.G.is_weight(p, q, (a, b, d)):
            self.G.remove_edge(p, q, (a, b, d))
            return p
        return None
Esempio n. 9
0
class ATMConverterDifferentTest(unittest.TestCase):
    def setUp(self):
        self.H = ATMGraph({'0', '1'}, '|-', ' ', 'q1', lambda q: '∧' if q == 'qaccept' else '∨')
        for i in range(1, 5):
            self.H.add_vertex('q' + str(i))
        self.H.add_vertex('qaccept')
        self.H.add_edge('q1', 'q1', ('|-', '|-', 'R'))
        self.H.add_edge('q1', 'q1', ('0', '0', 'R'))
        self.H.add_edge('q1', 'q1', ('1', '1', 'R'))
        self.H.add_edge('q1', 'q2', ('1', '1', 'R'))
        self.H.add_edge('q2', 'q3', ('0', '0', 'R'))
        self.H.add_edge('q2', 'q3', ('1', '1', 'R'))
        self.H.add_edge('q3', 'q4', ('0', '0', 'R'))
        self.H.add_edge('q4', 'qaccept', (' ', ' ', 'R'))

        def delta(q, a):
            transitions = {('q1', '|-'): [('q1', '|-', 'R')], ('q1', '0'): [('q1', '0', 'R')],
                           ('q1', '1'): [('q1', '1', 'R'), ('q2', '1', 'R')],
                           ('q2', '0'): [('q3', '0', 'R')], ('q2', '1'): [('q3', '1', 'R')],
                           ('q3', '0'): [('q4', '0', 'R')], ('q3', '1'): [('q4', '1', 'R')],
                           ('q4', ' '): [('qaccept', ' ', 'R')]}
            return [] if (q, a) not in transitions else transitions[(q, a)]

        def t(q):
            if q == 'qaccept':
                return '∧'
            else:
                return '∨'

        self.N = ATM({'q1', 'q2', 'q3', 'q4', 'qaccept'}, {'0', '1'}, {'0', '1', '|-', ' '}, '|-', ' ', delta, 'q1', t)

    def test_atm_to_graph(self):
        assert self.H != ATMConverter.to_graph(self.N)

    def test_graph_to_atm(self):
        assert self.N != ATMConverter.from_graph(self.H)
Esempio n. 10
0
class ATMConverterDeterministicTest(unittest.TestCase):
    def setUp(self):
        self.G = ATMGraph({'0'}, '|-', ' ', 'q1',
                          lambda q: '∧' if q == 'qaccept' else '∨')
        for i in range(1, 6):
            self.G.add_vertex('q' + str(i))
        self.G.add_vertex('qaccept')
        self.G.add_edge('q1', 'q1', ('|-', '|-', 'R'))
        self.G.add_edge('q1', 'q2', ('0', ' ', 'R'))
        self.G.add_edge('q2', 'qaccept', (' ', ' ', 'R'))
        self.G.add_edge('q2', 'q2', ('x', 'x', 'R'))
        self.G.add_edge('q2', 'q3', ('0', 'x', 'R'))
        self.G.add_edge('q3', 'q4', ('0', '0', 'R'))
        self.G.add_edge('q3', 'q3', ('x', 'x', 'R'))
        self.G.add_edge('q3', 'q5', (' ', ' ', 'L'))
        self.G.add_edge('q4', 'q4', ('x', 'x', 'R'))
        self.G.add_edge('q4', 'q3', ('0', 'x', 'R'))
        self.G.add_edge('q5', 'q5', ('x', 'x', 'L'))
        self.G.add_edge('q5', 'q5', ('0', '0', 'L'))
        self.G.add_edge('q5', 'q2', (' ', ' ', 'R'))

        Q = {'q1', 'q2', 'q3', 'q4', 'q5', 'qaccept'}
        Sigma = {'0'}
        Gamma = {' ', 'x', '0', '|-'}

        def delta(q, a):
            transitions = {('q1', '|-'): {('q1', '|-', 'R')}, ('q1', '0'): {('q2', ' ', 'R')},
                ('q2', ' '): {('qaccept', ' ', 'R')}, ('q2', 'x'): {('q2', 'x', 'R')}, ('q2', '0'): {('q3', 'x', 'R')},
                ('q3', '0'): {('q4', '0', 'R')}, ('q3', 'x'): {('q3', 'x', 'R')}, ('q3', ' '): {('q5', ' ', 'L')},
                ('q4', 'x'): {('q4', 'x', 'R')}, ('q4', '0'): {('q3', 'x', 'R')}, ('q5', 'x'): {('q5', 'x', 'L')},
                ('q5', '0'): {('q5', '0', 'L')}, ('q5', ' '): {('q2', ' ', 'R')}}
            return set() if (q, a) not in transitions else transitions[(q, a)]

        def t(q):
            if q == 'qaccept':
                return '∧'
            else:
                return '∨'

        self.M = ATM(Q, Sigma, Gamma, '|-', ' ', delta, 'q1', t)

    def test_atm_to_graph(self):
        assert self.G == ATMConverter.to_graph(self.M)

    def test_graph_to_atm(self):
        assert self.M == ATMConverter.from_graph(self.G)
Esempio n. 11
0
class ModelFunctionality(unittest.TestCase):
    def setUp(self):
        self.M = Model()
        Q = {'q1,∨', 'q2,∨', 'q3,∨', 'q4,∨', 'q5,∨', 'qaccept,∧'}
        for q in Q:
            self.M.add_state(q)
        Sig = {'0'}
        for a in Sig:
            self.M.add_input_symbol(a)
        self.M.change_left_end('|-')
        self.M.change_blank(' ')
        self.M.change_start('q1')
        delta = {
            ('q1', 'q1'): {'|-->|-,R'},
            ('q1', 'q2'): {'0-> ,R'},
            ('q2', 'qaccept'): {' -> ,R'},
            ('q2', 'q2'): {'x->x,R'},
            ('q2', 'q3'): {'0->x,R'},
            ('q3', 'q4'): {'0->0,R'},
            ('q3', 'q3'): {'x->x,R'},
            ('q3', 'q5'): {' -> ,L'},
            ('q4', 'q4'): {'x->x,R'},
            ('q4', 'q3'): {'0->x,R'},
            ('q5', 'q5'): {'x->x,L', '0->0,L'},
            ('q5', 'q2'): {' -> ,R'}
        }
        for p, q in delta:
            for m in delta[(p, q)]:
                self.M.add_transition(p, q, m)

        self.G = ATMGraph({'0'}, '|-', ' ', 'q1', lambda q: '∧'
                          if q == 'qaccept' else '∨')
        for i in range(1, 6):
            self.G.add_vertex('q' + str(i))
        self.G.add_vertex('qaccept')
        self.G.add_edge('q1', 'q1', ('|-', '|-', 'R'))
        self.G.add_edge('q1', 'q2', ('0', ' ', 'R'))
        self.G.add_edge('q2', 'qaccept', (' ', ' ', 'R'))
        self.G.add_edge('q2', 'q2', ('x', 'x', 'R'))
        self.G.add_edge('q2', 'q3', ('0', 'x', 'R'))
        self.G.add_edge('q3', 'q4', ('0', '0', 'R'))
        self.G.add_edge('q3', 'q3', ('x', 'x', 'R'))
        self.G.add_edge('q3', 'q5', (' ', ' ', 'L'))
        self.G.add_edge('q4', 'q4', ('x', 'x', 'R'))
        self.G.add_edge('q4', 'q3', ('0', 'x', 'R'))
        self.G.add_edge('q5', 'q5', ('x', 'x', 'L'))
        self.G.add_edge('q5', 'q5', ('0', '0', 'L'))
        self.G.add_edge('q5', 'q2', (' ', ' ', 'R'))

    def test_additions(self):
        assert self.G == self.M.G
        assert self.M.add_transition("r", "f", "j") is None
        assert self.M.add_state("q,r") is None
        assert self.M.change_start("reject") is None

    def test_graphical_representation(self):
        H = self.M.graphical_representation()
        F = to_pydot(H)
        F.write_png('graph1.png', prog='dot')

    def test_simulation(self):
        t, s, T = self.M.simulate(['0', '0', '0', '0'])
        assert t == 22 and s == 6
        F = to_pydot(T)
        F.write_png('tree.png', prog='dot')
        assert self.M.simulate(['0', '0', '0']) is None
        assert self.M.simulate(['0', '0', '0', '0'], 10, 6) is None
        assert self.M.simulate(['0', '0', '0', '0'], 22, 4) is None
Esempio n. 12
0
class TestGraphConverter(unittest.TestCase):
    def setUp(self):
        self.G = ATMGraph({'0'}, '|-', ' ', 'q1', lambda q: '∧'
                          if q == 'qaccept' else '∨')
        for i in range(1, 6):
            self.G.add_vertex('q' + str(i))
        self.G.add_vertex('qaccept')
        self.G.add_edge('q1', 'q1', ('|-', '|-', 'R'))
        self.G.add_edge('q1', 'q2', ('0', ' ', 'R'))
        self.G.add_edge('q2', 'qaccept', (' ', ' ', 'R'))
        self.G.add_edge('q2', 'q2', ('x', 'x', 'R'))
        self.G.add_edge('q2', 'q3', ('0', 'x', 'R'))
        self.G.add_edge('q3', 'q4', ('0', '0', 'R'))
        self.G.add_edge('q3', 'q3', ('x', 'x', 'R'))
        self.G.add_edge('q3', 'q5', (' ', ' ', 'L'))
        self.G.add_edge('q4', 'q4', ('x', 'x', 'R'))
        self.G.add_edge('q4', 'q3', ('0', 'x', 'R'))
        self.G.add_edge('q5', 'q5', ('x', 'x', 'L'))
        self.G.add_edge('q5', 'q5', ('0', '0', 'L'))
        self.G.add_edge('q5', 'q2', (' ', ' ', 'R'))

        self.H = ATMGraph({'0', '1'}, '|-', ' ', 'q1', lambda q: '∧'
                          if q == 'qaccept' else '∨')
        for i in range(1, 5):
            self.H.add_vertex('q' + str(i))
        self.H.add_vertex('qaccept')
        self.H.add_edge('q1', 'q1', ('|-', '|-', 'R'))
        self.H.add_edge('q1', 'q1', ('0', '0', 'R'))
        self.H.add_edge('q1', 'q1', ('1', '1', 'R'))
        self.H.add_edge('q1', 'q2', ('1', '1', 'R'))
        self.H.add_edge('q2', 'q3', ('0', '0', 'R'))
        self.H.add_edge('q2', 'q3', ('1', '1', 'R'))
        self.H.add_edge('q3', 'q4', ('0', '0', 'R'))
        self.H.add_edge('q3', 'q4', ('1', '1', 'R'))
        self.H.add_edge('q4', 'qaccept', (' ', ' ', 'R'))

    def test_to_graphics(self):
        H = GraphConverter.to_graphics(self.G)
        F = to_pydot(H)
        F.write_png('graph1.png', prog='dot')
        H = GraphConverter.to_graphics(self.H)
        F = to_pydot(H)
        F.write_png('graph2.png', prog='dot')

    def test_from_graphics(self):
        states_and_types = {
            'q1,∨', 'q2,∨', 'q3,∨', 'q4,∨', 'q5,∨', 'qaccept,∧'
        }
        Sigma = {'0'}
        left_end = '|-'
        blank = ' '
        start = 'q1'
        delta = {
            ('q1', 'q1'): {'|-->|-,R'},
            ('q1', 'q2'): {'0-> ,R'},
            ('q2', 'qaccept'): {' -> ,R'},
            ('q2', 'q2'): {'x->x,R'},
            ('q2', 'q3'): {'0->x,R'},
            ('q3', 'q4'): {'0->0,R'},
            ('q3', 'q3'): {'x->x,R'},
            ('q3', 'q5'): {' -> ,L'},
            ('q4', 'q4'): {'x->x,R'},
            ('q4', 'q3'): {'0->x,R'},
            ('q5', 'q5'): {'x->x,L', '0->0,L'},
            ('q5', 'q2'): {' -> ,R'}
        }
        H = GraphConverter.from_graphics(states_and_types, Sigma, left_end,
                                         blank, start, delta)
        assert H == self.G
Esempio n. 13
0
class ATMGraphDeterministicTest(unittest.TestCase):
    def setUp(self):
        self.G = ATMGraph({'0'}, '|-', ' ', 'q1',
                          lambda q: '∧' if q == 'qaccept' else '∨')
        for i in range(1, 6):
            self.G.add_vertex('q' + str(i))
        self.G.add_vertex('qaccept')
        self.G.add_edge('q1', 'q1', ('|-', '|-', 'R'))
        self.G.add_edge('q1', 'q2', ('0', ' ', 'R'))
        self.G.add_edge('q2', 'qaccept', (' ', ' ', 'R'))
        self.G.add_edge('q2', 'q2', ('x', 'x', 'R'))
        self.G.add_edge('q2', 'q3', ('0', 'x', 'R'))
        self.G.add_edge('q3', 'q4', ('0', '0', 'R'))
        self.G.add_edge('q3', 'q3', ('x', 'x', 'R'))
        self.G.add_edge('q3', 'q5', (' ', ' ', 'L'))
        self.G.add_edge('q4', 'q4', ('x', 'x', 'R'))
        self.G.add_edge('q4', 'q3', ('0', 'x', 'R'))
        self.G.add_edge('q5', 'q5', ('x', 'x', 'L'))
        self.G.add_edge('q5', 'q5', ('0', '0', 'L'))
        self.G.add_edge('q5', 'q2', (' ', ' ', 'R'))

    def test_vertices(self):
        for i in range(1, 6):
            assert self.G.is_vertex('q' + str(i))
        assert self.G.is_vertex('qaccept')

    def test_edges(self):
        self.assertEqual({('x', 'x', 'L'), ('0', '0', 'L')}, set(self.G.weights('q5', 'q5')))
        self.assertEqual({('0', ' ', 'R')}, set(self.G.weights('q1', 'q2')))

    def test_explicit_attributes(self):
        self.assertEqual({'0'}, self.G.Sigma)
        self.assertEqual('|-', self.G.left_end)
        self.assertEqual(' ', self.G.blank)
        self.assertEqual('q1', self.G.start)
        for q in self.G.vertices():
            if q == 'qaccept':
                self.assertEqual('∧', self.G.t(q))
            else:
                self.assertEqual('∨', self.G.t(q))

    def test_equality(self):
        assert self.G == self.G
Esempio n. 14
0
class ATMGraphNonDeterministicTest(unittest.TestCase):
    def setUp(self):
        self.H = ATMGraph({'0', '1'}, '|-', ' ', 'q1', lambda q: '∧' if q == 'qaccept' else '∨')
        for i in range(1, 5):
            self.H.add_vertex('q' + str(i))
        self.H.add_vertex('qaccept')
        self.H.add_edge('q1', 'q1', ('|-', '|-', 'R'))
        self.H.add_edge('q1', 'q1', ('0', '0', 'R'))
        self.H.add_edge('q1', 'q1', ('1', '1', 'R'))
        self.H.add_edge('q1', 'q2', ('1', '1', 'R'))
        self.H.add_edge('q2', 'q3', ('0', '0', 'R'))
        self.H.add_edge('q2', 'q3', ('1', '1', 'R'))
        self.H.add_edge('q3', 'q4', ('0', '0', 'R'))
        self.H.add_edge('q3', 'q4', ('1', '1', 'R'))
        self.H.add_edge('q4', 'qaccept', (' ', ' ', 'R'))

    def test_vertices(self):
        self.assertEqual({'q1', 'q2', 'q3', 'q4', 'qaccept'}, set(self.H.vertices()))

    def test_edges(self):
        self.assertEqual({('q1', 'q1'), ('q1', 'q2'), ('q2', 'q3'), ('q3', 'q4'), ('q4', 'qaccept')}, set(self.H.edges()))
        self.assertEqual({('|-', '|-', 'R'), ('0', '0', 'R'), ('1', '1', 'R')}, set(self.H.weights('q1', 'q1')))
        self.assertEqual({('0', '0', 'R'), ('1', '1', 'R')}, set(self.H.weights('q3', 'q4')))

    def test_explicit_attributes(self):
        self.assertEqual({'0', '1'}, self.H.Sigma)
        self.assertEqual('|-', self.H.left_end)
        self.assertEqual(' ', self.H.blank)
        self.assertEqual('q1', self.H.start)
        for q in self.H.vertices():
            if q == 'qaccept':
                self.assertEqual('∧', self.H.t(q))
            else:
                self.assertEqual('∨', self.H.t(q))