Search in sources :

Example 1 with UnitInstance

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

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

the class AntlrContextToM3CoreInstance method nonConvertibleUnitParser.

private UnitInstance nonConvertibleUnitParser(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.NonConvertibleMeasureExprContext 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);
        genericTypeInstance._rawTypeCoreInstance(measureInstance);
        superTypesGenericTypes.add(genericTypeInstance);
    }
    unitInstance._generalizations(superTypesGenericTypes.collect(superType -> GeneralizationInstance.createPersistent(this.repository, superType, unitInstance)));
    unitInstance._measure(measureInstance);
    return unitInstance;
}
Also used : PackageInstance(org.finos.legend.pure.m3.coreinstance.PackageInstance) GenericTypeInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericTypeInstance) RootRouteNodeInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.treepath.RootRouteNodeInstance) StereotypesContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.StereotypesContext) NotExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.NotExpressionContext) MultiplicityArgumentContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.MultiplicityArgumentContext) InstanceValueInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.InstanceValueInstance) TaggedValueContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.TaggedValueContext) ExpressionInstanceContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ExpressionInstanceContext) EnumDefinitionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.EnumDefinitionContext) InlineDSL(org.finos.legend.pure.m3.serialization.grammar.m3parser.inlinedsl.InlineDSL) LambdaPipeContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.LambdaPipeContext) InstanceAtomicRightSideContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.InstanceAtomicRightSideContext) ContravarianceTypeParameterContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ContravarianceTypeParameterContext) LambdaParamTypeContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.LambdaParamTypeContext) ClassDefinitionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ClassDefinitionContext) Iterate(org.eclipse.collections.impl.utility.Iterate) Enum(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Enum) PropertyRouteNode(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.treepath.PropertyRouteNode) Import_statementContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.Import_statementContext) GeneralizationInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.GeneralizationInstance) ListIterable(org.eclipse.collections.api.list.ListIterable) ConcreteFunctionDefinitionInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinitionInstance) TypeParameterInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.TypeParameterInstance) PrimitiveCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance) IdentifierContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.IdentifierContext) PackagePathContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.PackagePathContext) Function(org.eclipse.collections.api.block.function.Function) MutableStack(org.eclipse.collections.api.stack.MutableStack) FunctionTypeSignatureContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.FunctionTypeSignatureContext) ExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ExpressionContext) M3Paths(org.finos.legend.pure.m3.navigation.M3Paths) TreePathContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.TreePathContext) ProfileInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.extension.ProfileInstance) ProfileContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ProfileContext) TypeParameterContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.TypeParameterContext) MutableMap(org.eclipse.collections.api.map.MutableMap) Package(org.finos.legend.pure.m3.coreinstance.Package) LambdaFunctionInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunctionInstance) InstanceLiteralContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.InstanceLiteralContext) Sets(org.eclipse.collections.api.factory.Sets) TagInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.extension.TagInstance) LetExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.LetExpressionContext) ConstraintsContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ConstraintsContext) FunctionExpressionParametersContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.FunctionExpressionParametersContext) StereotypeInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.extension.StereotypeInstance) PureParserException(org.finos.legend.pure.m4.serialization.grammar.antlr.PureParserException) InstanceRightSideContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.InstanceRightSideContext) MappingLineContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.MappingLineContext) MappingContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.MappingContext) ListIterate(org.eclipse.collections.impl.utility.ListIterate) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) StereotypeDefinitionsContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.StereotypeDefinitionsContext) QualifiedNameContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.QualifiedNameContext) AssociationInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.AssociationInstance) AntlrSourceInformation(org.finos.legend.pure.m4.serialization.grammar.antlr.AntlrSourceInformation) ExistingPropertyRouteNode(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.treepath.ExistingPropertyRouteNode) ExpressionInstanceAtomicRightSideContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ExpressionInstanceAtomicRightSideContext) AssociationProjectionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.AssociationProjectionContext) ConstraintInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.constraint.ConstraintInstance) Stacks(org.eclipse.collections.api.factory.Stacks) Constraint(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.constraint.Constraint) SimpleFunctionExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.SimpleFunctionExpression) EnumValueContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.EnumValueContext) Tag(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.extension.Tag) Interval(org.antlr.v4.runtime.misc.Interval) NativeFunctionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.NativeFunctionContext) Predicate2(org.eclipse.collections.api.block.predicate.Predicate2) Generalization(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.Generalization) BuildMilestoningVariableExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.BuildMilestoningVariableExpressionContext) MutableList(org.eclipse.collections.api.list.MutableList) MultiplicityInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.MultiplicityInstance) PropertyExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.PropertyExpressionContext) Maps(org.eclipse.collections.api.factory.Maps) MutableSet(org.eclipse.collections.api.set.MutableSet) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) InstanceContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.InstanceContext) DefinitionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.DefinitionContext) ImportStub(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.ImportStub) BooleanPartContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.BooleanPartContext) ExpressionOrExpressionGroupContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ExpressionOrExpressionGroupContext) TaggedValue(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.extension.TaggedValue) QualifiedPropertyContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.QualifiedPropertyContext) TypeContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.TypeContext) ListAdapter(org.eclipse.collections.impl.list.mutable.ListAdapter) SourceState(org.finos.legend.pure.m3.serialization.runtime.SourceState) DefaultValueExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.DefaultValueExpressionContext) PackageableElement(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement) Multiplicity(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity) Unit(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Unit) QualifiedProperty(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty) TaggedValueInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.extension.TaggedValueInstance) RouteNode(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.treepath.RouteNode) DefaultValueInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.DefaultValueInstance) ProgramLineContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ProgramLineContext) CombinedArithmeticOnlyContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.CombinedArithmeticOnlyContext) InstanceReferenceContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.InstanceReferenceContext) TaggedValuesContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.TaggedValuesContext) ImportInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.ImportInstance) CodeBlockContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.CodeBlockContext) Any(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Any) ArithmeticPartContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ArithmeticPartContext) Type(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type) InlineDSLTextContent(org.finos.legend.pure.m3.serialization.grammar.m3parser.inlinedsl.InlineDSLTextContent) FunctionVariableExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.FunctionVariableExpressionContext) CombinedExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.CombinedExpressionContext) ExistingPropertyRouteNodeInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.treepath.ExistingPropertyRouteNodeInstance) SimpleFunctionExpressionInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.SimpleFunctionExpressionInstance) ExpressionInstanceRightSideContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ExpressionInstanceRightSideContext) NativeFunctionInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.NativeFunctionInstance) InstanceLiteralTokenContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.InstanceLiteralTokenContext) AssociationContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.AssociationContext) EnumStubInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.EnumStubInstance) PropertyStubInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.PropertyStubInstance) DslContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.DslContext) Token(org.antlr.v4.runtime.Token) ImportGroup(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.ImportGroup) VariableContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.VariableContext) TypeParameter(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.TypeParameter) RichIterable(org.eclipse.collections.api.RichIterable) TreePathClassBodyContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.TreePathClassBodyContext) StringEscapeUtils(org.apache.commons.lang3.StringEscapeUtils) EqualNotEqualContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.EqualNotEqualContext) QualifiedPropertyInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedPropertyInstance) ModelRepository(org.finos.legend.pure.m4.ModelRepository) DefaultValue(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.DefaultValue) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) SourceAndTargetMappingIdContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.SourceAndTargetMappingIdContext) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) FunctionDefinitionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.FunctionDefinitionContext) DerivedPropertyContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.DerivedPropertyContext) DefaultValueContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.DefaultValueContext) NewPropertyRouteNodeFunctionDefinition(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.treepath.NewPropertyRouteNodeFunctionDefinition) GenericTypeInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericTypeInstance) Instance(org.finos.legend.pure.m3.navigation.Instance) ImportsContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ImportsContext) Lists(org.eclipse.collections.api.factory.Lists) RootRouteNode(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.treepath.RootRouteNode) MilestoningClassProcessor(org.finos.legend.pure.m3.compiler.postprocessing.processor.milestoning.MilestoningClassProcessor) NewPropertyRouteNodeInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.treepath.NewPropertyRouteNodeInstance) FunctionTypeInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionTypeInstance) TreePathPropertyParameterTypeContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.TreePathPropertyParameterTypeContext) RuleContext(org.antlr.v4.runtime.RuleContext) FastList(org.eclipse.collections.impl.list.mutable.FastList) InlineDSLLibrary(org.finos.legend.pure.m3.serialization.grammar.m3parser.inlinedsl.InlineDSLLibrary) InstancePropertyAssignmentContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.InstancePropertyAssignmentContext) PackageInstance(org.finos.legend.pure.m3.coreinstance.PackageInstance) TagDefinitionsContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.TagDefinitionsContext) Stereotype(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.extension.Stereotype) ExpressionPartContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ExpressionPartContext) SourceInformation(org.finos.legend.pure.m4.coreinstance.SourceInformation) AllOrFunctionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.AllOrFunctionContext) ClassInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.ClassInstance) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) VariableExpressionInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpressionInstance) PropertiesContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.PropertiesContext) InstanceValue(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.InstanceValue) ClassProjectionInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.ClassProjectionInstance) PropertyRefContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.PropertyRefContext) PropertyStub(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.PropertyStub) ComplexPropertyContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ComplexPropertyContext) Source(org.finos.legend.pure.m3.serialization.runtime.Source) AtomicExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.AtomicExpressionContext) TypeArgumentsContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.TypeArgumentsContext) TypeParametersWithContravarianceAndMultiplicityParametersContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.TypeParametersWithContravarianceAndMultiplicityParametersContext) MultiplicityArgumentsContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.MultiplicityArgumentsContext) AssociationProjectionInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.AssociationProjectionInstance) ExpressionInstanceParserPropertyAssignmentContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ExpressionInstanceParserPropertyAssignmentContext) UnitInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.UnitInstance) ProcessorSupport(org.finos.legend.pure.m3.navigation.ProcessorSupport) ImportGroupInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.ImportGroupInstance) Property(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property) Tuples(org.eclipse.collections.impl.tuple.Tuples) Import(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.Import) TypeAndMultiplicityParametersContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.TypeAndMultiplicityParametersContext) MultiplicityValueInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.MultiplicityValueInstance) KeyExpressionInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.functions.lang.KeyExpressionInstance) List(java.util.List) SimplePropertyContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.SimplePropertyContext) org.finos.legend.pure.m3.navigation._package._Package(org.finos.legend.pure.m3.navigation._package._Package) VariableExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression) StereotypeContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.StereotypeContext) ConstraintContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ConstraintContext) Context(org.finos.legend.pure.m3.compiler.Context) SignedExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.SignedExpressionContext) Function0(org.eclipse.collections.api.block.function.Function0) PropertyContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.PropertyContext) RouteNodePropertyStub(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.treepath.RouteNodePropertyStub) PropertyInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.PropertyInstance) Pair(org.eclipse.collections.api.tuple.Pair) MeasureInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.MeasureInstance) Enumeration(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Enumeration) EnumerationInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.EnumerationInstance) NewPropertyRouteNode(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.treepath.NewPropertyRouteNode) RouteNodePropertyStubInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.treepath.RouteNodePropertyStubInstance) ImportStubInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.ImportStubInstance) FunctionTypePureTypeContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.FunctionTypePureTypeContext) LambdaFunction(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction) NewPropertyRouteNodeFunctionDefinitionInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.treepath.NewPropertyRouteNodeFunctionDefinitionInstance) M3Properties(org.finos.legend.pure.m3.navigation.M3Properties) PropertyOrFunctionExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.PropertyOrFunctionExpressionContext) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) UnitInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.UnitInstance) ClassInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.ClassInstance)

Example 3 with UnitInstance

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.UnitInstance 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)3 Generalization (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.Generalization)3 GeneralizationInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.GeneralizationInstance)3 ClassInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.ClassInstance)3 UnitInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.UnitInstance)3 GenericType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType)3 GenericTypeInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericTypeInstance)3 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)3 PrimitiveCoreInstance (org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance)3 Pair (org.eclipse.collections.api.tuple.Pair)2 TaggedValue (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.extension.TaggedValue)2 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 MultiplicityInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.MultiplicityInstance)2 FunctionType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType)2 FunctionTypeInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionTypeInstance)2 MeasureInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.MeasureInstance)2 Type (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type)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