use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumerationMapping in project legend-pure by finos.
the class TestNavigateForRelationalAndMappingFromCoordinates method testNavigateForEnumerationMapping.
@Test
public void testNavigateForEnumerationMapping() throws Exception {
Source source = this.runtime.createInMemorySource("enumerationMappingSample.pure", "###Pure\n" + "import a::*;\n" + "\n" + "Enum a::Gender\n" + "{\n" + " FEMALE,\n" + " \n" + " MALE\n" + "}\n" + "\n" + "Class a::Person\n" + "{\n" + " id : Integer[1];\n" + " name : String[1];\n" + " gender : Gender[1];\n" + "}\n" + "\n" + "###Relational\n" + "Database a::PersonDatabase\n" + "(\n" + " Schema personSchema (\n" + " Table person (ID INT, NAME VARCHAR(200), GENDER CHAR(1))\n" + " )\n" + ")\n" + "\n" + "###Mapping\n" + "import a::*;\n" + "\n" + "Mapping a::PersonMapping\n" + "(\n" + " Gender: EnumerationMapping GenderMapping\n" + " {\n" + " FEMALE: 'F',\n" + " MALE: 'M' \n" + " }\n" + "\n" + " Person[personAlias] : Relational\n" + " {\n" + " scope([PersonDatabase]personSchema.person)\n" + " (\n" + " id : ID,\n" + " name : NAME,\n" + " gender : EnumerationMapping GenderMapping: GENDER\n" + " )\n" + " }\n" + " \n" + ")");
this.runtime.compile();
CoreInstance found = source.navigate(43, 39, this.processorSupport);
Assert.assertTrue(found instanceof EnumerationMapping);
Assert.assertEquals("GenderMapping", ((EnumerationMapping) found)._name());
Assert.assertEquals("enumerationMappingSample.pure", found.getSourceInformation().getSourceId());
Assert.assertEquals(31, found.getSourceInformation().getLine());
Assert.assertEquals(4, found.getSourceInformation().getColumn());
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumerationMapping in project legend-pure by finos.
the class TestSimpleGrammar method testReferredEnumMappingFromIncludes.
@Test
public void testReferredEnumMappingFromIncludes() {
final String pureCode = "Enum TradeType\n" + "{\n" + " BUY,\n" + " SELL\n" + "}\n" + "Class Trade\n" + "{\n" + " type: TradeType[1];\n" + "}\n" + "\n" + "Class EquityTrade extends Trade\n" + "{\n" + " product: String[1];\n" + " quantity: Integer[1];\n" + "}\n" + "###Relational\n" + "\n" + "Database tradeDB\n" + "(\n" + " Table eqTradeTable\n" + " (\n" + " id INT PRIMARY KEY,\n" + " product VARCHAR(200),\n" + " type VARCHAR(10),\n" + " qty INTEGER\n" + " )\n" + ")\n" + "###Mapping\n" + "\n" + "Mapping tradeMapping1\n" + "(\n" + " TradeType: EnumerationMapping TradeSource1\n" + " {\n" + " BUY: ['BUY', 'B'],\n" + " SELL: ['SELL', 'S']\n" + " }\n" + ")\n" + "\n" + "Mapping tradeMapping2\n" + "(\n" + " include tradeMapping1\n" + "\n" + " TradeType: EnumerationMapping TradeSource2\n" + " {\n" + " BUY: ['CREDIT'],\n" + " SELL: ['DEBIT']\n" + " }\n" + ")\n" + "\n" + "Mapping tradeMapping3\n" + "(\n" + " include tradeMapping2\n" + "\n" + " EquityTrade: Relational\n" + " {\n" + " scope( [tradeDB] default.eqTradeTable)\n" + " (\n" + " product: product,\n" + " quantity: qty,\n" + " type : EnumerationMapping TradeSource1 : type\n" + " )\n" + " }\n" + ")\n" + "\n";
Loader.parseM3(pureCode, this.repository, new ParserLibrary(Lists.immutable.with(new M3AntlrParser(), new MappingParser(), new RelationalParser(), new EnumerationMappingParser())), ValidationType.DEEP, VoidM3M4StateListener.VOID_M3_M4_STATE_LISTENER, this.context);
this.runtime.compile();
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumerationMapping in project legend-pure by finos.
the class TestSimpleGrammar method testValidDuplicateEnumMapping.
@Test
public void testValidDuplicateEnumMapping() {
final String pureCode = "Enum TradeType\n" + "{\n" + " BUY,\n" + " SELL\n" + "}\n" + "###Mapping\n" + "Mapping tradeMapping\n" + "(\n" + " TradeType: EnumerationMapping TradeSource1\n" + " {\n" + " BUY: ['BUY', 'B'],\n" + " SELL: ['SELL', 'S']\n" + " }\n" + " TradeType: EnumerationMapping TradeSource2\n" + " {\n" + " BUY: ['CREDIT'],\n" + " SELL: ['DEBIT']\n" + " }\n" + ")\n";
Loader.parseM3(pureCode, this.repository, new ParserLibrary(Lists.immutable.with(new M3AntlrParser(), new MappingParser(), new RelationalParser(), new EnumerationMappingParser())), ValidationType.DEEP, VoidM3M4StateListener.VOID_M3_M4_STATE_LISTENER, this.context);
this.runtime.compile();
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumerationMapping in project legend-pure by finos.
the class MappingValidator method validateVisibility.
private static void validateVisibility(Mapping mapping, Context context, ValidatorState validatorState, ProcessorSupport processorSupport) throws PureCompilationException {
Package pkg = mapping._package();
if (pkg != null) {
String sourceId = mapping.getSourceInformation().getSourceId();
for (SetImplementation classMapping : mapping._classMappings()) {
Class _class = (Class) ImportStub.withImportStubByPass(classMapping._classCoreInstance(), processorSupport);
VisibilityValidation.validatePackageAndSourceVisibility(classMapping, pkg, sourceId, context, validatorState, processorSupport, _class);
}
for (EnumerationMapping enumerationMapping : mapping._enumerationMappings()) {
Enumeration enumeration = (Enumeration) ImportStub.withImportStubByPass(enumerationMapping._enumerationCoreInstance(), processorSupport);
VisibilityValidation.validatePackageAndSourceVisibility(enumerationMapping, pkg, sourceId, context, validatorState, processorSupport, enumeration);
}
for (MappingInclude include : mapping._includes()) {
Mapping included = (Mapping) ImportStub.withImportStubByPass(include._includedCoreInstance(), processorSupport);
VisibilityValidation.validatePackageAndSourceVisibility(mapping, pkg, sourceId, context, validatorState, processorSupport, included);
}
}
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumerationMapping in project legend-pure by finos.
the class PureInstanceSetImplementationValidator method run.
@Override
public void run(PureInstanceSetImplementation classMapping, MatcherState state, Matcher matcher, ModelRepository modelRepository, Context context) throws PureCompilationException {
ProcessorSupport processorSupport = state.getProcessorSupport();
Class mappedClass = (Class) ImportStub.withImportStubByPass(classMapping._classCoreInstance(), processorSupport);
LambdaFunction filter = classMapping._filter();
if (filter != null) {
Validator.validate(filter, (ValidatorState) state, matcher, processorSupport);
Type booleanType = (Type) processorSupport.package_getByUserPath(M3Paths.Boolean);
GenericType filterReturnType = ((FunctionType) processorSupport.function_getFunctionType(filter))._returnType();
if (filterReturnType._rawTypeCoreInstance() != booleanType) {
throw new PureCompilationException(((RichIterable<ValueSpecification>) filter._expressionSequence()).toList().get(0).getSourceInformation(), "A filter should be a Boolean expression");
}
}
MutableSet<String> requiredProperties = getRequiredProperties(mappedClass, processorSupport);
for (PropertyMapping propertyMapping : classMapping._propertyMappings()) {
Property property = (Property) ImportStub.withImportStubByPass(propertyMapping._propertyCoreInstance(), processorSupport);
requiredProperties.remove(org.finos.legend.pure.m3.navigation.property.Property.getPropertyName(property));
LambdaFunction transform = ((PurePropertyMapping) propertyMapping)._transform();
FunctionType fType = (FunctionType) processorSupport.function_getFunctionType(transform);
Validator.validate(transform, (ValidatorState) state, matcher, processorSupport);
GenericType expressionGenericType = fType._returnType();
GenericType propertyGenericType = ((Property) ImportStub.withImportStubByPass(propertyMapping._propertyCoreInstance(), processorSupport))._genericType();
Multiplicity expressionMultiplicity = fType._returnMultiplicity();
Multiplicity propertyMultiplicity = ((Property) ImportStub.withImportStubByPass(propertyMapping._propertyCoreInstance(), processorSupport))._multiplicity();
if (((PurePropertyMapping) propertyMapping)._transformerCoreInstance() != null) {
CoreInstance propertyRawType = ImportStub.withImportStubByPass(propertyGenericType._rawTypeCoreInstance(), processorSupport);
EnumerationMapping transformer = (EnumerationMapping) ImportStub.withImportStubByPass(((PurePropertyMapping) propertyMapping)._transformerCoreInstance(), processorSupport);
if (!propertyRawType.equals(transformer._enumeration())) {
throw new PureCompilationException(propertyMapping.getSourceInformation(), "Property : [" + property._name() + "] is of type : [" + PackageableElement.getUserPathForPackageableElement(propertyRawType) + "] but enumeration mapping : [" + transformer._name() + "] is defined on enumeration : [" + PackageableElement.getUserPathForPackageableElement(transformer._enumeration()) + "].");
}
} else if (ImportStub.withImportStubByPass(propertyGenericType._rawTypeCoreInstance(), processorSupport) instanceof DataType) {
if (!org.finos.legend.pure.m3.navigation.generictype.GenericType.isGenericCompatibleWith(expressionGenericType, propertyGenericType, processorSupport)) {
String valTypeString = org.finos.legend.pure.m3.navigation.generictype.GenericType.print(expressionGenericType, false, processorSupport);
String propertyTypeString = org.finos.legend.pure.m3.navigation.generictype.GenericType.print(propertyGenericType, false, processorSupport);
if (valTypeString.equals(propertyTypeString)) {
valTypeString = org.finos.legend.pure.m3.navigation.generictype.GenericType.print(expressionGenericType, true, processorSupport);
propertyTypeString = org.finos.legend.pure.m3.navigation.generictype.GenericType.print(propertyGenericType, true, processorSupport);
}
throw new PureCompilationException(((RichIterable<ValueSpecification>) ((PurePropertyMapping) propertyMapping)._transform()._expressionSequence()).toList().get(0).getSourceInformation(), "Type Error: '" + valTypeString + "' not a subtype of '" + propertyTypeString + "'");
}
} else {
Mapping mapping = (Mapping) ImportStub.withImportStubByPass(classMapping._parentCoreInstance(), processorSupport);
SetImplementation setImplementation = org.finos.legend.pure.m2.dsl.mapping.Mapping.getClassMappingById(mapping, propertyMapping._targetSetImplementationId(), processorSupport);
if (setImplementation == null) {
throw new PureCompilationException(propertyMapping.getSourceInformation(), "The set implementation '" + propertyMapping._targetSetImplementationId() + "' is unknown in the mapping '" + mapping.getName() + "'");
}
Type srcClass = setImplementation instanceof PureInstanceSetImplementation ? (Type) ImportStub.withImportStubByPass(((PureInstanceSetImplementation) setImplementation)._srcClassCoreInstance(), processorSupport) : null;
Type expRawType = (Type) ImportStub.withImportStubByPass(expressionGenericType._rawTypeCoreInstance(), processorSupport);
if (srcClass != null && srcClass != expRawType) {
throw new PureCompilationException(((RichIterable<ValueSpecification>) ((PurePropertyMapping) propertyMapping)._transform()._expressionSequence()).toList().get(0).getSourceInformation(), "Type Error: '" + PackageableElement.getUserPathForPackageableElement(srcClass) + "' is not '" + PackageableElement.getUserPathForPackageableElement(expRawType) + "'");
}
}
if (!((PurePropertyMapping) propertyMapping)._explodeProperty() && !org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity.subsumes(propertyMultiplicity, expressionMultiplicity)) {
throw new PureCompilationException(((RichIterable<ValueSpecification>) transform._expressionSequence()).toList().get(0).getSourceInformation(), "Multiplicity Error ' The property '" + org.finos.legend.pure.m3.navigation.property.Property.getPropertyName(propertyMapping._propertyCoreInstance()) + "' has a multiplicity range of " + org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity.print(propertyMultiplicity) + " when the given expression has a multiplicity range of " + org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity.print(expressionMultiplicity));
}
}
// TODO add this validation once violations have been removed
// if (requiredProperties.notEmpty())
// {
// StringBuilder message = new StringBuilder("The following required properties for ");
// _Class.print(message, mappedClass, true);
// message.append(" are not mapped: ");
// requiredProperties.toSortedList().appendString(message, ", ");
// throw new PureCompilationException(classMapping.getSourceInformation(), message.toString());
// }
}
Aggregations