use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.SetImplementation in project legend-pure by finos.
the class TestEmbeddedGrammar method testEmbeddedMappingWithIds.
@Test
public void testEmbeddedMappingWithIds() {
Loader.parseM3("import other::*;\n" + "import meta::pure::mapping::*;\n" + "\n" + "Class other::Person\n" + "{\n" + " name:String[1];\n" + " firm:Firm[1];\n" + " address:Address[0..1];\n" + "}\n" + "Class other::Firm\n" + "{\n" + " legalName:String[1];\n" + " employees:Person[1];\n" + " address:Address[1];\n" + "}\n" + "Class other::Address\n" + "{\n" + " line1:String[1];\n" + "}\n" + " function meta::pure::router::operations::union(o:OperationSetImplementation[1]):SetImplementation[*]\n" + " {\n" + " $o.parameters.setImplementation;\n" + " }\n" + "###Relational\n" + "Database mapping::db(\n" + " Table employeeFirmDenormTable\n" + " (\n" + " id INT PRIMARY KEY,\n" + " name VARCHAR(200),\n" + " firmId INT,\n" + " legalName VARCHAR(200),\n" + " address VARCHAR(200)\n" + " )\n" + " Table employeeFirmDenormTable2\n" + " (\n" + " id INT PRIMARY KEY,\n" + " name VARCHAR(200),\n" + " firmId INT,\n" + " legalName VARCHAR(200),\n" + " address VARCHAR(200)\n" + " )\n" + ")\n" + "###Mapping\n" + "import other::*;\n" + "import mapping::*;\n" + "Mapping mappingPackage::myMapping\n" + "(\n" + " Person[per1]: Relational\n" + " {\n" + " name : [db]employeeFirmDenormTable.name,\n" + " firm\n" + " (\n" + " ~primaryKey ([db]employeeFirmDenormTable.legalName)\n" + " legalName : [db]employeeFirmDenormTable.legalName,\n" + " address\n" + " (\n" + " line1: [db]employeeFirmDenormTable.address\n" + " )\n" + " ),\n" + " address\n" + " (\n" + " line1: [db]employeeFirmDenormTable.address\n" + " )\n" + " }\n" + " Person[per2]: Relational\n" + " {\n" + " name : [db]employeeFirmDenormTable2.name,\n" + " firm\n" + " (\n" + " ~primaryKey ([db]employeeFirmDenormTable2.legalName)\n" + " legalName : [db]employeeFirmDenormTable2.legalName,\n" + " address\n" + " (\n" + " line1: [db]employeeFirmDenormTable2.address\n" + " )\n" + " ),\n" + " address\n" + " (\n" + " line1: [db]employeeFirmDenormTable2.address\n" + " )\n" + " }\n" + " *Person : Operation\n" + " {\n" + " meta::pure::router::operations::union_OperationSetImplementation_1__SetImplementation_MANY_( per1, per2 ) \n" + " }\n" + ")\n", this.repository, new ParserLibrary(Lists.immutable.with(new M3AntlrParser(), new MappingParser(), new RelationalParser(), new OperationParser())), ValidationType.DEEP, VoidM3M4StateListener.VOID_M3_M4_STATE_LISTENER, this.context);
this.runtime.compile();
CoreInstance mapping = this.graphWalker.getMapping("mappingPackage::myMapping");
assertNotNull(mapping);
assertEquals(9, this.graphWalker.getClassMappings(mapping).size());
CoreInstance personMapping = this.graphWalker.getClassMappingById(mapping, "per1");
validatePersonMapping(personMapping, "employeeFirmDenormTable");
CoreInstance personMapping2 = this.graphWalker.getClassMappingById(mapping, "per2");
validatePersonMapping(personMapping2, "employeeFirmDenormTable2");
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.SetImplementation 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());
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.SetImplementation 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());
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.SetImplementation in project legend-pure by finos.
the class MappingValidator method validateId.
public static SetImplementation validateId(AssociationImplementation associationMapping, PropertyMapping propertyMapping, MapIterable<String, ? extends SetImplementation> classMappingIndex, String setImplementationId, String sourceOrTarget, ProcessorSupport processorSupport) {
SetImplementation setImplementation = classMappingIndex.get(setImplementationId);
Property property = (Property) ImportStub.withImportStubByPass(propertyMapping._propertyCoreInstance(), processorSupport);
if ("target".equals(sourceOrTarget)) {
// Target, so need to find the property on the other end
Association association = (Association) ImportStub.withImportStubByPass(associationMapping._associationCoreInstance(), processorSupport);
property = association._properties().select(Predicates.notEqual(property)).getFirst();
}
if (setImplementation == null) {
throw new PureCompilationException(propertyMapping.getSourceInformation(), "Unable to find " + sourceOrTarget + " class mapping (id:" + setImplementationId + ") for property \'" + propertyMapping._propertyCoreInstance().getName() + "\' in Association mapping \'" + ImportStub.withImportStubByPass(associationMapping._associationCoreInstance(), processorSupport).getName() + "\'. Make sure that you have specified a valid Class mapping id as the source id and target id, using the syntax \'property[sourceId, targetId]: ...\'.");
}
Class _class = (Class) ImportStub.withImportStubByPass(setImplementation._classCoreInstance(), processorSupport);
String propertyName = property.getName();
if (processorSupport.class_findPropertyUsingGeneralization(_class, propertyName) == null) {
throw new PureCompilationException(propertyMapping.getSourceInformation(), "Association mapping property \'" + propertyName + "\' in Association mapping \'" + ImportStub.withImportStubByPass(associationMapping._associationCoreInstance(), processorSupport).getName() + "\' is not a property of source class \'" + _class.getName() + "\'. Make sure that you have specified a valid source id.");
}
return setImplementation;
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.SetImplementation 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.");
}
}
}
}
}
Aggregations