sp_pexecutor.h
Go to the documentation of this file.
1 /** @file sp_pexecutor.h Executor for multiple synchronized timed generators */
2 
3 /*
4  FAU Discrete Event Systems Library (libfaudes)
5 
6  Copyright (C) 2007, 2008 Thomas Moor
7  Copyright (C) 2007 Ruediger Berndt
8  Exclusive copyright is granted to Klaus Schmidt
9 
10 */
11 
12 
13 
14 #ifndef FAUDES_PEXECUTOR_H
15 #define FAUDES_PEXECUTOR_H
16 
17 #include "corefaudes.h"
18 #include "tp_include.h"
19 #include "sp_executor.h"
20 #include "sp_simconditionset.h"
21 
22 
23 namespace faudes {
24 
25 /**
26  * Synchronized parallel execution of TimedGenerators
27  *
28  * \section SecSimulatorPEX1 Synchronisation
29  *
30  * The ParallelExecutor executes a family of timed generators with synchronized shared
31  * events according to Alur semantics. That is, we assume disjoint clock sets and synchronize
32  * shared events w.r.t. occurence at clock time.
33  *
34  * \section SecSimulatorPEX2 Implementation
35  *
36  * The external interface of a ParallelExecutor is the same as the single Executor, in that
37  * it indicats enabled events and in that it provides methods for executing
38  * events or letting time pass.
39  *
40  * Technically, a ParallelExecutor is a vector of executors. Clocks
41  * are treated on a per executor basis. That is, values of clocks in one generator are
42  * not effected by the reset of another generator, even if the respestive clock variables
43  * have the same index and name.
44  *
45  * \section SecSimulatorPEX3 File IO
46  *
47  * For token IO, the ParallelExecutor reads and writes the generators to execute within a
48  * section with default label "Executor". For disk space efficiency, the token IO
49  * format will use refernces by (relative) filename if the latter is known. Since TimedGenerators
50  * read any generator type from file, so does the ParallelExecutor. Example:
51  *
52  * \code
53  * <Executor>
54  * <Generators>
55  * "./some_generator.gen"
56  * "./other_generator.gen"
57  * </Generators>
58  * </Executor>
59  * \endcode
60  *
61  * @ingroup SimulatorPlugin
62  */
63 
64 class FAUDES_API ParallelExecutor : public Type {
65 
67 
68  public:
69  /** Typedef for parallel discrete state*/
70  typedef std::vector<Idx> ParallelState;
71 
72  /** Typedef for parallel clock values */
73  typedef std::vector< std::map<Idx,Time::Type> > ParallelClock;
74 
75  /** Typedef for parallel timed state, incl token io */
77  public:
80  protected:
81  virtual void DoRead(TokenReader& rTr, const std::string& rLabel = "", const Type* pContext=0);
82  virtual void DoWrite(TokenWriter& rTw, const std::string& rLabel="",const Type* pContext=0) const;
83  };
84 
85  /** Provide typedef from Executor */
87 
88  /**
89  * Construct an emtpy ParallelExecuter
90  */
91  ParallelExecutor(void);
92 
93  /**
94  * Copy constructor
95  */
96  ParallelExecutor(const ParallelExecutor& rOther);
97 
98  /**
99  * Construct from file.
100  *
101  * This constructor uses the DoRead method to initialize from file.
102  *
103  * @param rFileName
104  * Filename
105  *
106  * @exception Exception
107  * - non-deteministic generator (id 501)
108  * - token mismatch (id 502)
109  * - IO errors (id 1)
110  */
111  ParallelExecutor(const std::string& rFileName);
112 
113 
114  /**
115  * Explicit destructor.
116  */
117  virtual ~ParallelExecutor(void);
118 
119  /**
120  * Clear all data. Removes all generators/executors and resets the
121  * current state to a void value.
122  *
123  */
124  virtual void Clear(void);
125 
126  /**
127  * Number of TimedGenerators
128  *
129  */
130  Idx Size(void) const;
131 
132 
133  /**
134  * Add a TimedGenerator from file. This method uses the TimedGenerator's read to
135  * find the first generator in the file. If the generator found is not a TimedGenerator,
136  * timing data defaults to infinite invariants/guards and empty resets.
137  * After inserting generators and before starting to execute, you must call Reset() to update
138  * internal datastructures.
139  *
140  * @param rFileName
141  * File to read
142  *
143  * @exception Exception
144  * - non-deteministic generator (id 501)
145  * - token mismatch (id 502)
146  * - IO errors (id 1)
147  */
148  void Insert(const std::string& rFileName);
149 
150  /**
151  * Add a TimedGenerator.
152  * After inserting generators and before starting to execute, you must call Reset() to update
153  * internal datastructures.
154  *
155  * @param rGen
156  * Generator to add
157  *
158  * @exception Exception
159  * - non-deteministic generator (id 501)
160  */
161  void Insert(const TimedGenerator& rGen);
162 
163  /**
164  * Overall alphabet.
165  *
166  */
167  const EventSet& Alphabet(void) const;
168 
169  /**
170  * Goto initial state. Reset all clock values to zero, assign initial states to each executor.
171  */
172  virtual void Reset(void);
173 
174 
175 
176  /**
177  * Read-only access to individual executors.
178  *
179  */
180  typedef std::vector<Executor>::const_iterator Iterator;
181  Iterator Begin(void) const { return mExecutors.begin(); };
182  Iterator End(void) const { return mExecutors.end(); };
183  const Executor& At(int i) const { return mExecutors.at(i); };
184 
185 
186  /**
187  * Event index lookup.
188  *
189  * This convenience method refers to the global event symbol table.
190  *
191  * @param rName
192  * Name of event to lookup
193  *
194  * @return
195  * Valid index or 0 if non-existent
196  */
197  Idx EventIndex(const std::string& rName) const { return mAlphabet.Index(rName); };
198 
199  /**
200  * Event name lookup
201  *
202  * This convenience method refers to the global event symbol table.
203  *
204  * @param index
205  * Index of event to look up
206  *
207  * @return
208  * Name or empty std::string if non-existent
209  */
210  std::string EventName(Idx index) const { return mAlphabet.SymbolicName(index); };
211 
212  /**
213  * Get clock time.
214  *
215  */
216  Time::Type CurrentTime(void) const;
217 
218  /**
219  * Get logical time, ie number of transitions so far,
220  *
221  */
222  int CurrentStep(void) const;
223 
224  /**
225  * Test for deadlocked.
226  *
227  * The parallel executor is deadlocked if neither time can pass nor an event can
228  * be executed. Prototypical examples for such a situation is that the indvidual
229  * executers fail to agree on a common time interval, at which shaered events are
230  * enabled.
231  *
232  * @return True/false
233  */
234  bool IsDeadlocked() const;
235 
236  /**
237  * Check validity of executors.
238  *
239  * This is currently not implemented.
240  *
241  * @return
242  * True on success
243  */
244  virtual bool Valid(void) const {return true;};
245 
246  /**
247  * Get current state of the ParallelExecutor.
248  *
249  * With "the current state" w refer to all data relevant for
250  * events generated in future. This data consists of a discrete
251  * state vector and a mapping from clocks to closk values.
252  *
253  * @return
254  * Discrete state vector and clock value maps
255  */
256  const ParallelTimedState& CurrentParallelTimedState(void) const;
257 
258  /**
259  * Get current discrete state vector of the ParallelExecutor
260  *
261  * By "the current discrete state" we refer to a vector of indices
262  * that indicate the current state of the untimed transition structure.
263  *
264  * @return
265  * Discrete state vector
266  */
267  const ParallelState& CurrentParallelState(void) const;
268 
269  /**
270  * Set clock time.
271  * This does not affect clocks and, hence, is purely cosmetic.
272  * The trace buffer will record an invalid event.
273  *
274  * @param time
275  * New clock time
276  */
277  virtual void CurrentTime(Time::Type time);
278 
279  /**
280  * Set logical time (# of steps)
281  *
282  * This does not affect clocks and, hence, is purely cosmetic.
283  * Note that, in contrast to clock time, the individual
284  * generators do not agree in logical time.
285  * The trace buffer will get out of order and should be cleared.
286  *
287  * @param step
288  * New logical time
289  */
290  virtual void CurrentStep(int step);
291 
292  /**
293  * Set current state of the ParallelExecutor.
294  *
295  * This resets the parallel executor to the given state, incl clock values.
296  * Both, clock time and logical time is also reset (to 0).
297  *
298  * @return
299  * True for success
300  */
301  virtual bool CurrentParallelTimedState(const ParallelTimedState& ptstate);
302 
303  /**
304  * Let time pass without executing a transition. Return false if the duration specified
305  * cannot elapse without an event being executed.
306  *
307  * @param duration
308  * Amount of time that shall elapse.
309  * @return
310  * True for success
311  */
312  virtual bool ExecuteTime(Time::Type duration);
313 
314  /**
315  * Execute transition.
316  *
317  * Returns false if the transition
318  * cannot be executed at the current time.
319  *
320  * @param event
321  * Indicate transition to execute
322  * @return
323  * True on success
324  */
325  virtual bool ExecuteEvent(Idx event);
326 
327  /**
328  * Get maximal duration that can pass without executing an event.
329  *
330  * @return TimeInterval
331  *
332  */
333  const TimeInterval& EnabledTime() const;
334 
335  /**
336  * Get events that are enabled at current (timed) state.
337  *
338  * By "enabled" we refer to the synchronizes timed generators,
339  * that is, we do care about clock values, invariants and guards.
340  *
341  *
342  * @return
343  * Set of enabled events
344  */
345  const EventSet& EnabledEvents() const;
346 
347  /**
348  * Get events that are disabled at current (timed) state
349  *
350  * By "disabled" we refer to the synchronizes timed generators,
351  * that is, we do care about clock values, invariants and guards.
352  *
353  * @return
354  * Set of disabled events
355  */
356  const EventSet& DisabledEvents() const;
357 
358  /**
359  * Get an interval on which the set of enabled events is constant.
360  * Note: while this implementation tries to come up with a potentially large
361  * interval, it is not guaranteed to be maximal.
362  *
363  * @return TimeInterval
364  *
365  */
366  const TimeInterval& EnabledInterval() const;
367 
368  /**
369  * Get interval on which the specified event is enabled.
370  *
371  * Returns empty, if the event is not active or never
372  * simultanuosly enabled in all executors.
373  *
374  * @param event
375  *
376  * @return TimeInterval
377  *
378  */
379  TimeInterval EnabledEventTime(Idx event) const;
380 
381  /**
382  * Get interval on which the respective guard is satisfied.
383  *
384  * Returns empty, if the event is not active or if the guards
385  * are never simultanuosly satisfied in all executors.
386  *
387  * @param event
388  *
389  * @return TimeInterval
390  *
391  */
392  TimeInterval EnabledGuardTime(Idx event) const;
393 
394  /**
395  * Get events that are active in all TimedGenerators.
396  *
397  * By "active" we refer to the untimed transition structure,
398  * that is, we ignore clock values etc.
399  *
400  * @param stateVec
401  * Discrete state
402  * @return
403  * Active EventSet
404  */
405  EventSet ActiveEventSet(const ParallelState& stateVec) const;
406 
407  /**
408  * Test whether an event is active in a given discrete state.
409  *
410  * By "active" we refer to the untimed transition structure,
411  * that is, we ignore clock values etc.
412  *
413  * @param ev
414  * Event to test
415  * @param stateVec
416  * ParallelState
417  *
418  * @return
419  * True for active in all generators
420  */
421  bool Active(Idx ev, const ParallelState& stateVec) const;
422 
423  /**
424  * Test whether an event is active at current (discrete) state.
425  *
426  * By "active" we refer to the untimed transition structure,
427  * that is, we ignore clock values etc.
428  *
429  * @param ev
430  * Event ro test
431  *
432  * @return
433  * True for active in all generators
434  */
435  bool Active(Idx ev) const;
436 
437  /**
438  * Pretty printable string of timed parallel state
439  */
440  std::string PTSStr(const ParallelTimedState& ptstate) const;
441 
442  /**
443  * Pretty printable string of parallel state
444  */
445  std::string PSStr(const ParallelState& pstate) const;
446 
447  /**
448  * Pretty printable string of timed event
449  */
450  std::string TEStr(const TimedEvent& tevent) const;
451 
452  /**
453  * Pretty printable string of clock name
454  */
455  std::string CStr(Idx clock) const;
456 
457  /**
458  * Pretty printable string of event
459  */
460  std::string EStr(Idx event) const;
461 
462  /**
463  * Pretty printable string of current state
464  */
465  std::string CurrentParallelTimedStateStr(void) const;
466 
467  /**
468  * Pretty printable string of parallel state
469  */
470  std::string CurrentParallelStateStr(void) const;
471 
472  /** Compute enabled events and enabled interval (fake const) */
473  /** this is public only for performance experiments --- dont use */
474  void ComputeEnabled(void) const;
475 
476  /** Compute enabled core routine (non const) */
477  void ComputeEnabledNonConst(void);
478 
479 
480 
481 protected:
482 
483  /**
484  * Reads parallel executor from TokenReader, see also public wrappers Read() in faudes::Type.
485  *
486  * @param rTr
487  * TokenReader to read from
488  * @param rLabel
489  * Section to read, defaults to "Executor"
490  * @param pContext
491  * Read context to provide contextual information (ignored)
492  *
493  * @exception Exception
494  * - non-deterministic generator(s) (id 501)
495  * - token mismatch (id 502)
496  * - IO error (id 1)
497  */
498  virtual void DoRead(TokenReader& rTr, const std::string& rLabel = "", const Type* pContext=0);
499 
500  /**
501  * Write to TokenWriter, see also public wrappers Write() in faudes::Type.
502  *
503  * @param rTw
504  * Reference to TokenWriter
505  * @param rLabel
506  * Label of section to write, defaults to "Executor"
507  * @param pContext
508  * Write context to provide contextual information (ignored)
509  *
510  * @exception Exception
511  * - IO errors (id 2)
512  */
513  virtual void DoWrite(TokenWriter& rTw, const std::string& rLabel="", const Type* pContext=0) const;
514 
515  // /**
516  // * Write to TokenWriter, see also public wrappers XWrite() in faudes::Type.
517  // *
518  // * @param rTw
519  // * Reference to TokenWriter
520  // * @param rLabel
521  // * Label of section to write, defaults to "Executor"
522  // * @param pContext
523  // * Write context to provide contextual information (ignored)
524  // *
525  // * @exception Exception
526  // * - IO errors (id 2)
527  // */
528  // virtual void DoXWrite(TokenWriter& rTw, const std::string& rLabel="", const Type* pContext=0) const;
529 
530  /**
531  * Reads generator files section from TokenReader
532  *
533  * @param rTr
534  * TokenReader to read from
535  *
536  * @exception Exception
537  * - non-deterministic generator(s) (id 501)
538  * - token mismatch (id 502)
539  * - IO error (id 1)
540  */
541  virtual void DoReadGenerators(TokenReader& rTr);
542 
543  /**
544  * Write generator files section to TokenWriter
545  *
546  * @param rTw
547  * Reference to TokenWriter
548  *
549  * @exception Exception
550  * - IO errors (id 2)
551  */
552  virtual void DoWriteGenerators(TokenWriter& rTw) const;
553 
554 
555  /**
556  * Assignment method
557  *
558  * @param rSrc
559  * Source to assign from
560  */
561  void DoAssign(const ParallelExecutor& rSrc);
562 
563  /** compile internal data (eg overall alphabet) */
564  virtual void Compile();
565 
566  /** recent event */
568 
569 private:
570 
571  /** list of executors */
572  std::vector<Executor> mExecutors;
573 
574  /** list of executors */
575  std::vector<std::string> mExecutorNames;
576 
577  /** Internal non-const iterator */
578  typedef std::vector<Executor>::iterator iterator;
579 
580  /** overall alphabet */
582 
583  /** global time (real) */
585 
586  /** global time (step)*/
588 
589  /** enabled time */
591 
592  /** enabled events */
594 
595  /** disabled events */
597 
598  /** enabled interval */
600 
601  /** validity flag for fevents and ftime */
602  bool mEValid;
603 
604  /** current state */
606 
607  /** update parallel timed state() */
608  void UpdateParallelTimedState(void);
609 
610 
611 
612 
613 }; // end class ParallelExecutor
614 
615 
616 
617 } // namespace faudes
618 
619 
620 #endif
621 
#define FAUDES_API
Interface export/import symbols: windows.
Definition: cfl_platform.h:81
#define FAUDES_TYPE_DECLARATION(ftype, ctype, cbase)
faudes type declaration macro
Definition: cfl_types.h:867
An Executor is a timed generator that maintains a current state.
Definition: sp_executor.h:93
Set of indices with symbolic names.
Definition: cfl_nameset.h:69
Typedef for parallel timed state, incl token io.
Definition: sp_pexecutor.h:76
Synchronized parallel execution of TimedGenerators.
Definition: sp_pexecutor.h:64
virtual bool Valid(void) const
Check validity of executors.
Definition: sp_pexecutor.h:244
std::vector< Executor >::iterator iterator
Internal non-const iterator.
Definition: sp_pexecutor.h:578
bool mEValid
validity flag for fevents and ftime
Definition: sp_pexecutor.h:602
EventSet mAlphabet
overall alphabet
Definition: sp_pexecutor.h:581
std::vector< std::map< Idx, Time::Type > > ParallelClock
Typedef for parallel clock values.
Definition: sp_pexecutor.h:73
EventSet mEEvents
enabled events
Definition: sp_pexecutor.h:593
TimeInterval mEInterval
enabled interval
Definition: sp_pexecutor.h:599
Idx mRecentEvent
recent event
Definition: sp_pexecutor.h:567
Idx EventIndex(const std::string &rName) const
Event index lookup.
Definition: sp_pexecutor.h:197
Time::Type mCurrentTime
global time (real)
Definition: sp_pexecutor.h:584
std::vector< Executor >::const_iterator Iterator
Read-only access to individual executors.
Definition: sp_pexecutor.h:180
ParallelTimedState mCurrentParallelTimedState
current state
Definition: sp_pexecutor.h:605
std::vector< Idx > ParallelState
Typedef for parallel discrete state.
Definition: sp_pexecutor.h:70
Iterator Begin(void) const
Definition: sp_pexecutor.h:181
std::vector< Executor > mExecutors
list of executors
Definition: sp_pexecutor.h:572
TimeInterval mETime
enabled time
Definition: sp_pexecutor.h:590
std::vector< std::string > mExecutorNames
list of executors
Definition: sp_pexecutor.h:575
int mCurrentStep
global time (step)
Definition: sp_pexecutor.h:587
EventSet mDEvents
disabled events
Definition: sp_pexecutor.h:596
Iterator End(void) const
Definition: sp_pexecutor.h:182
std::string EventName(Idx index) const
Event name lookup.
Definition: sp_pexecutor.h:210
Executor::TimedState TimedState
Provide typedef from Executor.
Definition: sp_pexecutor.h:86
const Executor & At(int i) const
Definition: sp_pexecutor.h:183
Model of a time interval.
Int Type
Datatype for point on time axis.
Global Tyoedefs.
Definition: sp_executor.h:53
A TokenReader reads sequential tokens from a file or string.
A TokenWriter writes sequential tokens to a file, a string or stdout.
Generator with timing extensions.
Base class of all libFAUDES objects that participate in the run-time interface.
Definition: cfl_types.h:239
Includes all libFAUDES headers, no plugins.
TaNameSet< AttributeCFlags > Alphabet
Convenience typedef for event sets with controllability attributes.
libFAUDES resides within the namespace faudes.
uint32_t Idx
Type definition for index type (allways 32bit)
Execute transitions in a timed generator
Set of named simulation conditions.
Typedef for timed state.
Definition: sp_executor.h:101
Include timed plugin headers.

libFAUDES 2.32b --- 2024.03.01 --- c++ api documentaion by doxygen