sp_plpexecutor.h
Go to the documentation of this file.
1 /** @file sp_plpexecutor.h Executor that proposes transitions to execute */
2 
3 /*
4  FAU Discrete Event Systems Library (libfaudes)
5 
6  The proposing executor is based on the stochastic executor, which
7  has been developped by Christoph Doerr as part of his thesis project.
8 
9  Copyright (C) 2008 Thomas Moor
10  Copyright (C) 2007 Christoph Doerr
11  Exclusive copyright is granted to Thomas Moor
12 
13  todo:
14  - compile simevent set to one vector per type to speed up iterations
15  - fix vector pdfs
16  - test with elementary examples
17  - use compiled interval time constraints
18 
19 */
20 
21 #ifndef FAUDES_SP_PLPEXECUTOR_H
22 #define FAUDES_SP_PLPEXECUTOR_H
23 
24 #include "corefaudes.h"
25 #include "tp_include.h"
26 #include "sp_lpexecutor.h"
27 #include "sp_simeventset.h"
28 #include "sp_random.h"
29 
30 
31 namespace faudes {
32 
33 /**
34  * Executer that proposes which transition to execute.
35  *
36  * \section SecSimulatorPLPEX1 Priority and Stochastic Semantics
37  *
38  * Whilst the LoggingExecutor (and all lower level executors) is aware of which transitions are
39  * enabled, the ProposingExecutor actually proposes either one particular transition for execution or
40  * a particular amount of time to let pass. The proposal refers to additional semantics
41  * parametrised by data from SimEventAttribute. The decision procedure is organized in four stages,
42  * were the first stage that yields a proposal wins:
43  *
44  * 1.) SimPriorityEventAttribute, positive priority
45  * - if one or more events with positive priority are enabled, those with maximum priority
46  * form the candidate set
47  * - if the candidate set is non-emty propose one event by random (uniformly distributed) to
48  * be executed immediately
49  *
50  * 2.) SimStochasticEventAttribute, stochastic timing
51  * - if within the interval at which the set of enabled events remains constant (EnabledInterval)
52  * an event with stochastic timing is scheduled to occur, the earliest of such events form the candidate set.
53  * - if the candidate set is non-emty propose one event by random (uniformly distributed) to be
54  * executed at the scheduled clock time.
55  *
56  * 3.) passing by clock time
57  * - if the EnabledInterval is of positive duration, let this duration pass by.
58  * - if the EnabledInterval is of infinite duration, let all clock time pass and stop the simulation
59  *
60  * 4.) SimPriorityEventAttribute, negative priority
61  * - if one or more events with negative priority are enabled, those with maximum priority
62  * form the candidate set
63  * - if the candidate set is non-emty propose one event by random (uniformly distributed) to
64  * be executed immediately
65  *
66  * Note that the above procedure will never come up with a proposal that fails to satisfy
67  * invariant and guard conditions. In this sense, the above procedure is compliant with Alur semantics
68  * of timed automata.
69  *
70  * If the above procedure fails to indicate a transition to execute or clock time to let pass, the
71  * system is deadlocked. If the procedure sticks with case 3) and infinite duration, it might be
72  * either life locked (no enabled events) or just unwilling/unable to execute a negative priority event.
73  * The latter case can be used for sensor events in a hardware-in-the-loop simulation.
74  *
75  *
76  * \section SecSimulatorPLPEX2 Scheduling Stochastic Events
77  *
78  * The mechnism to schedule events with stochastic timing comes in three flavors.
79  * - SimStochasticEventAttribute::Extern
80  * The random variable models an external stochastic process. A sample is taken
81  * when the executor is reset to determine the first scheduled occurence. The schedule expires
82  * when it matched the current clock time, regardless whether the event is executed or not. When the
83  * schedule expires, a new sample is taken to determine the next scheduled occurence.
84  * - SimStochasticEventAttribute::Trigger
85  * The random variable is used to narrow down the effective guard interval to a
86  * point. By "effective guard interval" we refer to the interval of time in which the guard is satisfied
87  * w.r.t. the current timed state.
88  * A sample is taken when the executor enters a timed state with a non-empty the effective guard
89  * interval. In order to guarantee that the scheduled occurence lies within the guard, the density function
90  * is shaped accordingly. The schedule expires when either the event is actually executed or when the
91  * effective guard interval changes due to a transition.
92  * - SimStochasticEventAttribute::Delay
93  * The random variable models a delay relative to the clock time when the event is
94  * enabled. A sample is taken when the executor is reset to determine the initial amount of delay.
95  * During the execution sequence the executor accumulates the durations for which the event is enabled. The event
96  * is scheduled when the accumulated durations matches the delay. When the event is
97  * executed the schedule expires and the random variable is sampled to re-initialised the delay.
98  *
99  * Note that type Extern or Delay schedules can disable the respective event in a way that
100  * potentially leads to blocking behaviour even if the timed automata is non-blocking by Alur semantics.
101  * This is a consequence of the fact that both types model additional phenomena that are
102  * synchronized with the timed automata, and it is perferctly reasonable that this synchronisation
103  * introduces blocking situations.
104  * In contrast, events of type Trigger are not affected by the blocking issue provided that guards lie
105  * within the respective invariant.
106  *
107  * \section SecSimulatorPLPEX3 File IO
108  *
109  * For token IO, the ProposingExecutor reads and writes a section with default label
110  * "Executor". It contains a ParallelExecutor section to define a vector of generators,
111  * a SimConditionSet section to define relevant conditions, and a SimEventSet section to define
112  * the above event attributes. Example:
113  *
114  * \code
115  * <Executor>
116  *
117  * <Generators>
118  * "./some_generator.gen"
119  * "./other_generator.gen"
120  * </Generators>
121  *
122  * <Conditions>
123  *
124  * "IdleCond"
125  * <EventCondition>
126  * <StartEvents> "beta" "mue" </StartEvents>
127  * <StopEvents> "alpha" </StopEvents>
128  * </EventCondition>
129  *
130  * "DownCond"
131  * <StateCondition>
132  * <StateSet> "down" </StateSet> % refers to first generator
133  * <StateSet> "down" "repair" </StateSet> % refers to second generator
134  * </StateCondition>
135  *
136  * </Conditions>
137  *
138  * <SimEvents>
139  *
140  * "alpha"
141  * <Priority> 100 </Priority>
142  *
143  * "beta"
144  * <Stochastic> +Trigger+ +Gauss+ <Parameter> 10 5 </Parameter> </Stochastic>
145  *
146  * "mue"
147  * <Stochastic> +Delay+ +Gauss+ <Parameter> 20 5 </Parameter> </Stochastic>
148  *
149  * "lambda"
150  * <Priority> 100 </Priority>
151  *
152  * </SimEvents>
153  *
154  * </Executor>
155  * \endcode
156  *
157  * Technical detail: since the trace buffer only covers the dynamic state of the parallel executor,
158  * the RevertToStep method cannot recover the stochastic event states. Including stochastic states
159  * with the trace buffer is considered to expensive.
160  *
161  * @ingroup SimulatorPlugin
162  */
163 
164 
166 
168 
169  public:
170 
171  /*****************************************
172  *****************************************
173  *****************************************
174  *****************************************/
175 
176  /** @name Constructors & Destructor */
177  /** @{ doxygen group */
178 
179 
180  /**
181  * Creates an emtpy ProposingExecutor
182  */
184 
185 
186  /**
187  * Copy constrcutor
188  */
190 
191 
192 
193  /** @} doxygen group */
194 
195  /*****************************************
196  *****************************************
197  *****************************************
198  *****************************************/
199 
200  /** @name Simulation Event Attributes */
201  /** @{ doxygen group */
202 
203 
204  /**
205  * Simulation event attribute lookup
206  *
207  * @param index
208  *
209  * @return
210  * reference to attribute
211  */
212  const SimEventAttribute& EventAttribute(Idx index) const;
213 
214  /**
215  * Set simulation event attribute. Requires Reset().
216  *
217  * @param index
218  * Event index
219  * @param rAttr
220  * New attribute
221  * @exception Exception
222  * Index not found in EventSymbolMap (id 42)
223  *
224  */
225  void EventAttribute(Idx index, const SimEventAttribute& rAttr);
226 
227 
228  /**
229  * Set all simulation event attributes.
230  * Any previous attributes are removed.
231  * Any events not in rAlphabet become the default attribute attached (which is priority 0)
232  * Requires Reset().
233  *
234  * @param rAlphabet
235  * EventSet with SimEventAttrute data
236  */
237  void Alphabet(const sEventSet& rAlphabet);
238 
239  /**
240  * Access alphabet (incl simulation event attributes)
241  *
242  * @return
243  * Overall alphabet
244  */
245  const sEventSet& Alphabet(void) const { return mSimEvents; };
246 
247  /** @} doxygen group */
248 
249  /** @name Execution Proposal */
250  /** @{ doxygen group */
251 
252 
253  /**
254  * Execute next transition.
255  *
256  * Choose the transition to execute by priorities and
257  * stochastic properties.
258  *
259  * @return
260  * Executed TimedEvent
261  */
262  TimedEvent ExecuteNextTransition();
263 
264  /**
265  * Propose next transition.
266  *
267  * Propose a transition to execute by priorities and
268  * stochastic properties.
269  *
270  * @return
271  * Proposed TimedEvent
272  */
273  const TimedEvent& ProposeNextTransition();
274 
275  /**
276  * Inspect stochastic event states (debugging)
277  *
278  * return EventInfoMap string
279  */
280  std::string EventStatesToString(void) const;
281 
282 
283  /** @} doxygen group */
284 
285  /*****************************************
286  *****************************************
287  *****************************************
288  *****************************************/
289 
290  /** @name Re-implemenented from ParallelExecutor */
291  /** @{ doxygen group */
292 
293  /**
294  * Reset the ProposingExecutor.
295  * This includes a reset of the ParallelExecutor and the simulation event states.
296  * @param seed
297  * Seed for random generator
298  */
299  virtual void Reset(long int seed);
300 
301  /**
302  * Reset the ProposingExecutor.
303  *
304  * This method uses the systemtime as seed.
305  */
306 
307  virtual void Reset(void) { Reset(0); };
308 
309  /**
310  * Clear all data (generators, simulation attributes etc)
311  */
312  virtual void Clear(void);
313 
314 
315  /**
316  * Execute time duration.
317  *
318  * @return
319  * True on success
320  */
321  bool ExecuteTime(Time::Type duration);
322 
323  /**
324  * Execute event.
325  *
326  * @return
327  * True on success
328  */
329  bool ExecuteEvent(Idx event);
330 
331 
332  /**
333  * Execute event by transition
334  *
335  * @return
336  * True on success
337  */
338  bool ExecuteTransition(const TimedEvent& tevent);
339 
340  /**
341  * Revert executor to past step.
342  *
343  * This will revert only the executor dynamic state (incl clock values, current time).
344  * The condition and event states, however, will not be reverted.
345  *
346  * @return
347  * True on success
348  */
349  bool RevertToStep(Idx step);
350 
351  /** @} doxygen group */
352 
353  /*****************************************
354  *****************************************
355  *****************************************
356  *****************************************/
357 
358 
359  private:
360 
361  /**
362  * Simulation event attributes, incl stochastic and priority data
363  */
365 
366  /**
367  * Valid proposal available
368  */
369  bool mPValid;
370 
371  /**
372  * Available proposal
373  */
375 
376  /**
377  * Reset stochastic state of events.
378  */
379  void ResetProposer(long int seed=0);
380 
381  /**
382  * Evaluate random variable to schedule event.
383  * Referring to the specified stochastic attribute, take a random sample to
384  * schedule the next occurence of the event. The result is given as return value and is
385  * also recorded in the simulation state of the event attribute.
386  *
387  *
388  * @param event
389  * Event to schedule, by index
390  * @param pattr
391  * Pointer to event attribute
392  * @return
393  * Time of next event occurrence
394  */
395  Time::Type Schedule(Idx event, SimEventAttribute* pattr);
396 
397 protected:
398 
399  /**
400  * Reads proposing executor from TokenReader, see also public wrappers Type::Read.
401  *
402  * @param rTr
403  * TokenReader to read from
404  * @param rLabel
405  * Section to read, defaults to "LoggingExecutor"
406  * @param pContext
407  * Read context to provide contextual information (ignored)
408  *
409  * @exception Exception
410  * - non-deterministic generator(s) (id 501)
411  * - token mismatch (id 502)
412  * - IO error (id 1)
413  */
414  virtual void DoRead(TokenReader& rTr, const std::string& rLabel = "", const Type* pContext=0);
415 
416  /**
417  * Write to TokenWriter, see also public wrappers Type::Write.
418  *
419  * @param rTw
420  * Reference to TokenWriter
421  * @param rLabel
422  * Label of section to write, defaults to "LoggingExecutor"
423  * @param pContext
424  * Write context to provide contextual information (ignored)
425  *
426  * @exception Exception
427  * - IO errors (id 2)
428  */
429  virtual void DoWrite(TokenWriter& rTw, const std::string& rLabel="", const Type* pContext=0) const;
430 
431  /**
432  * Assignment method
433  *
434  * @param rSrc
435  * Source to assign from
436  */
437  void DoAssign(const ProposingExecutor& rSrc);
438 
439 
440 }; // end class ProposingExecutor
441 
442 
443 
444 } // namespace faudes
445 
446 
447 #endif
#define FAUDES_API
Definition: cfl_platform.h:80
#define FAUDES_TYPE_DECLARATION(ftype, ctype, cbase)
Definition: cfl_types.h:872
virtual void Reset(void)
const sEventSet & Alphabet(void) const
TaNameSet< AttributeCFlags > Alphabet
uint32_t Idx

libFAUDES 2.33b --- 2025.05.07 --- c++ api documentaion by doxygen