Search in sources :

Example 1 with GeneralizationInstance

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.GeneralizationInstance in project legend-pure by finos.

the class AntlrContextToM3CoreInstance method classParser.

private CoreInstance classParser(ClassDefinitionContext ctx, ImportGroup importId, boolean addLines) throws PureParserException {
    MutableList<Property<? extends CoreInstance, ?>> properties = Lists.mutable.empty();
    MutableList<QualifiedProperty<? extends CoreInstance>> qualifiedProperties = Lists.mutable.empty();
    MutableList<GenericType> superTypesGenericTypes = Lists.mutable.empty();
    boolean isDirectSubTypeofAny = false;
    MutableList<String> typeParameterNames = Lists.mutable.empty();
    MutableList<Boolean> contravariants = Lists.mutable.empty();
    MutableList<String> multiplicityParameterNames = Lists.mutable.empty();
    ImportStubInstance ownerType;
    ListIterable<CoreInstance> stereotypes = null;
    ListIterable<TaggedValue> tags = null;
    ClassInstance classInstance;
    if (ctx.stereotypes() != null) {
        stereotypes = this.stereotypes(ctx.stereotypes(), importId);
    }
    if (ctx.taggedValues() != null) {
        tags = this.taggedValues(ctx.taggedValues(), importId);
    }
    this.checkExists(ctx.qualifiedName().packagePath(), ctx.qualifiedName().identifier(), null);
    if (ctx.typeParametersWithContravarianceAndMultiplicityParameters() != null) {
        this.typeParametersWithContravarianceAndMultiplicityParameters(ctx.typeParametersWithContravarianceAndMultiplicityParameters(), typeParameterNames, contravariants, multiplicityParameterNames);
    }
    if (ctx.projection() != null) {
        return this.projectionParser(ctx, importId, addLines, stereotypes, tags);
    } else {
        if (ctx.EXTENDS() != null) {
            for (TypeContext typeCtx : ctx.type()) {
                superTypesGenericTypes.add(this.type(typeCtx, typeParameterNames, "", importId, addLines));
            }
        }
        String className = ctx.qualifiedName().identifier().getText();
        classInstance = ClassInstance.createPersistent(this.repository, className);
        PackageInstance packageInstance = this.buildPackage(ctx.qualifiedName().packagePath());
        classInstance._package(packageInstance);
        packageInstance._childrenAdd(classInstance);
        String fullName = this.getQualifiedNameString(ctx.qualifiedName());
        ownerType = ImportStubInstance.createPersistent(this.repository, this.sourceInformation.getPureSourceInformation(ctx.qualifiedName().identifier().getStart()), fullName, importId);
        LambdaContext lambdaContext = new LambdaContext(fullName.replace("::", "_"));
        MutableList<Constraint> constraints = this.constraints(classInstance, ctx.constraints(), importId, lambdaContext, addLines);
        this.propertyParser(ctx.classBody().properties(), properties, qualifiedProperties, typeParameterNames, multiplicityParameterNames, ownerType, importId, 0);
        classInstance.setSourceInformation(this.sourceInformation.getPureSourceInformation(ctx.getStart(), ctx.qualifiedName().identifier().getStart(), ctx.getStop()));
        if (superTypesGenericTypes.isEmpty()) {
            isDirectSubTypeofAny = true;
            GenericTypeInstance genericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
            genericTypeInstance._rawTypeCoreInstance(this.processorSupport.package_getByUserPath(M3Paths.Any));
            superTypesGenericTypes.add(genericTypeInstance);
        }
        GenericTypeInstance classifierGT = GenericTypeInstance.createPersistent(this.repository);
        ClassInstance classType = (ClassInstance) this.processorSupport.package_getByUserPath(M3Paths.Class);
        classifierGT._rawTypeCoreInstance(classType);
        GenericTypeInstance classifierGTTA = GenericTypeInstance.createPersistent(this.repository);
        classifierGTTA._rawTypeCoreInstance(classInstance);
        if (!typeParameterNames.isEmpty()) {
            MutableList<TypeParameter> typeParameters = Lists.mutable.of();
            MutableList<Pair<String, Boolean>> tps = typeParameterNames.zip(contravariants);
            for (Pair<String, Boolean> typeParam : tps) {
                TypeParameterInstance tp = TypeParameterInstance.createPersistent(this.repository, typeParam.getOne());
                tp._contravariant(typeParam.getTwo());
                typeParameters.add(tp);
            }
            classInstance._typeParameters(typeParameters);
            MutableList<GenericType> typeArgs = Lists.mutable.of();
            for (String typeParamName : typeParameterNames) {
                TypeParameterInstance tp = TypeParameterInstance.createPersistent(this.repository, typeParamName);
                GenericTypeInstance gt = GenericTypeInstance.createPersistent(this.repository);
                gt._typeParameter(tp);
                typeArgs.add(gt);
            }
            classifierGTTA._typeArguments(typeArgs);
        }
        if (!multiplicityParameterNames.isEmpty()) {
            MutableList<Multiplicity> multParameters = Lists.mutable.of();
            for (String multiplicityParam : multiplicityParameterNames) {
                MultiplicityInstance mult = MultiplicityInstance.createPersistent(this.repository, null, null);
                mult._multiplicityParameter(multiplicityParam);
                multParameters.add(mult);
            }
            classInstance._multiplicityParameters(this.processMultiplicityParametersInstance(multiplicityParameterNames));
            classifierGTTA._multiplicityArguments(multParameters);
        }
        classifierGT._typeArguments(Lists.mutable.<GenericType>of(classifierGTTA));
        classInstance._classifierGenericType(classifierGT);
        if (properties.notEmpty()) {
            classInstance._properties(properties);
        }
        if (qualifiedProperties.notEmpty()) {
            classInstance._qualifiedProperties(qualifiedProperties);
        }
        classInstance._name(ctx.qualifiedName().identifier().getText());
        if (stereotypes != null) {
            classInstance._stereotypesCoreInstance(stereotypes);
        }
        if (tags != null) {
            classInstance._taggedValues(tags);
        }
        if (constraints.notEmpty()) {
            classInstance._constraints(constraints);
        }
        MutableList<Generalization> generalizations = Lists.mutable.empty();
        for (GenericType superType : superTypesGenericTypes) {
            GeneralizationInstance generalizationInstance = GeneralizationInstance.createPersistent(this.repository, superType, classInstance);
            generalizations.add(generalizationInstance);
        }
        classInstance._generalizations(generalizations);
        if (isDirectSubTypeofAny) {
            MilestoningClassProcessor.addMilestoningProperty(classInstance, this.context, this.processorSupport, this.repository);
        }
        return classInstance;
    }
}
Also used : TypeParameter(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.TypeParameter) Constraint(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.constraint.Constraint) Generalization(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.Generalization) LambdaParamTypeContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.LambdaParamTypeContext) TypeContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.TypeContext) TreePathPropertyParameterTypeContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.TreePathPropertyParameterTypeContext) FunctionTypePureTypeContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.FunctionTypePureTypeContext) Multiplicity(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity) MultiplicityInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.MultiplicityInstance) QualifiedProperty(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty) Property(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property) ClassInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.ClassInstance) Pair(org.eclipse.collections.api.tuple.Pair) 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) TypeParameterInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.TypeParameterInstance) TaggedValue(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.extension.TaggedValue) PackageInstance(org.finos.legend.pure.m3.coreinstance.PackageInstance) GeneralizationInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.GeneralizationInstance) PrimitiveCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) ImportStubInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.ImportStubInstance) QualifiedProperty(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty)

