Search in sources :

Example 1 with EnumerationMapping

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());
}
Also used : EnumerationMapping(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumerationMapping) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) Source(org.finos.legend.pure.m3.serialization.runtime.Source) Test(org.junit.Test)

Example 2 with EnumerationMapping

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();
}
Also used : EnumerationMappingParser(org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.EnumerationMappingParser) MappingParser(org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.MappingParser) ParserLibrary(org.finos.legend.pure.m3.serialization.grammar.ParserLibrary) M3AntlrParser(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3AntlrParser) EnumerationMappingParser(org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.EnumerationMappingParser) RelationalParser(org.finos.legend.pure.m2.relational.serialization.grammar.v1.RelationalParser) Test(org.junit.Test)

Example 3 with EnumerationMapping

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();
}
Also used : EnumerationMappingParser(org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.EnumerationMappingParser) MappingParser(org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.MappingParser) ParserLibrary(org.finos.legend.pure.m3.serialization.grammar.ParserLibrary) M3AntlrParser(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3AntlrParser) EnumerationMappingParser(org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.EnumerationMappingParser) RelationalParser(org.finos.legend.pure.m2.relational.serialization.grammar.v1.RelationalParser) Test(org.junit.Test)

Example 4 with EnumerationMapping

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);
        }
    }
}
Also used : Enumeration(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Enumeration) Class(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Class) Package(org.finos.legend.pure.m3.coreinstance.Package) PureInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation)

Example 5 with EnumerationMapping

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());
// }
}
Also used : GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) PureInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation) EnumerationMapping(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumerationMapping) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) LambdaFunction(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction) PropertyMapping(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.PropertyMapping) EnumerationMapping(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumerationMapping) PurePropertyMapping(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PurePropertyMapping) Mapping(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.Mapping) RichIterable(org.eclipse.collections.api.RichIterable) DataType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.DataType) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) Type(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) ProcessorSupport(org.finos.legend.pure.m3.navigation.ProcessorSupport) Multiplicity(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity) PropertyMapping(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.PropertyMapping) PurePropertyMapping(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PurePropertyMapping) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) DataType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.DataType) Class(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Class) PurePropertyMapping(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PurePropertyMapping) PureInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation) SetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.SetImplementation) Property(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Aggregations

Test (org.junit.Test)15 EnumerationMapping (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumerationMapping)14 SetImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.SetImplementation)9 RuntimeTestScriptBuilder (org.finos.legend.pure.m3.RuntimeTestScriptBuilder)8 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)8 PureInstanceSetImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation)7 Mapping (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.Mapping)6 GenericType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType)6 RichIterable (org.eclipse.collections.api.RichIterable)5 EnumValueMapping (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumValueMapping)5 PropertyMapping (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.PropertyMapping)5 Property (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property)5 Type (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type)5 ImmutableList (org.eclipse.collections.api.list.ImmutableList)4 EngineErrorType (org.finos.legend.engine.protocol.pure.v1.model.context.EngineErrorType)4 InstanceSetImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.InstanceSetImplementation)4 Class (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Class)4 Optional (java.util.Optional)3 MutableList (org.eclipse.collections.api.list.MutableList)3 Lists (org.eclipse.collections.impl.factory.Lists)3