Skip to content

Commit

Permalink
Consolidate Code Generators for Functions (#62)
Browse files Browse the repository at this point in the history
* Allow alias in assignment #43
* Move calculation impl to the doEvaluate method #43
* Additiona FuncGenerator fixes. Adjusted test expectation #43
* Added type check for assign-output
* Added Reference type support #43
* Allow usage of output in assign expression #43
* Added cardinality and name validation. Better type inference for if-else
* Allow only-element for callable call
* assignment of an alias #53
* Validate list element access #65
  • Loading branch information
dhuebner authored and jim-h-wang committed Sep 23, 2019
1 parent 3ac4370 commit 52154e0
Show file tree
Hide file tree
Showing 41 changed files with 1,584 additions and 3,605 deletions.
6 changes: 6 additions & 0 deletions com.regnosys.rosetta.lib/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="model/basictypes.rosetta" kind="src" output="target/classes" path="xtend-gen">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="model/basictypes.rosetta" kind="src" output="target/classes" path="src/main/xtend-gen">
<attributes>
<attribute name="optional" value="true"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import java.math.BigDecimal;
import java.math.MathContext;

import com.rosetta.model.lib.functions.Mapper;
import com.rosetta.model.lib.functions.MapperS;

public class BigDecimalExtensions {

/**
Expand Down Expand Up @@ -39,4 +42,81 @@ public static BigDecimal divide(BigDecimal a, BigDecimal b) {
public static boolean closeTo(BigDecimal a, BigDecimal b, BigDecimal error) {
return a.subtract(b).abs().compareTo(error) < 0;
}
/**
*
* @see BigDecimal.valueOf(double);
*/
public static BigDecimal valueOf(double a) {
return BigDecimal.valueOf(a);
}
/**
*
* @see BigDecimal.valueOf(long);
*/
public static BigDecimal valueOf(long a) {
return BigDecimal.valueOf(a);
}
/**
*
* @see BigDecimal.valueOf(long, int);
*/
public static BigDecimal valueOf(long a, int scale) {
return BigDecimal.valueOf(a,scale);
}

/// for Mappers
/**
* Add a and b
*/
public static Mapper<BigDecimal> add(Mapper<BigDecimal> a, Mapper<BigDecimal> b) {
return MapperS.of(add(a.get(),b.get()));
}

/**
* Subtract b from a
*/
public static Mapper<BigDecimal> subtract(Mapper<BigDecimal> a, Mapper<BigDecimal> b) {
return MapperS.of(subtract(a.get(),b.get()));
}

/**
* Multiply a and b
*/
public static Mapper<BigDecimal> multiply(Mapper<BigDecimal> a, Mapper<BigDecimal> b) {
return MapperS.of(multiply(a.get(),b.get()));
}

/**
* Divide a by b
*/
public static Mapper<BigDecimal> divide(Mapper<BigDecimal> a, Mapper<BigDecimal> b) {
return MapperS.of(divide(a.get(),b.get()));
}

/**
* Is a close to b, with given error
*/
public static boolean closeTo(Mapper<BigDecimal> a, Mapper<BigDecimal> b, Mapper<BigDecimal> error) {
return closeTo(a.get(), b.get(), error.get());
}

/**
*
* @see BigDecimal.valueOf(long);
*/
public static Mapper<BigDecimal> valueOf(Mapper<Number> a) {
Number number = a.get();
if (number instanceof BigDecimal)
return MapperS.of(valueOf(((BigDecimal) number).doubleValue()));
return MapperS.of(valueOf(number.longValue()));
}

/**
*
* @see BigDecimal.valueOf(long, int);
*/
public static Mapper<BigDecimal> valueOf(Mapper<Integer> a, int scale) {
return MapperS.of(valueOf(a.get(), scale));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,31 @@ public static <T> Mapper<T> doIf(Mapper<Boolean> test, Mapper<T> ifthen, Mapper<
if (testResult) return ifthen;
else return elsethen;
}
public static <T> Mapper<T> doIf(Mapper<Boolean> test, Mapper<T> ifthen) {
boolean testResult = test.getMulti().stream().allMatch(b->b.booleanValue());
if (testResult) return ifthen;
else return null;
}

public static ComparisonResult doIf(ComparisonResult test, ComparisonResult ifthen, ComparisonResult elsethen) {
if (test.get()) return ifthen;
else return elsethen;
}
public static ComparisonResult doIf(ComparisonResult test, ComparisonResult ifthen) {
if (test.get()) return ifthen;
else return ComparisonResult.success();
}

public static <T> Mapper<T> doIf(ComparisonResult test, Mapper<T> ifthen, Mapper<T> elsethen) {
if (test.get()) return ifthen;
else return elsethen;
}

public static <T> Mapper<T> doIf(ComparisonResult test, Mapper<T> ifthen) {
if (test.get()) return ifthen;
else return null;
}

public static <T> ComparisonResult doWhenPresent(Mapper<T> whenPresent, ComparisonResult compare) {
if(exists(whenPresent, false).get())
return compare;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.regnosys.rosetta.generator.java.calculation

import com.google.inject.Inject
import com.regnosys.rosetta.generator.java.function.FuncGenerator
import com.regnosys.rosetta.generator.java.util.JavaNames
import com.regnosys.rosetta.rosetta.RosettaModel
import com.regnosys.rosetta.rosetta.simple.Function
import com.regnosys.rosetta.tests.util.ModelHelper
import java.util.function.Consumer
import org.eclipse.xtext.xbase.testing.RegisteringFileSystemAccess
Expand All @@ -11,20 +13,20 @@ import static org.junit.jupiter.api.Assertions.*

class CalculationGeneratorHelper {

@Inject CalculationGenerator generator
@Inject FuncGenerator generator
@Inject extension ModelHelper
@Inject RegisteringFileSystemAccess fsa
@Inject JavaNames.Factory factory

def void assertToGeneratedFunction(CharSequence actualModel, CharSequence expected) throws AssertionError {
actualModel.assertToGenerated(expected, [
generator.generateFunctions(fsa, it.elements, factory.create(it), "test")
generator.generate(factory.create(it), fsa, it.elements.filter(Function).filter[operations.nullOrEmpty].head, "test")
])
}

def void assertToGeneratedCalculation(CharSequence actualModel, CharSequence expected) throws AssertionError {
actualModel.assertToGenerated(expected, [
generator.generateCalculation(fsa, it.elements, factory.create(it), "test")
generator.generate(factory.create(it), fsa, it.elements.filter(Function).filter[!operations.nullOrEmpty].head, "test")
])
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.regnosys.rosetta.generator.java.calculation

import com.google.inject.Inject
import com.regnosys.rosetta.generator.java.expression.RosettaToJavaExtensions
import com.regnosys.rosetta.generator.java.util.ImportingStringConcatination
import com.regnosys.rosetta.generator.java.util.JavaNames
import com.regnosys.rosetta.rosetta.RosettaArgumentFeature
Expand Down
Loading

0 comments on commit 52154e0

Please sign in to comment.