DiaDes  0.1
DIAgnosis of Discrete-Event System
UnfoldingStateTable.hh
Go to the documentation of this file.
1 #ifndef UNFOLDINGSTATETABLE_HH_
2 #define UNFOLDINGSTATETABLE_HH_
3 #include<list>
7 
8 using namespace std;
9 
10 namespace Diades
11 {
12  namespace Sdmdl
13  {
14 
20  template<class T>
21  class StateNode {
22  private:
23  int _state;
24  T _inf;
25  bool _tag;
26  list<StateNode *> _children;
27 
28 
29  public:
34  StateNode():_state(),_inf(),_tag(false),_children(){}
35 
36 
43  StateNode(int i):_state(i),_inf(),_tag(false),_children(){
44  }
45 
53  StateNode(int i,const T & inf):_state(i),_inf(inf),_tag(true),_children(){}
54 
58  bool isTagged() const { return _tag; }
59 
63  int state() const { return _state; }
64 
69  void setInfo(const T & inf) {
70  _inf = inf;
71  _tag = true;
72 
73  }
74 
78  T getInfo() const {
79  return _inf;
80  }
81 
85  list<StateNode *> * children() { return &_children; }
86 
90  const list<StateNode *> * children() const { return &_children; }
91 
92  };
93 
97  template<typename T>
98  inline ostream& operator<<(ostream &os, const StateNode<T> & stateNode)
99  {
100  os << "***************************************************" << endl;
101  os << "Start StateNode: " << &stateNode << endl;
102  os << "***************************************************" << endl;
103  os << "\t state: " << stateNode.state() << endl;
104  os << "\t children: " << endl;
105  for(typename list<StateNode<T> *>::const_iterator it = stateNode.children()->begin();
106  it != stateNode.children()->end();
107  ++it)
108  {
109  os << **it << endl;
110  }
111  os << "***************************************************" << endl;
112  os << "End StateNode: " << &stateNode << endl;
113  os << "***************************************************" << endl;
114  return os;
115  }
116 
117 
127  template <class T>
129 
130  private:
131  list<StateNode<T> *> roots;
132 
133  public:
134 
140 
147  T getInfo(const UnfoldingState & s, bool & found) const;
148 
157  T insertIfNotFound(const UnfoldingState & s,
158  const T & info,
159  bool & found);
160 
164  void printTable() const;
165 
166  private:
167 
179  void insertCube(int * cube, int index, int size,
180  list<StateNode<T>*> & currentListNode,
181  typename list< StateNode<T>* >::iterator & nodeIt,
182  const T & info);
183  };
184 
185 
186 
187  template<class T> T UnfoldingStateTable<T>::getInfo(const UnfoldingState & s, bool & found) const
188  {
189  require(ComponentTypeInvalid, s.valid(), "UnfoldingStateTable<T>::getInfo: invalid unfolding state");
190  T inf;
191 
192  found = false;
193  int * cube = s.cube();
194  int size = s.cubeSize();
195 
196  int index = 0;
197  const list<StateNode<T> *> * currentListNode = &roots;
198  typename list<StateNode<T> *>::const_iterator currentNodeIt = currentListNode->begin();
199 
200  while(index < size)
201  {
202  if(currentNodeIt == currentListNode->end())
203  {
204  found = false;
205  }
206  else
207  {
208  StateNode<T> * currentNode = *currentNodeIt;
209  if( cube[index] < currentNode->state() )
210  {
211  found = false;
212  }
213  else
214  {
215  if(cube[index] == currentNode->state())
216  {
217  if(index + 1 != size)
218  {
219  currentListNode = currentNode->children();
220  currentNodeIt = currentListNode->begin();
221  }
222  else
223  {
224  found = currentNode->isTagged();
225  if(found)
226  {
227  inf = currentNode->getInfo();
228  }
229  }
230  ++index;
231  }
232  else
233  {
234  ++currentNodeIt;
235  }
236  }
237  }
238  }
239  return inf;
240  }
241 
242 
243  template<class T>
244  T UnfoldingStateTable<T>::insertIfNotFound(const UnfoldingState & s, const T & info, bool & found)
245  {
246  require(ComponentTypeInvalid, s.valid(), "UnfoldingStateTable<T>::insertIfNotFound: invalid unfolding state");
247  T inf;
248  int * cube = s.cube();
249  int size = s.cubeSize();
250  int index = 0;
251 
252  found = false;
253  list<StateNode<T> *> * currentListNode = &roots;
254  typename list<StateNode<T> *>::iterator currentNodeIt = currentListNode->begin();
255 
256  while(index < size)
257  {
258  if(currentNodeIt == currentListNode->end())
259  {
260 #ifdef __ASSERT
261  int previousSize = currentListNode->size();
262 #endif
263  insertCube(cube,index,size,*currentListNode,currentNodeIt,info);
264  assertion(ComponentTypeInvalid,currentListNode->size() - previousSize == 1,
265  "UnfoldingStateTable<T>::insertState is buggy");
266  index = size;
267  inf = info;
268  }
269  else
270  {
271  StateNode<T> * currentNode = *currentNodeIt;
272  if( cube[index] < currentNode->state() )
273  {
274  insertCube(cube,index,size,*currentListNode,currentNodeIt,info);
275  index = size;
276  inf = info;
277  }
278  else
279  {
280  if(cube[index] == currentNode->state())
281  {
282  if(index + 1 != size)
283  {
284  currentListNode = currentNode->children();
285  currentNodeIt = currentListNode->begin();
286  }
287  else
288  {
289  found = currentNode->isTagged();
290  if(found)
291  {
292  inf = currentNode->getInfo();
293  }
294  else
295  {
296  currentNode->setInfo(info);
297  inf = info;
298  }
299  }
300  ++index;
301  }
302  else
303  {
304  ++currentNodeIt;
305  }
306  }
307  }
308  }
309  return inf;
310  }
311 
312 
313  template<class T>
314  void UnfoldingStateTable<T>::insertCube(int * cube, int index, int size,
315  list<StateNode<T> *> & currentListNode,
316  typename list<StateNode<T> *>::iterator & nodeIt,
317  const T & info)
318  {
319 
320  list<StateNode<T> *> * currentListNodePtr = &currentListNode;
321  typename list<StateNode<T> *>::iterator currentNodeIt = nodeIt;
322  int index2 = index;
323  while(index2 < size)
324  {
325  StateNode<T> * node = 0;
326  if(index2 + 1 == size)
327  {
328  node = new StateNode<T>(cube[index2],info);
329  }
330  else
331  {
332  node = new StateNode<T>(cube[index2]);
333  }
334 
335 #ifdef __ASSERT
336  int previousSize = currentListNodePtr-> size();
337 #endif
338  currentListNodePtr->insert(currentNodeIt,node);
339 
340 
341  assertion(ComponentTypeInvalid, currentListNodePtr->size() - previousSize == 1,
342  "UnfoldingStateTable::insertState: the insertion has failed");
343  currentListNodePtr = node->children();
344  assertion(ComponentTypeInvalid, currentListNodePtr->empty(),
345  "insertBs: problem of memory managment");
346  currentNodeIt = currentListNodePtr->end();
347  ++index2;
348  }
349 
350  }
351 
352  template<class T>
354  {
355  if(roots.empty())
356  {
357  cout << "Empty table" << endl;
358  }
359 
360  for(typename list< StateNode<T> * >::const_iterator rootsIt = roots.begin();
361  rootsIt != roots.end();
362  ++rootsIt)
363  {
364  list< pair< const StateNode<T> *, typename list<StateNode<T> *>::const_iterator> > stack;
365  stack.push_back(make_pair(*rootsIt, (*rootsIt)->children()->begin()));
366  while(!stack.empty())
367  {
368  const StateNode<T> * currentNode = stack.back().first;
369  typename list<StateNode<T> *>::const_iterator currentChild = stack.back().second;
370  if(currentChild != currentNode->children()->end())
371  {
372  stack.back().second++;
373  stack.push_back(make_pair(*currentChild,(*currentChild)->children()->begin()));
374  }
375  else
376  {
377  if(currentNode->isTagged())
378  {
379  cout << "( ";
380  for(typename list< pair< const StateNode<T> *, typename list<StateNode<T> *>::const_iterator> >::const_iterator
381  stackIt= stack.begin();
382  stackIt != stack.end();
383  ++stackIt)
384  {
385  cout << stackIt->first->state() << " ";
386  }
387  cout << ")" << endl;
388  }
389  else
390  {
391  if((!currentNode->isTagged()) && (currentNode->children()->empty()))
392  {
393  cout << "There is a full sequence in the table that does not represent a Unfolding state: " << endl;
394  for(typename list< pair< const StateNode<T> *,
395  typename list<StateNode<T> *>::const_iterator> >::const_iterator stackIt= stack.begin();
396  stackIt != stack.end();
397  ++stackIt)
398  {
399  cout << stackIt->first->state() << " ";
400  }
401  }
402  }
403  stack.pop_back();
404  }
405  }
406  }
407  }
408 
409 
410 
411 
412 
413 
414 
415 
416 
417 
418 
419 
420 
421 
422 
423 
424 
425 
426 
427 
428 
429 
430 
431 
432 };
433 };
434 #endif /*UnfoldingSTATETABLE_HH_*/
const list< StateNode * > * children() const
T _inf
state of the StateNode
STL namespace.
list< StateNode * > _children
is the information there or not ?
bool _tag
informtaion of the StateNode
#define require(Exception, expr, message)
Definition: Assertion.hh:90
Namespace of the Diades project.
StateNode()
list of node children
list< StateNode * > * children()
#define assertion(Exception, expr, message)
Definition: Assertion.hh:106
StateNode(int i, const T &inf)