sp_executor.h
Go to the documentation of this file.
1 /** @file sp_executor.h Execute transitions in a timed generator */
2 
3 
4 /*
5  FAU Discrete Event Systems Library (libfaudes)
6 
7  Copyright (C) 2007 Ruediger Berndt
8  Copyright (C) 2007 Thomas Moor
9  Exclusive copyright is granted to Klaus Schmidt
10 
11 
12 */
13 
14 
15 
16 #ifndef FAUDES_SP_EXECUTOR_H
17 #define FAUDES_SP_EXECUTOR_H
18 
19 #include "corefaudes.h"
20 #include "tp_include.h"
21 
22 
23 // debugging: executor level
24 #ifdef FAUDES_DEBUG_EXECUTOR
25 #define FD_DX(message) FAUDES_WRITE_CONSOLE("FAUDES_EXECUTOR: " << message)
26 #else
27 #define FD_DX(message)
28 #endif
29 
30 // debugging: proposing executor level
31 #ifdef FAUDES_DEBUG_SIMULATOR
32 #define FD_DS(message) FAUDES_WRITE_CONSOLE("FAUDES_SIMULATOR: " << message)
33 #else
34 #define FD_DS(message)
35 #endif
36 
37 // debugging: device synchronization level
38 #ifdef FAUDES_DEBUG_SIMULATOR_SYNC
39 #define FD_DS_SYNC(message) FAUDES_WRITE_CONSOLE("FAUDES_RUNSYNC: " << message)
40 #else
41 #define FD_DS_SYNC(message)
42 #endif
43 
44 
45 
46 namespace faudes {
47 
48 
49 /** Global Tyoedefs */
50 
51 
52 /** typedef for external trace */
54 public:
57 };
58 
59 /**
60  * An Executor is a timed generator that maintains a current state.
61  *
62  * The Executor is the lowest level building block for the libFAUDES simulator plugin.
63  * It maintains a current state that consists of
64  * - the discrete state;
65  * - a value map that assigns values to clock variables;
66  * - the current step aka logical time;
67  * - the current global clock aka clock time; as with faudes timed automata,
68  * clock time is in faudes-time units which may be mapped to physical time by a fixed
69  * conversion factor; see also DeviceExecutor.
70  * The state can be retrieved by the method Executor::CurrentState(void),
71  * and it is updated whenever time or transitions are executed via Executor::ExecuteTime(Time::Type)
72  * or Executor::ExecuteEvent(Idx). If you plan to execute multiple generators with synchronized shared
73  * events, you will find the class ParallelExecutor with a very similar interface more appropriate.
74  *
75  * The Executor provides Methods that indicate which transitions currently are enabled, referring to the
76  * current state, the current clock values and the guard and invariant data from the TimedGenerator.
77  * In contrast to enabled events, there is also the notion of active events, which refers to
78  * the discrete state only, ignoring guards and invariants.
79  *
80  * Technically, the Executor class is derived from the TimedGenerator, however inheritence is private.
81  * The common way to initialise an Executor object is to construct it from a const ref to a TimedGenerator,
82  * which sets up additional internal data structures. Read-only access to the underlying generator
83  * is given by the faudes::Executor::Generator() method.
84  *
85  * Note: a prequisit of the current implementation is that the underlying generator
86  * is deterministic ie the current discrete state is uniquely determind by the external
87  * sequence of discrete events.
88  *
89  *
90  * @ingroup SimulatorPlugin
91  */
92 
93 class FAUDES_API Executor : protected TimedGenerator {
94 
96 
97  public:
98 
99 
100  /** Typedef for timed state */
101  typedef struct {
102  Idx State; // discrete state
103  std::map<Idx,Time::Type> ClockValue; // map clockindex to value
104  } TimedState;
105 
106 
107  /**
108  * Creates an emtpy Executer
109  */
110  Executor(void);
111 
112  /**
113  * Construct from tgenerator.
114  *
115  * Construction from a TimedGenerator will copy all given TimedGenerator data and complie it to
116  * some additional Executor specific data. Thus, if the original TimedGenerator changes
117  * the Executor will not reflect these changes.
118  *
119  * @param rGen
120  * Input generator
121  *
122  * @exception Exception
123  * - Nondetrministic input generator (id 501)
124  */
125  Executor(const TimedGenerator& rGen);
126 
127  /**
128  * Construct from TimedGenerator file.
129  *
130  * @param rFileName
131  * File to read
132  *
133  * @exception Exception
134  * - IO errors (id 1)
135  * - token mismatch (id 50, 51, 52, 80, 85)
136  * - nondetrministic generator (id 501)
137  */
138  Executor(const std::string& rFileName);
139 
140  /**
141  * Initialise from TimedGenerator.
142  *
143  * @param rGen
144  * Input generator
145  */
146  void Generator(const TimedGenerator& rGen);
147 
148 
149  /**
150  * Reference to the internal generator for inspection
151  *
152  * @return
153  * const reference of mTGen
154  */
155  const TimedGenerator& Generator(void) const;
156 
157  /**
158  * Generator name (for convenience)
159  *
160  */
161  const std::string& Name(void) const {return TimedGenerator::Name();} ;
162 
163  /**
164  * State name (for convenience)
165  *
166  */
167  std::string StateName(Idx idx) const {return TimedGenerator::StateName(idx);} ;
168 
169 
170  /**
171  * Event name (for convenience)
172  *
173  */
174  std::string EventName(Idx idx) const {return TimedGenerator::EventName(idx);} ;
175 
176 
177  /**
178  * Clear all data incl TimedGenerator
179  *
180  */
181  void Clear(void);
182 
183 
184  /**
185  * Reset all clocks and assign initial state.
186  */
187  void Reset();
188 
189  /**
190  * Get maximal interval of time that can pass without executing an event.
191  * This corresponds to the inveriant of the current timed state.
192  *
193  * @return TimeInterval
194  *
195  */
196  const TimeInterval& EnabledTime() const;
197 
198  /**
199  * Get set of events that are enabled at current (timed) state
200  *
201  * @return
202  * Set of enabled events
203  */
204  const EventSet& EnabledEvents() const;
205 
206  /**
207  * Get set of events that are disabled at current (timed) state
208  *
209  * @return
210  * Set of disabled events
211  */
212  const EventSet& DisabledEvents() const;
213 
214  /**
215  * Get maximal interval on which set of enabled events is constant
216  *
217  * @return TimeInterval
218  *
219  */
220  const TimeInterval& EnabledInterval() const;
221 
222 
223  /**
224  * Get interval on which an active event is enabled.
225  *
226  * @param event
227  * Active event
228  *
229  * @return TimeInterval
230  * (empty if event not active or active with guard violated)
231  *
232  */
233  TimeInterval EnabledEventTime(Idx event) const;
234 
235 
236  /**
237  * Get interval on which an active event satisfies its guard.
238  *
239  * @param event
240  * Active event
241  *
242  * @return TimeInterval
243  * (empty if event not active or active with guard violated)
244  *
245  */
246  TimeInterval EnabledGuardTime(Idx event) const;
247 
248 
249  /**
250  * Get set of events that are active at current (untimed) state.
251  *
252  * @return
253  * Set of active events
254  */
255  const EventSet& ActiveEventSet(void) const;
256 
257  /**
258  * Get set of transitions that are active at current (untimed) state
259  *
260  * @return
261  * Set of active trans
262  */
263  const TransSet& ActiveTransSet(void) const;
264 
265  /**
266  * Let time pass. Returns false if the specified amount
267  * of time cannot elapse without an event being executed
268  *
269  * @param time
270  * Amount of time that shall elapse.
271  * @return
272  * True/false -- success
273  */
274  bool ExecuteTime(Time::Type time);
275 
276  /**
277  * Execute transition. Returns false if the transition
278  * is not enabled and hence cannot be executed at the current time
279  *
280  * @param event
281  * Indicate transition to execute
282  * @return
283  * True/false -- success
284  */
285  bool ExecuteEvent(Idx event);
286 
287  /**
288  * Set timed state. Returns false if state or clock values are invalid.
289  *
290  * @param tstate
291  * State to set.
292  *
293  * @return
294  * True/false -- success
295  */
296  bool CurrentTimedState(const TimedState& tstate);
297 
298  /**
299  * Get timed state.
300  *
301  * @return
302  * Current discrete state and clock values.
303  */
304  const TimedState& CurrentTimedState(void) const;
305 
306  /**
307  * Set discrete state. Returns false if state is not
308  * in state set.
309  *
310  * @param index
311  * State index
312  *
313  * @return
314  * True/false -- success
315  */
316  bool CurrentState(Idx index);
317 
318  /**
319  * Get discrete state.
320  *
321  * @return
322  * Discret state by index
323  */
324  Idx CurrentState(void) const;
325 
326  /**
327  * Set value of clock variable.
328  * Returns false if clock not in clockset.
329  *
330  * @param clock
331  * Index of clock variable to set
332  * @param time
333  * Time to set
334  * @return
335  * True/false -- success
336  */
337  bool CurrentClockValue(Idx clock, Time::Type time);
338 
339  /**
340  * Get value of clock
341  *
342  * @param clock
343  * Index of clock variable
344  * @return time
345  * Value of clock variable
346  */
347  Time::Type CurrentClockValue(Idx clock) const;
348 
349  /**
350  * Set current time.
351  *
352  * @param time
353  * New current time
354  */
355  void CurrentTime(Time::Type time);
356 
357  /**
358  * Get current time
359  *
360  * @return time
361  * Current time
362  */
363  Time::Type CurrentTime(void) const;
364 
365  /**
366  * Set logic time (# of steps)
367  *
368  * @param step
369  * New logic time
370  */
371  void CurrentStep(int step);
372 
373 
374  /**
375  * Get logic time ie numer of transitions so far.
376  *
377  */
378  int CurrentStep(void) const;
379 
380  /**
381  * Returns true if timed generator is in a deadlocked state.
382  *
383  * @return true/false
384  */
385  bool IsDeadlocked() const;
386 
387 
388  /**
389  * Check if Executor is valid.
390  * Not implemented, should check for determinism and consitency of current state and clock values.
391  *
392  * @return
393  * Success
394  */
395  virtual bool Valid(void) const {return true;};
396 
397 
398  /**
399  * Pretty printable string of current state
400  */
401  std::string CurrentTimedStateStr(void) const;
402 
403  /**
404  * Pretty printable string of timed state
405  */
406  std::string TSStr(const TimedState& tstate) const;
407 
408  /**
409  * Pretty printable string of timed event
410  */
411  std::string TEStr(const TimedEvent& tevent) const;
412 
413  /**
414  * Pretty printable string of clock name
415  */
416  std::string CStr(Idx idx) const;
417 
418  /**
419  * Pretty printable string of event
420  */
421  std::string EStr(Idx idx) const;
422 
423  /**
424  * Pretty printable string of state
425  */
426  std::string SStr(Idx idx) const;
427 
428  // std faudes type interface
429  using TimedGenerator::Read;
430  using TimedGenerator::Write;
431 
432  protected:
433 
434  /**
435  * Assignment method.
436  *
437  * @param rSrc
438  * Source to assign from
439  */
440  void DoAssign(const Executor& rSrc);
441 
442  /**
443  * Reads configuration from TokenReader, see Typefor public wrappers.
444  *
445  * @param rTr
446  * TokenReader to read from
447  * @param rLabel
448  * Section to read
449  * @param pContext
450  * Read context to provide contextual information
451  *
452  * @exception Exception
453  * - IO error (id 1)
454  */
455  virtual void DoRead(TokenReader& rTr, const std::string& rLabel="", const Type* pContext=0);
456 
457  /**
458  * Writes configuration to TokenWriter, see Type for public wrappers.
459  *
460  * @param rTw
461  * TokenWriter to write to
462  * @param rLabel
463  * Section to write
464  * @param pContext
465  * Write context to provide contextual information
466  *
467  * @exception Exception
468  * - IO error (id 2)
469  */
470  virtual void DoWrite(TokenWriter& rTw,const std::string& rLabel="", const Type* pContext=0) const;
471 
472 
473 
474 
475  private:
476 
477  /** Current state incl clock values */
479 
480  /** Current clock time */
482 
483  /** Current logic time */
485 
486  /** Prepare internal data structurs from generator */
487  void Compile(void);
488 
489  /** Compute enabled events and enabled interval (fake const) */
490  void ComputeEnabled(void) const;
491 
492  /** Compute enabled core routine (non const) */
493  void ComputeEnabledNonConst(void);
494 
495  /** Record enabled time */
497 
498  /** Record enabled events */
500 
501  /** Record rime on shich mEEvents is constant */
503 
504  /** Record interval in which each guard is enabled */
505  std::map<Idx,TimeInterval> mEGuardInterval;
506 
507  /** Record disabled events */
509 
510  /** Record active events (ie regardles time) */
512 
513  /** Record active transitions (regardles time) */
515 
516  /** Validity flag for the above data */
517  bool mEValid;
518 
519  /** Compiled generator data: map transition to clock to interval constraint */
520  std::map<Transition, std::map<Idx,TimeInterval> > mTransClockIntervalMap;
521 
522  /** Compiled generator data: map state to clock to interval constraint */
523  std::map<Idx, std::map<Idx,TimeInterval> > mStateClockIntervalMap;
524 
525 }; // end class Executor
526 
527 
528 
529 } // namespace faudes
530 
531 
532 #endif
533 
#define FAUDES_API
Definition: cfl_platform.h:80
#define FAUDES_TYPE_DECLARATION(ftype, ctype, cbase)
Definition: cfl_types.h:872
EventSet mDEvents
Definition: sp_executor.h:508
std::map< Idx, TimeInterval > mEGuardInterval
Definition: sp_executor.h:505
virtual bool Valid(void) const
Definition: sp_executor.h:395
Time::Type mCurrentTime
Definition: sp_executor.h:481
const std::string & Name(void) const
Definition: sp_executor.h:161
EventSet mAEvents
Definition: sp_executor.h:511
std::map< Transition, std::map< Idx, TimeInterval > > mTransClockIntervalMap
Definition: sp_executor.h:520
std::map< Idx, std::map< Idx, TimeInterval > > mStateClockIntervalMap
Definition: sp_executor.h:523
std::string StateName(Idx idx) const
Definition: sp_executor.h:167
TimeInterval mETime
Definition: sp_executor.h:496
TimeInterval mEInterval
Definition: sp_executor.h:502
std::string EventName(Idx idx) const
Definition: sp_executor.h:174
TimedState mCurrentTimedState
Definition: sp_executor.h:478
EventSet mEEvents
Definition: sp_executor.h:499
TransSet mATrans
Definition: sp_executor.h:514
Time::Type mTime
Definition: sp_executor.h:56
void Read(const std::string &rFileName, const std::string &rLabel="", const Type *pContext=0)
Definition: cfl_types.cpp:262
void Write(const Type *pContext=0) const
Definition: cfl_types.cpp:140
const std::string & Name(void) const
std::string StateName(Idx index) const
std::string EventName(Idx index) const
vGenerator Generator
uint32_t Idx
std::map< Idx, Time::Type > ClockValue
Definition: sp_executor.h:103

libFAUDES 2.33c --- 2025.05.15 --- c++ api documentaion by doxygen