Example 2 with GeneralizationInstance

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.GeneralizationInstance in project legend-pure by finos.

the class AntlrContextToM3CoreInstance method enumParser.

private Enumeration<?> enumParser(EnumDefinitionContext ctx, ImportGroup importId) throws PureParserException {
    EnumerationInstance enumerationInstance;
    CoreInstance value;
    MutableList<CoreInstance> values = Lists.mutable.empty();
    ListIterable<CoreInstance> stereotypes = Lists.mutable.empty();
    ListIterable<TaggedValue> tags = Lists.mutable.empty();
    if (ctx.stereotypes() != null) {
        stereotypes = this.stereotypes(ctx.stereotypes(), importId);
    }
    if (ctx.taggedValues() != null) {
        tags = this.taggedValues(ctx.taggedValues(), importId);
    }
    this.checkExists(ctx.qualifiedName().packagePath(), ctx.qualifiedName().identifier(), null);
    PackageInstance packageInstance = this.buildPackage(ctx.qualifiedName().packagePath());
    GenericTypeInstance genericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
    ClassInstance enumerationType = (ClassInstance) this.processorSupport.package_getByUserPath(M3Paths.Enumeration);
    genericTypeInstance._rawTypeCoreInstance(enumerationType);
    enumerationInstance = EnumerationInstance.createPersistent(this.repository, ctx.qualifiedName().identifier().getText());
    enumerationInstance._name(ctx.qualifiedName().identifier().getText());
    enumerationInstance._package(packageInstance);
    GenericTypeInstance taGenericType = GenericTypeInstance.createPersistent(this.repository);
    taGenericType._rawTypeCoreInstance(enumerationInstance);
    genericTypeInstance._typeArguments(Lists.mutable.<GenericType>of(taGenericType));
    enumerationInstance._classifierGenericType(genericTypeInstance);
    packageInstance._childrenAdd(enumerationInstance);
    if (!tags.isEmpty()) {
        enumerationInstance._taggedValues(tags);
    }
    if (!stereotypes.isEmpty()) {
        enumerationInstance._stereotypesCoreInstance(stereotypes);
    }
    enumerationInstance._classifierGenericType(genericTypeInstance);
    GenericTypeInstance general = GenericTypeInstance.createPersistent(this.repository);
    ClassInstance enumType = (ClassInstance) this.processorSupport.package_getByUserPath(M3Paths.Enum);
    general._rawTypeCoreInstance(enumType);
    GeneralizationInstance gen = GeneralizationInstance.createPersistent(this.repository, general, enumerationInstance);
    enumerationInstance._generalizations(Lists.mutable.<Generalization>of(gen));
    for (EnumValueContext evCtx : ctx.enumValue()) {
        value = this.enumValue(evCtx, enumerationInstance, importId);
        values.add(value);
    }
    enumerationInstance.setSourceInformation(this.sourceInformation.getPureSourceInformation(ctx.getStart(), ctx.qualifiedName().identifier().getStart(), ctx.getStop()));
    enumerationInstance._values(values);
    return enumerationInstance;
}
Also used : PackageInstance(org.finos.legend.pure.m3.coreinstance.PackageInstance) GenericTypeInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericTypeInstance) EnumValueContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.EnumValueContext) GeneralizationInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.GeneralizationInstance) TaggedValue(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.extension.TaggedValue) PrimitiveCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) EnumerationInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.EnumerationInstance) ClassInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.ClassInstance)

