use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PurePropertyMapping 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.PurePropertyMapping in project legend-pure by finos.
the class PureInstanceSetImplementationProcessor method process.
@Override
public void process(PureInstanceSetImplementation classMapping, ProcessorState state, Matcher matcher, ModelRepository repository, Context context, ProcessorSupport processorSupport) {
Type srcClass = (Type) ImportStub.withImportStubByPass(classMapping._srcClassCoreInstance(), processorSupport);
SourceInformation srcGenericTypeSourceInfo = null;
LambdaFunction<?> filter = classMapping._filter();
if (filter != null) {
state.pushVariableContext();
if (srcClass != null) {
FunctionType fType = getFunctionType(filter, processorSupport);
fType._parameters(Lists.mutable.<VariableExpression>withAll(fType._parameters()).with(createSrcParameter(srcClass, srcGenericTypeSourceInfo, filter.getSourceInformation(), processorSupport)));
}
matcher.fullMatch(filter, state);
state.popVariableContext();
}
int i = 0;
for (PropertyMapping propertyMapping : classMapping._propertyMappings()) {
state.pushVariableContext();
if (((PurePropertyMapping) propertyMapping)._transformerCoreInstance() != null) {
GrammarInfoStub transformerStub = (GrammarInfoStub) ((PurePropertyMapping) propertyMapping)._transformerCoreInstance();
EnumerationMappingProcessor.processsEnumerationTransformer(transformerStub, propertyMapping, processorSupport);
}
LambdaFunction<?> transform = ((PurePropertyMapping) propertyMapping)._transform();
if (srcClass != null) {
FunctionType fType = getFunctionType(transform, processorSupport);
fType._parameters(Lists.mutable.<VariableExpression>withAll(fType._parameters()).with(createSrcParameter(srcClass, srcGenericTypeSourceInfo, propertyMapping.getSourceInformation(), processorSupport)));
}
matcher.fullMatch(transform, state);
ValueSpecification expressionSequence = transform._expressionSequence().getFirst();
if (expressionSequence != null) {
PropertyMappingValueSpecificationContext usageContext = (PropertyMappingValueSpecificationContext) processorSupport.newAnonymousCoreInstance(null, M2MappingPaths.PropertyMappingValueSpecificationContext);
usageContext._offset(i);
usageContext._propertyMapping(propertyMapping);
expressionSequence._usageContext(usageContext);
}
i++;
state.popVariableContext();
}
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PurePropertyMapping in project legend-pure by finos.
the class PureInstanceSetImplementationUnbind method run.
@Override
public void run(PureInstanceSetImplementation pureInstanceSetImplementation, MatcherState state, Matcher matcher, ModelRepository modelRepository, Context context) throws PureCompilationException {
ImportStub srcClass = (ImportStub) pureInstanceSetImplementation._srcClassCoreInstance();
if (srcClass != null) {
Shared.cleanUpReferenceUsage(srcClass, pureInstanceSetImplementation, state.getProcessorSupport());
Shared.cleanImportStub(srcClass, state.getProcessorSupport());
}
LambdaFunction filter = pureInstanceSetImplementation._filter();
if (filter != null) {
matcher.fullMatch(filter, state);
((FunctionType) org.finos.legend.pure.m3.navigation.importstub.ImportStub.withImportStubByPass(filter._classifierGenericType()._typeArguments().toList().get(0)._rawTypeCoreInstance(), state.getProcessorSupport()))._parametersRemove();
}
for (PropertyMapping propertyMapping : pureInstanceSetImplementation._propertyMappings()) {
Shared.cleanPropertyStub(propertyMapping._propertyCoreInstance(), state.getProcessorSupport());
LambdaFunction transform = ((PurePropertyMapping) propertyMapping)._transform();
matcher.fullMatch(transform, state);
FunctionType functionType = (FunctionType) org.finos.legend.pure.m3.navigation.importstub.ImportStub.withImportStubByPass(transform._classifierGenericType()._typeArguments().toList().get(0)._rawTypeCoreInstance(), state.getProcessorSupport());
functionType._parametersRemove();
functionType._functionRemove();
if (((PurePropertyMapping) propertyMapping)._transformerCoreInstance() != null) {
GrammarInfoStub transformerStub = (GrammarInfoStub) ((PurePropertyMapping) propertyMapping)._transformerCoreInstance();
transformerStub._value(transformerStub._original());
transformerStub._originalRemove();
}
}
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PurePropertyMapping in project legend-engine by finos.
the class HelperMappingBuilder method processPurePropertyMappingTransform.
public static LambdaFunction processPurePropertyMappingTransform(PurePropertyMapping ppm, Lambda lambda, PropertyMappingsImplementation owner, org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type inputVarType, CompileContext context, String mappingName) {
List<ValueSpecification> expressions = lambda.body;
VariableExpression lambdaParam = new Root_meta_pure_metamodel_valuespecification_VariableExpression_Impl("")._name("src")._multiplicity(context.pureModel.getMultiplicity("one"))._genericType(new Root_meta_pure_metamodel_type_generics_GenericType_Impl("")._rawType(inputVarType));
MutableList<VariableExpression> pureParameters = Lists.mutable.with(lambdaParam);
ProcessingContext ctx = new ProcessingContext("Pure M2M Transform Lambda");
ctx.addInferredVariables("src", lambdaParam);
MutableList<String> openVariables = Lists.mutable.empty();
MutableList<org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification> valueSpecifications = ListIterate.collect(expressions, p -> p.accept(new ValueSpecificationBuilder(context, openVariables, ctx)));
MutableList<String> cleanedOpenVariables = openVariables.distinct();
cleanedOpenVariables.removeAll(pureParameters.collect(e -> e._name()));
GenericType functionType = context.pureModel.buildFunctionType(pureParameters, valueSpecifications.getLast()._genericType(), valueSpecifications.getLast()._multiplicity());
String propertyName = owner._id() + "." + ppm.property.property;
String mappingPath = Root_meta_pure_functions_meta_elementToPath_PackageableElement_1__String_1_(owner._parent(), context.pureModel.getExecutionSupport()).replace("::", "_");
ctx.flushVariable("src");
return new Root_meta_pure_metamodel_function_LambdaFunction_Impl(propertyName, new SourceInformation(mappingPath, 0, 0, 0, 0), null)._classifierGenericType(new Root_meta_pure_metamodel_type_generics_GenericType_Impl("")._rawType(context.pureModel.getType("meta::pure::metamodel::function::LambdaFunction"))._typeArguments(FastList.newListWith(functionType)))._openVariables(cleanedOpenVariables)._expressionSequence(valueSpecifications);
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PurePropertyMapping in project legend-engine by finos.
the class ClassMappingSecondPassBuilder method visit.
@Override
public SetImplementation visit(PureInstanceClassMapping classMapping) {
PureInstanceSetImplementation s = (PureInstanceSetImplementation) parentMapping._classMappings().select(c -> c._id().equals(HelperMappingBuilder.getClassMappingId(classMapping, this.context))).getFirst();
s._propertyMappings().forEachWithIndex((ObjectIntProcedure<org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.PropertyMapping>) ((p, i) -> {
org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PurePropertyMapping pm = (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PurePropertyMapping) p;
org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property<?, ?> property = pm._property();
SourceInformation pSourceInformation = SourceInformationHelper.fromM3SourceInformation(p.getSourceInformation());
org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification last = pm._transform()._expressionSequence().getLast();
List<Type> typesToCheck;
if (property._genericType()._rawType()._classifierGenericType()._rawType()._name().equals("Class")) {
SetImplementation setImplementation;
if (p._targetSetImplementationId() != null && !p._targetSetImplementationId().equals("")) {
setImplementation = parentMapping._classMappingByIdRecursive(Lists.fixedSize.with(p._targetSetImplementationId()), this.context.pureModel.getExecutionSupport()).getFirst();
Assert.assertTrue(setImplementation != null, () -> "Can't find class mapping '" + p._targetSetImplementationId() + "'", pSourceInformation, EngineErrorType.COMPILATION);
} else {
setImplementation = parentMapping.classMappingByClass((org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Class<Object>) property._genericType()._rawType(), this.context.pureModel.getExecutionSupport()).getFirst();
Assert.assertTrue(setImplementation != null, () -> "Can't find class mapping for '" + HelperModelBuilder.getElementFullPath(property._genericType()._rawType(), this.context.pureModel.getExecutionSupport()) + "'", pSourceInformation, EngineErrorType.COMPILATION);
}
List<? extends InstanceSetImplementation> setImpls = core_pure_router_router_routing.Root_meta_pure_router_routing_resolveOperation_SetImplementation_MANY__Mapping_1__InstanceSetImplementation_MANY_(Lists.immutable.of(setImplementation), parentMapping, this.context.pureModel.getExecutionSupport()).toList();
typesToCheck = setImpls.stream().map(setImpl -> ((PureInstanceSetImplementation) setImpl)._srcClass()).collect(Collectors.toList());
} else if (((org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PurePropertyMapping) p)._transformer() != null) {
EnumerationMapping<Object> m = ((EnumerationMapping<Object>) ((org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PurePropertyMapping) p)._transformer());
Object val = m._enumValueMappings().getFirst()._sourceValues().getFirst();
if (val instanceof String) {
typesToCheck = Collections.singletonList(this.context.pureModel.getType("String"));
} else if (val instanceof Long) {
typesToCheck = Collections.singletonList(this.context.pureModel.getType("Integer"));
} else if (val instanceof EnumValue) {
typesToCheck = Collections.singletonList(this.context.resolveEnumeration(((EnumValue) val).fullPath, pSourceInformation));
} else if (val instanceof Enum) {
GenericType genericType = ((Enum) val)._classifierGenericType();
typesToCheck = genericType != null ? Collections.singletonList(this.context.resolveEnumeration(PackageableElement.getUserPathForPackageableElement(genericType._rawType()), pSourceInformation)) : Collections.emptyList();
} else {
typesToCheck = Collections.emptyList();
}
} else {
typesToCheck = Collections.singletonList(property._genericType()._rawType());
}
org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity multiplicityToCheck = pm._explodeProperty() != null && pm._explodeProperty() ? this.context.pureModel.getMultiplicity("zeromany") : property._multiplicity();
List<ValueSpecification> lines = ((PurePropertyMapping) classMapping.propertyMappings.get(i)).transform.body;
typesToCheck.stream().filter(Objects::nonNull).forEach(t -> HelperModelBuilder.checkTypeCompatibility(this.context, last._genericType()._rawType(), t, "Error in class mapping '" + HelperModelBuilder.getElementFullPath(parentMapping, this.context.pureModel.getExecutionSupport()) + "' for property '" + pm._property()._name() + "'", lines.get(lines.size() - 1).sourceInformation));
HelperModelBuilder.checkMultiplicityCompatibility(last._multiplicity(), multiplicityToCheck, "Error in class mapping '" + HelperModelBuilder.getElementFullPath(parentMapping, this.context.pureModel.getExecutionSupport()) + "' for property '" + pm._property()._name() + "'", lines.get(lines.size() - 1).sourceInformation);
}));
return s;
}
Aggregations