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");
}
});
}
}
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());
// }
}
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());
}
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());
}
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());
}
Aggregations