Example 3 with GeneralizationInstance

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.GeneralizationInstance in project legend-pure by finos.

the class AntlrContextToM3CoreInstance method measureParser.

/**
 * Parses the measure given its definition context.
 * Returns the parsed measure as a CoreInstance.
 */
private CoreInstance measureParser(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.MeasureDefinitionContext ctx, ImportGroup importId, boolean addLines) throws PureParserException {
    UnitInstance canonicalUnit;
    MutableList<UnitInstance> nonCanonicalUnits;
    MutableList<GenericType> superTypesGenericTypes = Lists.mutable.empty();
    MutableList<String> typeParameterNames = Lists.mutable.empty();
    MutableList<Boolean> contravariants = Lists.mutable.empty();
    MutableList<String> multiplicityParameterNames = Lists.mutable.empty();
    ListIterable<CoreInstance> stereotypes = Lists.mutable.empty();
    ListIterable<TaggedValue> tags = Lists.mutable.empty();
    MeasureInstance measureInstance;
    String measureName = ctx.qualifiedName().identifier().getText();
    measureInstance = MeasureInstance.createPersistent(this.repository, measureName);
    PackageInstance packageInstance = this.buildPackage(ctx.qualifiedName().packagePath());
    measureInstance._package(packageInstance);
    packageInstance._childrenAdd(measureInstance);
    String fullName = this.getQualifiedNameString(ctx.qualifiedName());
    measureInstance.setSourceInformation(this.sourceInformation.getPureSourceInformation(ctx.getStart(), ctx.qualifiedName().identifier().getStart(), ctx.getStop()));
    if (superTypesGenericTypes.isEmpty()) {
        GenericTypeInstance genericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
        genericTypeInstance._rawTypeCoreInstance(this.processorSupport.package_getByUserPath(M3Paths.Any));
        superTypesGenericTypes.add(genericTypeInstance);
    }
    GenericTypeInstance classifierGT = GenericTypeInstance.createPersistent(this.repository);
    ClassInstance measureType = (ClassInstance) this.processorSupport.package_getByUserPath(M3Paths.Measure);
    classifierGT._rawTypeCoreInstance(measureType);
    measureInstance._classifierGenericType(classifierGT);
    if (!typeParameterNames.isEmpty()) {
        MutableList<TypeParameter> typeParameters = Lists.mutable.of();
        MutableList<Pair<String, Boolean>> tps = typeParameterNames.zip(contravariants);
        for (Pair<String, Boolean> typeParam : tps) {
            TypeParameterInstance tp = TypeParameterInstance.createPersistent(this.repository, typeParam.getOne());
            tp._contravariant(typeParam.getTwo());
            typeParameters.add(tp);
        }
        MutableList<GenericType> typeArgs = Lists.mutable.of();
        for (String typeParamName : typeParameterNames) {
            TypeParameterInstance tp = TypeParameterInstance.createPersistent(this.repository, typeParamName);
            GenericTypeInstance gt = GenericTypeInstance.createPersistent(this.repository);
            gt._typeParameter(tp);
            typeArgs.add(gt);
        }
    }
    if (!multiplicityParameterNames.isEmpty()) {
        MutableList<Multiplicity> multParameters = Lists.mutable.of();
        for (String multiplicityParam : multiplicityParameterNames) {
            MultiplicityInstance mult = MultiplicityInstance.createPersistent(this.repository, null, null);
            mult._multiplicityParameter(multiplicityParam);
            multParameters.add(mult);
        }
    }
    measureInstance._name(ctx.qualifiedName().identifier().getText());
    if (!stereotypes.isEmpty()) {
        measureInstance._stereotypesCoreInstance(stereotypes);
    }
    if (!tags.isEmpty()) {
        measureInstance._taggedValues(tags);
    }
    MutableList<Generalization> generalizations = Lists.mutable.empty();
    for (GenericType superType : superTypesGenericTypes) {
        GeneralizationInstance generalizationInstance = GeneralizationInstance.createPersistent(this.repository, superType, measureInstance);
        generalizations.add(generalizationInstance);
    }
    measureInstance._generalizations(generalizations);
    if (null != ctx.measureBody().canonicalExpr()) {
        // traditional canonical unit pattern
        canonicalUnit = this.canonicalUnitParser(ctx.measureBody().canonicalExpr(), importId, measureInstance, ctx);
        measureInstance._canonicalUnit(canonicalUnit);
        nonCanonicalUnits = this.nonCanonicalUnitsParser(ctx.measureBody().measureExpr(), importId, measureInstance, ctx);
        if (nonCanonicalUnits.notEmpty()) {
            measureInstance._nonCanonicalUnits(nonCanonicalUnits);
        }
    } else {
        // non-convertible unit pattern
        MutableList<UnitInstance> nonConvertibleUnits = Lists.mutable.empty();
        for (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.NonConvertibleMeasureExprContext ncctx : ctx.measureBody().nonConvertibleMeasureExpr()) {
            UnitInstance currentUnit = this.nonConvertibleUnitParser(ncctx, importId, measureInstance, ctx);
            nonConvertibleUnits.add(currentUnit);
        }
        measureInstance._canonicalUnit(nonConvertibleUnits.get(0));
        if (nonConvertibleUnits.size() > 1) {
            measureInstance._nonCanonicalUnits(nonConvertibleUnits.subList(1, nonConvertibleUnits.size()));
        }
    }
    return measureInstance;
}
Also used : TypeParameter(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.TypeParameter) UnitInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.UnitInstance) Generalization(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.Generalization) Multiplicity(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity) MultiplicityInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.MultiplicityInstance) ClassInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.ClassInstance) Pair(org.eclipse.collections.api.tuple.Pair) 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) TypeParameterInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.TypeParameterInstance) TaggedValue(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.extension.TaggedValue) MeasureInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.MeasureInstance) PackageInstance(org.finos.legend.pure.m3.coreinstance.PackageInstance) GeneralizationInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.GeneralizationInstance) PrimitiveCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance)

