hyb_hgenerator.h
Go to the documentation of this file.
1 /** @file hyb_hgenerator.h Linear hybrid automata class TlhaGenerator */
2 
3 /*
4  Hybrid systems plug-in for FAU Discrete Event Systems Library (libfaudes)
5 
6  Copyright (C) 2010 Thomas Moor
7 
8 */
9 
10 
11 
12 #ifndef FAUDES_HYB_HGENERATOR_H
13 #define FAUDES_HYB_HGENERATOR_H
14 
15 
16 #include "corefaudes.h"
17 #include "hyb_attributes.h"
18 
19 //#undef FD_DG
20 //#define FD_DG(message) FAUDES_WRITE_CONSOLE("FAUDES_GENERATOR: " << message)
21 
22 
23 namespace faudes {
24 
25 
26 
27 /**
28  * Generator with linear hybrid automata extensions.
29  *
30  * \section SecHybridHenzinger Henzinger's Linear Hybrid Automata
31  *
32  * The TlhaGenerator implements a linear hybrid automaton as
33  * introduced by Henzinger et al. Here the state space consists of a discrete and a continuous
34  * component. The discrete component is also referre to as a location. While within a location,
35  * the continuous state evolves according to a differential inclusion.
36  * Furthermore, each location and each discrete transition
37  * is equipped with a so called invariant and a guard, respectively. While in a particular location,
38  * the continuous state must satisfy the invariant. Similarly, a transition becomes enabled only
39  * when the respective guard is satisfied. When a transition is executed, the continous state is
40  * reset according to a resetrelation. For the linear case implemented in libFAUDES, the assume that
41  * - the right hand side of the differential inclusion is constant and represented by a closed convex polyhedron;
42  * - invariants and guards are represented by closed convex polyhedra,
43  * - reset relations are affine transformations with an convex polyhedral offset.
44  *
45  *
46  *
47  * \section SecHybridImplement Implementation
48  *
49  * The TlhaGenerator is derived from the cGenerator and requires
50  * adequate attribute parameters that implement invariants, guards, resets and rates. Suitable
51  * attribute classes are provided by AttributeLhaState, AttributeLhaTrans and AttributeLhaGlobal
52  * which may be used either directly or as base classes for further derivatives.
53  * For the event attribute, the lhaGenerator assumes the AttributeCFlags interface. A convenience
54  * definition faudes::lhaGenerator is used for a minimal version with the above mentioned attribute parameters.
55  *
56  *
57  * \section tGeneratorFileFormat File IO
58  *
59  * The TlhaGenerator calsses use the TaGenerator file IO, i.e. the file format is the
60  * same up to hybrid automata related requirements from the attribute parameters. The below
61  * example is from the basic version lhaGenerator.
62  * @code
63 <Generator name="lha example" ftype="LinearHybridAutomaton">
64 
65 <Alphabet>
66 <Event name="north"/>
67 <Event name="south"/>
68 <Event name="west"/>
69 </Alphabet>
70 
71 <StateSet>
72 
73 <State id="1" name="NorthWest">
74 <Initial/>
75 <Rate>
76 <AMatrix rows="4" columns="2">
77  100 0
78 -100 0
79  0 100
80  0 -100
81 </AMatrix>
82 <BVector count="4">
83 -25 50 50 -25
84 </BVector>
85 </Rate>
86 <Invariant>
87 <AMatrix rows="4" columns="2">
88  1 0
89 -1 0
90  0 1
91  0 -1
92 </AMatrix>
93 <BVector count="4">
94 100 0 10 0
95 </BVector>
96 </Invariant>
97 <InitialConstraint>
98 <AMatrix rows="1" columns="2">
99  -1 0
100 </AMatrix>
101 <BVector count="1">
102 -80
103 </BVector>
104 </InitialConstraint>
105 </State>
106 
107 <State id="2" name="SouthWest">
108 <Rate>
109 <AMatrix rows="4" columns="2">
110  100 0
111 -100 0
112  0 100
113  0 -100
114 </AMatrix>
115 <BVector count="4">
116 -25 50 -25 50
117 </BVector>
118 </Rate>
119 <Invariant>
120 <AMatrix rows="4" columns="2">
121  1 0
122 -1 0
123  0 1
124  0 -1
125 </AMatrix>
126 <BVector count="4">
127 100 0 10 0
128 </BVector>
129 </Invariant>
130 </State>
131 
132 <State id="10" name="Target">
133 <Rate>
134 <AMatrix rows="0" columns="2"/>
135 <BVector count="0"/>
136 </Rate>
137 <Invariant>
138 <AMatrix rows="4" columns="2">
139  1 0
140 -1 0
141  0 1
142  0 -1
143 </AMatrix>
144 <BVector count="4">
145 100 0 10 0
146 </BVector>
147 </Invariant>
148 </State>
149 
150 </StateSet>
151 
152 <TransitionRelation>
153 
154 <Transition x1="1" event="north" x2="2">
155 <Guard>
156 <AMatrix rows="1" columns="2">
157 0 -1
158 </AMatrix>
159 <BVector count="1">
160 -9
161 </BVector>
162 </Guard>
163 <Reset>
164 <A1Matrix rows="4" columns="2">
165 -1 0
166  1 0
167  0 -1
168  0 1
169 </A1Matrix>
170 <A2Matrix rows="4" columns="2">
171  1 0
172 -1 0
173  0 1
174  0 -1
175 </A2Matrix>
176 <BVector count="4">
177 -1 1 1 1
178 </BVector>
179 </Reset>
180 </Transition>
181 
182 <Transition x1="1" event="west" x2="10">
183 <Guard>
184 <AMatrix rows="1" columns="2">
185 1 0
186 </AMatrix>
187 <BVector count="1">
188 1
189 </BVector>
190 </Guard>
191 </Transition>
192 
193 <Transition x1="2" event="south" x2="1">
194 <Guard>
195 <AMatrix rows="1" columns="2">
196 0 1
197 </AMatrix>
198 <BVector count="1">
199 1
200 </BVector>
201 </Guard>
202 </Transition>
203 
204 <Transition x1="2" event="west" x2="10">
205 <Guard>
206 <AMatrix rows="1" columns="2">
207 1 0
208 </AMatrix>
209 <BVector count="1">
210 1
211 </BVector>
212 </Guard>
213 </Transition>
214 
215 </TransitionRelation>
216 
217 <LhaStateSpace>
218 <AMatrix rows="4" columns="2">
219 1 0
220 -1 0
221 0 1
222 0 -1
223 </AMatrix>
224 <BVector count="4">
225 100 0 10 0
226 </BVector>
227 </LhaStateSpace>
228 </Generator>
229  * @endcode
230  *
231  *
232  * @ingroup HybridPlugin
233  */
234 
235 template <class GlobalAttr, class StateAttr, class EventAttr, class TransAttr>
236 class TlhaGenerator : public TaGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr> {
237 
238  public:
239  /**
240  * Constructor
241  */
242  TlhaGenerator(void);
243 
244  /**
245  * Copy constructor
246  *
247  * @param rOtherGen
248  */
249  TlhaGenerator(const TlhaGenerator& rOtherGen);
250 
251  /**
252  * Copy constructor (no attributes)
253  *
254  * @param rOtherGen
255  */
256  TlhaGenerator(const vGenerator& rOtherGen);
257 
258  /**
259  * Construct a from file
260  *
261  * @param rFileName
262  * Filename
263  *
264  * @exception Exception
265  * If opening/reading fails an Exception object is thrown (id 1, 50, 51)
266  */
267  TlhaGenerator(const std::string& rFileName);
268 
269  /**
270  * Construct on heap.
271  * Constructs a TlhaGenerator on heap with the same attribute types and the same event- and clock-symboltable.
272  *
273  * @return
274  * new Generator
275  */
276  TlhaGenerator* New(void) const;
277 
278  /**
279  * Construct copy on heap.
280  * Constructs a TlhaGenerator on heap with the same attribute types and the same event- and clock-symboltable.
281  *
282  * @return
283  * new Generator
284  */
285  TlhaGenerator* Copy(void) const;
286 
287 
288  /**
289  * Type test.
290  * Uses C++ dynamic cast to test whether the specified object
291  * casts to a System.
292  *
293  * @return
294  * TlhaGenerator reference if dynamic cast succeeds, else NULL
295  */
296  virtual const Type* Cast(const Type* pOther) const {
297  return dynamic_cast< const TlhaGenerator* > (pOther); };
298 
299 
300  /**
301  * Assignment operator (uses copy)
302  * Note: you must reimplement this operator in derived
303  * classes in order to handle internal pointers correctly
304  *
305  * @param rOtherGen
306  * Other generator
307  */
308  virtual TlhaGenerator& operator= (const TlhaGenerator& rOtherGen) { this->Assign(rOtherGen); return *this;};
309 
310  /**
311  * Assignment operator (uses copy)
312  *
313  * @param rOtherGen
314  * Other generator
315  */
316  virtual TlhaGenerator& operator= (const vGenerator& rOtherGen) { this->Assign(rOtherGen); return *this;};
317 
318 
319 
320  /**
321  * Get dimension of continuous statespace
322  *
323  * @return
324  * Ref to state space polyhedron
325  */
326  int Dimension(void) const;
327 
328  /**
329  * Get continuous statespace
330  *
331  * @return
332  * Ref to state space polyhedron
333  */
334  const Polyhedron& StateSpace(void) const;
335 
336  /**
337  * Set continuous statespace
338  *
339  * Note: if the new state space doe not match the dimension
340  * of the current state space, all invariants, guards etc are
341  * invalidated.
342  *
343  * @param rStateSpace
344  * New state space
345  */
346  void StateSpace(const Polyhedron& rStateSpace);
347 
348  /**
349  * Get invariant of state by index
350  *
351  * @param idx
352  * State index
353  * @return
354  * Const ref to invariant
355  * @exception Exception
356  * - state does not exist (id 90, 95)
357  */
358  const Polyhedron& Invariant(Idx idx) const;
359 
360  /**
361  * Get invariant of state by index
362  *
363  * @param idx
364  * State index
365  * @return
366  * Pointer to invariant
367  * @exception Exception
368  * - state does not exist (id 90, 95)
369  */
370  Polyhedron* Invariantp(Idx idx);
371 
372  /**
373  * Set invariant of state by index
374  *
375  * @param idx
376  * State index
377  * @param rInvariant
378  * New invariant
379  * @exception Exception
380  * - dimension mismatch (id 700)
381  * - state does not exist (id 90, 95)
382  */
383  void Invariant(Idx idx, const Polyhedron& rInvariant);
384 
385  /**
386  * Get invariant of state by name
387  *
388  * @param name
389  * State name
390  * @return
391  * Const ref to invariant
392  * @exception Exception
393  * - state does not exist (id 90, 95)
394  */
395  const Polyhedron& Invariant(const std::string& name) const;
396 
397  /**
398  * Set invariant of state by name
399  *
400  * @param name
401  * State name
402  * @param rInvariant
403  * New invariant
404  * @exception Exception
405  * - dimension mismatch (id 700)
406  * - state does not exist (id 90, 95)
407  */
408  void Invariant(const std::string& name, const Polyhedron& rInvariant);
409 
410 
411  /**
412  * Get initial constraint of state by index
413  *
414  * @param idx
415  * State index
416  * @return
417  * Const ref to initial constraint
418  * @exception Exception
419  * - state does not exist (id 90, 95)
420  */
421  const Polyhedron& InitialConstraint(Idx idx) const;
422 
423  /**
424  * Get initial constraint of state by index
425  *
426  * @param idx
427  * State index
428  * @return
429  * Pointer to initial constraint
430  * @exception Exception
431  * - state does not exist (id 90, 95)
432  */
434 
435  /**
436  * Set initial constraint of state by index
437  *
438  * @param idx
439  * State index
440  * @param rInitialConstraint
441  * New initial constraint
442  * @exception Exception
443  * - dimension mismatch (id 700)
444  * - state does not exist (id 90, 95)
445  */
446  void InitialConstraint(Idx idx, const Polyhedron& rInitialConstraint);
447 
448  /**
449  * Get initial constraint of state by name
450  *
451  * @param name
452  * State name
453  * @return
454  * Const ref to initial constraint
455  * @exception Exception
456  * - state does not exist (id 90, 95)
457  */
458  const Polyhedron& InitialConstraint(const std::string& name) const;
459 
460  /**
461  * Set initial constraint of state by name
462  *
463  * @param name
464  * State name
465  * @param rInitialConstraint
466  * New initial constraint
467  * @exception Exception
468  * - dimension mismatch (id 700)
469  * - state does not exist (id 90, 95)
470  */
471  void InitialConstraint(const std::string& name, const Polyhedron& rInitialConstraint);
472 
473 
474  /**
475  * Get rate of state by index
476  *
477  * @param idx
478  * State index
479  * @return
480  * Const ref to rate
481  * @exception Exception
482  * - state does not exist (id 90, 95)
483  */
484  const Polyhedron& Rate(Idx idx) const;
485 
486  /**
487  * Get rate of state by index
488  *
489  * @param idx
490  * State index
491  * @return
492  * Pointer to rate
493  * @exception Exception
494  * - state does not exist (id 90, 95)
495  */
496  Polyhedron* Ratep(Idx idx);
497 
498  /**
499  * Set rate of state by index
500  *
501  * @param idx
502  * State index
503  * @param rRate
504  * New rate
505  * @exception Exception
506  * - dimension mismatch (id 700)
507  * - state does not exist (id 90, 95)
508  */
509  void Rate(Idx idx, const Polyhedron& rRate);
510 
511  /**
512  * Get rate of state by name
513  *
514  * @param name
515  * State name
516  * @return
517  * Const ref to rate
518  * @exception Exception
519  * - state does not exist (id 90, 95)
520  */
521  const Polyhedron& Rate(const std::string& name) const;
522 
523  /**
524  * Set rate of state by name
525  *
526  * @param name
527  * State name
528  * @param rRate
529  * New rate
530  * @exception Exception
531  * - dimension mismatch (id 700)
532  * - state does not exist (id 90, 95)
533  */
534  void Rate(const std::string& name, const Polyhedron& rRate);
535 
536 
537  /**
538  * Get guard of a transition
539  *
540  * @param rTrans
541  * transition to manupilate
542  * @return
543  * Guard of transition.
544  * @exception Exception
545  * - transition does not exist (id 60)
546  */
547  const Polyhedron& Guard(const Transition& rTrans) const;
548 
549  /**
550  * Get guard of a transition
551  *
552  * @param rTrans
553  * transition to manupilate
554  * @return
555  * Pointer to guard of transition.
556  * @exception Exception
557  * - transition does not exist (id 60)
558  */
559  Polyhedron* Guardp(const Transition& rTrans);
560 
561  /**
562  * Sets guard of a transition
563  *
564  * @param rTrans
565  * transition to manuipulate
566  * @param rGuard
567  * new Guard of transition.
568  * @exception Exception
569  * - dimension mismatch (id 700)
570  * - transition does not exist (id 60)
571  */
572  void Guard(const Transition& rTrans, const Polyhedron& rGuard);
573 
574 
575  /**
576  * Get reset of a transition
577  *
578  * @param rTrans
579  * transition to manupilate
580  * @return
581  * Reset of transition.
582  * @exception Exception
583  * - transition does not exist (id 60)
584  */
585  const LinearRelation& Reset(const Transition& rTrans) const;
586 
587  /**
588  * Get reset of a transition
589  *
590  * @param rTrans
591  * transition to manupilate
592  * @return
593  * Pointer to reset of transition.
594  * @exception Exception
595  * - transition does not exist (id 60)
596  */
597  LinearRelation* Resetp(const Transition& rTrans);
598 
599  /**
600  * Sets reset of a transition
601  *
602  * @param rTrans
603  * transition to manuipulate
604  * @param rReset
605  * new Reset of transition.
606  * @exception Exception
607  * - dimension mismatch (id 700)
608  * - transition does not exist (id 60)
609  */
610  void Reset(const Transition& rTrans, const LinearRelation& rReset);
611 
612 
613  /**
614  * Check if generator is valid.
615  *
616  * The current implementation test consitency of dimensions.
617  *
618  * @return
619  * Success
620  */
621  virtual bool Valid(void) const;
622 
623 
624  /**
625  * Updates internal attributes. As a demo, we set
626  * state flag 0x20000000 for blocking states.
627  * Reimplement to your needs.
628  *
629  * @return true if value changed
630  */
631  virtual bool UpdateAttributes(void);
632 
633 protected:
634 
635  /**
636  * Read generator object from TokenReader, see Type::Read for public wrappers.
637  *
638  * We reimplement this vGenerator function in oredr to fix dimensions of default
639  * invariants etec.
640  *
641  * @param rTr
642  * TokenReader to read from
643  * @param rLabel
644  * Section to read
645  * @param pContext
646  * Read context to provide contextual information (ignored)
647  *
648  * @exception Exception
649  * - token mismatch (id 50, 51, 52, 80, 85)
650  * - IO error (id 1)
651  */
652  virtual void DoRead(TokenReader& rTr, const std::string& rLabel = "", const Type* pContext=0);
653 
654  /**
655  * Write generator object to TokenWriter, see Type::Write for public wrappers.
656  *
657  * We reimplement this vGenerator function in oredr opt for XML write
658  *
659  * @param rTe
660  * TokenWriter to write to
661  * @param rLabel
662  * Section label to write
663  * @param pContext
664  * Write context to provide contextual information (ignored)
665  *
666  * @exception Exception
667  * - IO error (id 1)
668  */
669  virtual void DoWrite(TokenWriter& rTw, const std::string& rLabel = "", const Type* pContext=0) const;
670 
671 
672 
673 }; // end class TlhaGeneraator
674 
675 
676 /** Convenience typedef for std lhaGenerator */
680 
681 
682 
683 // convenient scope macors
684 #define THIS TlhaGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
685 #define BASE TaGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
686 #define TEMP template <class GlobalAttr, class StateAttr, class EventAttr, class TransAttr>
687 
688 
689 // TlhaGenerator(void)
690 TEMP THIS::TlhaGenerator(void) : BASE() {
691  FD_DG("lhaGenerator(" << this << ")::lhaGenerator()");
692 }
693 
694 // TlhaGenerator(rOtherGen)
695 TEMP THIS::TlhaGenerator(const TlhaGenerator& rOtherGen) : BASE(rOtherGen) {
696  FD_DG("lhaGenerator(" << this << ")::lhaGenerator(rOtherGen)");
697 }
698 
699 // TlhaGenerator(rOtherGen)
700 TEMP THIS::TlhaGenerator(const vGenerator& rOtherGen) : BASE(rOtherGen) {
701  FD_DG("lhaGenerator(" << this << ")::lhaGenerator(rOtherGen) with csymtab");
702 }
703 
704 // TlhaGenerator(rFilename)
705 TEMP THIS::TlhaGenerator(const std::string& rFileName) : BASE(rFileName) {
706  FD_DG("lhaGenerator(" << this << ")::lhaGenerator(" << rFileName << ")");
707 }
708 
709 
710 // New
711 TEMP THIS* THIS::New(void) const {
712  // allocate
713  THIS* res = new THIS;
714  // fix base data
715  res->EventSymbolTablep(BASE::mpEventSymbolTable);
716  res->mStateNamesEnabled=BASE::mStateNamesEnabled;
717  // done
718  return res;
719 }
720 
721 // Copy
722 TEMP THIS* THIS::Copy(void) const {
723  // allocate
724  THIS* res = new THIS(*this);
725  // done
726  return res;
727 }
728 
729 // Dimension() const
730 TEMP int THIS::Dimension(void) const {
731  return BASE::pGlobalAttribute->mStateSpace.Dimension();
732 }
733 
734 // StateSpace()
735 TEMP const Polyhedron& THIS::StateSpace(void) const {
736  return BASE::pGlobalAttribute->mStateSpace;
737 }
738 
739 
740 // StateSpace()
741 TEMP void THIS::StateSpace(const Polyhedron& states) {
742  /*
743  if(Dimension()!=states.Dimension()) {
744  // warning? clear all?
745  }
746  */
747  BASE::pGlobalAttribute->mStateSpace=states;
748 }
749 
750 // Invariant(index)
751 TEMP const Polyhedron& THIS::Invariant(Idx stateindex) const {
752  if(!BASE::ExistsState(stateindex)) {
753  std::stringstream errstr;
754  errstr << "state index " << stateindex << " not found in StateSet" << std::endl;
755  throw Exception("lhaGenerator::Invariant", errstr.str(), 90);
756  }
757  return BASE::pStates->Attribute(stateindex).mInvariant;
758 }
759 
760 // Invariantp(index)
761 TEMP Polyhedron* THIS::Invariantp(Idx stateindex) {
762  if(!BASE::ExistsState(stateindex)) {
763  std::stringstream errstr;
764  errstr << "state index " << stateindex << " not found in StateSet" << std::endl;
765  throw Exception("lhaGenerator::Invariant", errstr.str(), 90);
766  }
767  return &BASE::pStates->Attributep(stateindex)->mInvariant;
768 }
769 
770 // Invariant(index, new invariant)
771 TEMP void THIS::Invariant(Idx stateindex, const Polyhedron& rInvariant) {
772  if(!BASE::ExistsState(stateindex)) {
773  std::stringstream errstr;
774  errstr << "state index " << stateindex << " not found in StateSet" << std::endl;
775  throw Exception("lhaGenerator::Invariant", errstr.str(), 90);
776  }
777  if(Dimension()!=rInvariant.Dimension()) {
778  std::stringstream errstr;
779  errstr << "dimension mismatch for state index " << stateindex;
780  throw Exception("lhaGenerator::Invariant", errstr.str(), 700);
781  }
782  BASE::pStates->Attributep(stateindex)->mInvariant=rInvariant;
783 }
784 
785 // Invariant(name)
786 TEMP const Polyhedron& THIS::Invariant(const std::string& statename) const {
787  Idx idx=BASE::StateIndex(statename);
788  return Invariant(idx);
789 }
790 
791 // Invariant(name, new inv)
792 TEMP void THIS::Invariant(const std::string& statename,const Polyhedron& rInvariant) {
793  Idx idx=BASE::StateIndex(statename);
794  Invariant(idx,rInvariant);
795 }
796 
797 
798 
799 // InitialConstraint(index)
800 TEMP const Polyhedron& THIS::InitialConstraint(Idx stateindex) const {
801  if(!BASE::ExistsState(stateindex)) {
802  std::stringstream errstr;
803  errstr << "state index " << stateindex << " not found in StateSet" << std::endl;
804  throw Exception("lhaGenerator::InitialConstraint", errstr.str(), 90);
805  }
806  const Polyhedron& iconstraint = BASE::pStates->Attributep(stateindex)->mInitialConstraint;
807  if(iconstraint.Dimension()==0) const_cast< Polyhedron& >(iconstraint).Dimension(Dimension());
808  return iconstraint;
809 }
810 
811 // InitialConstraintp(index)
812 TEMP Polyhedron* THIS::InitialConstraintp(Idx stateindex) {
813  if(!BASE::ExistsState(stateindex)) {
814  std::stringstream errstr;
815  errstr << "state index " << stateindex << " not found in StateSet" << std::endl;
816  throw Exception("lhaGenerator::InitialConstraint", errstr.str(), 90);
817  }
818  Polyhedron& iconstraint = BASE::pStates->Attributep(stateindex)->mInitialConstraint;
819  if(iconstraint.Dimension()==0) iconstraint.Dimension(Dimension());
820  return &iconstraint;
821 }
822 
823 // InitialConstraint(index, new constraint)
824 TEMP void THIS::InitialConstraint(Idx stateindex, const Polyhedron& rInitialConstraint) {
825  if(!BASE::ExistsState(stateindex)) {
826  std::stringstream errstr;
827  errstr << "state index " << stateindex << " not found in StateSet" << std::endl;
828  throw Exception("lhaGenerator::InitialConstraint", errstr.str(), 90);
829  }
830  if(Dimension()!=rInitialConstraint.Dimension()) {
831  std::stringstream errstr;
832  errstr << "dimension mismatch for state index " << stateindex;
833  throw Exception("lhaGenerator::InitialConstraint", errstr.str(), 700);
834  }
835  BASE::pStates->Attributep(stateindex)->mInitialConstraint=rInitialConstraint;
836 }
837 
838 // InitialConstraint(name)
839 TEMP const Polyhedron& THIS::InitialConstraint(const std::string& statename) const {
840  Idx idx=BASE::StateIndex(statename);
841  return InitialConstraint(idx);
842 }
843 
844 // InitialConstraint(name, new constraint)
845 TEMP void THIS::InitialConstraint(const std::string& statename,const Polyhedron& rInitialConstraint) {
846  Idx idx=BASE::StateIndex(statename);
847  InitialConstraint(idx,rInitialConstraint);
848 }
849 
850 // Rate(index)
851 TEMP const Polyhedron& THIS::Rate(Idx stateindex) const {
852  if(!BASE::ExistsState(stateindex)) {
853  std::stringstream errstr;
854  errstr << "state index " << stateindex << " not found in StateSet" << std::endl;
855  throw Exception("lhaGenerator::Rate", errstr.str(), 90);
856  }
857  return BASE::pStates->Attribute(stateindex).mRate;
858 }
859 
860 // Ratep(index)
861 TEMP Polyhedron* THIS::Ratep(Idx stateindex) {
862  if(!BASE::ExistsState(stateindex)) {
863  std::stringstream errstr;
864  errstr << "state index " << stateindex << " not found in StateSet" << std::endl;
865  throw Exception("lhaGenerator::Rate", errstr.str(), 90);
866  }
867  return &BASE::pStates->Attributep(stateindex)->mRate;
868 }
869 
870 // Rate(index, new invariant)
871 TEMP void THIS::Rate(Idx stateindex, const Polyhedron& rRate) {
872  if(!BASE::ExistsState(stateindex)) {
873  std::stringstream errstr;
874  errstr << "state index " << stateindex << " not found in StateSet" << std::endl;
875  throw Exception("lhaGenerator::Rate", errstr.str(), 90);
876  }
877  if(Dimension()!=rRate.Dimension()) {
878  std::stringstream errstr;
879  errstr << "dimension mismatch for state index " << stateindex;
880  throw Exception("lhaGenerator::Rate", errstr.str(), 700);
881  }
882  BASE::pStates->Attributep(stateindex)->mRate=rRate;
883 }
884 
885 // Rate(name)
886 TEMP const Polyhedron& THIS::Rate(const std::string& statename) const {
887  Idx idx=BASE::StateIndex(statename);
888  return Rate(idx);
889 }
890 
891 // Rate(name, new inv)
892 TEMP void THIS::Rate(const std::string& statename,const Polyhedron& rRate) {
893  Idx idx=BASE::StateIndex(statename);
894  Rate(idx,rRate);
895 }
896 
897 
898 
899 // Guard(index)
900 TEMP const Polyhedron& THIS::Guard(const Transition& trans) const {
901  if(!BASE::ExistsTransition(trans)) {
902  std::stringstream errstr;
903  errstr << "transition " << trans.Str() << " not found in TransRel" << std::endl;
904  throw Exception("lhaGenerator::Guard", errstr.str(), 90);
905  }
906  const Polyhedron& guard = BASE::pTransRel->Attributep(trans)->mGuard;
907  if(guard.Size()==0) const_cast< Polyhedron& >(guard).Dimension(Dimension());
908  return guard;
909 }
910 
911 // Guardp(index)
912 TEMP Polyhedron* THIS::Guardp(const Transition& trans) {
913  if(!BASE::ExistsTransition(trans)) {
914  std::stringstream errstr;
915  errstr << "transition " << trans.Str() << " not found in TransRel" << std::endl;
916  throw Exception("lhaGenerator::Guard", errstr.str(), 90);
917  }
918  Polyhedron& guard = BASE::pTransRel->Attributep(trans)->mGuard;
919  if(guard.Size()==0) const_cast< Polyhedron& >(guard).Dimension(Dimension());
920  return &guard;
921 }
922 
923 // Guard(index, new invariant)
924 TEMP void THIS::Guard(const Transition& trans, const Polyhedron& rGuard) {
925  if(!BASE::ExistsTransition(trans)) {
926  std::stringstream errstr;
927  errstr << "transition " << trans.Str() << " not found in TransRel" << std::endl;
928  throw Exception("lhaGenerator::Guard", errstr.str(), 90);
929  }
930  if(Dimension()!=rGuard.Dimension()) {
931  std::stringstream errstr;
932  errstr << "dimension mismatch for guard at transition " << trans.Str();
933  throw Exception("lhaGenerator::Guard", errstr.str(), 700);
934  }
935  BASE::pTransRel->Attributep(trans)->mGuard=rGuard;
936 }
937 
938 
939 // Reset(index)
940 TEMP const LinearRelation& THIS::Reset(const Transition& trans) const {
941  if(!BASE::ExistsTransition(trans)) {
942  std::stringstream errstr;
943  errstr << "transition " << trans.Str() << " not found in TransRel" << std::endl;
944  throw Exception("lhaGenerator::Reset", errstr.str(), 90);
945  }
946  const LinearRelation& reset = BASE::pTransRel->Attributep(trans)->mReset;
947  if((reset.Identity()) && (reset.Dimension()==0)) const_cast<LinearRelation&>(reset).Identity(Dimension());
948  return reset;
949 }
950 
951 // Resetp(index)
952 TEMP LinearRelation* THIS::Resetp(const Transition& trans) {
953  if(!BASE::ExistsTransition(trans)) {
954  std::stringstream errstr;
955  errstr << "transition " << trans.Str() << " not found in TransRel" << std::endl;
956  throw Exception("lhaGenerator::Reset", errstr.str(), 90);
957  }
958  LinearRelation& reset = BASE::pTransRel->Attributep(trans)->mReset;
959  if((reset.Identity()) && (reset.Dimension()==0)) reset.Identity(Dimension());
960  return &reset;
961 }
962 
963 // Reset(index, new invariant)
964 TEMP void THIS::Reset(const Transition& trans, const LinearRelation& rReset) {
965  if(!BASE::ExistsState(trans)) {
966  std::stringstream errstr;
967  errstr << "transition " << trans.Str() << " not found in TransRel" << std::endl;
968  throw Exception("lhaGenerator::Reset", errstr.str(), 90);
969  }
970  if(Dimension()!=rReset.Dimension()) {
971  std::stringstream errstr;
972  errstr << "dimension mismatch for guard at transition " << trans.Str();
973  throw Exception("lhaGenerator::Reset", errstr.str(), 700);
974  }
975  BASE::pTransRel->Attributep(trans)->mReset=rReset;
976 }
977 
978 
979 
980 // Valid()
981 TEMP bool THIS::Valid(void) const {
982  FD_DG("lhaGenerator(" << this << ")::Valid()");
983  //call base
984  if(!BASE::Valid()) return false;
985  // check my dimensions
986  StateSet::Iterator sit;
987  for(sit = BASE::StatesBegin(); sit!= BASE::StatesEnd(); ++sit) {
988  if(Invariant(*sit).Dimension()!=Dimension()) return false;
989  }
990  TransSet::Iterator tit;
991  for(tit = BASE::TransRelBegin(); tit!= BASE::TransRelEnd(); ++tit) {
992  if(Guard(*tit).Dimension()!=Dimension()) return false;
993  if(Reset(*tit).Dimension()!=Dimension()) return false;
994  }
995  // done
996  return true;
997 }
998 
999 
1000 // UpdateAttributes()
1001 TEMP bool THIS::UpdateAttributes(void) {
1002  FD_DG("lhaGenerator(" << this << ")::UpdateAttributes()");
1003  return true;
1004 }
1005 
1006 
1007 // DoRead()
1008 TEMP void THIS::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
1009  FD_DG("lhaGenerator(" << this << ")::DoRead(...)");
1010  std::cout << "lhaGenerator(" << this << ")::DoRead(...)";
1011  // call base for the actual reading
1012  BASE::DoRead(rTr,rLabel,pContext);
1013  // loop polyhedra and fix
1014  StateSet::Iterator sit;
1015  for(sit = BASE::StatesBegin(); sit!= BASE::StatesEnd(); ++sit) {
1016  if(Invariant(*sit).Dimension()==0) Invariantp(*sit)->Dimension(Dimension());
1017  if(Invariant(*sit).Dimension()!=Dimension()) {
1018  std::stringstream errstr;
1019  errstr << "dimension mismatch for invariant at state index " << *sit;
1020  throw Exception("lhaGenerator::DoRead", errstr.str(), 700);
1021  }
1022  if(InitialConstraint(*sit).Dimension()==0) InitialConstraintp(*sit)->Dimension(Dimension());
1023  if(InitialConstraint(*sit).Dimension()!=Dimension()) {
1024  std::stringstream errstr;
1025  errstr << "dimension mismatch for initial constraint at state index " << *sit;
1026  throw Exception("lhaGenerator::DoRead", errstr.str(), 700);
1027  }
1028  if(Rate(*sit).Dimension()==0) Ratep(*sit)->Dimension(Dimension());
1029  if(Rate(*sit).Dimension()!=Dimension()) {
1030  std::stringstream errstr;
1031  errstr << "dimension mismatch for rate at state index " << *sit;
1032  throw Exception("lhaGenerator::DoRead", errstr.str(), 700);
1033  }
1034  }
1035  TransSet::Iterator tit;
1036  for(tit = BASE::TransRelBegin(); tit!= BASE::TransRelEnd(); ++tit) {
1037  if(Guard(*tit).Dimension()==0) Guardp(*tit)->Dimension(Dimension());
1038  if(Guard(*tit).Dimension()!=Dimension()) {
1039  std::stringstream errstr;
1040  errstr << "dimension mismatch for guard transition index " << tit->Str();
1041  throw Exception("lhaGenerator::DoRead", errstr.str(), 700);
1042  }
1043  if(Reset(*tit).Dimension()==0) Resetp(*tit)->Dimension(Dimension());
1044  if(Reset(*tit).Dimension()!=Dimension()) {
1045  std::stringstream errstr;
1046  errstr << "dimension mismatch for reset transition index " << tit->Str();
1047  throw Exception("lhaGenerator::DoRead", errstr.str(), 700);
1048  }
1049  }
1050 }
1051 
1052 // DoWrite()
1053 TEMP void THIS::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const {
1054  this->DoXWrite(rTw,rLabel,pContext);
1055 }
1056 
1057 #undef BASE
1058 #undef THIS
1059 #undef TEMP
1060 
1061 
1062 
1063 } // namespace faudes
1064 
1065 
1066 #endif
1067 
#define FD_DG(message)
Debug: optional report on generator operations.
Faudes exception class.
Linear relation on R^n.
bool Identity(void) const
Test for data format.
int Dimension(void) const
Get dimension.
Polyhedron in R^n.
Idx Size(void) const
Get size.
int Dimension(void) const
Get dimension.
TBaseSet< Transition, TransSort::X1EvX2 >::Iterator Iterator
Iterator on transition.
Definition: cfl_transset.h:269
Generator with specified attribute types.
virtual TaGenerator & Assign(const Type &rSrc)
Copy from other faudes Type (try to cast to agenerator or pass to base)
Set of indices with attributes.
Definition: cfl_indexset.h:316
Set of Transitions with attributes.
Definition: cfl_transset.h:991
Generator with linear hybrid automata extensions.
virtual bool UpdateAttributes(void)
Updates internal attributes.
virtual bool Valid(void) const
Check if generator is valid.
virtual void DoRead(TokenReader &rTr, const std::string &rLabel="", const Type *pContext=0)
Read generator object from TokenReader, see Type::Read for public wrappers.
const Polyhedron & Rate(Idx idx) const
Get rate of state by index.
TlhaGenerator * Copy(void) const
Construct copy on heap.
const Polyhedron & InitialConstraint(Idx idx) const
Get initial constraint of state by index.
Polyhedron * Guardp(const Transition &rTrans)
Get guard of a transition.
virtual void DoWrite(TokenWriter &rTw, const std::string &rLabel="", const Type *pContext=0) const
Write generator object to TokenWriter, see Type::Write for public wrappers.
int Dimension(void) const
Get dimension of continuous statespace.
virtual const Type * Cast(const Type *pOther) const
Type test.
const Polyhedron & StateSpace(void) const
Get continuous statespace.
TlhaGenerator * New(void) const
Construct on heap.
Polyhedron * InitialConstraintp(Idx idx)
Get initial constraint of state by index.
const Polyhedron & Guard(const Transition &rTrans) const
Get guard of a transition.
TlhaGenerator(void)
Constructor.
Polyhedron * Ratep(Idx idx)
Get rate of state by index.
const Polyhedron & Invariant(Idx idx) const
Get invariant of state by index.
const LinearRelation & Reset(const Transition &rTrans) const
Get reset of a transition.
LinearRelation * Resetp(const Transition &rTrans)
Get reset of a transition.
virtual TlhaGenerator & operator=(const TlhaGenerator &rOtherGen)
Assignment operator (uses copy) Note: you must reimplement this operator in derived classes in order ...
Polyhedron * Invariantp(Idx idx)
Get invariant of state by index.
A TokenReader reads sequential tokens from a file or string.
A TokenWriter writes sequential tokens to a file, a string or stdout.
Triple (X1,Ev,X2) to represent current state, event and next state.
Definition: cfl_transset.h:57
std::string Str(void) const
Pretty print to string.
Definition: cfl_transset.h:111
Base class of all libFAUDES objects that participate in the run-time interface.
Definition: cfl_types.h:239
Base class of all FAUDES generators.
Includes all libFAUDES headers, no plugins.
#define TEMP
#define BASE
#define THIS
libFAUDES resides within the namespace faudes.
uint32_t Idx
Type definition for index type (allways 32bit)
TaIndexSet< AttributeLhaState > LhaStateSet
TaTransSet< AttributeLhaTrans > LhaTransSet
TlhaGenerator< AttributeLhaGlobal, AttributeLhaState, AttributeCFlags, AttributeLhaTrans > LinearHybridAutomaton
Convenience typedef for std lhaGenerator.

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