pd_gotogenerator.h
Go to the documentation of this file.
1 /** @file pd_gotogenerator.h LR machine */
2 
3 /* Pushdown plugin for FAU Discrete Event Systems Library (libfaudes)
4 
5  Copyright (C) 2013 Stefan Jacobi, Sven Schneider, Anne-Kathrin Hess
6 
7 */
8 
9 
10 #ifndef FAUDES_PD_GOTOGENERATOR_H
11 #define FAUDES_PD_GOTOGENERATOR_H
12 
13 #include "corefaudes.h"
14 #include "pd_gotoattributes.h"
15 #include "pd_parser.h"
16 
17 
18 namespace faudes {
19 
20 /**
21  * Simple generator for parsing with goto tables
22 *
23 *
24 *
25 *
26 * @ingroup PushdownPlugin
27 *
28 * @section Overview
29 * Overview
30 *
31 * @section Contents
32 * Contents
33 */
35 
36 private:
37 
40  std::set<Lr1Configuration> mInitState;
41 
42 public:
43 
44  /**
45  * Setter for transitions
46  *
47  * @param transitions
48  * the transitions to set
49  */
50  void Transitions(const LrmTransitionMap& transitions){ mTransitions = transitions;}
51 
52  /**
53  * Getter for transitions
54  *
55  * @return
56  * the transitions
57  */
58  const LrmTransitionMap& Transitions() const { return mTransitions; }
59 
60  /**
61  * Setter for config sets
62  *
63  * @param states
64  * the transitions to set
65  */
66  void States(const Lr1ConfigurationSetSet& states){ mStates = states; }
67 
68  /**
69  * Getter for states
70  *
71  * @return
72  * the states
73  */
74  const Lr1ConfigurationSetSet& States() const { return mStates; }
75 
76  /**
77  * Setter for initState
78  *
79  * @param initState
80  * the initStates to set
81  */
82  void InitState(const std::set<Lr1Configuration>& initState){ mInitState = initState; }
83 
84  /**
85  * Getter for initState
86  *
87  * @return
88  * the initState
89  */
90  const std::set<Lr1Configuration>& InitState() const { return mInitState; }
91 
92 };
93 //end class GotoMachine
94 
95 /**
96  * Generator for parsing with goto tables
97  *
98  *
99  *
100  * @ingroup PushdownPlugin
101  */
102 
103 template <class GlobalAttr, class StateAttr, class EventAttr, class TransAttr>
104 class TgotoGenerator : public TaGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr> {
105 
106 public:
107  /**
108  * Constructor
109  */
110  TgotoGenerator(void);
111 
112  /**
113  * Copy constructor
114  *
115  * @param rOtherGen
116  */
117  TgotoGenerator(const TgotoGenerator& rOtherGen);
118 
119  /**
120  * Copy constructor (no attributes)
121  *
122  * @param rOtherGen
123  */
124  TgotoGenerator(const vGenerator& rOtherGen);
125 
126  /**
127  * Assignment operator (uses copy)
128  * Note: you must reimplement this operator in derived
129  * classes in order to handle internal pointers correctly
130  *
131  * @param rOtherGen
132  * Other generator
133  */
134  virtual TgotoGenerator& operator= (const TgotoGenerator& rOtherGen) {this->Assign(rOtherGen); return *this;};
135 
136  /**
137  * Assignment operator (uses copy)
138  *
139  * @param rOtherGen
140  * Other generator
141  */
142  virtual TgotoGenerator& operator= (const vGenerator& rOtherGen) {this->Assign(rOtherGen); return *this;};
143 
144  /**
145  * Construct from file
146  *
147  * @param rFileName
148  * Name of file
149  *
150  * @exception Exception
151  * - file format errors (id 1, 50, 51, 52)
152  */
153  TgotoGenerator(const std::string& rFileName);
154 
155  /**
156  * Construct on heap.
157  * Constructs a TgotoGenerator on heap.
158  *
159  * @return
160  * new Generator
161  */
162  TgotoGenerator* New(void) const;
163 
164  /**
165  * Construct copy on heap.
166  * Constructs a TgotoGenerator on heap.
167  *
168  * @return
169  * new Generator
170  */
171  TgotoGenerator* Copy(void) const;
172 
173  /**
174  * Type test.
175  * Uses C++ dynamic cast to test whether the specified object
176  * casts to a TgotoGenerator.
177  *
178  * @return
179  * TgotoGenerator reference if dynamic cast succeeds, else NULL
180  */
181  virtual const Type* Cast(const Type* pOther) const {
182  return dynamic_cast< const TgotoGenerator* > (pOther);
183  };
184 
185 
186  /**
187  * Construct on stack.
188  * Constructs a TgotoGenerator on stack.
189  *
190  * @return
191  * new Generator
192  */
193  TgotoGenerator NewGotoGen(void) const;
194 
195  /**
196  * Add a transition to generator by indices. States
197  * must already exist!
198  *
199  * @param x1
200  * Predecessor state index
201  * @param symbol
202  * grammar symbol used for the transition
203  * @param x2
204  * Successor state index
205  *
206  * @return
207  * True, if the transition was new the generator
208  *
209  * @exception Exception
210  * - state or event not in generator (id 95)
211  */
212  bool SetTransition(Idx x1, const GrammarSymbolPtr& symbol, Idx x2);
213 
214  /**
215  * Getter for the symbol of a transitions
216  *
217  * @param rTrans
218  * the transition
219  * @return
220  * the symbol
221  */
222  GrammarSymbolPtr Symbol(const Transition& rTrans) const;
223 
224  /**
225  * Add a transition to generator
226  *
227  * @param rTrans
228  * the transition
229  * @param symbol
230  * grammar symbol used for the transitions
231  *
232  * @return
233  * True, if the transition was new in the generator
234  *
235  * @exception Exception
236  * - state or event not in generator (id 95)
237  */
238  bool SetTransition(const Transition& rTrans, const GrammarSymbolPtr& symbol);
239 
240  /**
241  * Setter for the configuration set of a state
242  *
243  * @param index
244  * the index of the state
245  * @param configs
246  * the configuration set
247  *
248  */
249  void ConfigSet(Idx index, const std::set<Lr1Configuration>& configs);
250 
251  /**
252  * Getter for the configuration set of a state
253  *
254  * @param index
255  * the index of the state
256  * @return
257  * the configuration set of the state
258  */
259  std::set<Lr1Configuration> const& ConfigSet(Idx index) const;
260 
261  /**
262  * Get first state with the specified configuration set as attribute
263  *
264  * @param configs
265  * the configuration set
266  * @return
267  * state index if the state was found, else 0
268  */
269  Idx StateIndex(const std::set<Lr1Configuration>& configs) const;
270 
271  /**
272  * Check if generator is valid
273  *
274  * @return
275  * Success
276  */
277  virtual bool Valid(void);
278 
279 }; //end class TgotoGenerator
280 
281 /** Convenience typedef for std GotoGenerator */
282 typedef TgotoGenerator<AttributeVoid, AttributeGotoState, AttributeVoid, AttributeGotoTransition>
284 
285 
286 // convenient scope macros
287 #define THISGOTO TgotoGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
288 #define BASEGOTO TaGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
289 #define TEMPGOTO template <class GlobalAttr, class StateAttr, class EventAttr, class TransAttr>
290 
291 // TgotoGenerator(void)
292 TEMPGOTO THISGOTO::TgotoGenerator(void) : BASEGOTO() {
293  // set basic members (cosmetic)
294  FD_DG("GotoGenerator(" << this << ")::GotoGenerator()");
295 }
296 
297 // TgotoGenerator(rOtherGen)
298 TEMPGOTO THISGOTO::TgotoGenerator(const TgotoGenerator& rOtherGen) : BASEGOTO(rOtherGen) {
299  FD_DG("GotoGenerator(" << this << ")::GotoGenerator(rOtherGen)");
300 }
301 
302 // TgotoGenerator(rOtherGen)
303 TEMPGOTO THISGOTO::TgotoGenerator(const vGenerator& rOtherGen) : BASEGOTO(rOtherGen) {
304  // set basic members (cosmetic)
305  FD_DG("GotoGenerator(" << this << ")::GotoGenerator(rOtherGen)");
306 }
307 
308 // TgotoGenerator(rFilename)
309 TEMPGOTO THISGOTO::TgotoGenerator(const std::string& rFileName) : BASEGOTO(rFileName) {
310  FD_DG("GotoGenerator(" << this << ")::GotoGenerator(rOtherGen)");
311 }
312 
313 
314 // New
315 TEMPGOTO THISGOTO* THISGOTO::New(void) const {
316  // allocate
317  THISGOTO* res = new THISGOTO;
318  // fix base data
319  res->EventSymbolTablep(BASEGOTO::mpEventSymbolTable);
320  res->mStateNamesEnabled=BASEGOTO::mStateNamesEnabled;
321  res->mReindexOnWrite=BASEGOTO::mReindexOnWrite;
322  // fix my data
323  return res;
324 }
325 
326 // Copy
327 TEMPGOTO THISGOTO* THISGOTO::Copy(void) const {
328  // allocate
329  THISGOTO* res = new THISGOTO(*this);
330  // done
331  return res;
332 }
333 
334 // NewTGen
335 TEMPGOTO THISGOTO THISGOTO::NewGotoGen(void) const {
336  // call base (fixes by assignment constructor)
337  THISGOTO res= BASEGOTO::NewAGen();
338  // fix my data
339  return res;
340 }
341 //SetTransition(rTrans, symbol)
342 TEMPGOTO bool THISGOTO::SetTransition(const Transition& rTrans, const GrammarSymbolPtr& symbol){
343 
344  TransAttr attr;
345  attr.Symbol(symbol);
346  return BASEGOTO::SetTransition(rTrans,attr);
347 }
348 
349 // SetTransition(x1, symbol, x2)
350 TEMPGOTO bool THISGOTO::SetTransition(Idx x1, const GrammarSymbolPtr& symbol, Idx x2) {
351  //events dont matter, so just insert a dummy event
352  Idx ev = BASEGOTO::InsEvent("dummy");
353  return SetTransition(Transition(x1,ev,x2), symbol);
354 }
355 //Symbol(rTrans)
356 TEMPGOTO GrammarSymbolPtr THISGOTO::Symbol(const Transition& rTrans) const{
357  return BASEGOTO::pTransRel->Attribute(rTrans).Symbol();
358 }
359 
360 //ConfigSet(index, configs)
361 TEMPGOTO void THISGOTO::ConfigSet(Idx index, const std::set<Lr1Configuration>& configs){
362  BASEGOTO::pStates->Attributep(index)->ConfigSet(configs);
363 }
364 
365 //ConfigSet(index)
366 TEMPGOTO std::set<Lr1Configuration> const& THISGOTO::ConfigSet(Idx index) const{
367  return BASEGOTO::pStates->Attributep(index)->ConfigSet();
368 }
369 
370 //StateIdx(configs)
371 TEMPGOTO Idx THISGOTO::StateIndex(const std::set<Lr1Configuration>& configs) const{
372 
373  StateSet::Iterator stateit;
374  //iterate over all states
375  for(stateit = BASEGOTO::StatesBegin(); stateit != BASEGOTO::StatesEnd(); stateit++){
376 
377  //look for the first state with the correct config set
378  std::set<Lr1Configuration> curretConfigs = BASEGOTO::pStates->Attributep(*stateit)->ConfigSet();
379 
380  //found the set
381  if(!(CompareConfigSet(configs, curretConfigs) || CompareConfigSet(curretConfigs, configs))){
382  return *stateit;
383  }
384  }
385  return 0;
386 }
387 
388 
389 // Valid() TODO checks?
390 TEMPGOTO bool THISGOTO::Valid(void) {
391  FD_DV("GotoGenerator(" << this << ")::Valid()");
392  //call base
393  if(!BASEGOTO::Valid()) return false;
394  // check my names
395 
396  // check my clockset
397 
398  // check all clocksymboltables
399 
400  return true;
401 }
402 
403 
404 } // namespace faudes
405 
406 
407 #endif
408 

libFAUDES 2.26g --- 2015.08.17 --- c++ api documentaion by doxygen