Example 4 with GeneralizationInstance

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.GeneralizationInstance in project legend-pure by finos.

the class AntlrContextToM3CoreInstance method projectionParser.

private CoreInstance projectionParser(ClassDefinitionContext ctx, ImportGroup importId, boolean addLines, ListIterable<CoreInstance> stereotypes, ListIterable<TaggedValue> tags) throws PureParserException {
    CoreInstance treePath = ctx.projection().dsl() != null ? this.dsl(ctx.projection().dsl(), importId).get(0) : this.treePath(ctx.projection().treePath(), importId);
    this.checkExists(ctx.qualifiedName().packagePath(), ctx.qualifiedName().identifier(), null);
    RootRouteNode rootNode = (RootRouteNode) treePath;
    String name = ctx.qualifiedName().identifier().getText();
    ClassProjectionInstance projection = ClassProjectionInstance.createPersistent(this.repository, name, this.sourceInformation.getPureSourceInformation(ctx.getStart(), ctx.qualifiedName().identifier().getStart(), ctx.getStop()), rootNode);
    projection._name(name);
    rootNode._owner(projection);
    PackageInstance packageInstance = this.buildPackage(ctx.qualifiedName().packagePath());
    projection._package(packageInstance);
    packageInstance._childrenAdd(projection);
    GenericTypeInstance classifierGT = GenericTypeInstance.createPersistent(this.repository);
    ClassInstance classType = (ClassInstance) this.processorSupport.package_getByUserPath(M3Paths.ClassProjection);
    classifierGT._rawTypeCoreInstance(classType);
    GenericTypeInstance classifierGTTA = GenericTypeInstance.createPersistent(this.repository);
    classifierGTTA._rawTypeCoreInstance(projection);
    classifierGT._typeArguments(Lists.mutable.<GenericType>of(classifierGTTA));
    projection._classifierGenericType(classifierGT);
    GenericTypeInstance superType = GenericTypeInstance.createPersistent(this.repository);
    superType._rawTypeCoreInstance(this.processorSupport.package_getByUserPath(M3Paths.Any));
    GeneralizationInstance generalizationInstance = GeneralizationInstance.createPersistent(this.repository, superType, projection);
    projection._generalizations(Lists.mutable.<Generalization>of(generalizationInstance));
    String fullName = this.getQualifiedNameString(ctx.qualifiedName());
    MutableList<Constraint> constraints = this.constraints(projection, ctx.constraints(), importId, new LambdaContext(fullName.replace("::", "_")), addLines);
    if (Iterate.notEmpty(stereotypes)) {
        projection._stereotypesCoreInstance(stereotypes);
    }
    if (Iterate.notEmpty(tags)) {
        projection._taggedValues(tags);
    }
    if (Iterate.notEmpty(constraints)) {
        projection._constraints(constraints);
    }
    return projection;
}
Also used : PackageInstance(org.finos.legend.pure.m3.coreinstance.PackageInstance) GenericTypeInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericTypeInstance) GeneralizationInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.GeneralizationInstance) RootRouteNode(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.treepath.RootRouteNode) Constraint(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.constraint.Constraint) PrimitiveCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) ClassProjectionInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.ClassProjectionInstance) ClassInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.ClassInstance)

Example 5 with GeneralizationInstance

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

PackageInstance (org.finos.legend.pure.m3.coreinstance.PackageInstance)5 GeneralizationInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.GeneralizationInstance)5 ClassInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.ClassInstance)5 GenericTypeInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericTypeInstance)5 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)5 PrimitiveCoreInstance (org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance)5 TaggedValue (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.extension.TaggedValue)3 Generalization (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.Generalization)3 GenericType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType)3 Pair (org.eclipse.collections.api.tuple.Pair)2 Constraint (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.constraint.Constraint)2 Multiplicity (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity)2 MultiplicityInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.MultiplicityInstance)2 UnitInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.UnitInstance)2 TypeParameter (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.TypeParameter)2 TypeParameterInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.TypeParameterInstance)2 ImportStubInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.ImportStubInstance)1 LambdaFunctionInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunctionInstance)1 Property (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property)1 QualifiedProperty (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty)1