Search in sources :

Example 1 with IntegerCoreInstance

use of org.finos.legend.pure.m4.coreinstance.primitive.IntegerCoreInstance in project legend-pure by finos.

the class ModelRepository method newIntegerCoreInstance.

private IntegerCoreInstance newIntegerCoreInstance(int value, CoreInstance classifier, int internalSyntheticId) {
    if ((this.integerCacheMin <= value) && (value <= this.integerCacheMax)) {
        int index = value - this.integerCacheMin;
        IntegerCoreInstance instance = this.integerCache.get(index);
        if (instance == null) {
            instance = PrimitiveCoreInstance.newIntegerCoreInstance(value, classifier, internalSyntheticId);
            if (!this.integerCache.compareAndSet(index, null, instance)) {
                instance = this.integerCache.get(index);
            }
        }
        return instance;
    }
    return PrimitiveCoreInstance.newIntegerCoreInstance(value, classifier, internalSyntheticId);
}
Also used : IntegerCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.IntegerCoreInstance)

Example 2 with IntegerCoreInstance

use of org.finos.legend.pure.m4.coreinstance.primitive.IntegerCoreInstance in project legend-pure by finos.

the class RoundWithScale method execute.

@Override
public CoreInstance execute(ListIterable<? extends CoreInstance> params, Stack<MutableMap<String, CoreInstance>> resolvedTypeParameters, Stack<MutableMap<String, CoreInstance>> resolvedMultiplicityParameters, VariableContext variableContext, CoreInstance functionExpressionToUseInStack, Profiler profiler, InstantiationContext instantiationContext, ExecutionSupport executionSupport, Context context, ProcessorSupport processorSupport) throws PureExecutionException {
    CoreInstance number = Instance.getValueForMetaPropertyToOneResolved(params.get(0), M3Properties.values, processorSupport);
    CoreInstance scale = Instance.getValueForMetaPropertyToOneResolved(params.get(1), M3Properties.values, processorSupport);
    BigDecimal decimal = number instanceof DecimalCoreInstance ? ((DecimalCoreInstance) number).getValue() : ((FloatCoreInstance) number).getValue();
    BigDecimal result = decimal.setScale(((IntegerCoreInstance) scale).getValue().intValue(), RoundingMode.HALF_UP);
    return NumericUtilities.toPureNumberValueExpression(result, number instanceof DecimalCoreInstance, this.repository, processorSupport);
}
Also used : DecimalCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.DecimalCoreInstance) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) FloatCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.FloatCoreInstance) IntegerCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.IntegerCoreInstance) IntegerCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.IntegerCoreInstance) DecimalCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.DecimalCoreInstance) BigDecimal(java.math.BigDecimal)

Example 3 with IntegerCoreInstance

use of org.finos.legend.pure.m4.coreinstance.primitive.IntegerCoreInstance in project legend-pure by finos.

the class TestEnumerationMapping method testValidHybridEnumToEnumMapping.

