Search in sources :

Example 1 with PureInstanceSetImplementation

use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation in project legend-pure by finos.

the class MappingValidator method validateOperationMappings.

private static void validateOperationMappings(Mapping mapping, MutableListMultimap<Type, CoreInstance> setImplementationsByClass, final ProcessorSupport processorSupport) {
    RichIterable<? extends MergeOperationSetImplementation> mergeOperations = mapping._classMappings().select(s -> s instanceof MergeOperationSetImplementation).collect(s -> (MergeOperationSetImplementation) s);
    if (!mergeOperations.isEmpty()) {
        mergeOperations.forEach(m -> {
            FunctionType ft = (FunctionType) processorSupport.function_getFunctionType(m._validationFunction()._expressionSequence().getFirst());
            if (!ft._returnType()._rawType()._name().equals("Boolean")) {
                throw new PureCompilationException(m.getSourceInformation(), "Merge validation function for class: " + ImportStub.withImportStubByPass(m._classCoreInstance(), processorSupport).getName() + " does not return Boolean");
            }
            MutableList<PureInstanceSetImplementation> mergesrc = setImplementationsByClass.get(m._class()).select(s -> s instanceof PureInstanceSetImplementation).collect(s -> (PureInstanceSetImplementation) s);
            MutableList<Type> mergeSrcClasses = mergesrc.collect(r -> r._srcClass());
            RichIterable<Type> validationParams = ft._parameters().collect(p -> p._genericType()._rawType());
            if (!validationParams.allSatisfy(p -> mergeSrcClasses.contains(p))) {
                throw new PureCompilationException(m.getSourceInformation(), "Merge validation function for class: " + ImportStub.withImportStubByPass(m._classCoreInstance(), processorSupport).getName() + " has an invalid parameter. All parameters must be a src class of a merged set");
            }
        });
    }
}
Also used : Maps(org.eclipse.collections.impl.factory.Maps) ValidatorState(org.finos.legend.pure.m3.compiler.validation.ValidatorState) Function(org.eclipse.collections.api.block.function.Function) Predicate(org.eclipse.collections.api.block.predicate.Predicate) Association(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.Association) EnumStub(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.EnumStub) MutableList(org.eclipse.collections.api.list.MutableList) Multimaps(org.eclipse.collections.impl.factory.Multimaps) FastList(org.eclipse.collections.impl.list.mutable.FastList) ProcessorSupport(org.finos.legend.pure.m3.navigation.ProcessorSupport) Context(org.finos.legend.pure.m3.compiler.Context) MutableSet(org.eclipse.collections.api.set.MutableSet) M2MappingPaths(org.finos.legend.pure.m2.dsl.mapping.M2MappingPaths) MatchRunner(org.finos.legend.pure.m3.tools.matcher.MatchRunner) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) RichIterable(org.eclipse.collections.api.RichIterable) VisibilityValidation(org.finos.legend.pure.m3.compiler.validation.VisibilityValidation) MutableMap(org.eclipse.collections.api.map.MutableMap) Property(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property) Store(org.finos.legend.pure.m3.coreinstance.meta.pure.store.Store) Validator(org.finos.legend.pure.m3.compiler.validation.Validator) Package(org.finos.legend.pure.m3.coreinstance.Package) Pair(org.eclipse.collections.api.tuple.Pair) PureInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation) org.finos.legend.pure.m3.coreinstance.meta.pure.mapping(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping) Enumeration(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Enumeration) Predicates(org.eclipse.collections.impl.block.factory.Predicates) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException) MutableListMultimap(org.eclipse.collections.api.multimap.list.MutableListMultimap) UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap) PackageableElement(org.finos.legend.pure.m3.navigation.PackageableElement.PackageableElement) ModelRepository(org.finos.legend.pure.m4.ModelRepository) Class(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Class) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) Type(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type) Matcher(org.finos.legend.pure.m3.tools.matcher.Matcher) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) Lists(org.eclipse.collections.impl.factory.Lists) ImportStub(org.finos.legend.pure.m3.navigation.importstub.ImportStub) Sets(org.eclipse.collections.impl.factory.Sets) MatcherState(org.finos.legend.pure.m3.tools.matcher.MatcherState) ListIterable(org.eclipse.collections.api.list.ListIterable) MapIterable(org.eclipse.collections.api.map.MapIterable) UnifiedSet(org.eclipse.collections.impl.set.mutable.UnifiedSet) 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) PureInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Example 2 with PureInstanceSetImplementation

