def _build_basic_sm(char): sm = StateMachine() sm.add_init_state(0) sm.add_finish_state(1) sm.add_catch_state(1) sm.add_transition(sm.init_state, sm.catch_state, char) return sm
def generate_simple_smc(): sm = StateMachine() sm.add_init_state(0) sm.add_finish_state(1) sm.add_catch_state(1) sm.add_transition(0, 1, 'a') return sm
def _build_and_sm(sm1, sm2): sm = StateMachine() sm.add_init_state(sm1.init_state) for transition in sm1.transitions: sm.add_transition(transition[0], transition[1], transition[2]) if sm1.catch_state > 0: sm2.renumber_states(sm1.catch_state - 1) else: sm2.renumber_states() for transition in sm2.transitions: sm.add_transition(transition[0], transition[1], transition[2]) sm.add_finish_state(sm2.catch_state) sm.add_catch_state(sm2.catch_state) return sm
def _build_zero_or_more_sm(sm1): sm = StateMachine() sm.add_init_state(0) sm1.renumber_states() sm1.add_transition(sm1.catch_state, sm1.init_state, BaseConfig.EPSILON) for transition in sm1.transitions: sm.add_transition(transition[0], transition[1], transition[2]) sm.add_finish_state(sm1.catch_state + 1) sm.add_catch_state(sm1.catch_state + 1) sm.add_transition(sm.init_state, sm1.init_state, BaseConfig.EPSILON) sm.add_transition(sm.init_state, sm.catch_state, BaseConfig.EPSILON) sm.add_transition(sm1.catch_state, sm.catch_state, BaseConfig.EPSILON) sm.states = sorted(sm.states) sm.transitions = sorted(sm.transitions) return sm
def _build_or_sm(sm1, sm2): sm = StateMachine() sm.add_init_state(0) sm1.renumber_states() sm2.renumber_states(from_number=sm1.catch_state) sm.add_transition(sm.init_state, sm1.init_state, BaseConfig.EPSILON) sm.add_transition(sm.init_state, sm2.init_state, BaseConfig.EPSILON) for transition in sm1.transitions: sm.add_transition(transition[0], transition[1], transition[2]) for transition in sm2.transitions: sm.add_transition(transition[0], transition[1], transition[2]) sm.add_finish_state(sm2.catch_state + 1) sm.add_catch_state(sm2.catch_state + 1) sm.add_transition(sm1.catch_state, sm.catch_state, BaseConfig.EPSILON) sm.add_transition(sm2.catch_state, sm.catch_state, BaseConfig.EPSILON) sm.states = sorted(sm.states) return sm
def test_add_catch_correct(self): smc = StateMachine() smc.add_catch_state(1) self.assertEqual(smc.catch_state, 1)
def test_add_catch_not_int(self): smc = StateMachine() with self.assertRaises(TypeError): smc.add_catch_state('a')
def test_add_catch_non_empty(self): smc = StateMachine() smc.add_catch_state(1) with self.assertRaises(ValueError): smc.add_catch_state(2)