Search in sources :

Example 1 with AggregationAwareSetImplementation

use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.aggregationAware.AggregationAwareSetImplementation 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" + "native function sum(f:Float[*]):Float[1];\n" + "\n" + "###Relational\n" + "Database db \n" + "(\n" + "   Table sales_base (id INT PRIMARY KEY, sales_date DATE, revenue FLOAT)\n" + "   Table calendar (date DATE PRIMARY KEY, fiscal_year INT, fiscal_qtr INT, fiscal_month INT)\n" + "   \n" + "   Table sales_by_date (sales_date DATE, net_revenue FLOAT)\n" + "   Table sales_by_qtr (sales_qtr_first_date DATE, net_revenue FLOAT)\n" + "   \n" + "   Join sales_calendar (sales_base.sales_date = calendar.date)\n" + "   Join sales_date_calendar (sales_by_date.sales_date = calendar.date)\n" + "   Join sales_qtr_calendar (sales_by_qtr.sales_qtr_first_date = calendar.date)\n" + "\n" + ")\n" + "\n" + "###Mapping\n" + "Mapping map\n" + "(\n" + "   FiscalCalendar [b] : Relational {\n" + "      scope([db]calendar)\n" + "      (\n" + "         date : date,\n" + "         fiscalYear : fiscal_year,\n" + "         fiscalQtr : fiscal_qtr,\n" + "         fiscalMonth : fiscal_month\n" + "      )\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 : Relational {\n" + "               salesDate ( \n" + "                     fiscalQtr : [db]@sales_qtr_calendar | calendar.fiscal_qtr, \n" + "                     fiscalYear : [db]@sales_qtr_calendar | calendar.fiscal_year\n" + "                  ),\n" + "               revenue : [db]sales_by_qtr.net_revenue\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 : Relational {\n" + "               scope([db]sales_by_date)\n" + "               (\n" + "                  salesDate [b] : [db]@sales_date_calendar,\n" + "                  revenue : net_revenue\n" + "               )\n" + "            }\n" + "         )\n" + "      ],\n" + "      ~mainMapping : Relational {\n" + "         scope([db]sales_base)\n" + "         (\n" + "            salesDate [b] : [db]@sales_calendar,\n" + "            revenue : revenue\n" + "         )\n" + "      }\n" + "   }\n" + ")";
    this.runtime.createInMemorySource("mapping.pure", source);
    this.runtime.compile();
    InstanceSetImplementation setImpl = (InstanceSetImplementation) ((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 RootRelationalInstanceSetImplementation);
    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 RootRelationalInstanceSetImplementation);
    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 RootRelationalInstanceSetImplementation);
    Assert.assertEquals("a_Aggregate_1", aggSetImpl._aggregateSetImplementations().toList().get(1)._setImplementation()._id());
}
Also used : RootRelationalInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.RootRelationalInstanceSetImplementation) InstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.InstanceSetImplementation) RootRelationalInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.RootRelationalInstanceSetImplementation) AggregationAwareSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.aggregationAware.AggregationAwareSetImplementation) SetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.SetImplementation) RootRelationalInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.RootRelationalInstanceSetImplementation) InstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.InstanceSetImplementation) AggregationAwareSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.aggregationAware.AggregationAwareSetImplementation) Test(org.junit.Test)

Example 2 with AggregationAwareSetImplementation

use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.aggregationAware.AggregationAwareSetImplementation 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" + "native function sum(f:Float[*]):Float[1];\n" + "\n" + "###Relational\n" + "Database db \n" + "(\n" + "   Table sales_base (id INT PRIMARY KEY, sales_date DATE, revenue FLOAT)\n" + "   Table calendar (date DATE PRIMARY KEY, fiscal_year INT, fiscal_qtr INT, fiscal_month INT)\n" + "   \n" + "   Table sales_by_date (sales_date DATE, net_revenue FLOAT)\n" + "   \n" + "   Join sales_calendar (sales_base.sales_date = calendar.date)\n" + "   Join sales_date_calendar (sales_by_date.sales_date = calendar.date)\n" + ")\n" + "\n" + "###Mapping\n" + "Mapping map\n" + "(\n" + "   FiscalCalendar [b] : Relational {\n" + "      scope([db]calendar)\n" + "      (\n" + "         date : date,\n" + "         fiscalYear : fiscal_year,\n" + "         fiscalQtr : fiscal_qtr,\n" + "         fiscalMonth : fiscal_month\n" + "      )\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 : Relational {\n" + "               scope([db]sales_by_date)\n" + "               (\n" + "                  salesDate [b] : [db]@sales_date_calendar,\n" + "                  revenue : net_revenue\n" + "               )\n" + "            }\n" + "         )\n" + "      ],\n" + "      ~mainMapping : Relational {\n" + "         scope([db]sales_base)\n" + "         (\n" + "            salesDate [b] : [db]@sales_calendar,\n" + "            revenue : revenue\n" + "         )\n" + "      }\n" + "   }\n" + ")";
    this.runtime.createInMemorySource("mapping.pure", source);
    this.runtime.compile();
    InstanceSetImplementation setImpl = (InstanceSetImplementation) ((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 RootRelationalInstanceSetImplementation);
    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 RootRelationalInstanceSetImplementation);
    Assert.assertEquals("a_Aggregate_0", aggSetImpl._aggregateSetImplementations().toList().get(0)._setImplementation()._id());
}
Also used : RootRelationalInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.RootRelationalInstanceSetImplementation) InstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.InstanceSetImplementation) RootRelationalInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.RootRelationalInstanceSetImplementation) AggregationAwareSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.aggregationAware.AggregationAwareSetImplementation) SetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.SetImplementation) RootRelationalInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.RootRelationalInstanceSetImplementation) InstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.InstanceSetImplementation) AggregationAwareSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.aggregationAware.AggregationAwareSetImplementation) Test(org.junit.Test)