use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation 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)

Example 3 with PureInstanceSetImplementation

use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation in project legend-pure by finos.

the class TestAggregationAwareMapping method testAggregationAwareMappingGrammarMultiAggregate.

@Test
public void testAggregationAwareMappingGrammarMultiAggregate() {
    String source = "###Pure\n" + "Class Sales\n" + "{\n" + "   id: Integer[1];\n" + "   salesDate: FiscalCalendar[1];\n" + "   revenue: Float[1];\n" + "}\n" + "\n" + "Class FiscalCalendar\n" + "{\n" + "   date: Date[1];\n" + "   fiscalYear: Integer[1];\n" + "   fiscalMonth: Integer[1];\n" + "   fiscalQtr: Integer[1];\n" + "}\n" + "\n" + "Class Sales_By_Date\n" + "{\n" + "   salesDate: FiscalCalendar[1];\n" + "   netRevenue: Float[1];\n" + "}\n" + "\n" + "Class Sales_By_Qtr\n" + "{\n" + "   salesQtrFirstDate: FiscalCalendar[1];\n" + "   netRevenue: Float[1];\n" + "}\n" + "\n" + "function meta::pure::functions::math::sum(numbers:Float[*]):Float[1]\n" + "{\n" + "    $numbers->plus();\n" + "}\n" + "###Mapping\n" + "Mapping map\n" + "(\n" + "   FiscalCalendar [b] : Pure {\n" + "      ~src FiscalCalendar\n" + "      date : $src.date,\n" + "      fiscalYear : $src.fiscalYear,\n" + "      fiscalMonth : $src.fiscalMonth,\n" + "      fiscalQtr : $src.fiscalQtr\n" + "   }\n" + "   \n" + "   Sales [a] : AggregationAware {\n" + "      Views : [\n" + "         (\n" + "            ~modelOperation : {\n" + "               ~canAggregate false,\n" + "               ~groupByFunctions (\n" + "                  $this.salesDate.fiscalYear,\n" + "                  $this.salesDate.fiscalQtr\n" + "               ),\n" + "               ~aggregateValues (\n" + "                  ( ~mapFn: $this.revenue, ~aggregateFn: $mapped->sum() )\n" + "               )\n" + "            },\n" + "            ~aggregateMapping : Pure {\n" + "               ~src Sales_By_Qtr\n" + "               salesDate [b] : $src.salesQtrFirstDate,\n" + "               revenue : $src.netRevenue\n" + "            }\n" + "         ),\n" + "         (\n" + "            ~modelOperation : {\n" + "               ~canAggregate true,\n" + "               ~groupByFunctions (\n" + "                  $this.salesDate\n" + "               ),\n" + "               ~aggregateValues (\n" + "                  ( ~mapFn: $this.revenue, ~aggregateFn: $mapped->sum() )\n" + "               )\n" + "            },\n" + "            ~aggregateMapping : Pure {\n" + "               ~src Sales_By_Date\n" + "               salesDate [b] : $src.salesDate,\n" + "               revenue : $src.netRevenue\n" + "            }\n" + "         )\n" + "      ],\n" + "      ~mainMapping : Pure {\n" + "         ~src Sales\n" + "         salesDate [b] : $src.salesDate,\n" + "         revenue : $src.revenue\n" + "      }\n" + "   }\n" + ")";
    this.runtime.createInMemorySource("mapping.pure", source);
    this.runtime.compile();
    InstanceSetImplementation setImpl = (InstanceSetImplementation) ((org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.Mapping) this.runtime.getCoreInstance("map"))._classMappings().toSortedList(new Comparator<SetImplementation>() {

        @Override
        public int compare(SetImplementation o1, SetImplementation o2) {
            return o1._id().compareTo(o2._id());
        }
    }).get(0);
    Assert.assertTrue(setImpl instanceof AggregationAwareSetImplementation);
    AggregationAwareSetImplementation aggSetImpl = (AggregationAwareSetImplementation) setImpl;
    Assert.assertEquals("a", aggSetImpl._id());
    Assert.assertNotNull(aggSetImpl._mainSetImplementation());
    Assert.assertTrue(aggSetImpl._mainSetImplementation() instanceof PureInstanceSetImplementation);
    Assert.assertEquals("a_Main", aggSetImpl._mainSetImplementation()._id());
    Assert.assertNotNull(aggSetImpl._aggregateSetImplementations());
    Assert.assertTrue(aggSetImpl._aggregateSetImplementations().size() == 2);
    Assert.assertNotNull(aggSetImpl._aggregateSetImplementations().toList().get(0)._aggregateSpecification());
    Assert.assertFalse(aggSetImpl._aggregateSetImplementations().toList().get(0)._aggregateSpecification()._canAggregate());
    Assert.assertTrue(aggSetImpl._aggregateSetImplementations().toList().get(0)._aggregateSpecification()._groupByFunctions().size() == 2);
    Assert.assertTrue(aggSetImpl._aggregateSetImplementations().toList().get(0)._aggregateSpecification()._aggregateValues().size() == 1);
    Assert.assertTrue(aggSetImpl._aggregateSetImplementations().toList().get(0)._setImplementation() instanceof PureInstanceSetImplementation);
    Assert.assertEquals("a_Aggregate_0", aggSetImpl._aggregateSetImplementations().toList().get(0)._setImplementation()._id());
    Assert.assertNotNull(aggSetImpl._aggregateSetImplementations().toList().get(1)._aggregateSpecification());
    Assert.assertTrue(aggSetImpl._aggregateSetImplementations().toList().get(1)._aggregateSpecification()._canAggregate());
    Assert.assertTrue(aggSetImpl._aggregateSetImplementations().toList().get(1)._aggregateSpecification()._groupByFunctions().size() == 1);
    Assert.assertTrue(aggSetImpl._aggregateSetImplementations().toList().get(1)._aggregateSpecification()._aggregateValues().size() == 1);
    Assert.assertTrue(aggSetImpl._aggregateSetImplementations().toList().get(1)._setImplementation() instanceof PureInstanceSetImplementation);
    Assert.assertEquals("a_Aggregate_1", aggSetImpl._aggregateSetImplementations().toList().get(1)._setImplementation()._id());
}
Also used : InstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.InstanceSetImplementation) PureInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation) PureInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation) AggregationAwareSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.aggregationAware.AggregationAwareSetImplementation) SetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.SetImplementation) InstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.InstanceSetImplementation) PureInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation) AggregationAwareSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.aggregationAware.AggregationAwareSetImplementation) Test(org.junit.Test)

