Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
JDAQCHSM.chsm
Go to the documentation of this file.
1// ; -*- mode: C++; -*-
2
3#include "chsm.h"
4
5#include <string>
6#include <sstream>
7
8
9/**
10 * DAQ state machine interface.
11 */
12class JDAQCHSM :
13 public CHSM::machine
14{
15public:
16 /**
17 * DAQ state class.
18 *
19 * The member method enter() makes a call to the virtual method JDAQCHSM::enterState(...),
20 * where:
21 * - first argument is the state entered;
22 * - second argument is the event that caused the transition;
23 *
24 * This method should be overwritten by the run control client to reply to the run control.
25 */
26 class JDAQState :
27 public CHSM::state
28 {
29 public:
30 /**
31 * Default CHSM constructor.
32 */
33 JDAQState(CHSM_STATE_ARGS) :
34 CHSM::state(CHSM_STATE_INIT)
35 {}
36
37
38 /**
39 * Enter this state.
40 *
41 * \param event event that triggered transition
42 * \param from pointer to state from where transition started
43 * \return true if all OK; else false
44 */
45 bool enter(const CHSM::event& event, CHSM::state* from)
46 {
47 try {
48
49 dynamic_cast<JDAQCHSM&>(this->chsm()).enterState(*this, event);
50
51 return CHSM::state::enter(event, from);
52 }
53 catch(const std::exception& error) {
54 return false;
55 }
56 }
57 };
58
59
60 /**
61 * Constructor.
62 *
63 * \param __name name of state machine
64 */
65 JDAQCHSM(CHSM_MACHINE_ARGS, const std::string& __name) :
66 CHSM::machine(CHSM_MACHINE_INIT),
67 name (__name),
68 detector_id(-1),
69 run_number (-1)
70 {}
71
72
73 /**
74 * Get name of state machine.
75 *
76 * \return name
77 */
78 const std::string& getName() const
79 {
80 return name;
81 }
82
83
84 /**
85 * Get detector identifier.
86 *
87 * \return detector identifier.
88 */
89 int getDetectorID() const
90 {
91 return detector_id;
92 }
93
94
95 /**
96 * Get run number.
97 *
98 * \return run number
99 */
100 int getRunNumber() const
101 {
102 return run_number;
103 }
104
105
106 /**
107 * Interface methods for actions corresponding to state transitions.
108 */
109 virtual void actionEnter() {}
110 virtual void actionExit() {}
111
112 virtual void actionInit (int, const char*) {}
113 virtual void actionConfigure (int, const char*) {}
114 virtual void actionStart (int, const char*) {}
115 virtual void actionPause (int, const char*) {}
116 virtual void actionContinue (int, const char*) {}
117 virtual void actionStop (int, const char*) {}
118 virtual void actionReset (int, const char*) {}
119 virtual void actionQuit (int, const char*) {}
120
121 virtual void actionCheck(int, const char*) {}
122 virtual void actionInput(int, const char*) {}
123
124 virtual void actionError() {}
125 virtual void actionRecover(int, const char*) {}
126
127
128protected:
129 /**
130 * Action when entering state.
131 *
132 * \param state entered state
133 * \param event event that triggered transition
134 */
135 virtual void enterState(const CHSM::state& state, const CHSM::event& event) = 0;
136
137
138 /**
139 * Type definition of action method.
140 */
141 typedef void (JDAQCHSM::*action)(int, const char*);
142
143
144 /**
145 * The method to execute the action.
146 *
147 * This method shall be implemented in the derived class.
148 *
149 * \param __action pointer to action method
150 * \param __event event that triggered the action
151 */
152 virtual void execute(action __action, const CHSM::event& __event) = 0;
153
154 std::string name;
155 int detector_id;
156 int run_number;
157};
158
159
160%%
161
162chsm<JDAQCHSM> JDAQStateMachine(const std::string __name) {
163
164 upon enter %{ actionEnter(); %}
165 upon exit %{ actionExit(); %}
166
167} is {
168
169 /**
170 * Type definition of an event with additional data
171 */
172 event ev_daq(int length, const char* buffer);
173
174 event<ev_daq> ev_init;
175 event<ev_daq> ev_configure;
176 event<ev_daq> ev_start;
177 event<ev_daq> ev_pause;
178 event<ev_daq> ev_continue;
179 event<ev_daq> ev_stop;
180 event<ev_daq> ev_reset;
181 event<ev_daq> ev_quit;
182 event<ev_daq> ev_off;
183 event<ev_daq> ev_check;
184 event<ev_daq> ev_input;
185 event<ev_daq> ev_recover;
186
187
188 set Main(RunControl, Responder) is {
189
190 /**
191 * The run control cluster.
192 */
193 cluster RunControl(Operational, Error) is {
194
195 cluster Operational(Idle, Standby, Ready, Paused, Running) {
196
197 ev_error->Error %{ actionError(); %};
198
199 } is {
200
201 state<JDAQState> Idle {
202
203 ev_init->Standby %{ execute(&JDAQCHSM::actionInit, event); %};
204 ev_off %{ JDAQCHSM::exit(); %};
205 }
206
207 state<JDAQState> Standby {
208
209 ev_configure->Ready %{ execute(&JDAQCHSM::actionConfigure, event); %};
210 ev_reset->Idle %{ execute(&JDAQCHSM::actionReset, event); %};
211 }
212
213 state<JDAQState> Ready {
214
215 ev_start->Running %{
216
217 std::istringstream is(std::string(ev_start->buffer, ev_start->length));
218
219 is >> run_number >> detector_id;
220
221 execute(&JDAQCHSM::actionStart, event);
222
223 %};
224
225 ev_quit->Standby %{ execute(&JDAQCHSM::actionQuit, event); %};
226 }
227
228 state<JDAQState> Paused {
229
230 ev_continue->Running %{ execute(&JDAQCHSM::actionContinue, event); %};
231 ev_stop->Standby %{ execute(&JDAQCHSM::actionStop, event); %};
232 }
233
234 state<JDAQState> Running {
235
236 ev_pause->Paused %{ execute(&JDAQCHSM::actionPause, event); %};
237 }
238 }
239
240
241 state<JDAQState> Error {
242
243 ev_recover->Operational %{ execute(&JDAQCHSM::actionRecover, event); %};
244 ev_off %{ JDAQCHSM::exit(); %};
245 }
246 }
247
248 /**
249 * This state is used to repond to user commands that are not part of the run control state machine.
250 */
251 state Responder {
252
253 ev_check %{ execute(&JDAQCHSM::actionCheck, event); %};
254 ev_input %{ execute(&JDAQCHSM::actionInput, event); %};
255 }
256 }
257}
258
259%%
260
261
262