@@ -46,6 +46,7 @@ import com.regnosys.rosetta.rosetta.expression.MinOperation
4646import com.regnosys.rosetta.rosetta.expression.MaxOperation
4747import com.regnosys.rosetta.rosetta.expression.SwitchOperation
4848import com.regnosys.rosetta.rosetta.expression.SwitchCaseGuard
49+ import com.regnosys.rosetta.rosetta.expression.SwitchCaseOrDefault
4950import com.regnosys.rosetta.rosetta.simple.Attribute
5051import com.regnosys.rosetta.rosetta.simple.Condition
5152import com.regnosys.rosetta.rosetta.simple.Data
@@ -61,44 +62,7 @@ class PythonExpressionGenerator {
6162
6263 public var List<String > importsFound
6364 public var ifCondBlocks = new ArrayList<String > ()
64- public var switchCondBlocks = new ArrayList<String > ()
65-
66- def String generateConditions (Data cls ) {
67- var nConditions = 0 ;
68- var result = ' ' ;
69- for (Condition cond : cls. conditions) {
70- result + = generateConditionBoilerPlate(cond, nConditions)
71- if (cond. isConstraintCondition)
72- result + = generateConstraintCondition(cls, cond)
73- else
74- result + = generateIfThenElseOrSwitch(cond)
75- nConditions++
76- }
77- return result
78- }
79-
80- def generateFunctionConditions (List<Condition > conditions , String condition_type ) {
81- var nConditions = 0 ;
82- var result = ' ' ;
83- for (Condition cond : conditions) {
84- result + = generateFunctionConditionBoilerPlate(cond, nConditions, condition_type)
85- result + = generateIfThenElseOrSwitch(cond)
86- nConditions++
87- }
88-
89- return result
90- }
91-
92- def generateExpressionThenElse (RosettaExpression expr , List<Integer > ifLevel ) {
93- ifCondBlocks. clear()
94- generateExpression(expr, ifLevel. get(0 ), false )
95- var blocks = " "
96- if (! ifCondBlocks. isEmpty()) {
97- ifLevel. set(0 , ifLevel. get(0 ) + 1 )
98- blocks = ' ' ' «FOR arg : ifCondBlocks»«arg»«ENDFOR»' ' '
99- }
100- return ' ' ' «blocks»' ' '
101- }
65+ public var isSwitchCond = false
10266
10367 def String generateExpression (RosettaExpression expr , int ifLevel , boolean isLambda ) {
10468 switch (expr) {
@@ -143,72 +107,6 @@ class PythonExpressionGenerator {
143107 }
144108 }
145109
146- private def boolean isConstraintCondition (Condition cond ) {
147- return isOneOf(cond) || isChoice(cond)
148- }
149-
150- private def boolean isOneOf (Condition cond ) {
151- return cond. expression instanceof OneOfOperation
152- }
153-
154- private def boolean isChoice (Condition cond ) {
155- return cond. expression instanceof ChoiceOperation
156- }
157-
158- private def generateConditionBoilerPlate (Condition cond , int nConditions ) {
159- ' ' '
160-
161- @rune_condition
162- def condition_«nConditions»_«cond.name»(self):
163- «IF cond.definition!==null»
164- """
165- «cond.definition»
166- """
167- «ENDIF»
168- item = self
169- ' ' '
170- }
171-
172- private def generateFunctionConditionBoilerPlate (Condition cond , int nConditions , String condition_type ) {
173- ' ' '
174-
175- @rune_local_condition(«condition_type»)
176- def condition_«nConditions»_«cond.name»(self):
177- «IF cond.definition!==null»
178- """
179- «cond.definition»
180- """
181- «ENDIF»
182- ' ' '
183- }
184-
185- private def generateConstraintCondition (Data cls , Condition cond ) {
186- val expression = cond. expression
187- var attributes = cls. attributes
188- var necessity = " necessity=True"
189- if (expression instanceof ChoiceOperation ) {
190- attributes = expression. attributes
191- if (expression. necessity == Necessity . OPTIONAL ) {
192- necessity = " necessity=False"
193- }
194- }
195- ' ' ' return rune_check_one_of(self, «FOR a : attributes SEPARATOR ", "»' «a. name»' «ENDFOR», «necessity»)
196- ' ' '
197- }
198-
199- private def generateIfThenElseOrSwitch (Condition c ) {
200- ifCondBlocks. clear()
201- switchCondBlocks. clear()
202- var expr = generateExpression(c. expression, 0 , false )
203- if (! switchCondBlocks. isEmpty()) {
204- var switchBlocks = ' ' ' «FOR arg : switchCondBlocks»«arg»«ENDFOR»' ' '
205- return ' ' ' «switchBlocks» «expr»' ' '
206- }
207- var blocks = (ifCondBlocks. isEmpty()) ? " " : ' ' ' «FOR arg : ifCondBlocks»«arg»«ENDFOR»' ' '
208- return ' ' ' «blocks» return «expr»
209- ' ' '
210- }
211-
212110 private def String generateConditionalExpression (RosettaConditionalExpression expr , int ifLevel , boolean isLambda ) {
213111 val ifExpr = generateExpression(expr. getIf(), ifLevel + 1 , isLambda)
214112 val ifThen = generateExpression(expr. ifthen, ifLevel + 1 , isLambda)
@@ -288,72 +186,67 @@ class PythonExpressionGenerator {
288186 private def String generateSwitchOperation (SwitchOperation expr , int ifLevel , boolean isLambda ) {
289187 // translate switch into a series of if / elif statements
290188 val attr = generateExpression(expr. argument, 0 , isLambda)
291- val arg = expr. argument as RosettaSymbolReference
292-
293- var funcNames = new ArrayList<String > ()
294189
295- for (thenExpr : expr. cases) {
296- val thenExprDef = generateExpression(thenExpr. getExpression(), ifLevel + 1 , isLambda)
297- val funcName = ' ' ' _then_«funcNames.size()+1»' ' '
298- funcNames. add(funcName)
299- switchCondBlocks. add(
300- ' ' '
301- def «funcName»():
302- return «thenExprDef»
303- ' ' '
304- )
305- }
190+ var _thenFuncsBuilder = new StringConcatenation ()
191+ var _switchLogicBuilder= new StringConcatenation ()
306192
307- // default case
308- val defaultExprDef = generateExpression(expr. getDefault(), 0 , isLambda)
309- val defaultFuncName = ' ' ' _then_default' ' '
310- funcNames. add(defaultFuncName)
311- switchCondBlocks. add(
312- ' ' '
313- def «defaultFuncName»():
314- return «defaultExprDef»
315- ' ' '
316- )
317- var _builder = new StringConcatenation ()
318-
319- // Generate switch logic
320193 val indent = " "
321- _builder. append(" switchAttribute = " )
322- _builder. append(attr)
323- _builder. newLine()
324- // Append each conditional
325- for (i : 0 .. < expr. cases. size) {
326- val guard = expr. cases. get(i). getGuard()
327-
328- val prefix = (i == 0 ) ? " if " : " elif "
329- _builder. append(indent)
330- _builder. append(prefix)
331- if (guard. getLiteralGuard() !== null ) {
332- val guardExpr = generateExpression(guard. getLiteralGuard(), 0 , isLambda)
333- _builder. append(" switchAttribute == " )
334- _builder. append(guardExpr)
335- } else {
336- val guardExpr = getGuardExpression(guard, isLambda)
337- _builder. append(guardExpr)
194+ isSwitchCond= true
195+
196+ for (pair : expr. cases. indexed) {
197+ val currentCase = pair. value as SwitchCaseOrDefault
198+ val funcName= (currentCase. isDefault()) ? " _then_default" : " _then_" + (pair. key+ 1 )
199+ val thenExprDef= (currentCase. isDefault()) ? generateExpression(expr. getDefault(), 0 , isLambda) : generateExpression(currentCase. getExpression(), ifLevel + 1 , isLambda)
200+
201+ _thenFuncsBuilder. append(indent)
202+ _thenFuncsBuilder. append(" def " + funcName + " ():" )
203+ _thenFuncsBuilder. newLine
204+ _thenFuncsBuilder. append(indent)
205+ _thenFuncsBuilder. append(" return " + thenExprDef)
206+ _thenFuncsBuilder. newLine
207+
208+ if (currentCase. isDefault()){
209+ // Default else
210+ _switchLogicBuilder. append(indent)
211+ _switchLogicBuilder. append(" else:" )
212+ _switchLogicBuilder. newLine()
213+ _switchLogicBuilder. append(indent)
214+ _switchLogicBuilder. append(" return " )
215+ _switchLogicBuilder. append(funcName)
216+ _switchLogicBuilder. append(" ()" )
217+ }
218+ else {
219+ val guard = currentCase. getGuard()
220+
221+ val prefix = (pair. key == 0 ) ? " if " : " elif "
222+ _switchLogicBuilder. append(indent)
223+ _switchLogicBuilder. append(prefix)
224+ if (guard. getLiteralGuard() !== null ) {
225+ val guardExpr = generateExpression(guard. getLiteralGuard(), 0 , isLambda)
226+ _switchLogicBuilder. append(" switchAttribute == " )
227+ _switchLogicBuilder. append(guardExpr)
228+ } else {
229+ val guardExpr = getGuardExpression(guard, isLambda)
230+ _switchLogicBuilder. append(guardExpr)
231+ }
232+ _switchLogicBuilder. append(" :" )
233+ _switchLogicBuilder. newLine()
234+ _switchLogicBuilder. append(indent)
235+ _switchLogicBuilder. append(" return " )
236+ _switchLogicBuilder. append(funcName)
237+ _switchLogicBuilder. append(" ()" )
238+ _switchLogicBuilder. newLine()
338239 }
339- _builder. append(" :" )
340- _builder. newLine()
341- _builder. append(indent)
342- _builder. append(" return " )
343- _builder. append(funcNames. get(i))
344- _builder. append(" ()" )
345- _builder. newLine()
346240 }
347-
348- // Default else
349- _builder. append(indent)
350- _builder. append(" else:" )
351- _builder. newLine()
241+
242+ val _builder= new StringConcatenation
243+ _builder. append(_thenFuncsBuilder. toString)
352244 _builder. append(indent)
353- _builder. append(" return " )
354- _builder. append(funcNames. last)
355- _builder. append(" ()" )
356-
245+ _builder. append(" switchAttribute = " )
246+ _builder. append(attr)
247+ _builder. newLine
248+ _builder. append(_switchLogicBuilder. toString)
249+
357250 return _builder. toString
358251 }
359252
@@ -434,6 +327,107 @@ class PythonExpressionGenerator {
434327 }
435328 }
436329
330+ def String generateTypeOrFunctionConditions (Data cls ) {
331+ var nConditions = 0 ;
332+ var result = ' ' ;
333+ for (Condition cond : cls. conditions) {
334+ result + = generateConditionBoilerPlate(cond, nConditions)
335+ if (cond. isConstraintCondition)
336+ result + = generateConstraintCondition(cls, cond)
337+ else
338+ result + = generateIfThenElseOrSwitch(cond)
339+ nConditions++
340+ }
341+ return result
342+ }
343+
344+ def generateFunctionConditions (List<Condition > conditions , String condition_type ) {
345+ var nConditions = 0 ;
346+ var result = ' ' ;
347+ for (Condition cond : conditions) {
348+ result + = generateFunctionConditionBoilerPlate(cond, nConditions, condition_type)
349+ result + = generateIfThenElseOrSwitch(cond)
350+ nConditions++
351+ }
352+
353+ return result
354+ }
355+
356+ def generateThenElseForFunction (RosettaExpression expr , List<Integer > ifLevel ) {
357+ ifCondBlocks. clear()
358+ generateExpression(expr, ifLevel. get(0 ), false )
359+ var blocks = " "
360+ if (! ifCondBlocks. isEmpty()) {
361+ ifLevel. set(0 , ifLevel. get(0 ) + 1 )
362+ blocks = ' ' ' «FOR arg : ifCondBlocks»«arg»«ENDFOR»' ' '
363+ }
364+ return ' ' ' «blocks»' ' '
365+ }
366+
367+ private def boolean isConstraintCondition (Condition cond ) {
368+ return isOneOf(cond) || isChoice(cond)
369+ }
370+
371+ private def boolean isOneOf (Condition cond ) {
372+ return cond. expression instanceof OneOfOperation
373+ }
374+
375+ private def boolean isChoice (Condition cond ) {
376+ return cond. expression instanceof ChoiceOperation
377+ }
378+
379+ private def generateConditionBoilerPlate (Condition cond , int nConditions ) {
380+ ' ' '
381+
382+ @rune_condition
383+ def condition_«nConditions»_«cond.name»(self):
384+ «IF cond.definition!==null»
385+ """
386+ «cond.definition»
387+ """
388+ «ENDIF»
389+ item = self
390+ ' ' '
391+ }
392+
393+ private def generateFunctionConditionBoilerPlate (Condition cond , int nConditions , String condition_type ) {
394+ ' ' '
395+
396+ @rune_local_condition(«condition_type»)
397+ def condition_«nConditions»_«cond.name»(self):
398+ «IF cond.definition!==null»
399+ """
400+ «cond.definition»
401+ """
402+ «ENDIF»
403+ ' ' '
404+ }
405+
406+ private def generateConstraintCondition (Data cls , Condition cond ) {
407+ val expression = cond. expression
408+ var attributes = cls. attributes
409+ var necessity = " necessity=True"
410+ if (expression instanceof ChoiceOperation ) {
411+ attributes = expression. attributes
412+ if (expression. necessity == Necessity . OPTIONAL ) {
413+ necessity = " necessity=False"
414+ }
415+ }
416+ ' ' ' return rune_check_one_of(self, «FOR a : attributes SEPARATOR ", "»' «a. name»' «ENDFOR», «necessity»)
417+ ' ' '
418+ }
419+
420+ private def generateIfThenElseOrSwitch (Condition c ) {
421+ ifCondBlocks. clear()
422+ isSwitchCond= false
423+
424+ var expr = generateExpression(c. expression, 0 , false )
425+ if (isSwitchCond) return expr
426+ var blocks = (ifCondBlocks. isEmpty()) ? " " : ' ' ' «FOR arg : ifCondBlocks»«arg»«ENDFOR»' ' '
427+ return ' ' ' «blocks» return «expr»
428+ ' ' '
429+ }
430+
437431 def addImportsFromConditions (String var iable , String namespace ) {
438432 val import = ' ' ' from «namespace».«variable» import «variable»' ' '
439433 if (importsFound !== null && ! importsFound. contains(import)) {
0 commit comments