Search in sources :

Example 1 with LambdaFunctionInstance

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;
}
Also used : GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) SimpleFunctionExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.SimpleFunctionExpression) LambdaFunctionInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunctionInstance) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) VariableExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression) ClassInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.ClassInstance) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Example 2 with LambdaFunctionInstance

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;
}
Also used : GenericTypeInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericTypeInstance) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) Type(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) FunctionTypeInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionTypeInstance) LambdaFunctionInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunctionInstance) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) Token(org.antlr.v4.runtime.Token)

Example 3 with LambdaFunctionInstance

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;
}
Also used : GenericTypeInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericTypeInstance) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) FunctionTypeInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionTypeInstance) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) UnitInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.UnitInstance) VariableExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression) Generalization(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.Generalization) PackageInstance(org.finos.legend.pure.m3.coreinstance.PackageInstance) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) Type(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) GeneralizationInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.GeneralizationInstance) LambdaFunctionInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunctionInstance) PrimitiveCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) ClassInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.ClassInstance)

Aggregations

LambdaFunctionInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunctionInstance)3 FunctionType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType)3 GenericType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType)3 ValueSpecification (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification)3 ClassInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.ClassInstance)2 FunctionTypeInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionTypeInstance)2 Type (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type)2 GenericTypeInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericTypeInstance)2 VariableExpression (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression)2 Token (org.antlr.v4.runtime.Token)1 PackageInstance (org.finos.legend.pure.m3.coreinstance.PackageInstance)1 Generalization (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.Generalization)1 GeneralizationInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.GeneralizationInstance)1 UnitInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.UnitInstance)1 SimpleFunctionExpression (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.SimpleFunctionExpression)1 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)1 PrimitiveCoreInstance (org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance)1 PureCompilationException (org.finos.legend.pure.m4.exception.PureCompilationException)1