def test_add_duplicate_states(self): states = [State(1), State(2)] sm = StateMachine() sm.add_state(states[0]) sm.add_state(states[1]) with self.assertRaises(DuplicateStateError): sm.add_state(states[0])
def test_add_explicit_states_and_transition(self): states = [State(1), State(2)] sm = StateMachine(states) c = Condition('.', 5, '=') sm.add_transition(states[0], states[1], [c]) self.assertEqual(len(sm), 2) self.assertEqual(len(sm._states[states[0]]), 1)
def test_add_implicit_states_and_transition(self): sm = StateMachine() c = Condition('.', 5, '=') state1 = State(1) state2 = State(2) sm.add_transition(state1, state2, [c]) self.assertEqual(len(sm), 2) self.assertEqual(len(sm._states[state1]), 1)
def test_add_states(self): states = [State(i) for i in range(3)] sm = StateMachine(states) self.assertEqual(len(sm._states), 3) for state in states: self.assertIn(state, sm) self.assertEqual(sm._states[state], set())
def test_dfa_allowed_transition(self): states = [State(1), State(2)] sm = DFA(states, initial_state=states[0]) c1 = Condition('.', 5, '=') c2 = Condition('.', 7, '>') sm.add_transition(states[0], states[1], [c1]) sm.add_transition(states[0], states[1], [c2]) sm.advance(5) self.assertEqual(sm.current_state, states[1]) sm.current_state = states[0] sm.advance(10) self.assertEqual(sm.current_state, states[1]) sm.current_state = states[0] sm.advance(6) self.assertEqual(sm.current_state, states[0])
def test_initial_state(self): states = [State(i) for i in range(3)] sm = StateMachine(states, initial_state=states[1]) self.assertEqual(sm.current_state, states[1])
from state_machine.StateToInput import StateToInput def return_termostat_input(messgae): try: input_from_termostat = input(messgae) float_temp = float(input_from_termostat) return float_temp except Exception as e: print(e) return 0 if __name__ == "__main__": t0_state = State( tag="T0", description= "HEAT: OFF and COOL: OFF; Tempreture is under the 25 degree.") t1_state = State( tag="T1", description= "HEAT: OFF and COOL: ON; Tempreture is above the 35 degree.", is_super_state=True) t2_state = State( tag="T2", description="HEAT: ON and COOL: OFF; Tempreture is under the 15 degree." ) states = [t0_state, t1_state, t2_state] i0_input = Input(unit="Centigrade", range_value=(35, 100)) i1_input = Input(unit="Centigrade", range_value=(15, 25)) i2_input = Input(unit="Centigrade", range_value=(-100, 15)) i3_input = Input(unit="Centigrade",
def test_is_transition_allowed(self): transition = Transition(State(1), State(2), [Condition('.', 1, '>'), Condition('.', 7, '<')]) self.assertTrue(transition.is_allowed(5)) self.assertFalse(transition.is_allowed(10)) self.assertFalse(transition.is_allowed(0))