Search in sources :

Example 1 with FunctionTypeInstance

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionTypeInstance in project legend-pure by finos.

the class AntlrContextToM3CoreInstance method qualifiedProperty.

private void qualifiedProperty(QualifiedPropertyContext ctx, MutableList<QualifiedProperty<? extends CoreInstance>> qualifiedProperties, MutableList<String> typeParameterNames, MutableList<String> multiplicityParameterNames, ImportStub isOwner, ImportGroup importId, boolean addLines, int qualifiedPropertyIndex) {
    ListIterable<CoreInstance> stereotypes = (ctx.stereotypes() == null) ? null : stereotypes(ctx.stereotypes(), importId);
    ListIterable<TaggedValue> tags = (ctx.taggedValues() == null) ? null : taggedValues(ctx.taggedValues(), importId);
    MutableList<VariableExpression> vars = Lists.mutable.of();
    ListIterable<ValueSpecification> code = Lists.fixedSize.empty();
    GenericType genericType = this.type(ctx.propertyReturnType().type(), typeParameterNames, "", importId, addLines);
    Multiplicity multiplicity = this.buildMultiplicity(ctx.propertyReturnType().multiplicity().multiplicityArgument());
    String propertyName = ctx.identifier().getText();
    final String qualifiedPropertyName = propertyName + "_" + qualifiedPropertyIndex;
    if (ctx.qualifiedPropertyBody() != null) {
        if (ctx.qualifiedPropertyBody().functionVariableExpression() != null) {
            ListIterate.collect(ctx.qualifiedPropertyBody().functionVariableExpression(), fveCtx -> functionVariableExpression(fveCtx, typeParameterNames, importId, ""), vars);
        }
        if (ctx.qualifiedPropertyBody().codeBlock() != null) {
            LambdaContext lambdaContext = new LambdaContext(isOwner._idOrPath().replace("::", "_") + "_" + qualifiedPropertyName);
            code = this.codeBlock(ctx.qualifiedPropertyBody().codeBlock(), typeParameterNames, importId, lambdaContext, addLines, "");
        }
    }
    GenericTypeInstance variableGenericType = GenericTypeInstance.createPersistent(this.repository);
    variableGenericType._rawTypeCoreInstance(isOwner);
    if (typeParameterNames.notEmpty()) {
        MutableList<GenericType> typeArgs = typeParameterNames.collect(n -> GenericTypeInstance.createPersistent(this.repository)._typeParameter(TypeParameterInstance.createPersistent(this.repository, n)));
        variableGenericType._typeArguments(typeArgs);
    }
    if (multiplicityParameterNames.notEmpty()) {
        MutableList<Multiplicity> multParameters = multiplicityParameterNames.collect(n -> MultiplicityInstance.createPersistent(this.repository, null, null)._multiplicityParameter(n));
        variableGenericType._multiplicityArguments(multParameters);
    }
    VariableExpressionInstance vei = VariableExpressionInstance.createPersistent(this.repository, variableGenericType, this.getPureOne(), "this");
    GenericTypeInstance ngt = GenericTypeInstance.createPersistent(this.repository);
    CoreInstance rawType = genericType._rawTypeCoreInstance();
    if (rawType != null) {
        if (rawType instanceof ImportStub) {
            ImportStub gtis = (ImportStub) rawType;
            ImportStubInstance is = ImportStubInstance.createPersistent(this.repository, gtis.getSourceInformation(), gtis._idOrPath(), gtis._importGroup());
            // ImportStubInstance is = ImportStubInstance.createPersistent(this.repository, gtis.getSourceInformation(), ((ImportStubInstance)gtis)._idOrPathAsCoreInstance().getName(), (ImportGroup)gtis._importGroup());
            ngt._rawTypeCoreInstance(is);
        } else {
            ngt._rawTypeCoreInstance(rawType);
        }
    }
    if (!genericType._typeArguments().isEmpty()) {
        ngt._typeArguments(genericType._typeArguments());
    }
    ngt._typeParameter(genericType._typeParameter());
    if (!genericType._multiplicityArguments().isEmpty()) {
        ngt._multiplicityArguments(genericType._multiplicityArguments());
    }
    QualifiedPropertyInstance qpi = QualifiedPropertyInstance.createPersistent(this.repository, qualifiedPropertyName, this.sourceInformation.getPureSourceInformation(ctx.identifier().getStart(), ctx.identifier().getStart(), ctx.getStop()), genericType, multiplicity, null);
    qpi._name(propertyName);
    qpi._functionName(propertyName);
    qpi._expressionSequence(code);
    qpi._stereotypesCoreInstance(stereotypes);
    qpi._taggedValues(tags);
    FunctionTypeInstance ft = FunctionTypeInstance.createPersistent(this.repository, qpi.getSourceInformation(), multiplicity, ngt);
    if (!typeParameterNames.isEmpty()) {
        ft._typeParameters(this.processTypeParametersInstance(this.repository, typeParameterNames));
    }
    ft._parameters(Lists.mutable.<VariableExpression>of(vei).withAll(vars));
    GenericTypeInstance ftGenericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
    ftGenericTypeInstance._rawTypeCoreInstance(ft);
    GenericTypeInstance genericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
    genericTypeInstance._rawTypeCoreInstance(this.processorSupport.package_getByUserPath(M3Paths.QualifiedProperty));
    genericTypeInstance._typeArguments(Lists.fixedSize.<GenericType>of(ftGenericTypeInstance));
    qpi._classifierGenericType(genericTypeInstance);
    qualifiedProperties.add(qpi);
}
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) TaggedValue(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.extension.TaggedValue) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) VariableExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression) ImportStub(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.ImportStub) Multiplicity(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity) PrimitiveCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) VariableExpressionInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpressionInstance) QualifiedPropertyInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedPropertyInstance) ImportStubInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.ImportStubInstance)

