pd_attributes.cpp
Go to the documentation of this file.
1 /** @file pd_attributes.cpp Attributes for 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_attributes.h"
11 namespace faudes {
12 
13 /*******************************
14  *
15  * Implementation of AttributePushdownState
16  *
17  */
18 
19 // faudes type std
20 FAUDES_TYPE_IMPLEMENTATION(Void,AttributePushdownState,AttributeFlags)
21 
22 
24  if(mpMerge != NULL){
25  delete mpMerge;
26  }
27 }
28 
30 
31  if(mpMerge != NULL){
32  delete mpMerge;
33  }
34 
35  //test subtype of pMerge and allocate memory according to subtype
36  //MergeStates
37  const MergeStates* ms = dynamic_cast<const MergeStates*>(&rMerge);
38  if(ms != NULL){
39  try{
40  mpMerge = new MergeStates(ms->States());
41  }
42  catch (std::bad_alloc& ba){
43  std::cerr << "bad_alloc caught in AttributePushdownState::SetMerge at new MergeStates(ms->States()): " << ba.what() << std::endl;
44  }
45  return;
46  }
47 
48  //MergeStateAnnotation
49  const MergeStateAnnotation* msa = dynamic_cast<const MergeStateAnnotation*>(&rMerge);
50  if(msa != NULL){
51  try{
52  mpMerge = new MergeStateAnnotation(msa->State(), msa->Annotation());
53  }
54  catch (std::bad_alloc& ba){
55  std::cerr << "bad_alloc caught in AttributePushdownState::SetMerge at new MergeStateAnnotation(msa->State(), msa->Annotation()): " << ba.what() << std::endl;
56  }
57  return;
58  }
59 
60  //MergeStateSplit
61  const MergeStateSplit* mss = dynamic_cast<const MergeStateSplit*>(&rMerge);
62  if(mss != NULL){
63  try{
64  if(mss->IsHead()){
65  mpMerge = new MergeStateSplit(mss->State());
66  }
67  else{
68  mpMerge = new MergeStateSplit(mss->State(), mss->Symbol());
69  }
70  }
71  catch (std::bad_alloc& ba){
72  std::cerr << "bad_alloc caught in AttributePushdownState::SetMerge at new MergeStateSplit(mss->Annotation(), mss->State(), mss->Symbol()): " << ba.what() << std::endl;
73  }
74  return;
75  }
76 
77  //MergeTransition
78  const MergeTransition* mt = dynamic_cast<const MergeTransition*>(&rMerge);
79  if(mt != NULL){
80  try{
81  mpMerge = new MergeTransition(mt->X1(), mt->Ev(), mt->X2(), mt->Pop(), mt->Push());
82  }
83  catch (std::bad_alloc& ba){
84  std::cerr << "bad_alloc caught in AttributePushdownState::SetMerge at new MergeTransition(mt->X1(), mt->Ev(), mt->X2(), mt->Pop(), mt->Push()): " << ba.what() << std::endl;
85  }
86  return;
87  }
88 
89  //MergeStateEvent
90  const MergeStateEvent* mse = dynamic_cast<const MergeStateEvent*>(&rMerge);
91  if(mse != NULL){
92  try{
93  mpMerge = new MergeStateEvent(mse->State(), mse->Event());
94  }
95  catch (std::bad_alloc& ba){
96  std::cerr << "bad_alloc caught in AttributePushdownState::SetMerge at new MergeTransition(mt->X1(), mt->Ev(), mt->X2(), mt->Pop(), mt->Push()): " << ba.what() << std::endl;
97  }
98  return;
99  }
100 
101  //invalid reference - do nothing
102  if(&rMerge == NULL){
103  return;
104  }
105  //should never get here
106  std::stringstream errstr;
107  errstr << "tried to set MergeAbstract with non-existent subtype " << typeid(rMerge).name() << std::endl;
108  throw Exception("AttributePushdownState::SetMerge()", errstr.str(), 1002);
109  return;
110 }
111 
112 // Assign my members
114  // call base (incl. virtual clear)
115  AttributeFlags::DoAssign(rSrcAttr);
116  // my additional members
117  this->SetMerge(*(rSrcAttr.mpMerge));
118  this->DfaState(rSrcAttr.DfaState());
119  //*(mpMerge)=*(rSrcAttr.mpMerge);
120 }
121 
122 // Equality
124  // base
125  if(!AttributeFlags::DoEqual(rOther)) return false;
126  // my members
127  if(mpMerge!=rOther.mpMerge) return false;
128  // pass
129  return true;
130 }
131 
132 
133 //DoWrite(rTw,rLabel,pContext);
134 void AttributePushdownState::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const {
135  (void) pContext;
136  if(IsDefault()) return;
137  AttributeFlags::DoWrite(rTw,"",pContext);
138  std::string label=rLabel;
139  if(label=="") label="State";
140  FD_DC("AttributePushdownState(" << this << ")::DoWrite(tr): to section " << label);
141  if(mpMerge != NULL)
142  mpMerge->Write(rTw,"");//TODO only compile this for debugging
143 }
144 
145 
146 //DoRead(rTr,rLabel,pContext)
147 void AttributePushdownState::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
148  // call base first
149  AttributeFlags::DoRead(rTr,"",pContext);
150  // figure my section
151  std::string label=rLabel;
152  if(label=="") label="State";
153  FD_DC("AttributePushdownState(" << this << ")::DoRead(tr): from section " << label);
154  // clear my data
155  mpMerge = NULL;
156  // test my section
157  Token token;
158  rTr.Peek(token);
159  if(!token.IsBegin()) return;
160  if(token.StringValue()!=label) return;
161  // read my section (can throw exceptions now)
162  //mInvariant.Read(rTr,label); //TODO read of mpMerge may be not needed since its only intended for debugging
163 }
164 
165 /*******************************
166  *
167  * Implementation of AttributePushdownTransition
168  *
169  */
170 
171 // faudes type std
173 
174 
175 bool AttributePushdownTransition::ClrPopPush(const std::vector<Idx>& rPop, const std::vector<Idx>& rPush){
176 
177  std::pair<std::vector<Idx>,std::vector<Idx> > popPushPair;
178  popPushPair.first = rPop;
179  popPushPair.second = rPush;
180 
181  int i = mPopPush.erase(popPushPair);
182  if(i != 0){
183  return true;
184  }
185  return false;
186 }
187 
188 // Assign my members
190  // call base (incl. virtual clear)
191  AttributeVoid::DoAssign(rSrcAttr);
192  // my additional members
193  mPopPush=rSrcAttr.mPopPush;
194 }
195 
196 // Equality
198  // my members
199  if(mPopPush!=rOther.mPopPush) return false;
200  // pass
201  return true;
202 }
203 
204 
205 //DoWrite(rTw,rLabel,pContext);
206 void AttributePushdownTransition::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const {
207  if(IsDefault()) return;
208  std::string label=rLabel;
209  FD_DC("AttributePushdownTransition(" << this << ")::DoWrite(tr): to section " << label);
210  if(label=="") label="PopPushes";
211 
212  Token token;
213  int oldcolumns = rTw.Columns();
214  rTw.Columns(8);
215 
216  // begin section commands
217  token.SetBegin(label);
218  rTw << token;
219 
220  //iterate over all PopPushPairs
221  PopPushSet::const_iterator setit;
222  std::vector<Idx>::const_iterator popit, pushit;
223  std::vector<Idx> pop, push;
224  for(setit = mPopPush.begin(); setit != mPopPush.end(); setit++){
225 
226  token.SetBegin("PopPush");
227  rTw << token;
228 
229  //write pop
230  pop = setit->first;
231  for(popit=pop.begin();popit != pop.end();popit++){
232 
233  token.SetBegin("Pop");
234  token.InsAttributeString("name", StackSymbolSet::StaticSymbolTablep()->Symbol(*popit));
235  rTw << token;
236 
237  token.SetEnd("Pop");
238  rTw << token;
239  }
240 
241  //write push
242  push = setit->second;
243  for(pushit=push.begin();pushit != push.end();pushit++){
244 
245  token.SetBegin("Push");
246  token.InsAttributeString("name", StackSymbolSet::StaticSymbolTablep()->Symbol(*pushit));
247  rTw << token;
248 
249  token.SetEnd("Push");
250  rTw << token;
251  }
252 
253  token.SetEnd("PopPush");
254  rTw << token;
255  }
256 
257  // commands
258  token.SetEnd(label);
259  rTw << token;
260 
261  rTw.Columns(oldcolumns);
262 }
263 
264 
265 //DoRead(rTr,rLabel,pContext)
266 void AttributePushdownTransition::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
267  // find my section
268  std::string label=rLabel;
269  if(label=="") label="PopPushes";
270  FD_DC("AttributePushdownTransition(" << this << ")::DoRead(tr): from section " << label);
271  // initialise my data
272  mPopPush.clear();
273  // test for my data
274  Token token;
275  rTr.Peek(token);
276  if(!token.IsBegin(label)) return;
277 
278  std::string symbol;
279  std::vector<Idx> pop, push;
280  // read my section
281  rTr.ReadBegin(label);
282  while(!rTr.Eos(label)) {
283  // read token
284  rTr.Peek(token);
285  // skip other than command
286  if(!token.IsBegin("PopPush")) {
287  rTr.Get(token);
288  continue;
289  }
290 
291  // ok, its a command section
292  rTr.ReadBegin("PopPush");
293 
294  pop.clear();
295  push.clear();
296 
297  StackSymbolSet ssSet;
298  while(rTr.Peek(token)){
299 
300  //read pop
301  if(token.IsBegin("Pop")){
302 
303  rTr.ReadBegin("Pop");
304  symbol = token.AttributeStringValue("name");
305  ssSet.Insert(symbol); //for generating stack symbol indices
306  pop.push_back(StackSymbolSet::StaticSymbolTablep()->Index(symbol));
307  rTr.ReadEnd("Pop");
308  }
309 
310  //read push
311  else if(token.IsBegin("Push")){
312 
313  rTr.ReadBegin("Push");
314  symbol = token.AttributeStringValue("name");
315  ssSet.Insert(symbol); ssSet.Insert(symbol); //for generating stack symbol indices
316  push.push_back(StackSymbolSet::StaticSymbolTablep()->Index(symbol));
317  rTr.ReadEnd("Push");
318  }
319 
320  //stop if end of section
321  else if(token.IsEnd("PopPush")){
322 
323  //insert
324  mPopPush.insert(std::make_pair(pop,push));
325  break;
326  }
327  }
328 
329 // // end section command
330  rTr.ReadEnd("PopPush");
331  // 3:
332 // std::stringstream errstr;
333 // errstr << "invalid transition timing" << rTr.FileLine();
334 // throw Exception("AttributeTimedTrans::Read", errstr.str(), 52);
335  }
336  rTr.ReadEnd(label);
337 }
338 
339 
340  /*******************************
341  *
342  * Implementation of AttributePushdownGlobal
343  *
344  */
345 
346  // faudes type std
348 
349  // Assign my members
350  void AttributePushdownGlobal::DoAssign(const AttributePushdownGlobal& rSrcAttr) {
351  // call base (incl. virtual clear)
352  AttributeVoid::DoAssign(rSrcAttr);
353  // my additional members
354  mStackSymbols=rSrcAttr.mStackSymbols;
355  mpStackSymbolTable=rSrcAttr.mpStackSymbolTable;
356  mStackBottom = rSrcAttr.mStackBottom;
357  }
358 
359  // Equality
361  // my members
362  if(mStackSymbols!=rOther.mStackSymbols) return false;
363  // pass
364  return true;
365  }
366 
367  //DoWrite(rTw,rLabel,pContext);
368  void AttributePushdownGlobal::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const {
369  (void) pContext;
370  if(IsDefault()) return;
371  std::string label=rLabel;
372  if(label=="") label="StackSymbols";
373  FD_DC("AttributePushdownGlobal(" << this << ")::DoWrite(tr): to section " << label);
374  mStackSymbols.XWrite(rTw,label);
375 
376  Token token;
377  token.SetBegin("StackBottom");
379  rTw << token;
380 
381  token.SetEnd("StackBottom");
382  rTw << token;
383  }
384 
385  //DoRead(rTr,rLabel,pContext)
386  void AttributePushdownGlobal::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
387  std::string label=rLabel;
388  if(label=="") label="StackSymbols";
389  FD_DC("AttributePushdownGlobal(" << this << ")::DoRead(tr): from section " << label);
390  (void) pContext;
392  Token token;
393  rTr.Peek(token);
394  if(!token.IsBegin()) return;
395  if(token.StringValue() == label){
396  mStackSymbols.Read(rTr,label);
397  }
398 
399 
400  rTr.Peek(token);
401  if(token.StringValue() == "StackBottom"){
402  rTr.ReadBegin("StackBottom");
403  std::string symbol = token.AttributeStringValue("name");
404  StackSymbolSet ssSet;
405  ssSet.Insert(symbol);
407  rTr.ReadEnd("StackBottom");
408  }
409  }
410 
411 
412 
413 } // namespace faudes
414 

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