use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunctionInstance in project legend-pure by finos.
the class FunctionExpressionProcessor method buildLambdaForMapWithProperty.
private static LambdaFunction<?> buildLambdaForMapWithProperty(FunctionExpression functionExpression, ListIterable<? extends ValueSpecification> qualifierParams, String propertyOrQualifiedPropertyNameProperty, GenericType sourceGenericType, ModelRepository repository, ProcessorSupport processorSupport) {
VariableExpression lambdaVarExpr = buildLambdaVariableExpression(functionExpression, repository, processorSupport);
GenericType varExpGenT = (GenericType) org.finos.legend.pure.m3.navigation.generictype.GenericType.copyGenericTypeAsInferredGenericType(sourceGenericType, functionExpression.getSourceInformation(), processorSupport);
lambdaVarExpr._genericType(varExpGenT);
lambdaVarExpr._multiplicity((Multiplicity) processorSupport.package_getByUserPath(M3Paths.PureOne));
FunctionType functionType = (FunctionType) repository.newAnonymousCoreInstance(functionExpression.getSourceInformation(), processorSupport.package_getByUserPath(M3Paths.FunctionType), true);
functionType._parameters(Lists.immutable.with(lambdaVarExpr));
GenericType functionTypeGt = (GenericType) repository.newAnonymousCoreInstance(functionExpression.getSourceInformation(), processorSupport.package_getByUserPath(M3Paths.GenericType), true);
functionTypeGt._rawTypeCoreInstance(functionType);
ClassInstance lambdaFunctionClass = (ClassInstance) processorSupport.package_getByUserPath(M3Paths.LambdaFunction);
GenericType lambdaGenericType = (GenericType) org.finos.legend.pure.m3.navigation.type.Type.wrapGenericType(lambdaFunctionClass, processorSupport);
lambdaGenericType._typeArguments(Lists.immutable.with(functionTypeGt));
VariableExpression paramVarExpr = buildLambdaVariableExpression(functionExpression, repository, processorSupport);
SimpleFunctionExpression propertySfe = (SimpleFunctionExpression) repository.newAnonymousCoreInstance(functionExpression.getSourceInformation(), processorSupport.package_getByUserPath(M3Paths.SimpleFunctionExpression), true);
String lambdaContextName;
if (M3Properties.qualifiedPropertyName.equals(propertyOrQualifiedPropertyNameProperty)) {
lambdaContextName = functionExpression._qualifiedPropertyName().getName();
propertySfe._qualifiedPropertyName(functionExpression._qualifiedPropertyName());
} else if (M3Properties.propertyName.equals(propertyOrQualifiedPropertyNameProperty)) {
lambdaContextName = functionExpression._propertyName().getName();
propertySfe._propertyName(functionExpression._propertyName());
} else {
throw new PureCompilationException("Unexpected property name" + propertyOrQualifiedPropertyNameProperty);
}
propertySfe._importGroup(functionExpression._importGroup());
propertySfe._parametersValues(Lists.immutable.<ValueSpecification>with(paramVarExpr).newWithAll(qualifierParams));
LambdaFunctionInstance lambdaFunctionInst = LambdaFunctionInstance.createPersistent(repository, lambdaContextName, functionExpression.getSourceInformation());
lambdaFunctionInst._expressionSequence(Lists.immutable.with(propertySfe));
lambdaFunctionInst._classifierGenericType(lambdaGenericType);
return lambdaFunctionInst;
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunctionInstance in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method lambdaPipe.
private Any lambdaPipe(LambdaPipeContext ctx, Token firstToken, ListIterable<VariableExpression> params, MutableList<String> typeParametersNames, LambdaContext lambdaContext, String space, boolean wrapFlag, ImportGroup importId, boolean addLines) {
Token lambdaStartToken = firstToken != null ? firstToken : ctx.PIPE().getSymbol();
ListIterable<ValueSpecification> block = codeBlock(ctx.codeBlock(), typeParametersNames, importId, lambdaContext, addLines, spacePlusTabs(space, 6));
FunctionTypeInstance signature = FunctionTypeInstance.createPersistent(this.repository, this.sourceInformation.getPureSourceInformation(lambdaStartToken), null, null);
if (Iterate.notEmpty(params)) {
signature._parameters(params);
}
// Note: we cannot set the function of the signature FunctionType, as this can cause stack overflow if serializing to M4
GenericTypeInstance genericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
Type type = (Type) this.processorSupport.package_getByUserPath(M3Paths.LambdaFunction);
genericTypeInstance._rawTypeCoreInstance(type);
GenericTypeInstance genericTypeInstanceTa = GenericTypeInstance.createPersistent(this.repository);
genericTypeInstanceTa._rawTypeCoreInstance(signature);
genericTypeInstance._typeArguments(Lists.mutable.<GenericType>of(genericTypeInstanceTa));
LambdaFunctionInstance lambdaFunction = LambdaFunctionInstance.createPersistent(this.repository, lambdaContext.getLambdaFunctionUniqueName(), this.sourceInformation.getPureSourceInformation(lambdaStartToken));
lambdaFunction._classifierGenericType(genericTypeInstance);
lambdaFunction._expressionSequence(block);
return wrapFlag ? this.doWrap(lambdaFunction, lambdaStartToken) : lambdaFunction;
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunctionInstance in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method unitParser.
/**
* Helps build the unitInstance for any canonical and non-canonical units and returns the parsed unitInstance.
*/
private UnitInstance unitParser(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.MeasureExprContext ctx, ImportGroup importId, MeasureInstance measureInstance, org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.MeasureDefinitionContext mctx) throws PureParserException {
UnitInstance unitInstance;
MutableList<GenericType> superTypesGenericTypes = Lists.mutable.empty();
this.checkExists(ctx.qualifiedName().packagePath(), ctx.qualifiedName().identifier(), null);
String unitName = mctx.qualifiedName().identifier().getText().concat(this.tilde).concat(ctx.qualifiedName().identifier().getText());
unitInstance = UnitInstance.createPersistent(this.repository, unitName);
PackageInstance packageInstance = buildPackage(Lists.mutable.withAll(mctx.qualifiedName().packagePath() == null ? Lists.mutable.empty() : ListAdapter.adapt(mctx.qualifiedName().packagePath().identifier())));
unitInstance._package(packageInstance);
packageInstance._childrenAdd(unitInstance);
unitInstance.setSourceInformation(this.sourceInformation.getPureSourceInformation(ctx.getStart(), ctx.qualifiedName().identifier().getStart(), ctx.getStop()));
GenericTypeInstance classifierGT = GenericTypeInstance.createPersistent(this.repository);
ClassInstance unitType = (ClassInstance) this.processorSupport.package_getByUserPath(M3Paths.Unit);
classifierGT._rawTypeCoreInstance(unitType);
unitInstance._classifierGenericType(classifierGT);
unitInstance._name(unitName);
if (superTypesGenericTypes.isEmpty()) {
GenericTypeInstance genericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
// set unit super type to be its measure (Kilogram -> Mass)
genericTypeInstance._rawTypeCoreInstance(measureInstance);
superTypesGenericTypes.add(genericTypeInstance);
}
MutableList<Generalization> generalizations = Lists.mutable.empty();
for (GenericType superType : superTypesGenericTypes) {
GeneralizationInstance generalizationInstance = GeneralizationInstance.createPersistent(this.repository, superType, unitInstance);
generalizations.add(generalizationInstance);
}
unitInstance._generalizations(generalizations);
unitInstance._measure(measureInstance);
// prepare lambda instance for the conversion function
String fullName = this.getQualifiedNameString(ctx.qualifiedName());
LambdaContext lambdaContext = new LambdaContext(fullName.replace("::", "_"));
MutableList<String> typeParametersNames = Lists.mutable.empty();
FunctionTypeInstance signature = FunctionTypeInstance.createPersistent(this.repository, this.sourceInformation.getPureSourceInformation(ctx.unitExpr().getStart(), ctx.unitExpr().getStart(), ctx.unitExpr().getStop()), null, null);
// prepare params
VariableExpression expr = this.lambdaParam(null, ctx.unitExpr().identifier(), typeParametersNames, "", importId);
expr._multiplicity(this.getPureOne());
expr._functionTypeOwner(signature);
GenericTypeInstance paramGenericType = GenericTypeInstance.createPersistent(this.repository);
CoreInstance paramType = this.processorSupport.package_getByUserPath(M3Paths.Number);
paramGenericType._rawTypeCoreInstance(paramType);
expr._genericType(paramGenericType);
signature._parameters(Lists.mutable.with(expr));
GenericTypeInstance genericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
Type type = (Type) this.processorSupport.package_getByUserPath(M3Paths.LambdaFunction);
genericTypeInstance._rawTypeCoreInstance(type);
GenericTypeInstance genericTypeInstanceTa = GenericTypeInstance.createPersistent(this.repository);
genericTypeInstanceTa._rawTypeCoreInstance(signature);
genericTypeInstance._typeArguments(Lists.mutable.<GenericType>of(genericTypeInstanceTa));
LambdaFunctionInstance lambdaFunctionInstance = LambdaFunctionInstance.createPersistent(this.repository, lambdaContext.getLambdaFunctionUniqueName(), this.sourceInformation.getPureSourceInformation(ctx.unitExpr().ARROW().getSymbol()));
lambdaFunctionInstance._classifierGenericType(genericTypeInstance);
signature._functionAdd(lambdaFunctionInstance);
ListIterable<ValueSpecification> block = this.codeBlock(ctx.unitExpr().codeBlock(), typeParametersNames, importId, lambdaContext, addLines, tabs(6));
lambdaFunctionInstance._expressionSequence(block);
unitInstance._conversionFunction(lambdaFunctionInstance);
return unitInstance;
}
Aggregations