@Test
public void testValidHybridEnumToEnumMapping() {
    this.runtime.createInMemorySource("model.pure", "###Pure\n" + "\n" + "Enum my::SourceEnum1\n" + "{\n" + "   A, B, C\n" + "}\n" + "\n" + "Enum my::SourceEnum2\n" + "{\n" + "   P, Q, R\n" + "}\n" + "\n" + "Enum my::TargetEnum\n" + "{\n" + "   U, V, W, X, Y, Z\n" + "}\n" + "\n" + "###Mapping\n" + "import my::*;\n" + "\n" + "Mapping my::TestMapping\n" + "(\n" + "   TargetEnum : EnumerationMapping enumsATargetEnum\n" + "   {\n" + "      U : my::SourceEnum1.A,\n" + "      V : my::SourceEnum1.A,\n" + "      W : [my::SourceEnum1.A, my::SourceEnum1.B],\n" + "      X : [my::SourceEnum1.A, my::SourceEnum1.B, my::SourceEnum1.C],\n" + "      Y : [my::SourceEnum1.A, my::SourceEnum1.B, my::SourceEnum1.C],\n" + "      Z : my::SourceEnum1.C\n" + "   }\n" + "   TargetEnum : EnumerationMapping enumsBTargetEnum\n" + "   {\n" + "      U : my::SourceEnum2.P,\n" + "      V : my::SourceEnum2.P\n," + "      W : [my::SourceEnum2.P, my::SourceEnum2. Q,my::SourceEnum2.R]\n" + "   }\n" + "   TargetEnum : EnumerationMapping integersTargetEnum2\n" + "   {\n" + "      X : [4,5,6],\n" + "      Y : 3\n" + "   }\n" + "   TargetEnum : EnumerationMapping stringsTargetEnum2\n" + "   {\n" + "      Y : ['One','Two','Three'],\n" + "      Z : 'A'\n" + "   }\n" + ")");
    this.runtime.compile();
    org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.Mapping mapping = (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.Mapping) this.runtime.getCoreInstance("my::TestMapping");
    EnumerationMapping enumerationMapping = mapping._enumerationMappings().detect(detectByEnumerationMappingName("enumsATargetEnum"));
    Assert.assertEquals("my::TargetEnum", PackageableElement.getUserPathForPackageableElement(enumerationMapping._enumeration()));
    ImmutableList<EnumValueMapping> enumValueMappings = (ImmutableList<EnumValueMapping>) enumerationMapping._enumValueMappings();
    Assert.assertEquals(6, enumValueMappings.size());
    EnumValueMapping enumValueMapping1 = enumValueMappings.get(0);
    Assert.assertEquals("U", enumValueMapping1._enum()._name());
    ImmutableList<CoreInstance> sourceValuesCoreInstances1 = (ImmutableList<CoreInstance>) enumValueMapping1._sourceValuesCoreInstance();
    Assert.assertEquals(1, sourceValuesCoreInstances1.size());
    Assert.assertTrue(sourceValuesCoreInstances1.get(0) instanceof EnumStub);
    Assert.assertEquals("my::SourceEnum1", PackageableElement.getUserPathForPackageableElement(((EnumStub) sourceValuesCoreInstances1.get(0))._enumeration()));
    ImmutableList<CoreInstance> sourceValues1 = (ImmutableList<CoreInstance>) enumValueMapping1._sourceValues();
    Assert.assertEquals(1, sourceValues1.size());
    Assert.assertTrue(sourceValues1.get(0) instanceof Enum);
    Assert.assertEquals("A", ((Enum) sourceValues1.get(0))._name());
    enumValueMappings = (ImmutableList<EnumValueMapping>) mapping._enumerationMappings().detect(detectByEnumerationMappingName("enumsBTargetEnum"))._enumValueMappings();
    EnumValueMapping enumValueMapping2 = enumValueMappings.get(1);
    Assert.assertEquals("V", enumValueMapping2._enum()._name());
    ImmutableList<CoreInstance> sourceValuesCoreInstances2 = (ImmutableList<CoreInstance>) enumValueMapping2._sourceValuesCoreInstance();
    Assert.assertEquals(1, sourceValuesCoreInstances2.size());
    Assert.assertTrue(sourceValuesCoreInstances2.get(0) instanceof EnumStub);
    Assert.assertEquals("my::SourceEnum2", PackageableElement.getUserPathForPackageableElement(((EnumStub) sourceValuesCoreInstances2.get(0))._enumeration()));
    ImmutableList<CoreInstance> sourceValues2 = (ImmutableList<CoreInstance>) enumValueMapping2._sourceValues();
    Assert.assertEquals(1, sourceValues2.size());
    Assert.assertTrue(sourceValues2.get(0) instanceof Enum);
    Assert.assertEquals("P", ((Enum) sourceValues2.get(0))._name());
    EnumValueMapping enumValueMapping3 = enumValueMappings.get(2);
    Assert.assertEquals("W", enumValueMapping3._enum()._name());
    ImmutableList<CoreInstance> sourceValuesCoreInstances3 = (ImmutableList<CoreInstance>) enumValueMapping3._sourceValuesCoreInstance();
    Assert.assertEquals(3, sourceValuesCoreInstances3.size());
    Assert.assertTrue(sourceValuesCoreInstances3.get(0) instanceof EnumStub);
    Assert.assertEquals("my::SourceEnum2", PackageableElement.getUserPathForPackageableElement(((EnumStub) sourceValuesCoreInstances3.get(0))._enumeration()));
    Assert.assertTrue(sourceValuesCoreInstances3.get(1) instanceof EnumStub);
    Assert.assertEquals("my::SourceEnum2", PackageableElement.getUserPathForPackageableElement(((EnumStub) sourceValuesCoreInstances3.get(1))._enumeration()));
    ImmutableList<CoreInstance> sourceValues3 = (ImmutableList<CoreInstance>) enumValueMapping3._sourceValues();
    Assert.assertEquals(3, sourceValues3.size());
    Assert.assertTrue(sourceValues3.get(0) instanceof Enum);
    Assert.assertEquals("P", ((Enum) sourceValues3.get(0))._name());
    Assert.assertTrue(sourceValues3.get(1) instanceof Enum);
    Assert.assertEquals("Q", ((Enum) sourceValues3.get(1))._name());
    Assert.assertTrue(sourceValues3.get(2) instanceof Enum);
    Assert.assertEquals("R", ((Enum) sourceValues3.get(2))._name());
    enumValueMappings = (ImmutableList<EnumValueMapping>) mapping._enumerationMappings().detect(detectByEnumerationMappingName("integersTargetEnum2"))._enumValueMappings();
    enumValueMapping1 = enumValueMappings.get(0);
    Assert.assertEquals("X", enumValueMapping1._enum()._name());
    ImmutableList<CoreInstance> sourceValuesCoreInstances4 = (ImmutableList<CoreInstance>) enumValueMapping1._sourceValuesCoreInstance();
    Assert.assertEquals(3, sourceValuesCoreInstances4.size());
    Assert.assertTrue(sourceValuesCoreInstances4.get(0) instanceof IntegerCoreInstance);
    MutableList<? extends Object> sourceValues4 = enumValueMapping1._sourceValues().toList();
    Assert.assertEquals(3, sourceValues4.size());
    Assert.assertEquals(4L, sourceValues4.get(0));
    Assert.assertEquals(5L, sourceValues4.get(1));
    Assert.assertEquals(6L, sourceValues4.get(2));
    enumValueMapping2 = enumValueMappings.get(1);
    Assert.assertEquals("Y", enumValueMapping2._enum()._name());
    ImmutableList<CoreInstance> sourceValuesCoreInstances5 = (ImmutableList<CoreInstance>) enumValueMapping2._sourceValuesCoreInstance();
    Assert.assertEquals(1, sourceValuesCoreInstances5.size());
    Assert.assertTrue(sourceValuesCoreInstances4.get(0) instanceof IntegerCoreInstance);
    MutableList<? extends Object> sourceValues5 = enumValueMapping2._sourceValues().toList();
    Assert.assertEquals(1, sourceValues5.size());
    Assert.assertEquals(3L, sourceValues5.get(0));
    enumValueMappings = (ImmutableList<EnumValueMapping>) mapping._enumerationMappings().detect(detectByEnumerationMappingName("stringsTargetEnum2"))._enumValueMappings();
    enumValueMapping1 = enumValueMappings.get(0);
    Assert.assertEquals("Y", enumValueMapping1._enum()._name());
    ImmutableList<CoreInstance> sourceValuesCoreInstances6 = (ImmutableList<CoreInstance>) enumValueMapping1._sourceValuesCoreInstance();
    Assert.assertEquals(3, sourceValuesCoreInstances6.size());
    Assert.assertTrue(sourceValuesCoreInstances4.get(0) instanceof IntegerCoreInstance);
    MutableList<? extends Object> sourceValues6 = enumValueMapping1._sourceValues().toList();
    Assert.assertEquals(3, sourceValues6.size());
    Assert.assertEquals("One", sourceValues6.get(0));
    Assert.assertEquals("Two", sourceValues6.get(1));
    Assert.assertEquals("Three", sourceValues6.get(2));
    enumValueMapping2 = enumValueMappings.get(1);
    Assert.assertEquals("Z", enumValueMapping2._enum()._name());
    ImmutableList<CoreInstance> sourceValuesCoreInstances7 = (ImmutableList<CoreInstance>) enumValueMapping2._sourceValuesCoreInstance();
    Assert.assertEquals(1, sourceValuesCoreInstances7.size());
    Assert.assertTrue(sourceValuesCoreInstances4.get(0) instanceof IntegerCoreInstance);
    MutableList<? extends Object> sourceValues7 = enumValueMapping2._sourceValues().toList();
    Assert.assertEquals(1, sourceValues7.size());
    Assert.assertEquals("A", sourceValues7.get(0));
}
Also used : Enum(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Enum) EnumStub(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.EnumStub) EnumerationMapping(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumerationMapping) ImmutableList(org.eclipse.collections.api.list.ImmutableList) EnumValueMapping(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumValueMapping) EnumerationMapping(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumerationMapping) IntegerCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.IntegerCoreInstance) EnumValueMapping(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumValueMapping) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) IntegerCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.IntegerCoreInstance) Test(org.junit.Test)

Aggregations

IntegerCoreInstance (org.finos.legend.pure.m4.coreinstance.primitive.IntegerCoreInstance)3 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)2 BigDecimal (java.math.BigDecimal)1 ImmutableList (org.eclipse.collections.api.list.ImmutableList)1 EnumValueMapping (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumValueMapping)1 EnumerationMapping (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumerationMapping)1 EnumStub (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.EnumStub)1 Enum (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Enum)1 DecimalCoreInstance (org.finos.legend.pure.m4.coreinstance.primitive.DecimalCoreInstance)1 FloatCoreInstance (org.finos.legend.pure.m4.coreinstance.primitive.FloatCoreInstance)1 Test (org.junit.Test)1