openETCS
case study for the European Train Control System developed for the authors dissertation
CPPGenerator.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010-2011
3  Johannes Feuser <feuser@uni-bremen.de>
4  This file is part of the openETCS library.
5 
6  The openETCS library is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  any later version.
10 
11  The openETCS library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with the openETCS library. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 /*!
21  * \author Johannes Feuser <feuser@uni-bremen.de>
22  * \brief
23  */
24 
25 #ifndef __OETCS_GEN_CPPGENERATOR_H__
26 #define __OETCS_GEN_CPPGENERATOR_H__
27 
28 
29 #include <chrono>
30 #include <iomanip>
31 #include <sstream>
32 
33 #include <string>
34 #include <map>
35 #include "AbstractModel.h"
36 #include <list>
37 #include <vector>
38 #include "Generator.h"
39 #include <regex>
40 
41 
42 
43 
44 namespace GOPPRR { struct CGraph; }
45 namespace GOPPRR { struct CProject; }
46 namespace GOPPRR { struct CObject; }
47 namespace GOPPRR { struct CPort; }
48 
49 
50 namespace oETCS {
51 
52 namespace GEN {
53 
54 
55 /*!
56  * \brief generator for C++ sources
57  */
59 {
60  public:
61  /*!
62  * \brief default oonstructor
63  */
64  explicit CCPPGenerator() throw();
65 
66 
67 
68  /*!
69  * \brief destructor
70  */
71  virtual ~CCPPGenerator() throw();
72 
73 
74 
75  /*!
76  * \brief generation method
77  *
78  * Generates from a syntax tree the corresponding output.
79  * \remark pure virtual method, which must be reimplemented by any
80  * inheriting class.
81  *
82  * \param[in] pTree pointer to the syntax tree used for generation
83  * \param[in] OutStream stream object used for output of generation
84  */
85  virtual void Generate(::DSM::CSyntaxTree * const pSyntaxTree, ::std::ostream & OutStream) throw(::oETCS::GEN::Error::CException);
86 
87 
88 
89 
90  private:
91  /*!
92  * \brief internal helper method
93  *
94  * Generates source code of a root graph (EVC state machine) from
95  * a GOPPRR syntax tree.
96  *
97  * \param[in] pRootGraph pointer to the root graph of the openETCS model
98  * \param[in] pProject pointer to the parent project of the openETCS model
99  * \param[in] OutStream stream object used for output of generation
100  */
101  void GenerateRootGraph(GOPPRR::CGraph * const pRootGraph, GOPPRR::CProject * const pProject, ::std::ostream & OutStream) throw(::GOPPRR::Error::CException);
102 
103 
104 
105  /*!
106  * \brief internal helper method
107  *
108  * Generates source code for creating all function block classes in a project.
109  *
110  * \param[in] pProject pointer to the parent project of the openETCS model
111  * \param[in] OutStream stream object used for output of generation
112  */
113  void GenerateFunctionBlockElements(GOPPRR::CProject * const pProject, ::std::ostream & OutStream) throw(::GOPPRR::Error::CException);
114 
115 
116 
117  /*!
118  * \brief internal helper method
119  *
120  * Generates source code of any sub type of function block graph from
121  * a GOPPRR syntax tree.
122  *
123  * \param[in] pFunctionBlock pointer to the function block graph
124  * \param[in] pProject pointer to the parent project of the openETCS model
125  * \param[in] OutStream stream object used for output of generation
126  */
127  void GenerateFunctionBlock(GOPPRR::CGraph * const pFunctionBlock, GOPPRR::CProject * const pProject, ::std::ostream & OutStream) throw(::GOPPRR::Error::CException);
128 
129 
130 
131  /*!
132  * \brief internal helper method
133  *
134  * Generates source code of any type of communication graph from
135  * a GOPPRR syntax tree.
136  *
137  * \param[in] pCommnication pointer to the function block graph
138  * \param[in] pProject pointer to the parent project of the openETCS model
139  * \param[in] OutStream stream object used for output of generation
140  */
141  void GenerateCommunication(GOPPRR::CGraph * const pCommunication, GOPPRR::CProject * const pProject, ::std::ostream & OutStream) throw(::GOPPRR::Error::CException);
142 
143 
144 
145  /*!
146  * \brief internal helper method
147  *
148  * Generates source code for data flow objects (CDataFlow) for a MainFunctionBlock
149  * graph
150  *
151  * \param[in] pMainFunctionBlock pointer to the main function block
152  * \param[in] pEVCGraph pointer to EVC root graph
153  * \param[in] pEVCState pointer to the related EVCState object
154  * \param[in] OutStream stream object used for output of generation
155  */
156  void GenerateDataFlow(GOPPRR::CGraph * const pMainFunctionBlock, GOPPRR::CGraph * const pEVCGraph, GOPPRR::CObject * const pEVCState, ::std::ostream & OutStream) throw(::GOPPRR::Error::CException);
157 
158 
159 
160  /*!
161  * \brief internal helper method
162  *
163  * Generates source code for adding objects in a sub function block to
164  * a certain data flow objectt
165  *
166  * \param[in] OID OID of the related main function block graph
167  * \param[in] pSubFunctionBlock pointer to current sub function block graph
168  * \param[in] OutStream stream object used for output of generation
169  */
170  void GenerateDataFlow(const ::std::string & OID, GOPPRR::CGraph * const pSubFunctionBlock, ::std::ostream & OutStream) throw(::GOPPRR::Error::CException);
171 
172 
173 
174  /*!
175  * \brief internal helper method
176  *
177  * Generates source code adding the related CBitFlow objects
178  * for a communication graph
179  *
180  * \param[in] OID OID of the related main function block graph
181  * \param[in] pCommGraph pointer to current communication graph
182  * \param[in] OutStream stream object used for output of generation
183  */
184  void GenerateDataFlowForCommunication(const ::std::string & OID, GOPPRR::CGraph * const pCommGraph, ::std::ostream & OutStream) throw(::GOPPRR::Error::CException);
185 
186 
187 
188  /*!
189  * \brief internal helper method
190  *
191  * Generates source code for DMI widgets (CDMIWidget) for a MainFunctionBlock
192  * graph
193  *
194  * \param[in] pMainFunctionBlock pointer to the main function block
195  * \param[in] pEVCGraph pointer to EVC root graph
196  * \param[in] pEVCState pointer to the related EVCState object
197  * \param[in] OutStream stream object used for output of generation
198  */
199  void GenerateDMI(GOPPRR::CGraph * const pMainFunctionBlock, GOPPRR::CGraph * const pEVCGraph, GOPPRR::CObject * const pEVCState, ::std::ostream & OutStream) throw(::GOPPRR::Error::CException);
200 
201 
202 
203  /*!
204  * \brief internal helper method
205  *
206  * Generates source code for DMI widgets (CDMIWidget) for a SubFunctionBlock
207  * graph
208  *
209  * \param[in] OID OID of the related main function block
210  * \param[in] pSubFunctionBlock pointer to the subfunction block
211  * \param[in] OutStream stream object used for output of generation
212  */
213  void GenerateDMI(const ::std::string & OID, GOPPRR::CGraph * const pSubFunctionBlock, ::std::ostream & OutStream) throw(::GOPPRR::Error::CException);
214 
215 
216 
217  /*!
218  * \brief internal helper method
219  *
220  * Generates source code for EVC transitions in a certain main function block.
221  *
222  * \param[in] pMainFunctionBlock pointer to the main function block graph
223  * \param[in] pEVCState pointer to related EVC state object
224  * \param[in] pRootGraph pointer to the root graph (EVCStateMachine)
225  * \param[in] OutStream stream object used for output of generation
226  */
227  void GenerateEVCTransitions(GOPPRR::CGraph * const pMainFunctionBlock, GOPPRR::CObject * const pEVCState, GOPPRR::CGraph * const pRootGraph, ::std::ostream & OutStream) throw(::GOPPRR::Error::CException);
228 
229 
230 
231  /*!
232  * \brief internal helper method
233  *
234  * Generates source code for creating all states of all control flows.
235  *
236  * \param[in] pProject pointer to the openETCS project object
237  * \param[in] OutStream stream object used for output of generation
238  */
239  void GenerateControlFlows(GOPPRR::CProject * const pProject, ::std::ostream & OutStream) throw(::GOPPRR::Error::CException);
240 
241 
242 
243  /*!
244  * \brief internal helper method
245  *
246  * Generates source code for creating all states of all control flows.
247  *
248  * \param[in] pRootGraph pointer to the root graph of the openETCS model
249  * \param[in] OutStream stream object used for output of generation
250  */
251  void GenerateTransitions(GOPPRR::CGraph * const pRootGraph, ::std::ostream & OutStream) throw(::GOPPRR::Error::CException);
252 
253 
254 
255  /*!
256  * \brief internal helper method
257  *
258  * Generates source code for creating all trainsitions of control flows in a certain
259  * function block graph (data flow).
260  *
261  * \param[in] pFunctionBlock pointer data flow graph
262  * \param[in] pControlFlow pointer to the related control flow graph
263  * \param[in] pEVCState pointer to parent EVC state object
264  * \param[in] pState pointer to the state object
265  * \param[in] OutStream stream object used for output of generation
266  */
267  void GenerateTransitions(GOPPRR::CGraph * const pFunctionBlock, GOPPRR::CObject * const pEVCState, ::std::ostream & OutStream) throw(::GOPPRR::Error::CException);
268 
269 
270 
271  /*!
272  * \brief internal helper method
273  *
274  * Generates source code for creating all trainsitions of control flows in a certain
275  * function block graph (data flow).
276  *
277  * \param[in] pFunctionBlock pointer to the data flow grah
278  * \param[in] pControlFlow pointer to the related control flow graph
279  * \param[in] pEVCState pointer to parent EVC state object
280  * \param[in] pState pointer to the state object
281  * \param[in] OutStream stream object used for output of generation
282  */
283  void GenerateTransitions(GOPPRR::CGraph * const pFunctionBlock, GOPPRR::CGraph * const pControlFlow, GOPPRR::CObject * const pEVCState, GOPPRR::CObject * const pState, ::std::ostream & OutStream) throw(::GOPPRR::Error::CException);
284 
285 
286 
287  /*!
288  * \brief internal helper method
289  *
290  * Generates source code of for the language classes
291  *
292  * \param[in] pProject pointer to the project of the openETCS model
293  * \param[in] OutStream stream object used for output of generation
294  */
295  void GenerateLanguage(GOPPRR::CProject * const pProject, ::std::ostream & OutStream) throw(::GOPPRR::Error::CException);
296 
297 
298 
299  /*!
300  * \brief internal helper method
301  *
302  * Builds the abstract function block model for a function block graph
303  * recursively.
304  *
305  * \param[in,out] pFunctionBlock: pointer to current function block graph
306  * \param[in] FBNodes map of nodes objects indexed by their OID
307  */
308  void BuildAbstractModel(GOPPRR::CGraph * const pFunctionBlock, ::std::map< ::std::string, oETCS::GEN::CFBNode >& FBNodes) throw(::GOPPRR::Error::CException);
309 
310 
311 
312  /*!
313  * \brief internal helper method
314  *
315  * Generates source code header, like initial comment block and includes.
316  * Is independent from model tree reprensentation
317  *
318  * \param[in] OutStream stream object used for output of generation
319  */
320  static void GenerateHeader(::std::ostream & OutStream) throw(::GOPPRR::Error::CException);
321 
322 
323 
324  /*!
325  * \brief internal helper method
326  *
327  * Generates source code footer.
328  *
329  * \param[in] OutStream stream object used for output of generation
330  */
331  static void GenerateFooter(::std::ostream & OutStream) throw(::GOPPRR::Error::CException);
332 
333 
334 
335  /*!
336  * \brief internal helper methods
337  *
338  * Evaluates, if a certain GOPPRR object is a function block element.
339  *
340  * \param[in] pObject pointer to the object to evaluate
341  *
342  * \return true, if object is a function block element
343  */
344  static bool IsFunctionBlock(GOPPRR::CObject * const pObject) throw(::GOPPRR::Error::CException);
345 
346 
347 
348  /*!
349  * \brief internal helper method
350  *
351  * Gets a string with the corresponding C++ type for a certain port
352  * of a function block object.
353  *
354  * \param[in] pPort pointer to the port
355  * \return string with the C++ data type
356  */
357  static ::std::string TypeFromPort(GOPPRR::CPort * const pPort) throw(::GOPPRR::Error::CException);
358 
359 
360 
361  /*!
362  * \brief internal helper method
363  *
364  * Gets a string with the corresponding data type prefix for certain port
365  * of a function block object.
366  *
367  * \param[in] pPort pointer to the port
368  * \return string with the prefix
369  */
370  static ::std::string PrefixFromPort(GOPPRR::CPort * const pPort) throw(::GOPPRR::Error::CException);
371 
372 
373 
374  /*!
375  * \brief internal helper method
376  *
377  * Processes recursively a abstract function block node model to find start
378  * nodes.
379  *
380  * \param[in] pNode node to be processed
381  * \param[in,out] ExecutionOrder: list with OID in order of their later execution
382  * \param[in,out] NodeStack: vector with pointer to already recursively processed nodes to detect data flow loops
383  */
384  static void ProcessAbstractModel(const oETCS::GEN::CFBNode & Node, ::std::list< ::std::string >& ExecutionOrder, ::std::vector< const oETCS::GEN::CFBNode * >& NodeStack) throw();
385 
386 
387 
388  /*!
389  * \brief map with function block meta model type names to C++ domain framework names
390  */
391  ::std::map< ::std::string, ::std::string > m_FBMap;
392 
393 
394 
395  /*!
396  * \brief map with function block meta model method/port names to C++ domain framework method names
397  */
398  ::std::map< ::std::string, ::std::string > m_FBMMap;
399 
400 
401 
402  /*!
403  * \brief map of already generated transition OIDs
404  */
405  ::std::map< ::std::string, bool > m_GeneratedTransitions;
406 
407 
408 
409  /*!
410  * \brief map of already generated EVC transition OIDs
411  */
412  ::std::map< ::std::string, bool > m_GeneratedEVCTransitions;
413 
414 
415 
416  /*!
417  * \brief map of already generated control flow OIDs
418  */
419  ::std::map< ::std::string, bool > m_GeneratedControlFlows;
420 
421 
422 
423 
424 }; // class CCPPGenerator : public oETCS::GEN::CGenerator
425 
426 
427 
428 
429 
430 } // namespace oETCS::GEN
431 
432 } // namespace oETCS
433 
434 #endif // __ OETCS_GEN_CPPGENERATOR_H__

Copyright (C) 2010-2012 Johannes Feuser (feuser@uni-bremen.de)
The openETCS library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.
The openETCS library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with the openETCS library. If not, see "http://www.gnu.org/licenses/.