@@ -64,43 +64,6 @@ class PythonExpressionGenerator {
6464 public var ifCondBlocks = new ArrayList<String > ()
6565 public var isSwitchCond = false
6666
67- def String generateConditions (Data cls ) {
68- var nConditions = 0 ;
69- var result = ' ' ;
70- for (Condition cond : cls. conditions) {
71- result + = generateConditionBoilerPlate(cond, nConditions)
72- if (cond. isConstraintCondition)
73- result + = generateConstraintCondition(cls, cond)
74- else
75- result + = generateIfThenElseOrSwitch(cond)
76- nConditions++
77- }
78- return result
79- }
80-
81- def generateFunctionConditions (List<Condition > conditions , String condition_type ) {
82- var nConditions = 0 ;
83- var result = ' ' ;
84- for (Condition cond : conditions) {
85- result + = generateFunctionConditionBoilerPlate(cond, nConditions, condition_type)
86- result + = generateIfThenElseOrSwitch(cond)
87- nConditions++
88- }
89-
90- return result
91- }
92-
93- def generateExpressionThenElse (RosettaExpression expr , List<Integer > ifLevel ) {
94- ifCondBlocks. clear()
95- generateExpression(expr, ifLevel. get(0 ), false )
96- var blocks = " "
97- if (! ifCondBlocks. isEmpty()) {
98- ifLevel. set(0 , ifLevel. get(0 ) + 1 )
99- blocks = ' ' ' «FOR arg : ifCondBlocks»«arg»«ENDFOR»' ' '
100- }
101- return ' ' ' «blocks»' ' '
102- }
103-
10467 def String generateExpression (RosettaExpression expr , int ifLevel , boolean isLambda ) {
10568 switch (expr) {
10669 // literals
@@ -144,70 +107,6 @@ class PythonExpressionGenerator {
144107 }
145108 }
146109
147- private def boolean isConstraintCondition (Condition cond ) {
148- return isOneOf(cond) || isChoice(cond)
149- }
150-
151- private def boolean isOneOf (Condition cond ) {
152- return cond. expression instanceof OneOfOperation
153- }
154-
155- private def boolean isChoice (Condition cond ) {
156- return cond. expression instanceof ChoiceOperation
157- }
158-
159- private def generateConditionBoilerPlate (Condition cond , int nConditions ) {
160- ' ' '
161-
162- @rune_condition
163- def condition_«nConditions»_«cond.name»(self):
164- «IF cond.definition!==null»
165- """
166- «cond.definition»
167- """
168- «ENDIF»
169- item = self
170- ' ' '
171- }
172-
173- private def generateFunctionConditionBoilerPlate (Condition cond , int nConditions , String condition_type ) {
174- ' ' '
175-
176- @rune_local_condition(«condition_type»)
177- def condition_«nConditions»_«cond.name»(self):
178- «IF cond.definition!==null»
179- """
180- «cond.definition»
181- """
182- «ENDIF»
183- ' ' '
184- }
185-
186- private def generateConstraintCondition (Data cls , Condition cond ) {
187- val expression = cond. expression
188- var attributes = cls. attributes
189- var necessity = " necessity=True"
190- if (expression instanceof ChoiceOperation ) {
191- attributes = expression. attributes
192- if (expression. necessity == Necessity . OPTIONAL ) {
193- necessity = " necessity=False"
194- }
195- }
196- ' ' ' return rune_check_one_of(self, «FOR a : attributes SEPARATOR ", "»' «a. name»' «ENDFOR», «necessity»)
197- ' ' '
198- }
199-
200- private def generateIfThenElseOrSwitch (Condition c ) {
201- ifCondBlocks. clear()
202- isSwitchCond= false
203-
204- var expr = generateExpression(c. expression, 0 , false )
205- if (isSwitchCond) return expr
206- var blocks = (ifCondBlocks. isEmpty()) ? " " : ' ' ' «FOR arg : ifCondBlocks»«arg»«ENDFOR»' ' '
207- return ' ' ' «blocks» return «expr»
208- ' ' '
209- }
210-
211110 private def String generateConditionalExpression (RosettaConditionalExpression expr , int ifLevel , boolean isLambda ) {
212111 val ifExpr = generateExpression(expr. getIf(), ifLevel + 1 , isLambda)
213112 val ifThen = generateExpression(expr. ifthen, ifLevel + 1 , isLambda)
@@ -427,6 +326,107 @@ class PythonExpressionGenerator {
427326 }
428327 }
429328
329+ def String generateTypeOrFunctionConditions (Data cls ) {
330+ var nConditions = 0 ;
331+ var result = ' ' ;
332+ for (Condition cond : cls. conditions) {
333+ result + = generateConditionBoilerPlate(cond, nConditions)
334+ if (cond. isConstraintCondition)
335+ result + = generateConstraintCondition(cls, cond)
336+ else
337+ result + = generateIfThenElseOrSwitch(cond)
338+ nConditions++
339+ }
340+ return result
341+ }
342+
343+ def generateFunctionConditions (List<Condition > conditions , String condition_type ) {
344+ var nConditions = 0 ;
345+ var result = ' ' ;
346+ for (Condition cond : conditions) {
347+ result + = generateFunctionConditionBoilerPlate(cond, nConditions, condition_type)
348+ result + = generateIfThenElseOrSwitch(cond)
349+ nConditions++
350+ }
351+
352+ return result
353+ }
354+
355+ def generateThenElseForFunction (RosettaExpression expr , List<Integer > ifLevel ) {
356+ ifCondBlocks. clear()
357+ generateExpression(expr, ifLevel. get(0 ), false )
358+ var blocks = " "
359+ if (! ifCondBlocks. isEmpty()) {
360+ ifLevel. set(0 , ifLevel. get(0 ) + 1 )
361+ blocks = ' ' ' «FOR arg : ifCondBlocks»«arg»«ENDFOR»' ' '
362+ }
363+ return ' ' ' «blocks»' ' '
364+ }
365+
366+ private def boolean isConstraintCondition (Condition cond ) {
367+ return isOneOf(cond) || isChoice(cond)
368+ }
369+
370+ private def boolean isOneOf (Condition cond ) {
371+ return cond. expression instanceof OneOfOperation
372+ }
373+
374+ private def boolean isChoice (Condition cond ) {
375+ return cond. expression instanceof ChoiceOperation
376+ }
377+
378+ private def generateConditionBoilerPlate (Condition cond , int nConditions ) {
379+ ' ' '
380+
381+ @rune_condition
382+ def condition_«nConditions»_«cond.name»(self):
383+ «IF cond.definition!==null»
384+ """
385+ «cond.definition»
386+ """
387+ «ENDIF»
388+ item = self
389+ ' ' '
390+ }
391+
392+ private def generateFunctionConditionBoilerPlate (Condition cond , int nConditions , String condition_type ) {
393+ ' ' '
394+
395+ @rune_local_condition(«condition_type»)
396+ def condition_«nConditions»_«cond.name»(self):
397+ «IF cond.definition!==null»
398+ """
399+ «cond.definition»
400+ """
401+ «ENDIF»
402+ ' ' '
403+ }
404+
405+ private def generateConstraintCondition (Data cls , Condition cond ) {
406+ val expression = cond. expression
407+ var attributes = cls. attributes
408+ var necessity = " necessity=True"
409+ if (expression instanceof ChoiceOperation ) {
410+ attributes = expression. attributes
411+ if (expression. necessity == Necessity . OPTIONAL ) {
412+ necessity = " necessity=False"
413+ }
414+ }
415+ ' ' ' return rune_check_one_of(self, «FOR a : attributes SEPARATOR ", "»' «a. name»' «ENDFOR», «necessity»)
416+ ' ' '
417+ }
418+
419+ private def generateIfThenElseOrSwitch (Condition c ) {
420+ ifCondBlocks. clear()
421+ isSwitchCond= false
422+
423+ var expr = generateExpression(c. expression, 0 , false )
424+ if (isSwitchCond) return expr
425+ var blocks = (ifCondBlocks. isEmpty()) ? " " : ' ' ' «FOR arg : ifCondBlocks»«arg»«ENDFOR»' ' '
426+ return ' ' ' «blocks» return «expr»
427+ ' ' '
428+ }
429+
430430 def addImportsFromConditions (String var iable , String namespace ) {
431431 val import = ' ' ' from «namespace».«variable» import «variable»' ' '
432432 if (importsFound !== null && ! importsFound. contains(import)) {
0 commit comments