test_StateMachine.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- Python -*-
00003 
00004 #
00005 # \file test_StateMachine.py
00006 # \brief State machine template class
00007 # \date $Date: 2007/08/30$
00008 # \author Shinji Kurihara
00009 #
00010 # Copyright (C) 2006
00011 #     Task-intelligence Research Group,
00012 #     Intelligent Systems Research Institute,
00013 #     National Institute of
00014 #         Advanced Industrial Science and Technology (AIST), Japan
00015 #     All rights reserved.
00016 #
00017 
00018 import sys
00019 sys.path.insert(1,"../")
00020 
00021 import unittest
00022 
00023 from StateMachine import *
00024 import OpenRTM_aist
00025 
00026     
00027 
00028 class TestStateMachine(unittest.TestCase):
00029 # state = [0,1,2]
00030   state = [RTC.INACTIVE_STATE, RTC.ACTIVE_STATE, RTC.ERROR_STATE]
00031 
00032   def setUp(self):
00033     self._sm = StateMachine(3)
00034 
00035     self._sm.setNOP(self.nullAction)
00036     
00037     self._sm.setEntryAction(RTC.ACTIVE_STATE, self.on_activated)
00038     self._sm.setEntryAction(RTC.ERROR_STATE,self.on_aborting)
00039     self._sm.setPreDoAction(RTC.ACTIVE_STATE, self.on_reset)
00040     self._sm.setDoAction(RTC.ACTIVE_STATE, self.on_execute)
00041     self._sm.setPostDoAction(RTC.ACTIVE_STATE, self.on_state_update)
00042     self._sm.setExitAction(RTC.ACTIVE_STATE, self.on_deactivated)
00043     self._sm.setExitAction(RTC.ERROR_STATE, self.on_reset)
00044     self._sm.setTransitionAction(self.transition)
00045 
00046     self._sm.setListener(self)
00047     st = StateHolder()
00048     st.prev = RTC.INACTIVE_STATE
00049     st.curr = RTC.INACTIVE_STATE
00050     st.next = RTC.INACTIVE_STATE
00051     self._sm.setStartState(st)
00052     self._sm.goTo(RTC.INACTIVE_STATE)
00053     
00054   def tearDown(self):
00055     OpenRTM_aist.Manager.instance().shutdownManager()
00056     return
00057 
00058   def nullAction(self, st):
00059     print "nullAction."
00060     return True
00061 
00062   def on_activated(self, st):
00063     print "on_activated."
00064     return True
00065 
00066   def on_deactivated(self, st):
00067     print "on_deactivated."
00068     return True
00069 
00070   def on_aborting(self, st):
00071     print "on_aborting."
00072     return True
00073 
00074   def on_error(self, st):
00075     print "on_error."
00076     return True
00077 
00078   def on_reset(self, st):
00079     print "on_reset."
00080     return True
00081 
00082   def on_execute(self, st):
00083     print "on_execute."
00084     return True
00085 
00086   def on_state_update(self, st):
00087     print "on_state_update."
00088     return True
00089 
00090   def transition(self, st):
00091     print "transition."
00092     return True
00093 
00094 
00095   def test_setNOP(self):
00096     self._sm.setNOP(self.nullAction)
00097   
00098   
00099   def test_getStates(self):
00100     self.assertEqual(self._sm.getStates().curr, RTC.INACTIVE_STATE)
00101     self.assertEqual(self._sm.getStates().prev, RTC.INACTIVE_STATE)
00102     self.assertEqual(self._sm.getStates().next, RTC.INACTIVE_STATE)
00103 
00104     st = StateHolder()
00105     st.prev = RTC.ERROR_STATE
00106     st.curr = RTC.ERROR_STATE
00107     st.next = RTC.ERROR_STATE
00108     self._sm.setStartState(st)
00109 
00110     self.assertEqual(self._sm.getStates().curr, RTC.ERROR_STATE)
00111     self.assertEqual(self._sm.getStates().prev, RTC.ERROR_STATE)
00112     self.assertEqual(self._sm.getStates().next, RTC.ERROR_STATE)
00113     
00114   def test_getState(self):
00115     self.assertEqual(self._sm.getState(), RTC.INACTIVE_STATE)
00116   
00117   def test_isIn(self):
00118     self.assertEqual(self._sm.isIn(RTC.INACTIVE_STATE), True)
00119   
00120   def test_goTo(self):
00121     self._sm.goTo(RTC.INACTIVE_STATE)
00122   
00123 #  def test_worker(self):
00124 #   self._sm.goTo(RTC.ACTIVE_STATE)
00125 #   self.assertEqual(self._sm.worker(), True)
00126 #   self.assertEqual(self._sm.worker(), True)
00127 #   self._sm.goTo(RTC.INACTIVE_STATE)
00128 #   self.assertEqual(self._sm.worker(), True)
00129 #   self.assertEqual(self._sm.worker(), True)
00130   
00131 
00132 ############### test #################
00133 if __name__ == '__main__':
00134         unittest.main()


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Aug 27 2015 14:17:28