Example 3 with AggregationAwareSetImplementation

use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.aggregationAware.AggregationAwareSetImplementation in project legend-pure by finos.

the class AggregationAwareValidator method run.

@Override
public void run(AggregationAwareSetImplementation instance, MatcherState state, Matcher matcher, ModelRepository modelRepository, Context context) throws PureCompilationException {
    ValidatorState validatorState = (ValidatorState) state;
    ProcessorSupport processorSupport = validatorState.getProcessorSupport();
    for (AggregateSetImplementationContainer container : instance._aggregateSetImplementations()) {
        for (AggregationFunctionSpecification aggregationFunctionSpecification : container._aggregateSpecification()._aggregateValues()) {
            FunctionType mapFnType = (FunctionType) ImportStub.withImportStubByPass(aggregationFunctionSpecification._mapFn()._classifierGenericType()._typeArguments().toList().get(0)._rawTypeCoreInstance(), processorSupport);
            Type mapFnReturnType = (Type) ImportStub.withImportStubByPass(mapFnType._returnType()._rawTypeCoreInstance(), processorSupport);
            if (!Instance.instanceOf(mapFnReturnType, M3Paths.DataType, processorSupport)) {
                throw new PureCompilationException(aggregationFunctionSpecification._mapFn().getSourceInformation(), "An aggregate specification's mapFunction return type should be a DataType (primitive type/enumeration)");
            }
            FunctionType aggregateFnType = (FunctionType) ImportStub.withImportStubByPass(aggregationFunctionSpecification._aggregateFn()._classifierGenericType()._typeArguments().toList().get(0)._rawTypeCoreInstance(), processorSupport);
            Type aggregateFnReturnType = (Type) ImportStub.withImportStubByPass(aggregateFnType._returnType()._rawTypeCoreInstance(), processorSupport);
            if (!Instance.instanceOf(aggregateFnReturnType, M3Paths.DataType, processorSupport)) {
                throw new PureCompilationException(aggregationFunctionSpecification._aggregateFn().getSourceInformation(), "An aggregate specification's aggregateFunction return type should be a DataType (primitive type/enumeration)");
            }
        }
        matcher.fullMatch(container._setImplementation(), state);
    }
    matcher.fullMatch(instance._mainSetImplementation(), state);
}
Also used : Type(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) ProcessorSupport(org.finos.legend.pure.m3.navigation.ProcessorSupport) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) AggregateSetImplementationContainer(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.aggregationAware.AggregateSetImplementationContainer) AggregationFunctionSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.aggregationAware.AggregationFunctionSpecification) ValidatorState(org.finos.legend.pure.m3.compiler.validation.ValidatorState) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Example 4 with AggregationAwareSetImplementation

use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.aggregationAware.AggregationAwareSetImplementation 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 5 with AggregationAwareSetImplementation

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

Aggregations

AggregationAwareSetImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.aggregationAware.AggregationAwareSetImplementation)13 InstanceSetImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.InstanceSetImplementation)11 SetImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.SetImplementation)11 Test (org.junit.Test)9 PureInstanceSetImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation)6 RootRelationalInstanceSetImplementation (org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.RootRelationalInstanceSetImplementation)4 PureModel (org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel)3 AggregateSetImplementationContainer (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.aggregationAware.AggregateSetImplementationContainer)2 AggregateSetImplementationContainer (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.aggregationAware.AggregateSetImplementationContainer)2 Collections (java.util.Collections)1 List (java.util.List)1 Objects (java.util.Objects)1 RichIterable (org.eclipse.collections.api.RichIterable)1 Function (org.eclipse.collections.api.block.function.Function)1 Function2 (org.eclipse.collections.api.block.function.Function2)1 Function3 (org.eclipse.collections.api.block.function.Function3)1 IntObjectToIntFunction (org.eclipse.collections.api.block.function.primitive.IntObjectToIntFunction)1 Procedure (org.eclipse.collections.api.block.procedure.Procedure)1 Procedure2 (org.eclipse.collections.api.block.procedure.Procedure2)1 Lists (org.eclipse.collections.api.factory.Lists)1