コード例 #1
0
 def setup_method(self):
     """ setup any state tied to the execution of the given method in a
     class.  setup_method is invoked for every test method of a class.
     """
     states = ['q0', 'q1', 'q2', 'q3', 'q4']
     transitions = []
     transitions_description = [
         'q0 q0 a a R B a R B B S',
         'q0 q1 B B S B B L B B S',
         'q0 q2 b b R B B S B b R',
         'q1 q3 B B S B B S B B S',
         'q2 q2 b b R B B S B b R',
         'q2 q4 c c S B B L B B L',
         'q4 q4 c c R a a L b b L',
         'q4 q3 B B S B B S B B S'
     ]
     for description in transitions_description:
         splited_description = description.split()
         transition = Transition(splited_description[0], splited_description[1])
         for tape_part in zip(*(splited_description[2:][i::3] for i in range(3))):
             transition.add_tape_part(tape_part[0], tape_part[1], tape_part[2])
         transitions.append(transition)
     tapes = [
         Tape("B", ['a', 'b', 'c', 'B'], []),
         Tape("B", ['a', 'B'], []),
         Tape("B", ['b', 'B'], []),
     ]
     self.tm = TuringMachine(states, 'q0', ['q3'], 'B', transitions, tapes)
コード例 #2
0
def turing_machine(lines, cmdline_args):
    input_alphabet = lines[0].split()
    tape_alphabet = lines[1]
    whitespace = lines[2]
    states = lines[3].split()
    initial_state = lines[4]
    final_states = lines[5].split()
    number_of_tapes = lines[6]
    transitions = []
    for description in lines[7:]:
        splited_description = description.split()
        transition = TuringTransition(splited_description[0],
                                      splited_description[1])
        for tape_part in zip(*(splited_description[2:][i::3]
                               for i in range(3))):
            transition.add_tape_part(tape_part[0], tape_part[1], tape_part[2])
        transitions.append(transition)
    tapes = []
    for i in range(0, int(number_of_tapes)):
        tapes.append(Tape(whitespace, tape_alphabet, list(cmdline_args[i])))
    tm = TuringMachine(states, initial_state, final_states, whitespace,
                       transitions)
    initial_configurations = tm.get_initial_configurations(tapes)
    tm.load_configurations(initial_configurations)
    result = tm.run()
    if result == True:
        if tm.get_decision() == "Accept":
            print("Aceitou")
        else:
            print("Rejeitou")
    else:
        print("Não sei (nunca parou)")
コード例 #3
0
def test_is_transition_valid_with_invalid_transition():
    transition = Transition('A', 'B')
    transition.add_tape_part('x', 'y', 'L')
    transition.add_tape_part('y', 'x', 'R')
    tape1 = Tape("B", ['x', 'y'], ['x', 'y'])
    tape2 = Tape("B", ['x', 'y'], ['x', 'x'])
    tapes = [tape1, tape2]
    instance = Instance('A', tapes)
    assert not instance.is_transition_valid(transition)
コード例 #4
0
def test_new_tape_data():
    transition = Transition('A', 'B')
    transition.add_tape_part('x', 'y', 'L')
    transition.add_tape_part('y', 'x', 'R')
    new_tape_data = transition.get_new_tape_data()
    assert new_tape_data[0][0] == 'y'
    assert new_tape_data[0][1] == 'L'
    assert new_tape_data[1][0] == 'x'
    assert new_tape_data[1][1] == 'R'
コード例 #5
0
def test_apply_transition_invalid_transition():
    transition = Transition('A', 'B')
    transition.add_tape_part('a', 'b', 'L')
    transition.add_tape_part('x', 'y', 'R')
    tape1 = Tape("B", ['a', 'b', 'c'], ['a', 'c'])
    tape2 = Tape("B", ['x', 'y', 'z'], ['z', 'z'])
    tapes = [tape1, tape2]
    instance = Instance('A', tapes)
    new_instance = instance.apply_transition(transition)
    assert new_instance == None
