Search in sources :

Example 1 with PackageableElementPtr

use of org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.PackageableElementPtr in project legend-engine by finos.

the class PureCollectionDataParseTreeWalker method visitInstance.

private AppliedFunction visitInstance(PureCollectionDataParserGrammar.InstanceContext ctx) {
    ctx.instancePropertyAssignment().stream().map(this::visitPropertyAssignment).collect(Collectors.toList());
    PackageableElementPtr newClass = new PackageableElementPtr();
    newClass.fullPath = PureGrammarParserUtility.fromQualifiedName(ctx.qualifiedName().packagePath() == null ? Collections.emptyList() : ctx.qualifiedName().packagePath().identifier(), ctx.qualifiedName().identifier());
    List<ValueSpecification> keyExpressions = ctx.instancePropertyAssignment().stream().map(this::visitPropertyAssignment).collect(Collectors.toList());
    Collection valueAssignments = new Collection();
    valueAssignments.values = keyExpressions;
    valueAssignments.multiplicity = PURE_ONE;
    AppliedFunction appliedFunction = new AppliedFunction();
    appliedFunction.parameters = Lists.mutable.with(newClass, cString("dummy"), valueAssignments);
    appliedFunction.function = "new";
    return appliedFunction;
}
Also used : ValueSpecification(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.ValueSpecification) AppliedFunction(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.application.AppliedFunction)

Example 2 with PackageableElementPtr

use of org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.PackageableElementPtr in project legend-engine by finos.

the class HelperValueSpecificationBuilder method processProperty.

public static org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification processProperty(CompileContext context, MutableList<String> openVariables, ProcessingContext processingContext, List<org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.ValueSpecification> parameters, String property, SourceInformation sourceInformation) {
    // for X.property. X is the first parameter
    processingContext.push("Processing property " + property);
    org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.ValueSpecification firstArgument = parameters.get(0);
    MutableList<org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification> processedParameters = ListIterate.collect(parameters, p -> p.accept(new ValueSpecificationBuilder(context, openVariables, processingContext)));
    org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType genericType;
    org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity multiplicity;
    org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification inferredVariable;
    org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification result;
    if (// Only for backward compatibility!
    firstArgument instanceof org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.Enum || (processedParameters.get(0)._genericType()._rawType().equals(context.pureModel.getType("meta::pure::metamodel::type::Enumeration")))) {
        org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Multiplicity m = new org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Multiplicity();
        m.lowerBound = 1;
        m.setUpperBound(1);
        CString enumValue = new CString();
        enumValue.values = Lists.mutable.of(property);
        enumValue.multiplicity = m;
        // validation to make sure the enum value can be found
        context.resolveEnumValue(((PackageableElementPtr) firstArgument).fullPath, enumValue.values.get(0), firstArgument.sourceInformation, sourceInformation);
        AppliedFunction extractEnum = new AppliedFunction();
        extractEnum.function = "extractEnumValue";
        extractEnum.parameters = Lists.mutable.of(firstArgument, enumValue);
        result = extractEnum.accept(new ValueSpecificationBuilder(context, openVariables, processingContext));
    } else {
        if (firstArgument instanceof Variable) {
            inferredVariable = processingContext.getInferredVariable(((Variable) firstArgument).name);
        } else {
            inferredVariable = processedParameters.get(0);
        }
        Type inferredType = inferredVariable._genericType()._rawType();
        if (// is an enum
        !(inferredType instanceof org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Class)) {
            inferredType = context.pureModel.getType("meta::pure::metamodel::type::Enum");
        }
        org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.AbstractProperty<?> foundProperty = findProperty(context, (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Class<?>) inferredType, parameters, property, sourceInformation);
        if (foundProperty instanceof Property) {
            genericType = (foundProperty._classifierGenericType()._typeArguments()).getLast();
            multiplicity = foundProperty._multiplicity();
        } else if (foundProperty instanceof QualifiedProperty) {
            FunctionType fType = (FunctionType) foundProperty._classifierGenericType()._typeArguments().getFirst()._rawType();
            genericType = fType._returnType();
            multiplicity = fType._returnMultiplicity();
        } else {
            throw new UnsupportedOperationException("Unhandled property: " + foundProperty);
        }
        if (// autoMap
        !inferredVariable._multiplicity().getName().equals("PureOne")) {
            processingContext.push("Building Automap for  " + property);
            org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Multiplicity m = new org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Multiplicity();
            m.lowerBound = 1;
            m.setUpperBound(1);
            List<org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.ValueSpecification> localParameters = Lists.mutable.ofAll(parameters);
            final String automapName = "v_automap";
            Lambda automapLambda = new Lambda();
            AppliedProperty appliedProperty = new AppliedProperty();
            appliedProperty.property = property;
            Variable automapvar = new Variable();
            Variable automaLambdaparam = new Variable();
            automapvar.name = automapName;
            appliedProperty.parameters = Lists.mutable.of(automapvar);
            if (!localParameters.isEmpty()) {
                localParameters.remove(0);
            }
            appliedProperty.parameters.addAll(localParameters);
            automaLambdaparam.name = automapName;
            automaLambdaparam._class = HelperModelBuilder.getElementFullPath(inferredVariable._genericType()._rawType(), context.pureModel.getExecutionSupport());
            automaLambdaparam.multiplicity = m;
            automapLambda.body = Lists.mutable.of(appliedProperty);
            List<Variable> lambdaParams = new FastList<>();
            lambdaParams.add(automaLambdaparam);
            automapLambda.parameters = lambdaParams;
            List<org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.ValueSpecification> newParams = Lists.mutable.of(parameters.get(0), automapLambda);
            result = context.buildFunctionExpression("map", null, newParams, openVariables, null, processingContext).getOne();
            processingContext.pop();
        } else {
            result = new Root_meta_pure_metamodel_valuespecification_SimpleFunctionExpression_Impl("")._func(foundProperty)._propertyName(new Root_meta_pure_metamodel_valuespecification_InstanceValue_Impl("")._values(Lists.fixedSize.of(foundProperty.getName())))._genericType(genericType)._multiplicity(multiplicity)._parametersValues(processedParameters);
        }
    }
    processingContext.pop();
    return result;
}
Also used : ValueSpecification(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.ValueSpecification) CString(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.CString) Multiplicity(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Multiplicity) FastList(org.eclipse.collections.impl.list.mutable.FastList) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) Class(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Class) QualifiedProperty(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty) Variable(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.Variable) CString(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.CString) Multiplicity(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Multiplicity) AbstractProperty(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.AbstractProperty) 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) AppliedProperty(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.application.AppliedProperty) Lambda(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.Lambda) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) ValueSpecification(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.ValueSpecification) 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) AppliedFunction(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.application.AppliedFunction) AppliedProperty(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.application.AppliedProperty)

Aggregations

ValueSpecification (org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.ValueSpecification)2 AppliedFunction (org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.application.AppliedFunction)2 FastList (org.eclipse.collections.impl.list.mutable.FastList)1 Multiplicity (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Multiplicity)1 Variable (org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.Variable)1 AppliedProperty (org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.application.AppliedProperty)1 CString (org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.CString)1 Lambda (org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.Lambda)1 AbstractProperty (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.AbstractProperty)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 Class (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Class)1 FunctionType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType)1 Type (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type)1 GenericType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType)1