CppGenerator.xtend 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. package be.uantwerpen.ansymo.semanticadaptation.cg.cpp.generation
  2. import be.uantwerpen.ansymo.semanticadaptation.generator.SemanticAdaptationGenerator
  3. import org.eclipse.xtext.generator.IFileSystemAccess2
  4. import org.eclipse.xtext.generator.IGeneratorContext
  5. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.SemanticAdaptation
  6. import org.eclipse.emf.ecore.resource.Resource
  7. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Adaptation
  8. import java.util.ArrayList
  9. import java.util.LinkedHashMap
  10. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.MappedScalarVariable
  11. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.InnerFMU
  12. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.exceptions.IncorrectAmountOfElementsException
  13. import java.io.File
  14. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.SAScalarVariable
  15. import org.eclipse.emf.common.util.EList
  16. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Port
  17. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.SVCausality
  18. import java.util.Collection
  19. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.InOutRules
  20. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.InRulesBlock
  21. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.InOutRulesBlockResult
  22. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.OutRulesBlock
  23. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.GlobalInOutVariable
  24. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.ControlRuleBlock
  25. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.RulesBlockResult
  26. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.ScalarVariable
  27. import java.util.List
  28. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.SVType
  29. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.ParamDeclarations
  30. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.InputOutputRuleType
  31. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.InnerFMUData
  32. class CppGenerator extends SemanticAdaptationGenerator {
  33. private var IFileSystemAccess2 fsa;
  34. private List<File> resourcePaths = newArrayList();
  35. override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
  36. this.fsa = fsa;
  37. for (SemanticAdaptation type : resource.allContents.toIterable.filter(SemanticAdaptation)) {
  38. type.compile;
  39. }
  40. }
  41. // TODO: Verify adaptation.name is not a C++ keyword
  42. def void compile(SemanticAdaptation adaptation) {
  43. for (Adaptation adap : adaptation.elements.filter(Adaptation)) {
  44. // Value used for scoping variables in the .sa file
  45. val adapInteralRefName = adap.name;
  46. // The CPP class name
  47. val adapClassName = adap.name.toFirstUpper;
  48. // This is the external name used in the model description file for the semantic adaptation FMU.
  49. val adapExternalName = adap.type.name;
  50. // List of FMUs with a pairing between its name and its type.name.
  51. //var ArrayList<Pair<String, String>> fmus = newArrayList();
  52. var ArrayList<InnerFMUData> innerFMUsData = newArrayList();
  53. // TODO: Currently only 1 inner fmu is supported
  54. val innerFmus = adap.inner.eAllContents.toList.filter(InnerFMU);
  55. // if (innerFmus.size > 1) {
  56. // throw new IncorrectAmountOfElementsException("Only one InnerFmu is supported.")
  57. // }
  58. if (innerFmus.isEmpty) {
  59. throw new IncorrectAmountOfElementsException("The adaptation does not contain any InnerFMUs.")
  60. }
  61. /*
  62. * This map will contain scalar variables from the FMUs defined in InnerFMU.
  63. * The structure is fmuName -> (SVName -> mappedSV) where SVName = mappedSV.name for easy lookup.
  64. * The mappedSV contains the original scalar variable and extra data such as define name.
  65. */
  66. var LinkedHashMap<String, LinkedHashMap<String, MappedScalarVariable>> mappedScalarVariables = newLinkedHashMap();
  67. /*
  68. * Loading the FMU defined in InnerFMU, the related model description file and its scalar variables.
  69. */
  70. // TODO: Add support for multiple inner fmus
  71. var ModelDescription md;
  72. for (fmu : adap.inner.eAllContents.toList.filter(InnerFMU)) {
  73. val fmuFile = new File(fmu.path.replace('\"', ''));
  74. this.resourcePaths.add(fmuFile);
  75. md = new ModelDescription(fmu.name, fmu.type.name, fmuFile);
  76. innerFMUsData.add(new InnerFMUData(fmu.name, fmu.type.name, md.guid));
  77. //fmus.add(fmu.name -> fmu.type.name);
  78. val LinkedHashMap<String, MappedScalarVariable> mSV = newLinkedHashMap();
  79. for (sv : md.sv.values) {
  80. var mappedSv = new MappedScalarVariable(sv);
  81. mappedSv.define = (mappedSv.mappedSv.owner + mappedSv.mappedSv.name).toUpperCase;
  82. mSV.put(mappedSv.mappedSv.name, mappedSv);
  83. }
  84. mappedScalarVariables.put(fmu.name, mSV);
  85. }
  86. // C++ Defines for accessing FMU scalar variables.
  87. val String fmusDefines = calcDefines(mappedScalarVariables);
  88. // Compile Params
  89. var LinkedHashMap<String, GlobalInOutVariable> params = newLinkedHashMap;
  90. val String paramsConstructorSource = compileParams(params, adap.params);
  91. /*
  92. * This map contains all the ScalarVariables for the semantic adaptation.
  93. * The are not populated yet, but they will be during the compilation of the in and out rule blocks.
  94. */
  95. var LinkedHashMap<String, SAScalarVariable> SASVs = calcSASVsFromInportsOutports(adapInteralRefName,
  96. adap.inports, adap.outports)
  97. // C++ defines for accessing semantic adaptation scalar variables
  98. val String SADefines = calcSADefines(SASVs.values);
  99. // Compile the transparent in mappings
  100. // Compile the in rules
  101. val inRuleResult = compileInOutRuleBlocks(InputOutputRuleType.Input, adaptation.eAllContents.toIterable.filter(
  102. InRulesBlock).map[x|x as InOutRules], adapClassName, adapInteralRefName, mappedScalarVariables, SASVs,
  103. params);
  104. // Compile the out rules
  105. val outRuleResult = compileInOutRuleBlocks(InputOutputRuleType.Output, adaptation.eAllContents.toIterable.
  106. filter(OutRulesBlock).map[x|x as InOutRules], adapClassName, adapInteralRefName, mappedScalarVariables,
  107. SASVs, params);
  108. // Compile the Control Rules. These might use the out vars, so pass these along.
  109. val crtlRuleResult = compileControlRuleBlock(adaptation.eAllContents.toIterable.filter(ControlRuleBlock),
  110. adapClassName, adapInteralRefName, mappedScalarVariables, SASVs, params, outRuleResult.gVars);
  111. /*
  112. * Compile the constructor, destructor and initialize functions
  113. */
  114. val String deAndConstructorAndInitializeSource = compileDeAndConstructorAndInitialize(
  115. adapClassName,
  116. innerFMUsData,
  117. md.guid,
  118. paramsConstructorSource,
  119. inRuleResult.constructorInitialization,
  120. outRuleResult.constructorInitialization,
  121. crtlRuleResult.constructorInitialization
  122. );
  123. /*
  124. * Compile getRuleThis function
  125. */
  126. val String getRuleThisSource = compileGetRuleThis(adapClassName);
  127. /*
  128. * The in and out rules have populated the semantic adaptation scalar variables we can generate the getFmiValue* and setFmiValue functions.
  129. */
  130. val String getFuncsSource = compileGetFmiValueFunctions(adapClassName, SASVs);
  131. val String setFuncsSource = compileSetFmiValueFunctions(adapClassName, SASVs);
  132. // Compile the source file
  133. val String sourceInclude = '''#include "«adapClassName».h"''';
  134. val sourceFile = compileSource(
  135. sourceInclude,
  136. deAndConstructorAndInitializeSource,
  137. getRuleThisSource,
  138. getFuncsSource,
  139. setFuncsSource,
  140. inRuleResult.generatedCpp,
  141. outRuleResult.generatedCpp,
  142. crtlRuleResult.generatedCpp
  143. );
  144. fsa.generateFile(adapClassName + ".cpp", sourceFile);
  145. // Merge the global variables for use in compiling the header file.
  146. // TODO: Check for duplicates
  147. var LinkedHashMap<String, GlobalInOutVariable> allGVars = newLinkedHashMap();
  148. allGVars.putAll(params);
  149. allGVars.putAll(outRuleResult.gVars);
  150. allGVars.putAll(inRuleResult.gVars);
  151. allGVars.putAll(crtlRuleResult.gVars);
  152. // Compile the header file
  153. val headerFile = compileHeader(
  154. adapClassName,
  155. fmusDefines,
  156. SADefines,
  157. inRuleResult.functionSignatures,
  158. outRuleResult.functionSignatures,
  159. crtlRuleResult.functionSignatures,
  160. allGVars,
  161. innerFMUsData,
  162. SASVs.values.map[CalcSVar()].toList
  163. );
  164. fsa.generateFile(adapClassName + ".h", headerFile);
  165. // Compile the model description file
  166. val modelDescCreator = new ModelDescriptionCreator(adapExternalName);
  167. val modelDescription = modelDescCreator.generateModelDescription(SASVs.values);
  168. fsa.generateFile("modelDescription.xml", modelDescription);
  169. // Compile the fmu.cpp file
  170. val fmuCppFile = StaticGenerators.GenFmuCppFile(adapClassName);
  171. fsa.generateFile("Fmu.cpp", fmuCppFile);
  172. }
  173. }
  174. def String compileParams(LinkedHashMap<String, GlobalInOutVariable> gVars, EList<ParamDeclarations> params) {
  175. val paramsConditionSwitch = new ParamConditionSwitch(gVars);
  176. var String paramsConstructorSource = "";
  177. for (paramDecl : params) {
  178. val doSwitchRes = paramsConditionSwitch.doSwitch(paramDecl);
  179. paramsConstructorSource += doSwitchRes.code;
  180. }
  181. return paramsConstructorSource;
  182. }
  183. def calcSADefines(Collection<SAScalarVariable> variables) {
  184. var ArrayList<String> defines = newArrayList();
  185. for (SASV : variables) {
  186. defines.add("#define " + SASV.defineName + " " + SASV.valueReference);
  187. }
  188. return defines.join("\n");
  189. }
  190. def calcDefines(LinkedHashMap<String, LinkedHashMap<String, MappedScalarVariable>> map) {
  191. var ArrayList<String> defines = newArrayList();
  192. for (fmuEntries : map.entrySet) {
  193. for (MappedScalarVariable mSV : fmuEntries.value.values) {
  194. defines.add("#define " + mSV.define + " " + mSV.valueReference);
  195. }
  196. }
  197. return defines.join("\n");
  198. }
  199. // Compiles the final source file
  200. def String compileSource(String include, String constructor, String getRuleThis, String getFunctions,
  201. String setFunctions, String inFunctions, String outFunctions, String controlFunction) {
  202. return '''
  203. «include»
  204. namespace adaptation
  205. {
  206. «constructor»
  207. «getRuleThis»
  208. «getFunctions»
  209. «setFunctions»
  210. «inFunctions»
  211. «controlFunction»
  212. «outFunctions»
  213. }
  214. '''
  215. }
  216. /*
  217. * Compiles the header file split into two: The first part contains the includes and using namespace definitions and start the ,
  218. * the second part contains the class
  219. */
  220. def String compileHeader(String adapClassName, String fmusDefines, String SADefines, List<String> inRulesFuncSig,
  221. List<String> outRulesFuncSig, List<String> crtlRulesFuncSig,
  222. LinkedHashMap<String, GlobalInOutVariable> globalVariables, ArrayList<InnerFMUData> fmus,
  223. Collection<ScalarVariable> sVars) {
  224. return '''
  225. #ifndef SRC_«adapClassName.toUpperCase»_H
  226. #define SRC_«adapClassName.toUpperCase»_H
  227. #include "SemanticAdaptation.h"
  228. #include <memory>
  229. #include "Fmu.h"
  230. using namespace std;
  231. using namespace fmi2;
  232. namespace adaptation
  233. {
  234. «fmusDefines»
  235. «SADefines»
  236. class «adapClassName» : public SemanticAdaptation<«adapClassName»>, public enable_shared_from_this<«adapClassName»>
  237. {
  238. public:
  239. «adapClassName»(shared_ptr<std::string> fmiInstanceName, shared_ptr<string> resourceLocation, const fmi2CallbackFunctions* functions);
  240. void initialize();
  241. virtual ~«adapClassName»();
  242. void setFmiValue(fmi2ValueReference id, int value);
  243. void setFmiValue(fmi2ValueReference id, bool value);
  244. void setFmiValue(fmi2ValueReference id, double value);
  245. void setFmiValue(fmi2ValueReference id, string value);
  246. int getFmiValueInteger(fmi2ValueReference id);
  247. bool getFmiValueBoolean(fmi2ValueReference id);
  248. double getFmiValueReal(fmi2ValueReference id);
  249. string getFmiValueString(fmi2ValueReference id);
  250. private:
  251. «adapClassName»* getRuleThis();
  252. /*in rules*/
  253. «inRulesFuncSig.map[x | x+";"].join("\n")»
  254. /*out rules*/
  255. «outRulesFuncSig.map[x | x+";"].join("\n")»
  256. «crtlRulesFuncSig.map[x | x+";"].join("\n")»
  257. «FOR fmu : fmus»
  258. shared_ptr<FmuComponent> «fmu.name»;
  259. «ENDFOR»
  260. «FOR sv : sVars»
  261. «Conversions.fmiTypeToCppType(sv.type)» «sv.name»;
  262. «IF sv.causality == SVCausality.input»
  263. bool isSet«sv.name»;
  264. «ENDIF»
  265. «ENDFOR»
  266. «FOR v : globalVariables.entrySet»
  267. «Conversions.fmiTypeToCppType(v.value.type)» «v.key»;
  268. «ENDFOR»
  269. };
  270. }
  271. #endif
  272. ''';
  273. }
  274. /*
  275. * Compiles the source file constructor, destructor and the initialize function
  276. */
  277. def String compileDeAndConstructorAndInitialize(String adapClassName, ArrayList<InnerFMUData> fmus,
  278. String guid, String paramsCons, String inCons, String outCons, String crtlCons) {
  279. var ArrayList<String> initialisations = newArrayList();
  280. for( fmu : fmus)
  281. {
  282. initialisations.add('''
  283. auto path = make_shared<string>(*resourceLocation);
  284. path->append(string("«fmu.typeName».fmu"));
  285. auto «fmu.name»Fmu = make_shared<fmi2::Fmu>(*path);
  286. «fmu.name»Fmu->initialize();
  287. this->«fmu.name» = «fmu.name»Fmu->instantiate("«fmu.name»",fmi2CoSimulation, "«fmu.guid»", true, true, shared_from_this());
  288. if(this->«fmu.name»->component == NULL)
  289. this->lastErrorState = fmi2Fatal;
  290. this->instances->push_back(this->«fmu.name»);
  291. ''');
  292. }
  293. return '''
  294. «adapClassName»::«adapClassName»(shared_ptr<std::string> fmiInstanceName,shared_ptr<string> resourceLocation, const fmi2CallbackFunctions* functions) :
  295. SemanticAdaptation(fmiInstanceName, resourceLocation, createInputRules(),createOutputRules(), functions)
  296. {
  297. «paramsCons»
  298. «inCons»
  299. «outCons»
  300. «crtlCons»
  301. }
  302. void «adapClassName»::initialize()
  303. {
  304. «initialisations.join("\r\n")»
  305. ««« «FOR fmu : fmus»
  306. ««« auto path = make_shared<string>(*resourceLocation);
  307. ««« path->append(string("«fmu.value».fmu"));
  308. ««« auto «fmu.key»Fmu = make_shared<fmi2::Fmu>(*path);
  309. ««« «fmu.key»Fmu->initialize();
  310. ««« this->«fmu.key» = «fmu.key»Fmu->instantiate("«fmu.key»",fmi2CoSimulation, "«guid»", true, true, shared_from_this());
  311. «««
  312. ««« if(this->«fmu.key»->component == NULL)
  313. ««« this->lastErrorState = fmi2Fatal;
  314. ««« this->instances->push_back(this->«fmu.key»);
  315. ««« «ENDFOR»
  316. }
  317. «adapClassName»::~«adapClassName»()
  318. {
  319. }
  320. ''';
  321. }
  322. /*
  323. * Compiles the source file function getRuleThis
  324. */
  325. def String compileGetRuleThis(String adaptationName) {
  326. return '''
  327. «adaptationName»* «adaptationName»::getRuleThis()
  328. {
  329. return this;
  330. }
  331. '''
  332. }
  333. /*
  334. * Compiles the source file functions getFmiValue<double, int, string, bool>
  335. */
  336. def String compileGetFmiValueFunctions(String adaptationName, LinkedHashMap<String, SAScalarVariable> variables) {
  337. var ArrayList<String> cpp = newArrayList();
  338. var List<ScalarVariable> convertedSASVs = variables.values.map[CalcSVar()].toList;
  339. var convertedSASVsOrdered = convertedSASVs.filter[causality === SVCausality.output].groupBy[type];
  340. for (SVType type : SVType.values) {
  341. val functionSignature = '''«Conversions.fmiTypeToCppType(type)» «adaptationName»::getFmiValue«type.toString»(fmi2ValueReference id)''';
  342. val functionReturn = '''return «Conversions.fmiTypeToCppDefaultValue(type)»''';
  343. if (convertedSASVsOrdered.containsKey(type)) {
  344. cpp.add(
  345. '''
  346. «functionSignature»
  347. {
  348. switch (id)
  349. {
  350. «FOR svInner : convertedSASVsOrdered.get(type)»
  351. case «variables.get(svInner.name).defineName»:
  352. {
  353. return this->«svInner.name»;
  354. }
  355. «ENDFOR»
  356. default:
  357. {
  358. «functionReturn»;
  359. }
  360. }
  361. }
  362. '''
  363. );
  364. } else {
  365. cpp.add(
  366. '''
  367. «functionSignature»
  368. {
  369. «functionReturn»;
  370. }
  371. '''
  372. );
  373. }
  374. }
  375. return cpp.join("\n");
  376. }
  377. /*
  378. * Compiles the source file functions setFmiValue<double, int, string, bool>*
  379. */
  380. def String compileSetFmiValueFunctions(String adapClassName, LinkedHashMap<String, SAScalarVariable> variables) {
  381. var ArrayList<String> cpp = newArrayList();
  382. var List<ScalarVariable> convertedSASVs = variables.values.map[CalcSVar()].filter [
  383. causality === SVCausality.input
  384. ].toList;
  385. var convertedSASVsOrdered = convertedSASVs.groupBy[type];
  386. for (SVType type : SVType.values) {
  387. cpp.add(
  388. '''
  389. void «adapClassName»::setFmiValue(fmi2ValueReference id, «Conversions.fmiTypeToCppType(type)» value)
  390. {
  391. «IF convertedSASVsOrdered.containsKey(type)»
  392. switch (id)
  393. {
  394. «FOR svInner : convertedSASVsOrdered.get(type)»
  395. case «variables.get(svInner.name).defineName»:
  396. {
  397. this->«svInner.name» = value;
  398. this->isSet«svInner.name» = true;
  399. break;
  400. }
  401. «ENDFOR»
  402. default:
  403. {
  404. }
  405. }
  406. «ENDIF»
  407. }
  408. '''
  409. );
  410. }
  411. return cpp.join("\n");
  412. }
  413. /*
  414. * Compiles the source file function executeInternalControlFlow.
  415. * Calculates necessary information on function signatures necessary for generation of the header file.
  416. */
  417. def InOutRulesBlockResult compileControlRuleBlock(Iterable<ControlRuleBlock> crtlRuleBlocks, String adaptationClassName,
  418. String adaptationName, LinkedHashMap<String, LinkedHashMap<String, MappedScalarVariable>> mSVars,
  419. LinkedHashMap<String, SAScalarVariable> SASVs, LinkedHashMap<String, GlobalInOutVariable> params, LinkedHashMap<String, GlobalInOutVariable> outVars) {
  420. var cpp = "";
  421. val visitor = new ControlConditionSwitch(adaptationClassName, adaptationName, mSVars, SASVs, params, outVars);
  422. for (crtlRule : crtlRuleBlocks) {
  423. cpp += visitor.doSwitch(crtlRule).code;
  424. }
  425. return new InOutRulesBlockResult(cpp, visitor.functionSignatures, visitor.globalVars, visitor.constructorInitialization);
  426. }
  427. def String SplitAtSpaceAndRemoveFirst(String content) {
  428. content.substring(content.indexOf(" ") + 1, content.length);
  429. }
  430. def String removeEmptyArgumentParenthesis(String content) {
  431. return content.substring(0, content.length - 2);
  432. }
  433. /*
  434. * Compiles the source file functions <in/out>_rule_<condition, body, flush>.
  435. * Calculates necessary information on global in/out variables necessary for generation of the header file.
  436. * Calculates necessary information on function signatures necessary for generation of the header file.
  437. */
  438. def InOutRulesBlockResult compileInOutRuleBlocks(InputOutputRuleType ioType, Iterable<InOutRules> rulesBlocks,
  439. String adaptationClassName, String adaptationName,
  440. LinkedHashMap<String, LinkedHashMap<String, MappedScalarVariable>> mSVars,
  441. LinkedHashMap<String, SAScalarVariable> SASVs, LinkedHashMap<String, GlobalInOutVariable> params) {
  442. val visitor = if (ioType == InputOutputRuleType.Input)
  443. new InRulesConditionSwitch(adaptationClassName, adaptationName, mSVars, SASVs, params)
  444. else
  445. new OutRulesConditionSwitch(adaptationClassName, adaptationName, mSVars, SASVs, params);
  446. val functionName = "create" + ioType + "Rules()";
  447. var String cpp = "";
  448. val ruleBlock = rulesBlocks.head;
  449. if (ruleBlock !== null) {
  450. cpp += visitor.doSwitch(ruleBlock).code;
  451. if (!visitor.functionSignatures.empty) {
  452. var ArrayList<String> createRulesFunction = newArrayList();
  453. for (var int i = 0; i < (visitor.functionSignatures.length); i += 3) {
  454. createRulesFunction.add(
  455. '''
  456. list->push_back(
  457. (Rule<«adaptationClassName»>){
  458. &«adaptationClassName»::«visitor.functionSignatures.get(i).SplitAtSpaceAndRemoveFirst.removeEmptyArgumentParenthesis»,
  459. &«adaptationClassName»::«visitor.functionSignatures.get(i+1).SplitAtSpaceAndRemoveFirst.removeEmptyArgumentParenthesis»,
  460. &«adaptationClassName»::«visitor.functionSignatures.get(i+2).SplitAtSpaceAndRemoveFirst.removeEmptyArgumentParenthesis»
  461. });
  462. ''');
  463. }
  464. val functionPrefix = '''shared_ptr<list<Rule<«adaptationClassName»>>>''';
  465. visitor.functionSignatures.add(functionPrefix + " " + functionName)
  466. cpp += '''
  467. «functionPrefix» «adaptationClassName»::«functionName»
  468. {
  469. auto list = make_shared<std::list<Rule<«adaptationClassName»>>>();
  470. «createRulesFunction.join("\n")»
  471. return list;
  472. }
  473. '''
  474. }
  475. }
  476. return new InOutRulesBlockResult(cpp, visitor.functionSignatures, visitor.getGlobalVars,
  477. visitor.constructorInitialization);
  478. }
  479. /*
  480. * Calculates the semantic adaptation scalar variables via input ports and output ports.
  481. * Note: These a not fully populated yet as the in rules and out rules must be compiled first.
  482. */
  483. def LinkedHashMap<String, SAScalarVariable> calcSASVsFromInportsOutports(String definePrefix, EList<Port> inports,
  484. EList<Port> outports) {
  485. var LinkedHashMap<String, SAScalarVariable> saSVs = newLinkedHashMap();
  486. var int valueReference = 0;
  487. for (inport : inports) {
  488. var saSV = new SAScalarVariable();
  489. saSV.SetPartOfMD(true);
  490. saSV.valueReference = valueReference++;
  491. saSV.name = inport.name;
  492. saSV.defineName = (definePrefix + inport.name).toUpperCase
  493. saSV.causality = SVCausality.input;
  494. saSVs.put(saSV.name, saSV);
  495. }
  496. for (outport : outports) {
  497. var saSV = new SAScalarVariable();
  498. saSV.SetPartOfMD(true);
  499. saSV.valueReference = valueReference++;
  500. saSV.defineName = (definePrefix + outport.name).toUpperCase
  501. saSV.name = outport.name;
  502. saSV.causality = SVCausality.output;
  503. saSVs.put(saSV.name, saSV);
  504. }
  505. return saSVs;
  506. }
  507. def List<File> getResourcePaths() {
  508. return resourcePaths;
  509. }
  510. }