Example #1
0
    def test_base(self):
        obj = ObjTopState()
        st = obj.get_state()
        self.assertTrue(ObjStandbyState == st)
        obj.send_switch_active()

        runtime_actor.dispatch_all_msg()
        st = obj.get_state()
        self.assertTrue(ObjActiveState == st)

        obj.send_problem("WARNING")
        runtime_actor.dispatch_all_msg()
        st = obj.get_state()
        self.assertTrue(ObjStandbyState == st)

        obj.send_problem_resolved()
        runtime_actor.dispatch_all_msg()
        st = obj.get_state()
        self.assertTrue(ObjActiveState == st)

        obj.send_problem("Error")
        runtime_actor.dispatch_all_msg()
        st = obj.get_state()
        self.assertTrue(ObjNotActiveState == st)

        obj.send_raise("raise error")
        runtime_actor.dispatch_all_msg()
        st = obj.get_state()
        self.assertTrue(ObjErrorState == st)
        self.assertTrue(obj._error.message == "raise error")
Example #2
0
import hsm
from hsm import actor
from hsm import runtime

class Machine(actor.TopState):
    def _error(self):
	self.transition(ErrorState)
	
@actor.initial_state
class Off(Machine):
    def on_start(self):
	self.transition(On)
	
class On(Machine):
    def on_stop(self):
	    self.transition(Off) 
	    
class ErrorState(Machine):
    def on_error(self):
	print "Error"

if __name__ == '__main__':
    print "test simple fsm"
    mac = Machine()
    st = mac.get_state()
    assert(Off == st)
    mac.send_start()
    runtime.dispatch_all_msg()
    st = mac.get_state()
    assert(On == st)