tp_1_tgenerator.cpp
Go to the documentation of this file.
1 /** @file tp_1_tgenerator.cpp
2 
3 Tutorial, class faudes::TimedGenerator. This tutorial demonstrates
4 basic maintenance of TimedGenerator objects
5 
6 @ingroup Tutorials
7 
8 @include tp_1_tgenerator.cpp
9 
10 */
11 
12 #include "libfaudes.h"
13 
14 
15 // make the faudes namespace available to our program
16 using namespace faudes;
17 
18 
19 
20 /////////////////
21 // main program
22 /////////////////
23 
24 int main() {
25 
26  ////////////////////////////////////////////////////
27  // constructor and file io
28  // EventSets
29  ////////////////////////////////////////////////////
30 
31  // Create an empty TimedGenerator object
32  TimedGenerator g1;
33 
34  // Create a TimedGenerator by reading a TimedGenerator file
35  TimedGenerator g2("data/tsimplemachine.gen");
36 
37  // Create a TimedGenerator by reading a Generator file (no timing, no control)
38  TimedGenerator g3("data/simplemachine.gen");
39 
40  // Read a TimedGenerator from a System file (no timing)
41  g1.Read("data/csimplemachine.gen");
42 
43  // Copy a Generator to a TimedGenerator (no timing, no control)
44  TimedGenerator g4;
45  Generator g5("data/simplemachine.gen");
46  g4.Assign(g5);
47 
48  // Write the TimedGenerator to a files
49  g1.Write("tmp_tsimplemachine1.gen");
50  g2.Write("tmp_tsimplemachine2.gen");
51  g3.Write("tmp_tsimplemachine3.gen");
52  g4.Write("tmp_tsimplemachine4.gen");
53 
54  // See result on console
55  std::cout << "######################################\n";
56  std::cout << "# tc simple machine (\n";
57  g2.DWrite();
58  std::cout << "######################################\n";
59 
60  // Test protocol
61  FAUDES_TEST_DUMP("construct g1",g1);
62  FAUDES_TEST_DUMP("construct g2",g2);
63  FAUDES_TEST_DUMP("construct g3",g3);
64  FAUDES_TEST_DUMP("construct g4",g4);
65 
66 
67  ////////////////////////////////////////////////////
68  // access to controllability status of events
69  //
70  // (using System methods)
71  ////////////////////////////////////////////////////
72 
73  // Retrieve an EventSet containing all the controllabe events
74 
75  EventSet cevents = g2.ControllableEvents();
76 
77  // See result on console
78  std::cout << "######################################\n";
79  std::cout << "# controllabel events\n";
80  cevents.DWrite();
81  std::cout << "######################################\n";
82 
83  // Test protocol
84  FAUDES_TEST_DUMP("contr attrib",cevents);
85 
86 
87  ////////////////////////////////////////////////////
88  // access/edit timing
89  ////////////////////////////////////////////////////
90 
91  // Insert a clock to tcsimplemachine
92  g2.InsClock("cRepair");
93 
94  // Read clocks from tcsimplemachine and set them for csimplemachine
95  ClockSet clocks = g2.Clocks();
96 
97  // Report
98  std::cout << "######################################\n";
99  std::cout << "# tcsimple machine clocks \n";
100  clocks.DWrite();
101  std::cout << "######################################\n";
102 
103 
104  // Add constraints to invariant
105  TimeConstraint inv;
106  inv.Insert("cRepair",ElemConstraint::LessEqual,20);
107  g2.InsInvariant("down", inv);
108 
109  // Add constraints to guard and a reset
110  TransSet::Iterator tit;
111  tit=g2.FindTransition("down","lambda","idle");
112  if(tit==g2.TransRelEnd()) {}; // not found
113  TimeConstraint guard;
114  guard.Insert("cRepair",ElemConstraint::GreaterEqual,10);
115  g2.InsGuard(*tit, guard);
116  tit=g2.FindTransition("busy","mue","down");
117  if(tit==g2.TransRelEnd()) {}; // not found
118  ClockSet resets;
119  resets.Insert("cRepair");
120  g2.InsResets(*tit, resets);
121 
122  // Fix name
123  g2.Name("tc simple machine with timed repair");
124 
125  // Report
126  std::cout << "######################################\n";
127  std::cout << "# tcsimple machine with timed repair \n";
128  g2.DWrite();
129  g2.ActiveClocks().DWrite();
130  std::cout << "######################################\n";
131 
132  // Copy to g1 and remove a clock
133  g1=g2;
134  g1.DelClock("cBusy");
135 
136  // Report
137  std::cout << "######################################\n";
138  std::cout << "# tcsimple machine with untimed busy \n";
139  g1.DWrite();
140  g1.ActiveClocks().DWrite();
141  std::cout << "######################################\n";
142 
143 
144  // Test protocol
145  FAUDES_TEST_DUMP("edit timing 1",g1);
146  FAUDES_TEST_DUMP("edit timing 2",g2);
147 
148 
149  // Read clocks from tcsimplemachine and set them for csimplemachine
150  std::cout << "######################################\n";
151  std::cout << "# csimple machine (active clocks) \n";
152  g3.InsClocks(g2.ActiveClocks());
153  std::cout << g3.Clocks().ToString() << "\n";
154 
155  // Read invriants from tcsimplemachine and set them for csimplemachine
156  std::cout << "######################################\n";
157  std::cout << "# csimple machine (invariants) \n";
158  StateSet::Iterator sit;
159  for(sit=g2.StatesBegin(); sit!=g2.StatesEnd(); sit++) {
160  TimeConstraint invariant = g2.Invariant(*sit);
161  std::string state = g2.StateName(*sit);
162  std::cout << state << " inv " << invariant.ToString() << "\n";
163  g3.Invariant(state, invariant);
164  }
165 
166  // Read guards and resets from tcsimplemachine and set them for csimplemachine
167  std::cout << "######################################\n";
168  std::cout << "# csimple machine (guards and resets) \n";
169  TransSet::Iterator stit, dtit;
170  for(stit=g2.TransRelBegin(); stit!= g2.TransRelEnd(); stit++) {
171  std::cout << "src transition: " << g2.TStr(*stit) << "\n";
172  std::cout << "guard: " << g2.Guard(*stit).ToString() << "\n";
173  std::cout << "resets: " << g2.Resets(*stit).ToString() << "\n";
174  dtit=g3.FindTransition(g2.StateName(stit->X1),g2.EventName(stit->Ev),g2.StateName(stit->X2));
175  g3.Guard(*dtit,g2.Guard(*stit));
176  g3.Resets(*dtit,g2.Resets(*stit));
177  }
178 
179  // Report
180  std::cout << "######################################\n";
181  std::cout << "# csimple machine with timing \n";
182  g3.DWrite();
183  std::cout << "######################################\n";
184 
185 
186  // Check validiy (internal names and symboltables)
187  if(g3.Valid())
188  std::cout << "############# valid #################\n";
189 
190  // Test protocol
191  FAUDES_TEST_DUMP("edit timing 3",g3);
192 
193  return 0;
194 }
195 
196 
197 
#define FAUDES_TEST_DUMP(mes, dat)
Test protocol record macro ("mangle" filename for platform independance)
Definition: cfl_helper.h:483
Container class to model a set of clocks.
Set of indices with symbolic names.
Definition: cfl_nameset.h:69
bool Insert(const Idx &rIndex)
Add an element by index.
TBaseSet< Transition, TransSort::X1EvX2 >::Iterator Iterator
Iterator on transition.
Definition: cfl_transset.h:269
EventSet ControllableEvents(void) const
Get EventSet with controllable events.
virtual TcGenerator & Assign(const Type &rSource)
Assignment method.
A TimeConstraint is a set of elementary clock constraints.
std::string ToString(void) const
Write to a std::string.
Iterator Insert(const ElemConstraint &rElemConstr)
Adds an elementary clock constraint to the time constraint.
Generator with timing extensions.
bool InsClock(Idx index)
Add an existing clock to mClcoks by index.
void InsGuard(const Transition &rTrans, const TimeConstraint &rConstraints)
adds constraints to Guard of a transition
const ClockSet & Clocks(void) const
Get clockset as const reference.
void InsInvariant(const std::string &name, const TimeConstraint &rConstraints)
Ins invariant of state by name.
const TimeConstraint & Invariant(Idx idx) const
Get invariant of state by index.
bool DelClock(Idx index)
Delete clock from generator by index.
virtual bool Valid(void) const
Check if generator is valid.
ClockSet ActiveClocks(void) const
Returns all clocks used by all TimeConstraints and Resets.
void InsResets(const Transition &rTrans, const ClockSet &rMoreResets)
adds Resets of a transition
void InsClocks(const ClockSet &rClockSet)
Add new named clocks to generator.
void Resets(const Transition &rTrans, const ClockSet &rResets)
Sets Resets of a transition.
void Guard(const Transition &rTrans, const TimeConstraint &rGuard)
Sets Guard of a transition.
void DWrite(const Type *pContext=0) const
Write configuration data to console, debugging format.
Definition: cfl_types.cpp:225
void Read(const std::string &rFileName, const std::string &rLabel="", const Type *pContext=0)
Read configuration data from file with label specified.
Definition: cfl_types.cpp:261
std::string ToString(const std::string &rLabel="", const Type *pContext=0) const
Write configuration data to a string.
Definition: cfl_types.cpp:169
void Write(const Type *pContext=0) const
Write configuration data to console.
Definition: cfl_types.cpp:139
Base class of all FAUDES generators.
StateSet::Iterator StatesBegin(void) const
Iterator to Begin() of state set.
TransSet::Iterator TransRelBegin(void) const
Iterator to Begin() of transition relation.
std::string TStr(const Transition &rTrans) const
Return pretty printable transition (eg for debugging)
std::string StateName(Idx index) const
State name lookup.
void Name(const std::string &rName)
Set the generator's name.
StateSet::Iterator StatesEnd(void) const
Iterator to End() of state set.
TransSet::Iterator TransRelEnd(void) const
Iterator to End() of transition relation.
std::string EventName(Idx index) const
Event name lookup.
TransSet::Iterator FindTransition(const std::string &rX1, const std::string &rEv, const std::string &rX2) const
iterator to transition given by x1, ev, x2
Includes all libFAUDES headers, incl plugings
libFAUDES resides within the namespace faudes.
int main()

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