pd_lang_k.cpp
Go to the documentation of this file.
1 /** @file pd_lang_k.cpp Test class to derive strings from pushdown automata */
2 
3 
4 /* Pushdown plugin for FAU Discrete Event Systems Library (libfaudes)
5 
6  Copyright (C) 2013 Stefan Jacobi, Sven Schneider, Anne-Kathrin Hess
7 
8 */
9 
10 #include "pd_lang_k.h"
11 
12 namespace faudes {
13 
14 std::set<std::string> LangK::FindLangK(uint k, bool showStack){
15 
16  Idx i = pd.InitState();
17  std::string word = "";
18  std::vector<Idx> stack;
19  stack.push_back(pd.StackBottom());
20  this->k = k;
21  words.clear();
22  //search for words
23  Traverse(i,word,stack,0,showStack);
24  return words;
25 }
26 
28  std::cout << "found the following words while traversing at most " << k << " transitions:" << std::endl;
29  std::set<std::string>::iterator it;
30  for(it = words.begin(); it != words.end(); it++){
31  std::cout << *it << std::endl;
32  }
33 }
34 
35 void LangK::Traverse(Idx i, std::string word, std::vector<Idx> stack, uint depth, bool showStack){
36 
37 
38  //print current state for better debugging visualization
39  if(showStack){
40  std::cout<< "Traverse("<<i<<","<< word <<",";
41  std::vector<Idx>::reverse_iterator stackit;
42  for(stackit = stack.rbegin(); stackit != stack.rend(); stackit++){
43  std::cout << " " << pd.StackSymbolName(*stackit);
44  }
45  std::cout<<"), depth " << depth <<std::endl;
46  }
47 
48 
49  //add word if current state is final state
50  if(pd.ExistsMarkedState(i)){
51  if(word.compare("") == 0){
52  words.insert(FAUDES_PD_LAMBDA);
53  }
54  else{
55  words.insert(word);
56  }
57  }
58 
59  //test if final word length is reached
60  if(depth == k){
61  //add current word with indication that it is not finished and only if its not already been inserted
62  if(!pd.ExistsMarkedState(i)){
63  words.insert(word + " ...?");
64  }
65  //stop recursion
66  return;
67  }
68 
69 
71  std::vector<Idx> pop, currentStack;
72  std::vector<Idx>::const_iterator itpush, itpop;
73  PopPushSet popPush;
74  PopPushSet::const_iterator ppit;
75  std::vector<Idx>::const_reverse_iterator itstack;
76  uint j;
77  //examine all transitions with i as start state
78  for(it = pd.TransRelBegin(i); it != pd.TransRelEnd(i); it++){
79 
80  //examine all pop/push pairs of the current transition
81  popPush = pd.PopPush(*it);
82  for(ppit = popPush.begin(); ppit != popPush.end(); ppit++){
83 
84  //take copy of current stack to work on
85  currentStack = stack;
86 
87  //test if pop is lambda pop
88  if(pd.IsStackSymbolLambda(ppit->first.front())){
89  //pop is not necessary since it's a lambda pop
90  //push onto stack, but don't push lambda!
91  if(pd.IsStackSymbolLambda(!ppit->second.front())){
92  currentStack.insert(currentStack.end(), ppit->second.rbegin(), ppit->second.rend());
93  }
94  //traverse next state
95  std::string newWord = word;
96  if(!pd.IsEventLambda(it->Ev)){
97  newWord = word + pd.EventName(it->Ev) + " ";
98  }
99  Traverse(it->X2,newWord,currentStack, depth+1,showStack);
100  continue;
101  }
102 
103  //pop is no lambda pop
104  //test if pop matches stack top
105  itpop = ppit->first.begin();
106  for(itstack = currentStack.rbegin(); itstack != currentStack.rend(); itstack++){
107 
108  //stack top and pop are not equal, stop comparison
109  if(*itstack != *itpop){
110  break;
111  }
112 
113  //If all pop elements were equal and the end of pop is reached, then pop has been fully compared and is therefore equal to the stack top
114  itpop++;
115  if(itpop == ppit->first.end()){
116 
117  //pop from stack
118  for(j = 0; j < ppit->first.size(); j++){
119  currentStack.pop_back();
120  }
121  //push onto stack, but don't push lambda!
122  if(!pd.IsStackSymbolLambda(ppit->second.front())){
123  currentStack.insert(currentStack.end(), ppit->second.rbegin(), ppit->second.rend());
124  }
125  //traverse next state
126  std::string newWord = word;
127  if(!pd.IsEventLambda(it->Ev)){
128  newWord = word + pd.EventName(it->Ev) + " ";
129  }
130  Traverse(it->X2,newWord,currentStack, depth+1,showStack);
131  break;
132  };
133 
134  }
135  }
136 
137  }
138 }
139 
140 
141 } // namespace faudes
142 

libFAUDES 2.24g --- 2014.09.15 --- c++ api documentaion by doxygen