RulesConditionSwitch.xtend 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. package be.uantwerpen.ansymo.semanticadaptation.cg.cpp.generation
  2. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.util.SemanticAdaptationSwitch
  3. import java.util.LinkedHashMap
  4. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.SVType
  5. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.GlobalInOutVariable
  6. import java.util.List
  7. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.MappedScalarVariable
  8. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.SAScalarVariable
  9. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.ReturnInformation
  10. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Literal
  11. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.RealLiteral
  12. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.IntLiteral
  13. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.BoolLiteral
  14. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.OutRulesBlock
  15. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.DataRule
  16. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.InRulesBlock
  17. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.RuleCondition
  18. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.StateTransitionFunction
  19. import org.eclipse.emf.ecore.EObject
  20. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Assignment
  21. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Multi
  22. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Neg
  23. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.SingleVarDeclaration
  24. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.If
  25. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.CompositeOutputFunction
  26. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Variable
  27. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.LValueDeclaration
  28. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Declaration
  29. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.IsSet
  30. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Div
  31. import org.eclipse.emf.common.util.EList
  32. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.InOutRules
  33. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Min
  34. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.exceptions.TypeException
  35. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Minus
  36. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.For
  37. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.SaveState
  38. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Rollback
  39. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Range
  40. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Close
  41. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.BreakStatement
  42. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Port
  43. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.CurrentTime
  44. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Var
  45. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Plus
  46. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.LessThan
  47. class RulesConditionSwitch extends BasicConditionSwitch {
  48. // Global params
  49. protected var LinkedHashMap<String, GlobalInOutVariable> params;
  50. protected final String adaptationName;
  51. protected final String adaptationClassName;
  52. protected Integer count = 0;
  53. /** See the method {@link #createFunctionSignature(String, String)} and subclasses */
  54. public final String functionPrefix;
  55. protected List<String> functionSignatures = newArrayList();
  56. /*
  57. * Intermediate variable used for referencing external FMU.
  58. * Currently only used by OutRulesConditionSwitch.
  59. */
  60. protected String externalVariableOwner;
  61. protected final LinkedHashMap<String, LinkedHashMap<String, MappedScalarVariable>> mSVars;
  62. protected final LinkedHashMap<String, SAScalarVariable> SASVs;
  63. // Global in and out variables
  64. protected var LinkedHashMap<String, GlobalInOutVariable> gVars = newLinkedHashMap();
  65. LinkedHashMap<String, GlobalInOutVariable> inVars;
  66. LinkedHashMap<String, GlobalInOutVariable> outVars;
  67. LinkedHashMap<String, GlobalInOutVariable> crtlVars;
  68. protected boolean inRuleCondition;
  69. protected boolean inRuleTransition;
  70. protected boolean inRuleOutput;
  71. protected boolean inControlRule;
  72. // Flag to signal whether the declarations to be processed are global or local.
  73. protected boolean globalDeclaration = false;
  74. // Add scope information to this.
  75. protected var LinkedHashMap<String, SVType> localDeclarations = newLinkedHashMap();
  76. protected String forLoopIterVar;
  77. protected boolean forLoopIterVarIsSet = false;
  78. new(
  79. String adaptationClassName,
  80. String adaptationName,
  81. String functionPrefix,
  82. LinkedHashMap<String, LinkedHashMap<String, MappedScalarVariable>> mSVars,
  83. LinkedHashMap<String, SAScalarVariable> SASVs,
  84. LinkedHashMap<String, GlobalInOutVariable> params,
  85. LinkedHashMap<String, GlobalInOutVariable> inVars,
  86. LinkedHashMap<String, GlobalInOutVariable> outVars,
  87. LinkedHashMap<String, GlobalInOutVariable> crtlVars
  88. ) {
  89. super();
  90. this.params = params;
  91. this.SASVs = SASVs;
  92. this.adaptationName = adaptationName;
  93. this.adaptationClassName = adaptationClassName;
  94. this.functionPrefix = functionPrefix;
  95. this.mSVars = mSVars;
  96. this.outVars = outVars;
  97. this.inVars = inVars;
  98. this.crtlVars = crtlVars;
  99. }
  100. /*
  101. * UTILITY FUNCTIONS
  102. */
  103. /**
  104. * This function adds a header style function signature to the list <i>functionsignatures</i>
  105. * and returns the source style function signature
  106. */
  107. public def String createFunctionSignature(String functionName, String type, int count,
  108. List<String> functionSignatures) {
  109. val functionSignature = this.functionPrefix + functionName + count + "()";
  110. functionSignatures.add(type + " " + functionSignature);
  111. return type + " " + this.adaptationClassName + "::" + functionSignature;
  112. }
  113. public def getDataRuleCount() { return this.count; }
  114. def void incrementCount() {
  115. this.count++;
  116. }
  117. /*
  118. * COMPILATION FUNCTIONS
  119. */
  120. public def Pair<String, LinkedHashMap<String, GlobalInOutVariable>> getGlobalVars(EList<Declaration> gVars) {
  121. // Get the global variables added to globalVars
  122. this.globalDeclaration = true;
  123. val List<String> constructorInits = newArrayList();
  124. for (gVar : gVars) {
  125. constructorInits.add('''«doSwitch(gVar).code»''');
  126. }
  127. this.globalDeclaration = false;
  128. return constructorInits.join(System.lineSeparator) -> this.gVars;
  129. }
  130. protected def ReturnInformation doSwitch(EList<Declaration> gVars, InOutRules object) {
  131. var retVal = new ReturnInformation();
  132. for (dataRule : object.eAllContents.toIterable.filter(DataRule)) {
  133. this.incrementCount;
  134. retVal.appendCode(doSwitch(dataRule).code);
  135. }
  136. return retVal;
  137. }
  138. override ReturnInformation caseDataRule(DataRule object) {
  139. var retVal = new ReturnInformation();
  140. inRuleCondition = true;
  141. val conditionSwitch = doSwitch(object.condition);
  142. inRuleCondition = false;
  143. inRuleTransition = true;
  144. val transitionSwitch = doSwitch(object.statetransitionfunction);
  145. inRuleTransition = false;
  146. inRuleOutput = true;
  147. val outputSwitch = doSwitch(object.outputfunction);
  148. inRuleOutput = false;
  149. retVal.code = '''
  150. «conditionSwitch.code»
  151. «transitionSwitch.code»
  152. «outputSwitch.code»
  153. '''
  154. return retVal;
  155. }
  156. override ReturnInformation caseRuleCondition(RuleCondition object) {
  157. var retVal = new ReturnInformation();
  158. val functionSignature = createFunctionSignature("condition", "bool", this.count, this.functionSignatures);
  159. retVal.code = '''
  160. «functionSignature»{
  161. double h = 0;
  162. double dt = 0;
  163. return «doSwitch(object.condition).code»;
  164. }
  165. ''';
  166. return retVal;
  167. }
  168. override ReturnInformation caseStateTransitionFunction(StateTransitionFunction object) {
  169. var retVal = new ReturnInformation();
  170. val functionSig = createFunctionSignature("body", "void", this.count, this.functionSignatures);
  171. retVal.code = '''
  172. «functionSig»{
  173. double h = 0;
  174. double dt = 0;
  175. «IF object.expression !== null»
  176. «val result = doSwitch(object.expression)»
  177. «result.code»«if (!result.isExpression) ";"»
  178. «ENDIF»
  179. «IF object.statements !== null»
  180. «FOR stm : object.statements»
  181. «val result = doSwitch(stm)»
  182. «result.code»«if (!result.isExpression) ";"»
  183. «ENDFOR»
  184. «ENDIF»
  185. «IF object.assignment !== null»
  186. «val result = doSwitch(object.assignment)»
  187. «result.code»«if (!result.isExpression) ";"»
  188. «ENDIF»
  189. }
  190. ''';
  191. return retVal;
  192. }
  193. override ReturnInformation caseIf(If object) {
  194. var retVal = new ReturnInformation();
  195. retVal.isExpression = true;
  196. retVal.code = '''
  197. if(«doSwitch(object.ifcondition).code»){
  198. «FOR stm : object.ifstatements»
  199. «val result = doSwitch(stm)»
  200. «result.code»«if (!result.isExpression) ";"»
  201. «ENDFOR»
  202. }
  203. ''';
  204. if (object.elsestatements.length > 0) {
  205. retVal.appendCode('''
  206. else {
  207. «FOR stm : object.elsestatements»
  208. «doSwitch(stm).code»;
  209. «ENDFOR»
  210. }
  211. ''')
  212. }
  213. return retVal;
  214. }
  215. private def calcConSaSvData(SAScalarVariable SASV, ReturnInformation rI) {
  216. if (SASV !== null) {
  217. if (rI.typeIsSet) {
  218. SASV.type = rI.type;
  219. SASV.variability = Conversions.fmiTypeToFmiVariability(rI.type);
  220. return;
  221. } else if (rI.conGlobVar !== null) {
  222. SASV.type = rI.conGlobVar.type;
  223. SASV.variability = Conversions.fmiTypeToFmiVariability(rI.conGlobVar.type);
  224. return;
  225. }
  226. }
  227. throw new Exception("Not enough information to determine content of the SASV: " + SASV.name);
  228. }
  229. override ReturnInformation caseAssignment(Assignment object) {
  230. var retVal = new ReturnInformation();
  231. var lValSwitch = doSwitch(object.lvalue);
  232. var rValSwitch = doSwitch(object.expr);
  233. // Here we set the information necessary to create a scalar variables in the model description for the SA.
  234. if (inRuleTransition) {
  235. if (rValSwitch.conSaSv !== null) {
  236. calcConSaSvData(rValSwitch.conSaSv, lValSwitch);
  237. }
  238. } else if (inRuleOutput) {
  239. calcConSaSvData(lValSwitch.conSaSv, rValSwitch);
  240. }
  241. retVal.code = '''«lValSwitch.code» = «rValSwitch.code»''';
  242. return retVal;
  243. }
  244. override ReturnInformation caseSingleVarDeclaration(SingleVarDeclaration object) {
  245. var retVal = new ReturnInformation();
  246. var exprRes = doSwitch(object.expr);
  247. var String code = "";
  248. if (globalDeclaration) {
  249. // This is an in var, out var or crtl var declaration
  250. code = '''this->internalState.«object.name» = «exprRes.code»''';
  251. var globVar = new GlobalInOutVariable(object.name, exprRes.type);
  252. gVars.put(object.name, globVar)
  253. } else {
  254. // This is a local declaration.
  255. val String type = Conversions.fmiTypeToCppType(exprRes.type)
  256. code = '''«type» «object.name» = «exprRes.code»''';
  257. this.localDeclarations.put(object.name, exprRes.type);
  258. }
  259. retVal.code = code;
  260. return retVal;
  261. }
  262. override ReturnInformation caseCompositeOutputFunction(CompositeOutputFunction object) {
  263. var retVal = new ReturnInformation();
  264. val functionSig = createFunctionSignature("flush", "void", this.count, this.functionSignatures);
  265. retVal.code = '''
  266. «functionSig»{
  267. double h = 0;
  268. double dt = 0;
  269. «FOR stm : object.statements»
  270. «val result = doSwitch(stm)»
  271. «result.code»«if(!result.isExpression) ";"»
  272. «ENDFOR»
  273. }
  274. ''';
  275. return retVal;
  276. }
  277. override ReturnInformation caseVariable(Variable object) {
  278. var retVal = new ReturnInformation();
  279. if (object.owner === null || object.owner.name == this.adaptationName) {
  280. if ((SASVs !== null && SASVs.containsKey(object.ref.name)) || params.containsKey(object.ref.name) ||
  281. (outVars !== null && outVars.containsKey(object.ref.name)) ||
  282. (inVars !== null && inVars.containsKey(object.ref.name)) ||
  283. (crtlVars !== null && crtlVars.containsKey(object.ref.name))) {
  284. retVal.code = '''this->internalState.«object.ref.name»''';
  285. if (SASVs !== null && SASVs.containsKey(object.ref.name)) {
  286. retVal.conSaSv = SASVs.get(object.ref.name);
  287. } else if (crtlVars !== null && crtlVars.containsKey(object.ref.name)) {
  288. retVal.conGlobVar = crtlVars.get(object.ref.name);
  289. } else if (params.containsKey(object.ref.name)) {
  290. retVal.conGlobVar = params.get(object.ref.name);
  291. } else if (outVars !== null && outVars.containsKey(object.ref.name)) {
  292. retVal.conGlobVar = outVars.get(object.ref.name);
  293. } else if (inVars !== null && inVars.containsKey(object.ref.name)) {
  294. retVal.conGlobVar = inVars.get(object.ref.name);
  295. }
  296. } else if (localDeclarations.containsKey(object.ref.name)) {
  297. retVal.code = '''«object.ref.name»'''
  298. retVal.type = localDeclarations.get(object.ref.name);
  299. } else {
  300. throw new Exception("Variable not found: " + object.ref.name);
  301. }
  302. } else {
  303. this.externalVariableOwner = object.owner.name;
  304. val res = doSwitch(object.ref);
  305. retVal.code = res.code;
  306. retVal.type = res.type;
  307. }
  308. return retVal;
  309. }
  310. override ReturnInformation caseLValueDeclaration(LValueDeclaration object) {
  311. var retVal = new ReturnInformation();
  312. retVal.code = '''«object.name»''';
  313. return retVal;
  314. }
  315. override ReturnInformation caseVar(Var object) {
  316. var retVal = new ReturnInformation();
  317. return retVal;
  318. }
  319. override ReturnInformation caseDeclaration(Declaration object) {
  320. var retVal = new ReturnInformation();
  321. val code = '''
  322. «FOR decl : object.declarations»
  323. «val res = doSwitch(decl)»
  324. «res.code»;
  325. «ENDFOR»
  326. '''
  327. retVal.code = code;
  328. retVal.isExpression = true;
  329. return retVal;
  330. }
  331. override ReturnInformation caseIsSet(IsSet object) {
  332. var retInfo = new ReturnInformation();
  333. retInfo.code = '''this->internalState.isSet«(object.args as Variable).ref.name»''';
  334. return retInfo;
  335. }
  336. override ReturnInformation caseFor(For object) {
  337. {
  338. var retVal = new ReturnInformation();
  339. retVal.isExpression = true;
  340. val iterator = doSwitch(object.iterator);
  341. forLoopIterVar = iterator.code;
  342. forLoopIterVarIsSet = true;
  343. val iterable = doSwitch(object.iterable);
  344. retVal.appendCode('''
  345. for (int «forLoopIterVar» = «iterable.code»){
  346. «FOR stm : object.statements»
  347. «val result = doSwitch(stm)»
  348. «result.code»«if (!result.isExpression) ";"»
  349. «ENDFOR»
  350. }
  351. ''')
  352. forLoopIterVarIsSet = false;
  353. return retVal;
  354. }
  355. }
  356. override ReturnInformation caseRange(Range object) {
  357. var retVal = new ReturnInformation();
  358. val left = doSwitch(object.left);
  359. val right = doSwitch(object.right);
  360. retVal.appendCode('''«left.code»; «forLoopIterVar»<=«right.code»; «forLoopIterVar»++''')
  361. return retVal;
  362. }
  363. }