pev_priorities.h
Go to the documentation of this file.
1 /** @file pev_priorities.h Classes EventPriorities */
2 
3 
4 /* FAU Discrete Event Systems Library (libfaudes)
5 
6  Copyright (C) 2025 Yiheng Tang, Thomas Moor
7  Exclusive copyright is granted to Klaus Schmidt
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef FAUDES_PEV_PRIORITIES_H
24 #define FAUDES_PEV_PRIORITIES_H
25 
26 #include "corefaudes.h"
27 
28 namespace faudes {
29 
30 /**
31  * The class AttributePriority is intendened to be used as an event
32  * attribute. the class is derived from AttributeCFlags
33  * and accommedates an additional non-negative integer to represent
34  * an event priority. See also the template TpEventSet<Attr> for a convenience
35  * wrapper of an alphabet with attributes of type AttributePriority per event..
36  *
37  * @ingroup PrioritiesPlugin
38  */
40 
41 FAUDES_TYPE_DECLARATION(Void,AttributePriority,AttribiteCFlags)
42 
43 public:
44  /**
45  * Default constructor, sets priority to 0
46  */
47  AttributePriority(void) : AttributeCFlags() {mPriority=0;}
48 
49  /**
50  * Copy constructor.
51  */
53  DoAssign(rSrc); }
54 
55  /** Destructor */
56  virtual ~AttributePriority(void) {}
57 
58  /**
59  * Clear to default (mandatory for serialisation)
60  */
61  void Clear(void) { mPriority = 0; }
62 
63  /**
64  * Test for default value
65  *
66  * @return
67  * True, if this attribute has its default value
68  */
69  virtual bool IsDefault(void) const {
70  return (mPriority==0) && AttributeCFlags::IsDefault(); };
71 
72  /** set priority */
73  void Priority (Idx prio) {mPriority = prio;}
74 
75  /** get priority */
76  Idx Priority(void) const { return mPriority;}
77 
78  /** compare priority (a>b iff a preempts b)*/
79  bool operator>(const AttributePriority& rOther) const {
80  if(mPriority > rOther.mPriority ) return true;
81  return false;
82  }
83 
84 protected:
85  /**
86  * priority value. 0 is highest.
87  */
88  Idx mPriority = 0;
89 
90  /**
91  * Assignment method.
92  *
93  * @param rSrcAttr
94  * Source to assign from
95  */
96  void DoAssign(const AttributePriority& rSrcAttr);
97 
98  /**
99  * Test equality of configuration data.
100  *
101  * @param rOther
102  * Other attribute to compare with.
103  * @return
104  * True on match.
105  */
106  bool DoEqual(const AttributePriority& rOther) const;
107 
108  /**
109  * Reads attribute from TokenReader, see AttributeVoid for public wrappers.
110  * Label and Context argument are ignored.
111  *
112  * @param rTr
113  * TokenReader to read from
114  * @param rLabel
115  * Section to read
116  * @param pContext
117  * Read context to provide contextual information
118  *
119  * @exception Exception
120  * - IO error (id 1)
121  */
122  virtual void DoRead(TokenReader& rTr, const std::string& rLabel="", const Type* pContext=nullptr);
123 
124  /**
125  * Writes attribute to TokenWriter, see AttributeVoid for public wrappers.
126  * Note: this class always wrtites in XML.
127  * Note: Label and Context argument are ignored.
128  *
129  * @param rTw
130  * TokenWriter to write to
131  * @param rLabel
132  * Section to write
133  * @param pContext
134  * Write context to provide contextual information
135  *
136  * @exception Exception
137  * - IO error (id 2)
138  */
139  virtual void DoWrite(TokenWriter& rTw,const std::string& rLabel="", const Type* pContext=nullptr) const;
140 
141  /**
142  * Writes attribute to TokenWriter, see AttributeVoid for public wrappers.
143  * Note: this class always wrtites in XML.
144  * Note: Label and Context argument are ignored.
145  *
146  * @param rTw
147  * TokenWriter to write to
148  * @param rLabel
149  * Section to write
150  * @param pContext
151  * Write context to provide contextual information
152  *
153  * @exception Exception
154  * - IO error (id 2)
155  */
156  virtual void DoXWrite(TokenWriter& rTw,const std::string& rLabel="", const Type* pContext=nullptr) const;
157 
158 
159 };
160 
161 /*
162 ********************************************************
163 ********************************************************
164 ********************************************************
165 
166 TpEventSet
167 
168 convenience class to declare universal event priorities
169 
170 ********************************************************
171 ********************************************************
172 ********************************************************
173 */
174 
175 /**
176  * The template TpEventSet<Attr> provides access members for event priorities.
177  * The template parameter is expected to be a descendant of AttributePriority.
178  * There also is the convenience typedef faudes::EventPriorities for this intended usecase.
179  *
180  * Token IO is proper XML with default priority 0. Example:
181  * <PRE>
182  * &lt;Alphabet&gt;
183  * &lt;!-- avent alpha with priority 10 --&gt;
184  * &lt;Event name="alpha"&gt;
185  * &lt;Priority value="10"/&gt;
186  * &lt;/Event&gt;
187  * &lt;!-- avent beta with default priority 0 --&gt;
188  * &lt;Event name="beta"/&gt;
189  * &lt;Priority value="20"/&gt;
190  * &lt;/Event&gt;
191  * &lt;/Alphabet&gt;
192  * </PRE>
193  *
194  * @ingroup PrioritiesPlugin
195  */
196 template <class Attr>
197 class FAUDES_TAPI TpEventSet : public TaEventSet<Attr> {
198 
199  /** std faudes type declarations */
200  FAUDES_TYPE_TDECLARATION(Void,TpEventSet,TaEventSet<Attr>)
201 
202  /**
203  * Default constructor
204  */
205  TpEventSet(void) : TaEventSet<Attr>(){};
206 
207  /**
208  * Copy constructor
209  *
210  * @param rOther
211  */
212  TpEventSet(const EventSet& rOther) : TaEventSet<Attr>(rOther){};
213 
214  /**
215  * Copy constructor
216  *
217  * @param rOther
218  */
219  TpEventSet(const TpEventSet& rOther) : TaEventSet<Attr>(rOther) {};
220 
221  /**
222  * Construct from file
223  *
224  * @param rFileName
225  * Filename
226  * @param rLabel
227  * Label to read from
228  *
229  * @exception Exception
230  * If opening/reading fails an Exception object is thrown (id 1, 50, 51)
231  */
232  TpEventSet(const std::string& rFileName, const std::string& rLabel="") : TaEventSet<Attr>(rFileName,rLabel) {};
233 
234 
235  /** recycle Iterator from nameset */
236  using NameSet::Iterator;
237 
238 
239  /**
240  * Get priority by symbolic name
241  *
242  * @param rName
243  * specify element
244  *
245  * @exception Exception
246  * Base class with throw if synbol does not exist
247  *
248  * @return priority of specified event
249  */
250  Idx Priority(const std::string& rName) const { return this->Attribute(rName).Priority(); }
251 
252  /**
253  * Get Priority by index
254  *
255  * @param rIdx
256  * specify element
257  *
258  * @exception Exception
259  * Base class with throw if specified element does not exist
260  *
261  * @return priority of specified event
262  */
263  Idx Priority(const Idx idx) const { return this->Attribute(idx).Priority(); }
264 
265  /**
266  * Set priority by symbolic name
267  *
268  * @param rName
269  * specify element
270  *
271  * @param prio
272  * specify priority
273  *
274  * @exception Exception
275  * Base class with throw if synbol does not exist
276  *
277  */
278  void Priority(const std::string& rName, const Idx prio) { this->Attributep(rName)->Priority(prio); }
279 
280  /**
281  * Set Priority by index
282  *
283  * @param idx
284  * spcify element
285  *
286  * @param prio
287  * specify priority
288  *
289  * @exception Exception
290  * Base class with throw if specified element does not exist
291  *
292  */
293  void Priority(const Idx idx, const Idx prio) { this->Attributep(idx)->Priority(prio); }
294 
295  /**
296  * Insert with priority by index
297  *
298  * @param idx
299  * spcify element
300  *
301  * @param prio
302  * specify priority
303  *
304  */
305  void InsPriority(const Idx idx, const Idx prio) {
306  this->Insert(idx); Priority(idx,prio);
307  }
308 
309  /**
310  * Insert with priority by name
311  *
312  * @param rName
313  * spcify element
314  *
315  * @param prio
316  * specify priority
317  *
318  */
319  void InsPriority(const std::string& rName, const Idx prio) {
320  Idx idx=this->Insert(rName);
321  Priority(idx,prio);
322  }
323 
324  /**
325  * Set Priorities from other prioritised event set
326  *
327  * @param rOtherSet
328  * set to get priorities from
329  *
330  */
331  void Priorities(const TpEventSet& rOtherSet) {
332  NameSet::Iterator eit=this->Begin();
333  NameSet::Iterator eit_end=this->End();
334  for(;eit!=eit_end;++eit) {
335  if(rOtherSet.Exists(*eit))
336  Priority(*eit,rOtherSet.Priority(*eit));
337  }
338  }
339 
340 
341  /**
342  * Get lowest priority
343  *
344  * @return lowest priority
345  */
346  Idx LowestPriority(void) const {
347  Idx low=FAUDES_IDX_MAX;
348  NameSet::Iterator eit = this->Begin();
349  NameSet::Iterator eit_end = this->End();
350  for(;eit!=eit_end;eit++){
351  Idx prio=Priority(*eit);
352  if(prio<low) low =prio;
353  }
354  return low;
355  }
356 
357  /**
358  * Get highest priority
359  *
360  * @return highest priority
361  */
362  Idx HighestPriority(void) const {
363  Idx high=0;
364  NameSet::Iterator eit = this->Begin();
365  NameSet::Iterator eit_end = this->End();
366  for(;eit!=eit_end;eit++){
367  Idx prio=Priority(*eit);
368  if(prio>high) high =prio;
369  }
370  return high;
371  }
372 
373  /**
374  * Normalise priorities
375  *
376  * Rearrange priorities to be consecutive intergers ranging from 1 to the highest priority.
377  *
378  */
379  void NormalisePriorities(void) {
380  std::map<Idx,Idx> priomap;
381  NameSet::Iterator eit = this->Begin();
382  NameSet::Iterator eit_end = this->End();
383  for(;eit!=eit_end;++eit)
384  priomap[this->Priority(*eit)]=0;
385  std::map<Idx,Idx>::iterator pit;
386  Idx prio=1;
387  for(pit=priomap.begin();pit!=priomap.end();++pit)
388  pit->second=prio++;
389  eit = this->Begin();
390  eit_end = this->End();
391  for(;eit!=eit_end;++eit)
392  this->Priority(*eit,priomap[this->Priority(*eit)]);
393  }
394 
395  protected:
396 
397  /**
398  * Reimplement DoWrite to enforce XML token-io
399  *
400  * @param rTw
401  * TokenWriter to write to
402  * @param rLabel
403  * Section to write
404  * @param pContext
405  * Write context to provide contextual information
406  *
407  * @exception Exception
408  * - IO error (id 2)
409  */
410  virtual void DoWrite(TokenWriter& rTw,const std::string& rLabel="", const Type* pContext=nullptr) const {
411  NameSet::DoXWrite(rTw,rLabel,pContext);
412  }
413 };
414 
415 
416 
417 
418 
419 /**
420  * Convenience typedef for std alphabet with priorities
421  *
422  * @ingroup PrioritiesPlugin
423  */
425 
426 /**
427  * Convenience typedef for fairness constraints (one EventSet per constraint)
428  *
429  * @ingroup PrioritiesPlugin
430  */
432 
433 
434 
435 /*
436 ********************************************************
437 ********************************************************
438 ********************************************************
439 
440 TpEventSet: implementation
441 
442 ********************************************************
443 ********************************************************
444 ********************************************************
445 */
446 
447 /* convenience access to relevant scopes */
448 #define THIS TpEventSet<Attr>
449 #define BASE TaEventSet<Attr>
450 
451 FAUDES_TYPE_TIMPLEMENTATION(Void,TpEventSet<Attr>,TaEventSet<Attr>,template <class Attr>)
452 
453 #undef BASE
454 #undef THIS
455 
456 
457 
458 
459 }//namespace
460 #endif//.H
#define FAUDES_IDX_MAX
#define FAUDES_API
Definition: cfl_platform.h:80
#define FAUDES_TAPI
Definition: cfl_platform.h:83
#define FAUDES_TYPE_TDECLARATION(ftype, ctype, cbase)
Definition: cfl_types.h:883
#define FAUDES_TYPE_DECLARATION(ftype, ctype, cbase)
Definition: cfl_types.h:872
#define FAUDES_TYPE_TIMPLEMENTATION(ftype, ctype, cbase, ctemp)
Definition: cfl_types.h:977
virtual bool IsDefault(void) const
Idx Priority(void) const
bool operator>(const AttributePriority &rOther) const
virtual ~AttributePriority(void)
virtual bool IsDefault(void) const
AttributePriority(const AttributePriority &rSrc)
virtual void DoXWrite(TokenWriter &tw, const std::string &rLabel="", const Type *pContext=0) const
TpEventSet(const std::string &rFileName, const std::string &rLabel="")
Idx Priority(const std::string &rName) const
void NormalisePriorities(void)
Idx LowestPriority(void) const
TpEventSet(const TpEventSet &rOther)
void InsPriority(const std::string &rName, const Idx prio)
void Priorities(const TpEventSet &rOtherSet)
Idx HighestPriority(void) const
void Priority(const std::string &rName, const Idx prio)
Idx Priority(const Idx idx) const
void Priority(const Idx idx, const Idx prio)
void InsPriority(const Idx idx, const Idx prio)
TpEventSet(const EventSet &rOther)
virtual void DoWrite(TokenWriter &rTw, const std::string &rLabel="", const Type *pContext=nullptr) const
#define TaEventSet
Definition: cfl_nameset.h:901
TBaseVector< EventSet > FairnessConstraints
TpEventSet< AttributePriority > EventPriorities
uint32_t Idx

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