omg_3_rabin.cpp
Go to the documentation of this file.
1 /** @file omg_3_rabin.cpp
2 
3 Tutorial on Rabin automata
4 
5 @ingroup Tutorials
6 
7 @include omg_3_rabin.cpp
8 
9 */
10 
11 #include "libfaudes.h"
12 
13 using namespace faudes;
14 
15 
16 int main(void) {
17 
18  ////////////////////////////////////////////////////
19  // Have some state sets to play with
20  ////////////////////////////////////////////////////
21 
22  StateSet statesa, statesb, statesc;
23  statesa.FromString("<S> 1 2 3 </S>");
24  statesb.FromString("<S> 4 5 6 </S>");
25  statesc.FromString("<S> 7 8 9 </S>");
26 
27  ////////////////////////////////////////////////////
28  // RabinPair
29  ////////////////////////////////////////////////////
30 
31  // say hello
32  std::cout << "======== a Rabin pair" << std::endl;
33 
34  // set up a Rabin pair programatically
35  RabinPair rpair;
36  rpair.RSet().InsertSet(statesa);
37  rpair.ISet().InsertSet(statesa + statesb);
38 
39  // further inspect/manipulate as any state set; e.g.
40  rpair.ISet().Erase(5);
41 
42  // report result
43  rpair.Write();
44  std::cout << std::endl;
45 
46  ////////////////////////////////////////////////////
47  // RabinAceptance
48  ////////////////////////////////////////////////////
49 
50  // say hello
51  std::cout << "======== a Rabin acceptance condition" << std::endl;
52 
53  // set up a Rabin acceptance condition programatically
54  RabinAcceptance raccept;
55  raccept.Insert(rpair);
56  rpair.Clear();
57  rpair.RSet().InsertSet(statesc);
58  rpair.ISet().InsertSet(statesc + statesb);
59  raccept.Insert(rpair);
60 
61  // report
62  raccept.Write();
63  std::cout << std::endl;
64  raccept.SWrite();
65  std::cout << std::endl;
66 
67  // test file io
68  std::cout << "======== serialisation " << std::endl;
69  raccept.Write("tmp_raccept.txt");
70  RabinAcceptance rareadback;
71  rareadback.Read("tmp_raccept.txt");
72  bool eq1= rareadback==raccept;
73  if(eq1) {
74  std::cout << "== readback ok" << std::endl;
75  } else {
76  std::cout << "== readback test case FAIL" << std::endl;
77  }
78  std::cout << std::endl;
79 
80  // record test case
81  FAUDES_TEST_DUMP("readback from file (expecy true)", eq1);
82 
83 
84  // manipulate/inspect with the BaseVector interface, e.g. iterate over Rabin pairs
85  RabinAcceptance::Iterator rit;
86  rit=raccept.Begin();
87  for(;rit!=raccept.End();++rit) {
88  rpair=*rit;
89  std::cout << "found a Robin pair with RSet " << rpair.RSet().ToString() << std::endl;
90  }
91  std::cout << std::endl;
92 
93  // manipulate/inspect with the BaseVector interface, e.g. edit Rabin pair bhy iterator
94  rit=raccept.Begin();
95  rit->ISet().Erase(6); // this is why we use a vector and not a set ...
96 
97  // check euality operator // ... with sorting this would make much more sense
98  bool eq2= rareadback==raccept;
99  if(eq2) {
100  std::cout << "== still equal? FAIL" << std::endl;
101  } else {
102  std::cout << "== sensed mismatch: ok" << std::endl;
103  }
104  std::cout << std::endl;
105 
106  // record test case
107  FAUDES_TEST_DUMP("readback from file (expecy true)", eq2);
108 
109  // report as
110  raccept.Write();
111  std::cout << std::endl;
112 
113 
114  ////////////////////////////////////////////////////
115  // Testing strict XML and file IO
116  ////////////////////////////////////////////////////
117 
118  // show
119  std::cout << "======== strict XML serialisation" << std::endl;
120  raccept.XWrite();
121  std::cout << std::endl;
122 
123  // record test case
124  FAUDES_TEST_DUMP("raccept stats",raccept);
125 
126 
127  ////////////////////////////////////////////////////
128  // Rabin automata basics
129  ////////////////////////////////////////////////////
130 
131  // raed from file
132  std::cout << "======== Rabin automaton from file" << std::endl;
133  RabinAutomaton ar;
134  ar.Read("data/omg_rabinaut.gen");
135 
136  // show
137  std::cout << "=== automaton" << std::endl;
138  ar.XWrite();
139  std::cout << "=== acceptance condition only" << std::endl;
140  ar.RabinAcceptance().XWrite();
141  std::cout << std::endl;
142  std::cout << "=== edit in automaton" << std::endl;
143  ar.RabinAcceptance().Begin()->RSet().Insert(11);
144  ar.RabinAcceptance().XWrite();
145  std::cout << "=== statistics" << std::endl;
146  ar.SWrite();
147  std::cout << std::endl;
148 
149 
150  ////////////////////////////////////////////////////
151  // Rabin automaton trim
152  ////////////////////////////////////////////////////
153 
154  std::cout << "===== trimming/lifeness" << std::endl;
155 
156  // read from file
157  ar.Read("data/omg_rnottrim.gen");
158  // test
159  bool trim=IsRabinTrim(ar);
160  if(trim)
161  std::cout << "test automaton is trim (test case ERROR)" << std::endl;
162  else
163  std::cout << "test automaton is not trim (expected)" << std::endl;
164  // show life states per Rabin pair
165  raccept=ar.RabinAcceptance();
166  rit=raccept.Begin();
167  for(;rit!=raccept.End();++rit) {
168  StateSet inv;
169  RabinLifeStates(ar,*rit,inv);
170  std::cout << "=== life states for Rabin pair " << rit->Name() << std::endl;
171  ar.WriteStateSet(inv);
172  }
173  std::cout << std::endl;
174  // record test case
175  FAUDES_TEST_DUMP("trim test 1",trim);
176  // copy for docs
177  ar.Write("tmp_omg_rnottrim.gen");
178  try {
179  ar.GraphWrite("tmp_omg_rnottrim.png");
180  } catch(faudes::Exception& exception) {
181  std::cout << "omg_3_rabin: cannot execute graphviz' dot. " << std::endl;
182  }
183 
184  // trim the automaton
185  std::cout << "=== trim Rabin automaton" << std::endl;
186  RabinTrim(ar);
187  // test
188  trim=IsRabinTrim(ar);
189  if(trim)
190  std::cout << "trimed automaton is trim (expected)" << std::endl;
191  else
192  std::cout << "trimed automaton is not trim (test case ERROR)" << std::endl;
193  // record test case
194  FAUDES_TEST_DUMP("trim",ar);
195  FAUDES_TEST_DUMP("trim test 2",trim);
196  // copy for docs
197  ar.Write("tmp_omg_rtrim.gen");
198  try {
199  ar.GraphWrite("tmp_omg_rtrim.png");
200  } catch(faudes::Exception& exception) {
201  std::cout << "omg_3_rabin: cannot execute graphviz' dot. " << std::endl;
202  }
203 
204  // simplify the automaton
205  std::cout << "=== simplify Rabin acceptance" << std::endl;
206  ar.Read("data/omg_rnottrim.gen");
207  RabinSimplify(ar);
208  // test
209  trim=IsRabinTrim(ar);
210  if(trim)
211  std::cout << "simplified automaton is trim (test case ERROR)" << std::endl;
212  else
213  std::cout << "simplified is not trim (expected)" << std::endl;
214  // copy for docs
215  ar.Write("tmp_omg_rsimple.gen");
216  try {
217  ar.GraphWrite("tmp_omg_rsimple.png");
218  } catch(faudes::Exception& exception) {
219  std::cout << "omg_3_rabin: cannot execute graphviz' dot. " << std::endl;
220  }
221  // record test case
222  FAUDES_TEST_DUMP("simplify",ar);
223  FAUDES_TEST_DUMP("trim test",trim);
224 
225 
226  // best effort
227  std::cout << "=== trim and simplify Rabin acceptance" << std::endl;
228  ar.Read("data/omg_rnottrim.gen");
229  RabinTrim(ar);
230  RabinSimplify(ar);
231  // test
232  trim=IsRabinTrim(ar);
233  if(trim)
234  std::cout << "trimed automaton is trim (expected)" << std::endl;
235  else
236  std::cout << "trimed automaton is not trim (test case ERROR)" << std::endl;
237  // copy for docs
238  ar.Write("tmp_omg_rtrims.gen");
239  try {
240  ar.GraphWrite("tmp_omg_rtrims.png");
241  } catch(faudes::Exception& exception) {
242  std::cout << "omg_3_rabin: cannot execute graphviz' dot. " << std::endl;
243  }
244  // record test case
245  FAUDES_TEST_DUMP("trim + simplify",ar);
246  FAUDES_TEST_DUMP("trim test 3",trim);
247 }
248 
#define FAUDES_TEST_DUMP(mes, dat)
Definition: cfl_utils.h:505
StateSet & RSet(void)
Definition: omg_rabinacc.h:68
virtual void Clear(void)
StateSet & ISet(void)
Definition: omg_rabinacc.h:84
virtual void Insert(const T &rElem)
Iterator End(void)
Iterator Begin(void)
void RabinAcceptance(const faudes::RabinAcceptance &rRabAcc)
Definition: omg_rabinaut.h:326
void Read(const std::string &rFileName, const std::string &rLabel="", const Type *pContext=0)
Definition: cfl_types.cpp:267
std::string ToString(const std::string &rLabel="", const Type *pContext=0) const
Definition: cfl_types.cpp:175
void FromString(const std::string &rString, const std::string &rLabel="", const Type *pContext=0)
Definition: cfl_types.cpp:281
virtual void XWrite(const std::string &pFileName, const std::string &rLabel="", const Type *pContext=0) const
Definition: cfl_types.cpp:206
void Write(const Type *pContext=0) const
Definition: cfl_types.cpp:145
void SWrite(TokenWriter &rTw) const
Definition: cfl_types.cpp:262
void WriteStateSet(const StateSet &rStateSet) const
void GraphWrite(const std::string &rFileName, const std::string &rOutFormat="", const std::string &rDotExec="dot") const
virtual void InsertSet(const TBaseSet &rOtherSet)
Definition: cfl_baseset.h:2194
virtual bool Erase(const T &rElem)
Definition: cfl_baseset.h:2226
bool RabinTrim(RabinAutomaton &rRAut)
void RabinLifeStates(const TransSet &rTransRel, const TransSetX2EvX1 &rRevTransRel, const RabinPair &rRPair, StateSet &rLife)
bool IsRabinTrim(const RabinAutomaton &rRAut)
void RabinSimplify(RabinAutomaton &rRAut)
int main(void)
Definition: omg_3_rabin.cpp:16

libFAUDES 2.33l --- 2025.09.16 --- c++ api documentaion by doxygen