Example 4 with PureInstanceSetImplementation

use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation in project legend-pure by finos.

the class TestAggregationAwareMapping method testAggregationAwareMappingGrammarMultiViewsMultiAggregateValues.

@Test
public void testAggregationAwareMappingGrammarMultiViewsMultiAggregateValues() {
    String source = "###Pure\n" + "Class Sales\n" + "{\n" + "   id: Integer[1];\n" + "   salesDate: FiscalCalendar[1];\n" + "   revenue: Float[1];\n" + "}\n" + "\n" + "Class FiscalCalendar\n" + "{\n" + "   date: Date[1];\n" + "   fiscalYear: Integer[1];\n" + "   fiscalMonth: Integer[1];\n" + "   fiscalQtr: Integer[1];\n" + "}\n" + "\n" + "Class Sales_By_Date\n" + "{\n" + "   salesDate: FiscalCalendar[1];\n" + "   netRevenue: Float[1];\n" + "}\n" + "\n" + "Class Sales_By_Qtr\n" + "{\n" + "   salesQtrFirstDate: FiscalCalendar[1];\n" + "   netRevenue: Float[1];\n" + "}\n" + "\n" + "function meta::pure::functions::math::sum(numbers:Float[*]):Float[1]\n" + "{\n" + "    $numbers->plus();\n" + "}\n" + "###Mapping\n" + "Mapping map\n" + "(\n" + "   FiscalCalendar [b] : Pure {\n" + "      ~src FiscalCalendar\n" + "      date : $src.date,\n" + "      fiscalYear : $src.fiscalYear,\n" + "      fiscalMonth : $src.fiscalMonth,\n" + "      fiscalQtr : $src.fiscalQtr\n" + "   }\n" + "   \n" + "   Sales [a] : AggregationAware {\n" + "      Views : [\n" + "         (\n" + "            ~modelOperation : {\n" + "               ~canAggregate false,\n" + "               ~groupByFunctions (\n" + "                  $this.salesDate.fiscalYear,\n" + "                  $this.salesDate.fiscalQtr\n" + "               ),\n" + "               ~aggregateValues (\n" + "                  ( ~mapFn: $this.revenue, ~aggregateFn: $mapped->sum() )\n" + "               )\n" + "            },\n" + "            ~aggregateMapping : Pure {\n" + "               ~src Sales_By_Qtr\n" + "               salesDate [b] : $src.salesQtrFirstDate,\n" + "               revenue : $src.netRevenue\n" + "            }\n" + "         ),\n" + "         (\n" + "            ~modelOperation : {\n" + "               ~canAggregate true,\n" + "               ~groupByFunctions (\n" + "                  $this.salesDate\n" + "               ),\n" + "               ~aggregateValues (\n" + "                  ( ~mapFn: $this.revenue, ~aggregateFn: $mapped->sum() ),\n" + "                  ( ~mapFn: $this.revenue, ~aggregateFn: $mapped->sum() )\n" + "               )\n" + "            },\n" + "            ~aggregateMapping : Pure {\n" + "               ~src Sales_By_Date\n" + "               salesDate [b] : $src.salesDate,\n" + "               revenue : $src.netRevenue\n" + "            }\n" + "         )\n" + "      ],\n" + "      ~mainMapping : Pure {\n" + "         ~src Sales\n" + "         salesDate [b] : $src.salesDate,\n" + "         revenue : $src.revenue\n" + "      }\n" + "   }\n" + ")";
    this.runtime.createInMemorySource("mapping.pure", source);
    this.runtime.compile();
    InstanceSetImplementation setImpl = (InstanceSetImplementation) ((org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.Mapping) this.runtime.getCoreInstance("map"))._classMappings().toSortedList(new Comparator<SetImplementation>() {

        @Override
        public int compare(SetImplementation o1, SetImplementation o2) {
            return o1._id().compareTo(o2._id());
        }
    }).get(0);
    Assert.assertTrue(setImpl instanceof AggregationAwareSetImplementation);
    AggregationAwareSetImplementation aggSetImpl = (AggregationAwareSetImplementation) setImpl;
    Assert.assertEquals("a", aggSetImpl._id());
    Assert.assertNotNull(aggSetImpl._mainSetImplementation());
    Assert.assertTrue(aggSetImpl._mainSetImplementation() instanceof PureInstanceSetImplementation);
    Assert.assertEquals("a_Main", aggSetImpl._mainSetImplementation()._id());
    Assert.assertNotNull(aggSetImpl._aggregateSetImplementations());
    Assert.assertTrue(aggSetImpl._aggregateSetImplementations().size() == 2);
    Assert.assertNotNull(aggSetImpl._aggregateSetImplementations().toList().get(0)._aggregateSpecification());
    Assert.assertFalse(aggSetImpl._aggregateSetImplementations().toList().get(0)._aggregateSpecification()._canAggregate());
    Assert.assertTrue(aggSetImpl._aggregateSetImplementations().toList().get(0)._aggregateSpecification()._groupByFunctions().size() == 2);
    Assert.assertTrue(aggSetImpl._aggregateSetImplementations().toList().get(0)._aggregateSpecification()._aggregateValues().size() == 1);
    Assert.assertTrue(aggSetImpl._aggregateSetImplementations().toList().get(0)._setImplementation() instanceof PureInstanceSetImplementation);
    Assert.assertEquals("a_Aggregate_0", aggSetImpl._aggregateSetImplementations().toList().get(0)._setImplementation()._id());
    Assert.assertNotNull(aggSetImpl._aggregateSetImplementations().toList().get(1)._aggregateSpecification());
    Assert.assertTrue(aggSetImpl._aggregateSetImplementations().toList().get(1)._aggregateSpecification()._canAggregate());
    Assert.assertTrue(aggSetImpl._aggregateSetImplementations().toList().get(1)._aggregateSpecification()._groupByFunctions().size() == 1);
    Assert.assertTrue(aggSetImpl._aggregateSetImplementations().toList().get(1)._aggregateSpecification()._aggregateValues().size() == 2);
    Assert.assertTrue(aggSetImpl._aggregateSetImplementations().toList().get(1)._setImplementation() instanceof PureInstanceSetImplementation);
    Assert.assertEquals("a_Aggregate_1", aggSetImpl._aggregateSetImplementations().toList().get(1)._setImplementation()._id());
}
Also used : InstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.InstanceSetImplementation) PureInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation) PureInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation) AggregationAwareSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.aggregationAware.AggregationAwareSetImplementation) SetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.SetImplementation) InstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.InstanceSetImplementation) PureInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation) AggregationAwareSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.aggregationAware.AggregationAwareSetImplementation) Test(org.junit.Test)

