test_StateMachine.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- Python -*-
3 
4 #
5 # \file test_StateMachine.py
6 # \brief State machine template class
7 # \date $Date: 2007/08/30$
8 # \author Shinji Kurihara
9 #
10 # Copyright (C) 2006
11 # Task-intelligence Research Group,
12 # Intelligent Systems Research Institute,
13 # National Institute of
14 # Advanced Industrial Science and Technology (AIST), Japan
15 # All rights reserved.
16 #
17 
18 import sys
19 sys.path.insert(1,"../")
20 
21 import unittest
22 
23 from StateMachine import *
24 import OpenRTM_aist
25 
26 
27 
28 class TestStateMachine(unittest.TestCase):
29 # state = [0,1,2]
30  state = [RTC.INACTIVE_STATE, RTC.ACTIVE_STATE, RTC.ERROR_STATE]
31 
32  def setUp(self):
33  self._sm = StateMachine(3)
34 
35  self._sm.setNOP(self.nullAction)
36 
37  self._sm.setEntryAction(RTC.ACTIVE_STATE, self.on_activated)
38  self._sm.setEntryAction(RTC.ERROR_STATE,self.on_aborting)
39  self._sm.setPreDoAction(RTC.ACTIVE_STATE, self.on_reset)
40  self._sm.setDoAction(RTC.ACTIVE_STATE, self.on_execute)
41  self._sm.setPostDoAction(RTC.ACTIVE_STATE, self.on_state_update)
42  self._sm.setExitAction(RTC.ACTIVE_STATE, self.on_deactivated)
43  self._sm.setExitAction(RTC.ERROR_STATE, self.on_reset)
44  self._sm.setTransitionAction(self.transition)
45 
46  self._sm.setListener(self)
47  st = StateHolder()
48  st.prev = RTC.INACTIVE_STATE
49  st.curr = RTC.INACTIVE_STATE
50  st.next = RTC.INACTIVE_STATE
51  self._sm.setStartState(st)
52  self._sm.goTo(RTC.INACTIVE_STATE)
53 
54  def tearDown(self):
55  OpenRTM_aist.Manager.instance().shutdownManager()
56  return
57 
58  def nullAction(self, st):
59  print "nullAction."
60  return True
61 
62  def on_activated(self, st):
63  print "on_activated."
64  return True
65 
66  def on_deactivated(self, st):
67  print "on_deactivated."
68  return True
69 
70  def on_aborting(self, st):
71  print "on_aborting."
72  return True
73 
74  def on_error(self, st):
75  print "on_error."
76  return True
77 
78  def on_reset(self, st):
79  print "on_reset."
80  return True
81 
82  def on_execute(self, st):
83  print "on_execute."
84  return True
85 
86  def on_state_update(self, st):
87  print "on_state_update."
88  return True
89 
90  def transition(self, st):
91  print "transition."
92  return True
93 
94 
95  def test_setNOP(self):
96  self._sm.setNOP(self.nullAction)
97 
98 
99  def test_getStates(self):
100  self.assertEqual(self._sm.getStates().curr, RTC.INACTIVE_STATE)
101  self.assertEqual(self._sm.getStates().prev, RTC.INACTIVE_STATE)
102  self.assertEqual(self._sm.getStates().next, RTC.INACTIVE_STATE)
103 
104  st = StateHolder()
105  st.prev = RTC.ERROR_STATE
106  st.curr = RTC.ERROR_STATE
107  st.next = RTC.ERROR_STATE
108  self._sm.setStartState(st)
109 
110  self.assertEqual(self._sm.getStates().curr, RTC.ERROR_STATE)
111  self.assertEqual(self._sm.getStates().prev, RTC.ERROR_STATE)
112  self.assertEqual(self._sm.getStates().next, RTC.ERROR_STATE)
113 
114  def test_getState(self):
115  self.assertEqual(self._sm.getState(), RTC.INACTIVE_STATE)
116 
117  def test_isIn(self):
118  self.assertEqual(self._sm.isIn(RTC.INACTIVE_STATE), True)
119 
120  def test_goTo(self):
121  self._sm.goTo(RTC.INACTIVE_STATE)
122 
123 # def test_worker(self):
124 # self._sm.goTo(RTC.ACTIVE_STATE)
125 # self.assertEqual(self._sm.worker(), True)
126 # self.assertEqual(self._sm.worker(), True)
127 # self._sm.goTo(RTC.INACTIVE_STATE)
128 # self.assertEqual(self._sm.worker(), True)
129 # self.assertEqual(self._sm.worker(), True)
130 
131 
132 
133 if __name__ == '__main__':
134  unittest.main()
def on_error(self, st)
def on_aborting(self, st)
def on_state_update(self, st)
def on_activated(self, st)
def on_deactivated(self, st)
def on_execute(self, st)
def transition(self, st)
def on_reset(self, st)
def nullAction(self, st)


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Mon Feb 28 2022 23:01:07