use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumValueMapping in project legend-pure by finos.
the class TestEnumerationMapping method testValidComplexEnumToEnumMapping.
@Test
public void testValidComplexEnumToEnumMapping() {
this.runtime.createInMemorySource("model.pure", "###Pure\n" + "\n" + "Enum my::SourceEnum\n" + "{\n" + " A, B, C\n" + "}\n" + "\n" + "Enum my::TargetEnum\n" + "{\n" + " X, Y\n" + "}\n" + "\n" + "###Mapping\n" + "import my::*;\n" + "\n" + "Mapping my::TestMapping\n" + "(\n" + " TargetEnum : EnumerationMapping\n" + " {\n" + " X : SourceEnum.A,\n" + " Y : [SourceEnum.B, my::SourceEnum.C]\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().getFirst();
Assert.assertEquals("my::TargetEnum", PackageableElement.getUserPathForPackageableElement(enumerationMapping._enumeration()));
ImmutableList<EnumValueMapping> enumValueMappings = (ImmutableList<EnumValueMapping>) enumerationMapping._enumValueMappings();
Assert.assertEquals(2, enumValueMappings.size());
EnumValueMapping enumValueMapping1 = enumValueMappings.get(0);
Assert.assertEquals("X", 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::SourceEnum", 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());
EnumValueMapping enumValueMapping2 = enumValueMappings.get(1);
Assert.assertEquals("Y", enumValueMapping2._enum()._name());
ImmutableList<CoreInstance> sourceValuesCoreInstances2 = (ImmutableList<CoreInstance>) enumValueMapping2._sourceValuesCoreInstance();
Assert.assertEquals(2, sourceValuesCoreInstances2.size());
Assert.assertTrue(sourceValuesCoreInstances2.get(0) instanceof EnumStub);
Assert.assertEquals("my::SourceEnum", PackageableElement.getUserPathForPackageableElement(((EnumStub) sourceValuesCoreInstances2.get(0))._enumeration()));
Assert.assertTrue(sourceValuesCoreInstances2.get(1) instanceof EnumStub);
Assert.assertEquals("my::SourceEnum", PackageableElement.getUserPathForPackageableElement(((EnumStub) sourceValuesCoreInstances2.get(1))._enumeration()));
ImmutableList<CoreInstance> sourceValues2 = (ImmutableList<CoreInstance>) enumValueMapping2._sourceValues();
Assert.assertEquals(2, sourceValues2.size());
Assert.assertTrue(sourceValues2.get(0) instanceof Enum);
Assert.assertEquals("B", ((Enum) sourceValues2.get(0))._name());
Assert.assertTrue(sourceValues2.get(1) instanceof Enum);
Assert.assertEquals("C", ((Enum) sourceValues2.get(1))._name());
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumValueMapping in project legend-pure by finos.
the class TestEnumerationMapping method testValidSimpleEnumToEnumMapping.
@Test
public void testValidSimpleEnumToEnumMapping() {
this.runtime.createInMemorySource("model.pure", "###Pure\n" + "\n" + "Enum my::SourceEnum\n" + "{\n" + " A, B\n" + "}\n" + "\n" + "Enum my::TargetEnum\n" + "{\n" + " X, Y\n" + "}\n" + "\n" + "###Mapping\n" + "import my::*;\n" + "\n" + "Mapping my::TestMapping\n" + "(\n" + " TargetEnum : EnumerationMapping\n" + " {\n" + " X : SourceEnum.A,\n" + " Y : my::SourceEnum.B\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().getFirst();
Assert.assertEquals("my::TargetEnum", PackageableElement.getUserPathForPackageableElement(enumerationMapping._enumeration()));
ImmutableList<EnumValueMapping> enumValueMappings = (ImmutableList<EnumValueMapping>) enumerationMapping._enumValueMappings();
Assert.assertEquals(2, enumValueMappings.size());
EnumValueMapping enumValueMapping1 = enumValueMappings.get(0);
Assert.assertEquals("X", 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::SourceEnum", 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());
EnumValueMapping enumValueMapping2 = enumValueMappings.get(1);
Assert.assertEquals("Y", 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::SourceEnum", 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("B", ((Enum) sourceValues2.get(0))._name());
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumValueMapping in project legend-pure by finos.
the class EnumerationMappingUnbind method run.
@Override
public void run(EnumerationMapping enumerationMapping, MatcherState state, Matcher matcher, ModelRepository modelRepository, Context context) throws PureCompilationException {
ProcessorSupport processorSupport = state.getProcessorSupport();
MutableList<? extends CoreInstance> enumerations = Lists.mutable.with(enumerationMapping._enumerationCoreInstance()).withAll(EnumerationMappingProcessor.collectSourceEnumerationCoreInstances(enumerationMapping));
Shared.cleanUpReferenceUsages(enumerations, enumerationMapping, processorSupport);
Shared.cleanImportStubs(enumerations, processorSupport);
for (EnumValueMapping enumValueMapping : (RichIterable<? extends EnumValueMapping>) enumerationMapping._enumValueMappings()) {
Shared.cleanEnumStubs(Lists.mutable.with(enumValueMapping._enumCoreInstance()).withAll(EnumerationMappingProcessor.collectSourceEnumCoreInstances(enumValueMapping)), processorSupport);
}
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumValueMapping in project legend-engine by finos.
the class HelperMappingBuilder method processEnumMapping.
public static org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumerationMapping<Object> processEnumMapping(EnumerationMapping em, Mapping pureMapping, CompileContext context) {
// validate there is no mixed protocol format for source value
List<Object> sourceValues = em.enumValueMappings.stream().flatMap(enumValueMapping -> enumValueMapping.sourceValues.stream()).collect(Collectors.toList());
if (sourceValues.stream().anyMatch(sourceValue -> !(sourceValue instanceof EnumValueMappingSourceValue)) && sourceValues.stream().anyMatch(sourceValue -> sourceValue instanceof EnumValueMappingSourceValue)) {
throw new EngineException("Mixed formats for enum value mapping source values", em.sourceInformation, EngineErrorType.COMPILATION);
}
// process enum value mappings
String id = getEnumerationMappingId(em);
return new Root_meta_pure_mapping_EnumerationMapping_Impl<>(id, SourceInformationHelper.toM3SourceInformation(em.sourceInformation), null)._classifierGenericType(new Root_meta_pure_metamodel_type_generics_GenericType_Impl("")._rawType(context.pureModel.getType("meta::pure::mapping::EnumerationMapping"))._typeArguments(FastList.newListWith(new Root_meta_pure_metamodel_type_generics_GenericType_Impl("")._rawType(context.pureModel.getType("meta::pure::metamodel::type::Any")))))._name(id)._parent(pureMapping)._enumeration(context.resolveEnumeration(em.enumeration, em.sourceInformation))._enumValueMappings(ListIterate.collect(em.enumValueMappings, v -> new Root_meta_pure_mapping_EnumValueMapping_Impl(null, SourceInformationHelper.toM3SourceInformation(em.sourceInformation), null)._enum(context.resolveEnumValue(em.enumeration, v.enumValue))._sourceValues(convertSourceValues(em, v.sourceValues, context))));
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumValueMapping 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));
}
Aggregations