hio_constraint.cpp
Go to the documentation of this file.
1 /** @file hio_constraint.cpp Generator with I/O-constraint attributes */
2 
3 /* Hierarchical IO Systems Plug-In for FAU Discrete Event Systems Library (libfaudes)
4 
5  Copyright (C) 2006 Sebastian Perk
6  Copyright (C) 2006 Thomas Moor
7  Copyright (C) 2006 Klaus Schmidt
8 
9 */
10 
11 #include "hio_constraint.h"
12 
13 namespace faudes {
14 
15 // IsHioConstraintForm()
16 bool IsHioConstraintForm(HioConstraint& rHioConstraint,
17  StateSet& rQY,
18  StateSet& rQU,
19  EventSet& rErrEvSet,
20  TransSet& rErrTrSet,
21  StateSet& rErrStSet,
22  std::string& rReportStr)
23  {
24  FD_DF("IsHioConstraintForm("<< rHioConstraint.Name() << ",...)");
25 
26  // prepare results
27  rQY.Clear();
28  rQU.Clear();
29 
30  rErrEvSet.Clear();
31  rErrEvSet.Name("rErrEvSet");
32 
33  rErrTrSet.Clear();
34  rErrTrSet.Name("rErrTrSet");
35 
36  rErrStSet.Clear();
37  rErrStSet.Name("rErrStSet");
38 
39  // used to locally store error states/transitions on each condition
40  StateSet locErrStSet;
41  TransSet locErrTrSet;
42 
43  rReportStr.clear();
44 
45  // meant to be set false on violation of any condition:
46  bool finalResult = true;
47  // used to locally store result on each condition
48  bool localResult = true;
49 
50  // helpers
51 
52  EventSet y = rHioConstraint.YEvents();
53  EventSet u = rHioConstraint.UEvents();
54 
55  StateSet initStates = rHioConstraint.InitStates();
56  StateSet accessibleStates = rHioConstraint.AccessibleSet();
57  StateSet deadEnds;
58 
59  EventSet::Iterator evit;
60  StateSet::Iterator sit;
62 
63  // Info string header
64  rReportStr.append("#########################################################\n");
65  rReportStr.append("########## IsHioConstraintForm("+rHioConstraint.Name()+",...) - test results:\n");
66 
67  /**************************** Precondition: determinism ***********************/
68  // HioConstraint must be deterministic
69  if(!rHioConstraint.IsDeterministic()){
70  rReportStr.append("##### fail: generator is not deterministic!\n");
71  if(initStates.Size()>1) {
72  rErrStSet = initStates;
73  rReportStr.append("##### (amongst others, there is more than one initial state)\n");
74  }
75  finalResult = false;
76  }
77 
78  rReportStr.append("#####\n");
79 
80  // test all conditions verifying I/O-constraint form:
81 
82  /**************************** Condition (i) ***********************/
83  localResult = true;
84  rReportStr.append("########## Condition (i):\n");
85 
86  //YP, UP, YE, UE nonempty?
87  if (y.Empty()) {
88  rReportStr.append("##### fail: empty Y alphabet.\n");
89  localResult=false;
90  finalResult = false;
91  }
92  if (u.Empty()) {
93  rReportStr.append("##### fail: empty U alphabet.\n");
94  localResult=false;
95  finalResult = false;
96  }
97 
98  // check for disjoint eventsets Y and U and for
99  // Y u U == Sigma, ie unique HioEventFlags.
100  // note: by construction, any event has the exclusive property U or Y.
101  // thus, condition (i) is always met at this point.
102 
103  rReportStr.append("##### Condition (i) passed.\n");
104  rReportStr.append("#####\n");
105  /*************************** Condition (i) finished *****************************/
106 
107 
108  /*************************** Condition (ii) ***********************/
109  localResult = true;
110  rReportStr.append("########## Condition (ii):\n");
111 
112  // check if in states QYpYe, QUp and QUe only Y-, UP- and UE-events are active, respectively.
113  for(sit = accessibleStates.Begin(); sit != accessibleStates.End(); ++sit) {
114 
115  bool isY = false;
116  bool isU = false;
117  bool goodState = true;
118 
119  EventSet activeEv = rHioConstraint.ActiveEventSet(*sit);
120 
121  if(activeEv.Empty()) {
122  //dead ends violate condition (vii)
123  deadEnds.Insert(*sit);
124  }
125  else {
126 
127  // get attribute of first event and compare with remaining events
128  evit = activeEv.Begin();
129  isY = rHioConstraint.IsY(*evit);
130  isU = rHioConstraint.IsU(*evit);
131 
132  for(; evit != activeEv.End(); evit++) {
133  if( (isY && !rHioConstraint.IsY(*evit)) ||
134  (isU && !rHioConstraint.IsU(*evit))) {
135  goodState = false;
136  localResult = false;
137  finalResult = false;
138  // add state to error set, go to next state
139  locErrStSet.Insert(*sit);
140  rErrStSet.Insert(*sit);
141  break; // leave loop over active events
142  }
143  }
144 
145  activeEv.Clear();
146 
147  if(!goodState) continue; // if undecidable go on with next state
148 
149  // set state attribute
150  if(isY) {
151  rQY.Insert(*sit);
152  rHioConstraint.SetQY(*sit);
153  }
154  else if(isU) {
155  rQU.Insert(*sit);
156  rHioConstraint.SetQU(*sit);
157  }
158  }
159  }
160 
161  if(localResult) rReportStr.append("##### Condition (ii) passed.\n");
162  // In case of failing condition (ii) further inspection is omitted, as too many consecutive faults are expected.
163  else {
164  rReportStr.append("##### fail: found states with undecidable attribute:\n");
165  rReportStr.append(locErrStSet.ToString()+"\n");
166  locErrStSet.Clear();
167  rReportStr.append("##### Condition (ii) failed.\n");
168  rReportStr.append("########## Termination due to crucial failure. ##########\n");
169  rReportStr.append("###################### No success. ######################\n");
170  rReportStr.append("#########################################################\n");
171  return false;
172  }
173  rReportStr.append("#####\n");
174  /*************************** Condition (ii) finished ****************************/
175 
176 
177  /*************************** Condition (iii) **********************/
178  localResult = true;
179  rReportStr.append("########## Condition (iii):\n");
180 
181  //check if the initial state is a QY-state
182  if(!(initStates <= rQY)) {
183  rReportStr.append("##### fail: some init state(s) is (are) not a QY-state:\n");
184  locErrStSet=initStates-rQY;
185  rReportStr.append(locErrStSet.ToString()+"\n");
186  rErrStSet.InsertSet(locErrStSet);
187  locErrStSet.Clear();
188  localResult = false;
189  finalResult = false;
190  }
191  if(localResult) rReportStr.append("##### Condition (iii) passed.\n");
192  else rReportStr.append("##### Condition (iii) failed.\n");
193  rReportStr.append("#####\n");
194 
195  /*************************** Condition (iii) finished ***************************/
196 
197 
198  /*************************** Condition (iv) ***********************/
199  localResult = true;
200  rReportStr.append("########## Condition (iv):\n");
201 
202  // Y-events have to lead to a QU-state
203  for(sit = rQY.Begin(); sit != rQY.End(); ++sit) {
204  for(tit = rHioConstraint.TransRelBegin(*sit); tit != rHioConstraint.TransRelEnd(*sit); ++tit) {
205  // Y-event to QU-state
206  if( (rHioConstraint.IsY(tit->Ev) && !rQU.Exists(tit->X2)) ) {
207  // add transition to error transition set
208  locErrTrSet.Insert(*tit);
209  rErrTrSet.Insert(*tit);
210  finalResult = false;
211  localResult = false;
212  }
213  }
214  }
215 
216  if(localResult) rReportStr.append("##### Condition (iv) passed.\n");
217  else {
218  rReportStr.append("##### fail: found Y-transitions leading to wrong states:\n");
219  rReportStr.append(locErrTrSet.ToString()+"\n");
220  locErrTrSet.Clear();
221  rReportStr.append("##### Condition (iv) failed.\n");
222  }
223  rReportStr.append("#####\n");
224  /*************************** Condition (iv) finished ****************************/
225 
226 
227  /*************************** Condition (v) ************************/
228  localResult = true;
229  rReportStr.append("########## Condition (v):\n");
230 
231  // U-events have to lead to a QY-state
232  for(sit = rQU.Begin(); sit != rQU.End(); ++sit) {
233  for(tit = rHioConstraint.TransRelBegin(*sit); tit != rHioConstraint.TransRelEnd(*sit); ++tit) {
234  if(!rQY.Exists(tit->X2)) {
235  locErrTrSet.Insert(*tit);
236  rErrTrSet.Insert(*tit);
237  finalResult = false;
238  localResult = false;
239  }
240  }
241  }
242 
243  if(localResult) rReportStr.append("##### Condition (v) passed.\n");
244  else {
245  rReportStr.append("##### fail: found U-transitions leading to wrong states:\n");
246  rReportStr.append(locErrTrSet.ToString()+"\n");
247  locErrTrSet.Clear();
248  rReportStr.append("##### Condition (v) failed.\n");
249  }
250  rReportStr.append("#####\n");
251  /*************************** Condition (v) finished *****************************/
252 
253 
254  /*************************** Condition (vi) **********************/
255  localResult = true;
256  rReportStr.append("########## Condition (vi):\n");
257 
258  // Y must be free in QY-states
259  for(sit = rQY.Begin(); sit != rQY.End(); ++sit) {
260 
261  if(!(y <= rHioConstraint.ActiveEventSet(*sit))) {
262  locErrStSet.Insert(*sit);
263  rErrStSet.Insert(*sit);
264  finalResult = false;
265  localResult = false;
266  }
267  }
268 
269  if(localResult) rReportStr.append("##### Condition (vi) passed.\n");
270  else {
271  rReportStr.append("##### fail: found QY-states with inactive Y-events:\n");
272  rReportStr.append(locErrStSet.ToString()+"\n");
273  locErrStSet.Clear();
274  rReportStr.append("##### Condition (vi) failed.\n");
275  }
276  rReportStr.append("#####\n");
277  /*************************** Condition (vi) finished ***************************/
278 
279 
280  /*************************** Condition (vii) ************************/
281  localResult = true;
282  rReportStr.append("########## Condition (vii):\n");
283 
284  // found dead ends?
285  if(!deadEnds.Empty()) {
286  finalResult = false;
287  localResult = false;
288  rErrStSet.InsertSet(deadEnds);
289  rReportStr.append("##### fail: found dead ends:\n");
290  rReportStr.append(deadEnds.ToString()+"\n");
291  deadEnds.Clear();
292  rReportStr.append("##### Condition (vii) failed.\n");
293  }
294  rReportStr.append("##### Condition (vii) passed.\n");
295  /*************************** Condition (vii) finished *****************************/
296 
297 
298  /*************************** Condition (viii) ***********************/
299  localResult = true;
300  rReportStr.append("########## Condition (viii):\n");
301 
302  // Qm==Q?
303  if(!(accessibleStates<=rHioConstraint.MarkedStates())) {
304  finalResult = false;
305  localResult = false;
306  }
307 
308  if(localResult) rReportStr.append("##### Condition (viii) passed.\n");
309  else {
310  rReportStr.append("##### fail: not all accessible states are marked:\n");
311  locErrStSet=accessibleStates - rHioConstraint.MarkedStates();
312  rErrStSet.InsertSet(locErrStSet);
313  rReportStr.append(locErrStSet.ToString()+"\n");
314  locErrStSet.Clear();
315  rReportStr.append("##### Condition (viii) failed.\n");
316  }
317  rReportStr.append("#####\n");
318  /*************************** Condition (viii) finished ****************************/
319 
320 
321  /*************************** Condition (ix) ************************/
322  rReportStr.append("########## Condition (ix):\n");
323 
324  // make accessible if necessary
325  if(!rHioConstraint.IsAccessible()) {
326  rHioConstraint.Accessible();
327  rReportStr.append("##### warning: non-accessible states have been removed.\n");
328  rReportStr.append("##### Condition (ix) repaired.\n");
329  }
330  else rReportStr.append("##### Condition (ix) passed.\n");
331  /*************************** Condition (ix) finished *****************************/
332 
333 
334 
335  /*************************** Final Result ************************/
336 
337  rReportStr.append("##################### Final result: #####################\n");
338  if(finalResult) {
339  rReportStr.append("############ Generator is in HioConstraintForm. ###########\n");
340  rReportStr.append("#########################################################\n");
341  return true;
342  }
343  else {
344  rReportStr.append("############## Generator is NOT in HioConstraintForm. ##############\n");
345  rReportStr.append("#########################################################\n");
346  return false;
347  }
348 
349 }// END OF IsHioConstraintForm()
350 
351 //IsHioConstraintForm wrapper functions
352 bool IsHioConstraintForm(HioConstraint& rHioConstraint, std::string& rReportStr)
353 {
354  StateSet rQY, rQU;
355  EventSet rErrEvSet;
356  TransSet rErrTrSet;
357  StateSet rErrStSet;
358 
359  return IsHioConstraintForm(rHioConstraint, rQY, rQU, rErrEvSet, rErrTrSet, rErrStSet,rReportStr);
360 }
361 
362 bool IsHioConstraintForm(HioConstraint& rHioConstraint)
363 {
364  StateSet rQY, rQU;
365  EventSet rErrEvSet;
366  TransSet rErrTrSet;
367  StateSet rErrStSet;
368  std::string rReportStr;
369 
370  return IsHioConstraintForm(rHioConstraint, rQY, rQU, rErrEvSet, rErrTrSet, rErrStSet, rReportStr);
371 }
372 
373 // rti function interface
374 void HioStatePartition(HioConstraint& rHioConstraint) {
375  IsHioConstraintForm(rHioConstraint);
376 }
377 
378 
379 
380 } // end namespace
#define FD_DF(message)
Debug: optional report on user functions.
Set of indices.
Definition: cfl_indexset.h:78
Idx Insert(void)
Insert new index to set.
Set of indices with symbolic names.
Definition: cfl_nameset.h:69
bool IsU(Idx index) const
Is event U-event(by index)
bool IsY(Idx index) const
Is event Y-event(by index)
EventSet UEvents(void) const
Get EventSet with U-events.
void SetQU(Idx index)
Mark event as QU-state (by index)
void SetQY(Idx index)
Mark event as QY-state (by index)
EventSet YEvents(void) const
Get EventSet with Y-events.
bool Insert(const Transition &rTransition)
Add a Transition.
TBaseSet< Transition, TransSort::X1EvX2 >::Iterator Iterator
Iterator on transition.
Definition: cfl_transset.h:269
std::string ToString(const std::string &rLabel="", const Type *pContext=0) const
Write configuration data to a string.
Definition: cfl_types.cpp:169
const StateSet & MarkedStates(void) const
Return const ref of marked states.
EventSet ActiveEventSet(Idx x1) const
Return active event set at state x1.
const StateSet & InitStates(void) const
Const ref to initial states.
TransSet::Iterator TransRelBegin(void) const
Iterator to Begin() of transition relation.
bool Accessible(void)
Make generator accessible.
bool IsAccessible(void) const
Check if generator is accessible.
StateSet AccessibleSet(void) const
Compute set of accessible states.
void Name(const std::string &rName)
Set the generator's name.
TransSet::Iterator TransRelEnd(void) const
Iterator to End() of transition relation.
bool IsDeterministic(void) const
Check if generator is deterministic.
bool Empty(void) const
Test whether if the TBaseSet is Empty.
Definition: cfl_baseset.h:1824
bool Exists(const T &rElem) const
Test existence of element.
Definition: cfl_baseset.h:2115
virtual void Clear(void)
Clear all set.
Definition: cfl_baseset.h:1902
Iterator End(void) const
Iterator to the end of set.
Definition: cfl_baseset.h:1896
virtual void InsertSet(const TBaseSet &rOtherSet)
Insert elements given by rOtherSet.
Definition: cfl_baseset.h:1987
Iterator Begin(void) const
Iterator to the begin of set.
Definition: cfl_baseset.h:1891
const std::string & Name(void) const
Return name of TBaseSet.
Definition: cfl_baseset.h:1755
Idx Size(void) const
Get Size of TBaseSet.
Definition: cfl_baseset.h:1819
Generator with I/O-constraint attributes.
libFAUDES resides within the namespace faudes.
bool IsHioConstraintForm(HioConstraint &rHioConstraint, StateSet &rQY, StateSet &rQU, EventSet &rErrEvSet, TransSet &rErrTrSet, StateSet &rErrStSet, std::string &rReportStr)
IsHioConstraintForm: check if rHioConstraint is in I/O-constraint form and assign state attributes.
void HioStatePartition(HioConstraint &rHioConstraint)
Function definition for run-time interface.

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