def test_add_fsm_to_state_when_memory_is_empty(self): fsm = FSM(1) nested_fsm = FSM('1_1') fsm.add_fsm_to_memory(1, nested_fsm) print "test_add_fsm_to_state_when_memory_is_empty: %s" % fsm.memory self.assertEqual(fsm.memory.get(1), [nested_fsm])
def test_nested_transition_for_first_time(self): # Test set_up fsm = FSM(1) nested_fsm = FSM('1_1') fsm.memory = {1: [nested_fsm]} # build test data testData = self.get_test_fsm() fsm.add_nested_transition('a', 1, '1_1', '1_2', None, None) print "test_nested_transition_for_first_time: %s" % fsm.memory self.assertEqual(fsm.memory.get(1), [testData])
def test_get_normal_transition_when_there_is_no_match_but_such_transition_exist( self): fsm = FSM(1) #fsm.state_transitions = {('b', 1):(None, 2)} first_fsm = FSM('1_1') first_fsm.state_transitions = { ('a', '1_1'): (None, None, '1_2'), (fsm.END_PAR_TRANSITION, '1_2'): (None, None, 2) } fsm.memory = {2: [first_fsm]} (_, _, next_state) = fsm.get_transition('a', 2) print "test_get_normal_transition_when_there_is_no_match_but_such_transition_exist:%s" % fsm.memory self.assertEqual(fsm.memory, {2: []}) self.assertEqual(next_state, 2)
def test_add_transition_to_memory_when_new_transitionStack_is_started(self): fsm = FSM(1) fsm.memory = {1: [['a']], 2 : [['b', 'c'], ['d']]} fsm.add_transition_to_memory_old('e', 2, True, dummy_action) print fsm.memory self.assertEqual(fsm.memory.get(2), [['b', 'c'], ['d'], deque(['e'])]) self.assertEqual(fsm.memory.get(1), [['a']])
def __init__(self, parent= None): self.current_state = 1 self.fsm = FSM(self.current_state) self.processing_par_state = False self.start_new_par_branch = False self.state_gen = generate_ints() # Choice States self.choice_start_state = -1 self.choice_end_state = -1 # Recursion states self.recursions_states = {} self.parent = parentFSM def move_current_state(self, value = None): if value is None: self.current_state = self.state_gen.next() else: self.current_state = value return self.current_state def get_current_state(self): return self.current_state def add_transition(self, transition): elif self.current_fsm.parent is not None: self.fsm.add_nested_transition(transition, self.parent.get_current_state() self.get_current_state(), checkMessages, self.move_current_state()) else:
def test_get_transition_from_memory_when_there_is_a_match(self): fsm = FSM(1) first_fsm = self.get_test_fsm() fsm.memory = {1: [first_fsm]} (_, _, next_state) = fsm.get_transition('a', 1) self.assertEqual(next_state, 1) self.assertEqual(fsm.memory.get(1), [first_fsm])
def test_get_transition_from_memory_when_there_is_match(self): fsm = FSM(1) fsm.memory = {1: [['a']], 2 : [['b', 'c'], ['d']]} (action, next_state) = fsm.get_transition('b', '2') self.assertEqual(next_state,2) (action, next_state) = fsm.get_transition('d', 2) self.assertEqual(next_state,2)
def test_get_normal_transition_when_there_is_a_match(self): fsm = FSM(1) fsm.state_transitions = {('b', 1): (None, None, 2)} first_fsm = self.get_test_fsm() fsm.memory = {2: [first_fsm]} (_, _, next_state) = fsm.get_transition('b', 1) self.assertEqual(next_state, 2) self.assertEqual(fsm.memory.get(2), [first_fsm])
def test_add_fsm_to_state_that_is_already_in_memory(self): fsm = FSM(1) first_fsm = self.get_test_fsm() fsm.memory = {1: [first_fsm]} second_fsm = self.get_test_fsm() fsm.add_fsm_to_memory(1, second_fsm) print "test_add_fsm_to_state_that_is_already_in_memory%s" % fsm.memory self.assertEqual(len(fsm.memory), 1) self.assertEqual(fsm.memory.get(1), [first_fsm, second_fsm])
def test_get_transition_from_memory_when_there_is_match(self): fsm = FSM(1) fsm.memory = {1: [deque(['a'])], 2 : [deque(['b', 'c']), deque(['d'])]} (_, next_state) = fsm.get_transition_old('b', 2) self.assertEqual(next_state,2) self.assertEqual(fsm.memory, {1: [deque(['a'])], 2 : [deque(['c']), deque(['d'])]}) (_, next_state) = fsm.get_transition_old('d', 2) self.assertEqual(next_state,2)
def __init__(self, parent= None): self.state_gen = generate_ints() self.current_state = self.state_gen.next() if (parent is not None): self.parent = parent self.fsm = FSM(format_state_name(self.current_state, self.parent.get_current_state())) self.set_interrupt_transition = self.parent.set_interrupt_transition self.top_parent = parent.top_parent else: self.fsm = FSM(self.current_state) self.top_parent = self self.set_interrupt_transition = False self.start_new_par_branch = False # Choice States self.choice_start_state = [] self.choice_end_state = [] # Recursion states self.recursions_states = {} self.parent = parent
def test_add_fsm_to_state_that_is_not_in_memory_and_memory_is_not_empty( self): fsm = FSM(1) first_fsm = self.get_test_fsm() fsm.memory = {1: [first_fsm]} second_fsm = self.get_test_fsm() fsm.add_fsm_to_memory(2, second_fsm) print 'test_add_fsm_to_state_that_is_not_in_memory_and_memory_is_not_empty: %s' % fsm.memory self.assertEqual(len(fsm.memory), 2) self.assertEqual(fsm.memory.get(1), [first_fsm]) self.assertEqual(fsm.memory.get(1), [second_fsm])
def __init__(self, input, state=None, *args, **kwargs): if state is None: state = RecognizerSharedState() super(BuildFSM, self).__init__(input, state, *args, **kwargs) self.memory = [] self.current_state = 1 self.fsm = FSM(self.current_state) self.processing_par_state = False self.start_new_par_branch = False self.state_gen = generate_ints() # Choice States self.choice_start_state = -1 self.choice_end_state = -1 # Recursion states self.recursions_states = {}
def get_test_fsm(self): # build test data testData = FSM('1_1') testData.state_transitions = {('a', '1_1'): (None, None, '1_2')} return testData
def test_add_transition_to_memory_old_when_attach_to_existing_transitionStack(self): fsm = FSM(1) fsm.memory = {1: [['a']], 2 : [['b', 'c'], ['d']]} fsm.add_transition_to_memory_old('e', 2, False, dummy_action) print fsm.memory self.assertEqual(fsm.memory.get(2), [['b', 'c'], ['d', 'e']])
def test_add_transition_to_memory_for_first_time_when_newTransitionStack_set_to_False(self): fsm = FSM(1) fsm.add_transition_to_memory_old('a', 1, False, dummy_action) print fsm.memory self.assertEqual(fsm.memory.get(1), toDeque([['a']]))
def test_get_normal_transition_when_there_is_no_match(self): fsm = FSM(1) fsm.state_transitions = {('b', 1): (None, None, 2)} first_fsm = self.get_test_fsm() fsm.memory = {2: [first_fsm]} self.assertRaises(ExceptionFSM, fsm.get_transition, 'c', 1)