ControlConditionSwitch.xtend 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package be.uantwerpen.ansymo.semanticadaptation.cg.cpp.generation
  2. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.GlobalInOutVariable
  3. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.ReturnInformation
  4. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.SAScalarVariable
  5. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.SVType
  6. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.ControlRuleBlock
  7. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.CurrentTime
  8. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.CustomControlRule
  9. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.DoStep
  10. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.DoStepFun
  11. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.StepSize
  12. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Variable
  13. import java.util.LinkedHashMap
  14. class ControlConditionSwitch extends RulesConditionSwitch {
  15. new(
  16. String adaptationClassName,
  17. String adaptationName,
  18. LinkedHashMap<String, SAScalarVariable> SASVs,
  19. LinkedHashMap<String, GlobalInOutVariable> params
  20. ) {
  21. super(adaptationClassName, adaptationName, "", null, SASVs, params);
  22. }
  23. override ReturnInformation caseVariable(Variable object) {
  24. var retVal = new ReturnInformation();
  25. // H and t are protected variables.
  26. if ((object.owner === null || object.owner.name == this.adaptationName) &&
  27. (object.ref.name == "H" || object.ref.name == "t")) {
  28. retVal.type = SVType.Real;
  29. retVal.forceType = true;
  30. retVal.code = object.ref.name;
  31. } else {
  32. retVal = super.caseVariable(object);
  33. }
  34. return retVal;
  35. }
  36. override ReturnInformation caseControlRuleBlock(ControlRuleBlock object) {
  37. this.globalDeclaration = true;
  38. var retVal = new ReturnInformation();
  39. // Get the global variables added to globalVars
  40. for (gVar : object.globalCtrlVars) {
  41. constructorInitialization += doSwitch(gVar).code;
  42. }
  43. this.globalDeclaration = false;
  44. retVal.appendCode(doSwitch(object.rule).code);
  45. return retVal;
  46. }
  47. override ReturnInformation caseCustomControlRule(CustomControlRule object) {
  48. var retVal = new ReturnInformation();
  49. var String tempDoSwitchCode = "";
  50. for (ruleStm : object.controlRulestatements) {
  51. tempDoSwitchCode += doSwitch(ruleStm).code;
  52. }
  53. tempDoSwitchCode += System.lineSeparator() + '''return «doSwitch(object.returnstatement.expr).code»;
  54. ''';
  55. var functionPrefix = "double ";
  56. var functionNameArgs = "executeInternalControlFlow(double H, double t)"
  57. functionSignatures.add(functionPrefix + functionNameArgs);
  58. retVal.code = '''
  59. «functionPrefix+this.adaptationClassName»::«functionNameArgs»
  60. {
  61. «tempDoSwitchCode»
  62. }
  63. ''';
  64. return retVal;
  65. }
  66. override ReturnInformation caseDoStepFun(DoStepFun object) {
  67. var retVal = new ReturnInformation();
  68. retVal.code = '''this->do_step(«object.fmu.name»,«doSwitch(object.h).code»,«doSwitch(object.t).code»);''';
  69. retVal.type = SVType.Integer;
  70. return retVal;
  71. }
  72. override ReturnInformation caseDoStep(DoStep object) {
  73. var retVal = new ReturnInformation();
  74. retVal.code = '''this->do_step(«object.fmu.name»,«doSwitch(object.h).code»,«doSwitch(object.t).code»);''';
  75. return retVal;
  76. }
  77. override ReturnInformation caseStepSize(StepSize object) {
  78. var retVal = new ReturnInformation();
  79. retVal.code = '''H''';
  80. return retVal;
  81. }
  82. override ReturnInformation caseCurrentTime(CurrentTime object) {
  83. var retVal = new ReturnInformation();
  84. retVal.code = '''t''';
  85. return retVal;
  86. }
  87. }