Example 5 with PureInstanceSetImplementation

use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation in project legend-pure by finos.

the class TestAggregationAwareMapping method testAggregationAwareMappingGrammarSingleAggregate.

@Test
public void testAggregationAwareMappingGrammarSingleAggregate() {
    String source = "###Pure\n" + "Class Sales\n" + "{\n" + "   id: Integer[1];\n" + "   salesDate: FiscalCalendar[1];\n" + "   revenue: Float[1];\n" + "}\n" + "\n" + "Class FiscalCalendar\n" + "{\n" + "   date: Date[1];\n" + "   fiscalYear: Integer[1];\n" + "   fiscalMonth: Integer[1];\n" + "   fiscalQtr: Integer[1];\n" + "}\n" + "\n" + "Class Sales_By_Date\n" + "{\n" + "   salesDate: FiscalCalendar[1];\n" + "   netRevenue: Float[1];\n" + "}\n" + "\n" + "function meta::pure::functions::math::sum(numbers:Float[*]):Float[1]\n" + "{\n" + "    $numbers->plus();\n" + "}\n" + "###Mapping\n" + "Mapping map\n" + "(\n" + "   FiscalCalendar [b] : Pure {\n" + "      ~src FiscalCalendar\n" + "      date : $src.date,\n" + "      fiscalYear : $src.fiscalYear,\n" + "      fiscalMonth : $src.fiscalMonth,\n" + "      fiscalQtr : $src.fiscalQtr\n" + "   }\n" + "   \n" + "   Sales [a] : AggregationAware {\n" + "      Views : [\n" + "         (\n" + "            ~modelOperation : {\n" + "               ~canAggregate true,\n" + "               ~groupByFunctions (\n" + "                  $this.salesDate\n" + "               ),\n" + "               ~aggregateValues (\n" + "                  ( ~mapFn: $this.revenue, ~aggregateFn: $mapped->sum() )\n" + "               )\n" + "            },\n" + "            ~aggregateMapping : Pure {\n" + "               ~src Sales_By_Date\n" + "               salesDate [b] : $src.salesDate,\n" + "               revenue : $src.netRevenue\n" + "            }\n" + "         )\n" + "      ],\n" + "      ~mainMapping : Pure {\n" + "         ~src Sales\n" + "         salesDate [b] : $src.salesDate,\n" + "         revenue : $src.revenue\n" + "      }\n" + "   }\n" + ")";
    this.runtime.createInMemorySource("mapping.pure", source);
    this.runtime.compile();
    InstanceSetImplementation setImpl = (InstanceSetImplementation) ((org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.Mapping) this.runtime.getCoreInstance("map"))._classMappings().toSortedList(new Comparator<SetImplementation>() {

        @Override
        public int compare(SetImplementation o1, SetImplementation o2) {
            return o1._id().compareTo(o2._id());
        }
    }).get(0);
    Assert.assertTrue(setImpl instanceof AggregationAwareSetImplementation);
    AggregationAwareSetImplementation aggSetImpl = (AggregationAwareSetImplementation) setImpl;
    Assert.assertEquals("a", aggSetImpl._id());
    Assert.assertNotNull(aggSetImpl._mainSetImplementation());
    Assert.assertTrue(aggSetImpl._mainSetImplementation() instanceof PureInstanceSetImplementation);
    Assert.assertEquals("a_Main", aggSetImpl._mainSetImplementation()._id());
    Assert.assertNotNull(aggSetImpl._aggregateSetImplementations());
    Assert.assertTrue(aggSetImpl._aggregateSetImplementations().size() == 1);
    Assert.assertNotNull(aggSetImpl._aggregateSetImplementations().toList().get(0)._aggregateSpecification());
    Assert.assertTrue(aggSetImpl._aggregateSetImplementations().toList().get(0)._aggregateSpecification()._canAggregate());
    Assert.assertTrue(aggSetImpl._aggregateSetImplementations().toList().get(0)._aggregateSpecification()._groupByFunctions().size() == 1);
    Assert.assertTrue(aggSetImpl._aggregateSetImplementations().toList().get(0)._aggregateSpecification()._aggregateValues().size() == 1);
    Assert.assertTrue(aggSetImpl._aggregateSetImplementations().toList().get(0)._setImplementation() instanceof PureInstanceSetImplementation);
    Assert.assertEquals("a_Aggregate_0", aggSetImpl._aggregateSetImplementations().toList().get(0)._setImplementation()._id());
}
Also used : InstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.InstanceSetImplementation) PureInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation) PureInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation) AggregationAwareSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.aggregationAware.AggregationAwareSetImplementation) SetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.SetImplementation) InstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.InstanceSetImplementation) PureInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation) AggregationAwareSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.aggregationAware.AggregationAwareSetImplementation) Test(org.junit.Test)

Aggregations

PureInstanceSetImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation)12 SetImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.SetImplementation)8 InstanceSetImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.InstanceSetImplementation)7 AggregationAwareSetImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.aggregationAware.AggregationAwareSetImplementation)7 Test (org.junit.Test)6 PropertyMapping (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.PropertyMapping)4 PurePropertyMapping (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PurePropertyMapping)4 FunctionType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType)4 Type (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type)4 GenericType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType)4 RichIterable (org.eclipse.collections.api.RichIterable)3 PureModel (org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel)3 EnumerationMapping (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumerationMapping)3 LambdaFunction (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction)3 PureCompilationException (org.finos.legend.pure.m4.exception.PureCompilationException)3 Pair (org.eclipse.collections.api.tuple.Pair)2 Lists (org.eclipse.collections.impl.factory.Lists)2 FastList (org.eclipse.collections.impl.list.mutable.FastList)2 Mapping (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.Mapping)2 Property (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property)2