コード例 #6
0
def test_new_transition():
    transition = Transition('A', 'B')
    transition.add_tape_part('x', 'y', 'L')
    transition.add_tape_part('y', 'x', 'R')
    assert transition.current_state == 'A'
    assert transition.new_state == 'B'
    assert len(transition.tape_data) == 2
    assert transition.tape_data[0].current_tape_symbol == 'x'
    assert transition.tape_data[0].new_tape_symbol == 'y'
    assert transition.tape_data[0].direction == 'L'
    assert transition.tape_data[1].current_tape_symbol == 'y'
    assert transition.tape_data[1].new_tape_symbol == 'x'
    assert transition.tape_data[1].direction == 'R'
コード例 #7
0
def test_apply_transition_valid_transition():
    transition = Transition('A', 'B')
    transition.add_tape_part('a', 'b', 'L')
    transition.add_tape_part('x', 'y', 'R')
    tape1 = Tape("B", ['a', 'b', 'c'], ['a', 'c'])
    tape2 = Tape("B", ['x', 'y', 'z'], ['x', 'z'])
    tapes = [tape1, tape2]
    instance = Instance('A', tapes)
    new_instance = instance.apply_transition(transition)
    assert new_instance.current_state == 'B'
    assert new_instance.tapes[0].content == ['B', 'b', 'c']
    assert new_instance.tapes[0].get_content() == 'B'
    assert new_instance.tapes[1].content == ['y', 'z']
    assert new_instance.tapes[1].get_content() == 'z'
コード例 #8
0
def test_get_valid_transitions():
    transition1 = Transition('A', 'B')
    transition1.add_tape_part('x', 'y', 'L')
    transition1.add_tape_part('y', 'x', 'R')
    transition2 = Transition('A', 'B')
    transition2.add_tape_part('x', 'y', 'L')
    transition2.add_tape_part('x', 'x', 'R')
    transitions = [transition1, transition2]
    tape1 = Tape("B", ['x', 'y'], ['x', 'y'])
    tape2 = Tape("B", ['x', 'y'], ['y', 'x'])
    tapes = [tape1, tape2]
    instance = Instance('A', tapes)
    valid_transitions = instance.get_valid_transitions(transitions)
    assert transition1 in valid_transitions
    assert transition2 not in valid_transitions
コード例 #9
0
def test_match_tape_symbol_with_tape_number():
    transition = Transition('A', 'B')
    transition.add_tape_part('x', 'y', 'L')
    transition.add_tape_part('y', 'x', 'R')
    assert transition.match_tape_symbol('x', 0) == True
    assert transition.match_tape_symbol('y', 0) == False
    assert transition.match_tape_symbol('x', 1) == False
    assert transition.match_tape_symbol('y', 1) == True
コード例 #10
0
    def __init__(self,
                 initial_state=None,
                 accepting_states=None,
                 blank_symbol=None,
                 tape_data="",
                 transition_data=None):
        self._current_state = initial_state
        self._accepting_states = accepting_states if accepting_states else []
        self._tape = Tape(tape_data, blank_symbol)
        self._transitions = TransitionTable()

        if transition_data:
            for transition_string in transition_data:
                self._transitions.add_transition(
                    Transition.from_data_string(transition_string))
コード例 #11
0
def test_get_direction():
    transition = Transition('A', 'B')
    transition.add_tape_part('x', 'y', 'L')
    transition.add_tape_part('y', 'x', 'R')
    assert transition.get_direction(0) == 'L'
    assert transition.get_direction(1) == 'R'
コード例 #12
0
def test_get_new_tape_symbol():
    transition = Transition('A', 'B')
    transition.add_tape_part('x', 'y', 'L')
    transition.add_tape_part('y', 'x', 'R')
    assert transition.get_new_tape_symbol(0) == 'y'
    assert transition.get_new_tape_symbol(1) == 'x'
コード例 #13
0
def test_match_invalid_state_invalid_tape_symbols():
    transition = Transition('A', 'B')
    transition.add_tape_part('x', 'y', 'L')
    transition.add_tape_part('y', 'x', 'R')
    assert transition.match('B', ['y', 'x']) == False
コード例 #14
0
def test_match_valid():
    transition = Transition('A', 'B')
    transition.add_tape_part('x', 'y', 'L')
    transition.add_tape_part('y', 'x', 'R')
    assert transition.match('A', ['x', 'y']) == True
コード例 #15
0
def test_match_state_err():
    transition = Transition('A', 'B')
    transition.add_tape_part('x', 'y', 'L')
    transition.add_tape_part('y', 'x', 'R')
    assert transition.match_state('B') == False