Example 2 with FunctionTypeInstance

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionTypeInstance 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 FunctionTypeInstance

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionTypeInstance 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)

Example 4 with FunctionTypeInstance

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionTypeInstance in project legend-pure by finos.

the class AntlrContextToM3CoreInstance method type.

public GenericType type(TypeContext ctx, MutableList<String> typeParametersNames, String space, ImportGroup importId, boolean addLines) {
    if (ctx.qualifiedName() != null) {
        ListIterable<GenericType> renderedTypeArguments = this.typeArguments(ctx.typeArguments(), typeParametersNames, importId, addLines);
        ListIterable<Multiplicity> renderedMultiplicityArguments = this.multiplicityArguments(ctx.multiplicityArguments());
        return this.processType(ctx.qualifiedName(), typeParametersNames, renderedTypeArguments, renderedMultiplicityArguments, importId);
    }
    if (ctx.unitName() != null) {
        return this.processUnitType(ctx.unitName(), importId);
    }
    GenericType returnType = this.type(ctx.type(), typeParametersNames, spacePlusTabs(space, 5), importId, addLines);
    Multiplicity returnMultiplicity = this.buildMultiplicity(ctx.multiplicity().multiplicityArgument());
    SourceInformation sourceInfo = this.sourceInformation.getPureSourceInformation(ctx.getStart(), ctx.getStart(), ctx.getStop());
    FunctionTypeInstance functionTypeInstance = FunctionTypeInstance.createPersistent(this.repository, sourceInfo, returnMultiplicity, returnType);
    MutableList<VariableExpression> params = ListIterate.collect(ctx.functionTypePureType(), fCtx -> typeFunctionTypePureType(fCtx, typeParametersNames, space, importId, addLines));
    if (params.notEmpty()) {
        functionTypeInstance._parameters(params);
    }
    GenericTypeInstance genericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
    genericTypeInstance._rawTypeCoreInstance(functionTypeInstance);
    return genericTypeInstance;
}
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) Multiplicity(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity) VariableExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression) AntlrSourceInformation(org.finos.legend.pure.m4.serialization.grammar.antlr.AntlrSourceInformation) SourceInformation(org.finos.legend.pure.m4.coreinstance.SourceInformation)

Aggregations

FunctionTypeInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionTypeInstance)4 GenericType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType)4 GenericTypeInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericTypeInstance)4 ValueSpecification (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification)3 VariableExpression (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression)3 LambdaFunctionInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunctionInstance)2 Multiplicity (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity)2 FunctionType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType)2 Type (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type)2 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)2 PrimitiveCoreInstance (org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance)2 Token (org.antlr.v4.runtime.Token)1 PackageInstance (org.finos.legend.pure.m3.coreinstance.PackageInstance)1 ImportStub (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.ImportStub)1 ImportStubInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.ImportStubInstance)1 TaggedValue (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.extension.TaggedValue)1 QualifiedPropertyInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedPropertyInstance)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 ClassInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.ClassInstance)1