Skip to content

Commit 4a35794

Browse files
cleanup ExpressionGenerator and revert back to DSL v9.28.2
1 parent 458aa4b commit 4a35794

File tree

3 files changed

+29
-41
lines changed

3 files changed

+29
-41
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,13 @@
156156
<repoServerHost>s01.oss.sonatype.org</repoServerHost>
157157
<stagingTimeoutInMinutes>10</stagingTimeoutInMinutes>
158158

159+
<!--
159160
<rosetta.dsl.version>9.47.0</rosetta.dsl.version>
160161
<rosetta.bundle.version>11.57.1</rosetta.bundle.version>
162+
-->
161163

162-
<!--
163164
<rosetta.dsl.version>9.28.2</rosetta.dsl.version>
164165
<rosetta.bundle.version>11.34.0</rosetta.bundle.version>
165-
-->
166166

167167
<xtext.version>2.27.0</xtext.version>
168168

src/main/java/com/regnosys/rosetta/generator/python/expressions/PythonExpressionGenerator.xtend

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class PythonExpressionGenerator {
7171
if (cond.isConstraintCondition)
7272
result += generateConstraintCondition(cls, cond)
7373
else
74-
result += generateExpressionCondition(cond)
74+
result += generateIfThenElseOrSwitch(cond)
7575
nConditions++
7676
}
7777
return result
@@ -82,7 +82,7 @@ class PythonExpressionGenerator {
8282
var result = '';
8383
for (Condition cond : conditions) {
8484
result += generateFunctionConditionBoilerPlate(cond, nConditions, condition_type)
85-
result += generateExpressionCondition(cond)
85+
result += generateIfThenElseOrSwitch(cond)
8686
nConditions++
8787
}
8888

@@ -196,23 +196,17 @@ class PythonExpressionGenerator {
196196
'''
197197
}
198198

199-
private def generateExpressionCondition(Condition c) {
199+
private def generateIfThenElseOrSwitch(Condition c) {
200200
ifCondBlocks.clear()
201-
switchCondBlocks = new ArrayList<String>()
201+
switchCondBlocks.clear()
202202
var expr = generateExpression(c.expression, 0, false)
203-
var blocks = ""
204-
var switch_blocks = ""
205-
if (!ifCondBlocks.isEmpty()) {
206-
blocks = ''' «FOR arg : ifCondBlocks»«arg»«ENDFOR»'''
207-
}
208203
if (!switchCondBlocks.isEmpty()) {
209-
switch_blocks = ''' «FOR arg : switchCondBlocks»«arg»«ENDFOR»'''
204+
var switchBlocks = ''' «FOR arg : switchCondBlocks»«arg»«ENDFOR»'''
205+
return '''«switchBlocks» «expr»'''
210206
}
211-
if (switch_blocks.equals(""))
212-
return '''«blocks» return «expr»
213-
'''
214-
else
215-
return '''«switch_blocks» «expr»'''
207+
var blocks = (ifCondBlocks.isEmpty()) ? "" : ''' «FOR arg : ifCondBlocks»«arg»«ENDFOR»'''
208+
return '''«blocks» return «expr»
209+
'''
216210
}
217211

218212
private def String generateConditionalExpression(RosettaConditionalExpression expr, int ifLevel, boolean isLambda) {
@@ -292,6 +286,7 @@ class PythonExpressionGenerator {
292286
}
293287

294288
private def String generateSwitchOperation(SwitchOperation expr, int ifLevel, boolean isLambda) {
289+
// translate switch into a series of if / elif statements
295290
val attr = generateExpression(expr.argument, 0, isLambda)
296291
val arg = expr.argument as RosettaSymbolReference
297292

src/main/java/com/regnosys/rosetta/generator/python/func/PythonFunctionGenerator.xtend

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ import com.regnosys.rosetta.rosetta.simple.Data
1212
import com.regnosys.rosetta.rosetta.simple.Function
1313
import com.regnosys.rosetta.rosetta.simple.Operation
1414
import com.regnosys.rosetta.rosetta.simple.Segment
15-
import com.regnosys.rosetta.rosetta.simple.ShortcutDeclaration
16-
import com.regnosys.rosetta.rosetta.simple.Condition
17-
import com.regnosys.rosetta.rosetta.RosettaFeature
18-
import com.regnosys.rosetta.rosetta.RosettaTyped
1915
import java.util.ArrayList
2016
import java.util.Collections
2117
import java.util.HashMap
@@ -287,7 +283,7 @@ class PythonFunctionGenerator {
287283
private def generateAddOperation(AssignPathRoot root, Operation operation, Function function, String expression) {
288284
val lineSeparator = System.getProperty("line.separator")
289285
val attribute = root as Attribute
290-
val fullPath = generateFullPath(operation.getPath().getReversedFeatures(), root.name)
286+
val fullPath = generateFullPath(operation.getPath().getReversedAttributes, root.name)
291287

292288
var result = ""
293289
if (attribute.typeCall.type instanceof RosettaEnumeration) {
@@ -328,7 +324,7 @@ class PythonFunctionGenerator {
328324
var result = new StringBuilder()
329325
result.append("'")
330326
while (currentPath !== null) {
331-
result.append(currentPath.getFeature().name)
327+
result.append(currentPath.getAttribute().name)
332328
if (currentPath.next !== null) {
333329
result.append("->")
334330
}
@@ -340,8 +336,8 @@ class PythonFunctionGenerator {
340336

341337
private def getNextPathElementName(Segment path) {
342338
if (path !== null) {
343-
val feature = path.getFeature()
344-
return "'" + feature.name + "'"
339+
val attribute = path.getAttribute()
340+
return "'" + attribute.name + "'"
345341
}
346342
return null
347343
}
@@ -351,39 +347,36 @@ class PythonFunctionGenerator {
351347
return expression;
352348
}
353349

354-
val feature = path.getFeature();
355-
if (feature instanceof RosettaTyped) {
356-
return '''_get_rosetta_object('«feature.typeCall.type.name»', «getNextPathElementName(path.next)», «buildObject(expression, path.next)»)'''
357-
}
358-
throw new IllegalArgumentException("Cannot build object for feature " + feature.getName() + " of type " + feature.class.simpleName)
350+
val attribute = path.getAttribute();
351+
return '''_get_rune_object('«attribute.typeCall.type.name»', «getNextPathElementName(path.next)», «buildObject(expression, path.next)»)'''
359352
}
360353

361-
private def String generateFullPath(Iterable<RosettaFeature> features, String root) {
362-
if (features.isEmpty) {
354+
private def String generateFullPath(Iterable<Attribute> attrs, String root) {
355+
if (attrs.isEmpty) {
363356
return "self"
364357
}
365358

366-
val attr = features.head
367-
val remainingAttrs = features.tail.toList
359+
val attr = attrs.head
360+
val remainingAttrs = attrs.tail.toList
368361

369-
val nextPath = if (remainingAttrs.isEmpty) '''rosetta_resolve_attr(self, «root»)''' else generateFullPath(
362+
val nextPath = if (remainingAttrs.isEmpty) '''rune_resolve_attr(self, «root»)''' else generateFullPath(
370363
remainingAttrs, root)
371364

372-
return '''rosetta_resolve_attr(«nextPath», '«attr.name»')'''
365+
return '''rune_resolve_attr(«nextPath», '«attr.name»')'''
373366
}
374367

375-
private def getReversedFeatures(Segment segment) {
376-
val features = new ArrayList<RosettaFeature>();
368+
private def getReversedAttributes(Segment segment) {
369+
val attributes = new ArrayList<Attribute>();
377370
var current = segment;
378371

379372
while (current !== null) {
380-
features.add(current.getFeature());
373+
attributes.add(current.getAttribute());
381374
current = current.getNext();
382375
}
383376

384-
Collections.reverse(features);
377+
Collections.reverse(attributes);
385378

386-
return features;
379+
return attributes;
387380
}
388381

389382
def addImportsFromConditions(String variable, String namespace) {

0 commit comments

Comments
 (0)