Search in sources :

Example 1 with InstanceSetImplementation

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

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

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

the class MappingValidator method validateAssociationMappings.

private static void validateAssociationMappings(Mapping mapping, final ProcessorSupport processorSupport) {
    RichIterable<? extends AssociationImplementation> associationMappings = mapping._associationMappings();
    if (!associationMappings.isEmpty()) {
        MapIterable<String, ? extends SetImplementation> classMappingIndex = org.finos.legend.pure.m2.dsl.mapping.Mapping.getClassMappingsByIdIncludeEmbedded(mapping, processorSupport);
        // Add association mappings to Class mappings
        for (AssociationImplementation associationMapping : associationMappings) {
            for (PropertyMapping propertyMapping : associationMapping._propertyMappings()) {
                Property property = (Property) ImportStub.withImportStubByPass(propertyMapping._propertyCoreInstance(), processorSupport);
                final String propertyName = property.getName();
                SetImplementation sourceClassMapping = validateId(associationMapping, propertyMapping, classMappingIndex, propertyMapping._sourceSetImplementationId(), "source", processorSupport);
                String targetId = propertyMapping._targetSetImplementationId();
                SetImplementation targetClassMapping = validateId(associationMapping, propertyMapping, classMappingIndex, propertyMapping._targetSetImplementationId(), "target", processorSupport);
                if (targetClassMapping instanceof EmbeddedSetImplementation) {
                    throw new PureCompilationException(propertyMapping.getSourceInformation(), "Invalid target class mapping for property \'" + propertyName + "\' in Association mapping \'" + ImportStub.withImportStubByPass(associationMapping._associationCoreInstance(), processorSupport).getName() + "\'. Target \'" + targetId + "\' is an embedded class mapping, embedded mappings are only allowed to be the source in an Association Mapping.");
                }
                ListIterable<? extends PropertyMapping> sourcePropertyMappings = ((InstanceSetImplementation) sourceClassMapping)._propertyMappings().toList();
                PropertyMapping alreadyMapped = sourcePropertyMappings.detect(new Predicate<PropertyMapping>() {

                    @Override
                    public boolean accept(PropertyMapping propertyMapping) {
                        return propertyName.equals(ImportStub.withImportStubByPass(propertyMapping._propertyCoreInstance(), processorSupport).getName());
                    }
                });
                if (alreadyMapped != null) {
                    throw new PureCompilationException(propertyMapping.getSourceInformation(), "Property \'" + propertyName + "\' is mapped twice, once in Association mapping \'" + ImportStub.withImportStubByPass(associationMapping._associationCoreInstance(), processorSupport).getName() + "\' and once in Class mapping \'" + ((Class) ImportStub.withImportStubByPass(targetClassMapping._classCoreInstance(), processorSupport))._name() + "\'. Only one mapping is allowed.");
                }
            }
        }
    }
}
Also used : PureInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation) Property(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Example 4 with InstanceSetImplementation

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

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

InstanceSetImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.InstanceSetImplementation)19 SetImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.SetImplementation)16 AggregationAwareSetImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.aggregationAware.AggregationAwareSetImplementation)12 PureInstanceSetImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation)9 Test (org.junit.Test)9 Property (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property)6 GenericType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType)6 Mapping (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.Mapping)5 PropertyMapping (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.PropertyMapping)5 Class (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Class)5 RootRelationalInstanceSetImplementation (org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.RootRelationalInstanceSetImplementation)5 RichIterable (org.eclipse.collections.api.RichIterable)4 EngineErrorType (org.finos.legend.engine.protocol.pure.v1.model.context.EngineErrorType)4 MappingClass (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.MappingClass)4 Optional (java.util.Optional)3 MutableList (org.eclipse.collections.api.list.MutableList)3 Lists (org.eclipse.collections.impl.factory.Lists)3 FastList (org.eclipse.collections.impl.list.mutable.FastList)3 ListIterate (org.eclipse.collections.impl.utility.ListIterate)3 PureModel (org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel)3