Search in sources :

Example 1 with TypeAndMultiplicity

use of org.finos.legend.engine.language.pure.compiler.toPureGraph.handlers.inference.TypeAndMultiplicity in project legend-engine by finos.

the class PackageableElementFirstPassBuilder method visit.

@Override
public PackageableElement visit(Function function) {
    // NOTE: in the protocol, we still store the function name as is, but in the function index, we will store the function based on its function signature
    String functionSignature = HelperModelBuilder.getSignature(function);
    String functionFullName = this.context.pureModel.buildPackageString(function._package, functionSignature);
    String functionName = this.context.pureModel.buildPackageString(function._package, HelperModelBuilder.getFunctionNameWithoutSignature(function));
    final org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition<?> targetFunc = new Root_meta_pure_metamodel_function_ConcreteFunctionDefinition_Impl<>(functionSignature, SourceInformationHelper.toM3SourceInformation(function.sourceInformation), null);
    this.context.pureModel.functionsIndex.put(functionFullName, targetFunc);
    ProcessingContext ctx = new ProcessingContext("Function '" + functionFullName + "' First Pass");
    org.finos.legend.pure.m3.coreinstance.Package pack = this.context.pureModel.getOrCreatePackage(function._package);
    org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition<?> res = targetFunc._name(// function signature here - e.g. isAfterDay_Date_1__Date_1__Boolean_1_
    HelperModelBuilder.getTerseSignature(function))._functionName(// function name to be used in the handler map -> meta::pure::functions::date::isAfterDay
    functionName)._classifierGenericType(new Root_meta_pure_metamodel_type_generics_GenericType_Impl("")._rawType(this.context.pureModel.getType("meta::pure::metamodel::function::ConcreteFunctionDefinition"))._typeArguments(Lists.fixedSize.of(PureModel.buildFunctionType(ListIterate.collect(function.parameters, p -> (VariableExpression) p.accept(new ValueSpecificationBuilder(this.context, Lists.mutable.empty(), ctx))), this.context.resolveGenericType(function.returnType, function.sourceInformation), this.context.pureModel.getMultiplicity(function.returnMultiplicity)))))._stereotypes(ListIterate.collect(function.stereotypes, s -> this.context.resolveStereotype(s.profile, s.value, s.profileSourceInformation, s.sourceInformation)))._taggedValues(ListIterate.collect(function.taggedValues, t -> new Root_meta_pure_metamodel_extension_TaggedValue_Impl("")._tag(this.context.resolveTag(t.tag.profile, t.tag.value, t.tag.profileSourceInformation, t.sourceInformation))._value(t.value)))._package(pack);
    HelperModelBuilder.processFunctionConstraints(function, this.context, res, ctx);
    pack._childrenAdd(res);
    this.context.pureModel.handlers.register(new UserDefinedFunctionHandler(functionFullName, res, ps -> new TypeAndMultiplicity(this.context.resolveGenericType(function.returnType, function.sourceInformation), this.context.pureModel.getMultiplicity(function.returnMultiplicity)), ps -> {
        List<ValueSpecification> vs = ListIterate.collect(function.parameters, p -> p.accept(new ValueSpecificationBuilder(this.context, Lists.mutable.empty(), ctx)));
        if (ps.size() == function.parameters.size()) {
            int size = ps.size();
            // TODO clean up the check....
            try {
                for (int i = 0; i < size; i++) {
                    HelperModelBuilder.checkCompatibility(this.context, ps.get(i)._genericType()._rawType(), ps.get(i)._multiplicity(), vs.get(i)._genericType()._rawType(), vs.get(i)._multiplicity(), "Error in function '" + functionFullName + "'", function.body.get(function.body.size() - 1).sourceInformation);
                }
            } catch (Exception e) {
                return false;
            }
            return true;
        }
        return false;
    }));
    return res;
}
Also used : Class(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Class) VariableExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression) PackageableElementVisitor(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElementVisitor) EngineErrorType(org.finos.legend.engine.protocol.pure.v1.model.context.EngineErrorType) QualifiedProperty(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty) Mapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.Mapping) ListIterate(org.eclipse.collections.impl.utility.ListIterate) DataElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.data.DataElement) FastList(org.eclipse.collections.impl.list.mutable.FastList) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) UserDefinedFunctionHandler(org.finos.legend.engine.language.pure.compiler.toPureGraph.handlers.UserDefinedFunctionHandler) org.finos.legend.pure.generated(org.finos.legend.pure.generated) List(java.util.List) PackageableConnection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.connection.PackageableConnection) org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain) PackageableElement(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) Lists(org.eclipse.collections.impl.factory.Lists) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) ListIterable(org.eclipse.collections.api.list.ListIterable) SectionIndex(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex) TypeAndMultiplicity(org.finos.legend.engine.language.pure.compiler.toPureGraph.handlers.inference.TypeAndMultiplicity) PackageableRuntime(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.runtime.PackageableRuntime) TypeAndMultiplicity(org.finos.legend.engine.language.pure.compiler.toPureGraph.handlers.inference.TypeAndMultiplicity) VariableExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) UserDefinedFunctionHandler(org.finos.legend.engine.language.pure.compiler.toPureGraph.handlers.UserDefinedFunctionHandler) FastList(org.eclipse.collections.impl.list.mutable.FastList) List(java.util.List)

Example 2 with TypeAndMultiplicity

use of org.finos.legend.engine.language.pure.compiler.toPureGraph.handlers.inference.TypeAndMultiplicity in project legend-engine by finos.

the class FunctionHandler method process.

public SimpleFunctionExpression process(List<ValueSpecification> vs) {
    TypeAndMultiplicity inferred = returnInference.infer(vs);
    Assert.assertTrue(func != null, () -> "Func is null");
    return new Root_meta_pure_metamodel_valuespecification_SimpleFunctionExpression_Impl("")._func(func)._functionName(func._functionName())._genericType(inferred.genericType)._multiplicity(inferred.multiplicity)._parametersValues(Lists.mutable.withAll(vs));
}
Also used : TypeAndMultiplicity(org.finos.legend.engine.language.pure.compiler.toPureGraph.handlers.inference.TypeAndMultiplicity) Root_meta_pure_metamodel_valuespecification_SimpleFunctionExpression_Impl(org.finos.legend.pure.generated.Root_meta_pure_metamodel_valuespecification_SimpleFunctionExpression_Impl)

Aggregations

TypeAndMultiplicity (org.finos.legend.engine.language.pure.compiler.toPureGraph.handlers.inference.TypeAndMultiplicity)2 List (java.util.List)1 ListIterable (org.eclipse.collections.api.list.ListIterable)1 Lists (org.eclipse.collections.impl.factory.Lists)1 FastList (org.eclipse.collections.impl.list.mutable.FastList)1 ListIterate (org.eclipse.collections.impl.utility.ListIterate)1 UserDefinedFunctionHandler (org.finos.legend.engine.language.pure.compiler.toPureGraph.handlers.UserDefinedFunctionHandler)1 EngineErrorType (org.finos.legend.engine.protocol.pure.v1.model.context.EngineErrorType)1 PackageableElementVisitor (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElementVisitor)1 PackageableConnection (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.connection.PackageableConnection)1 DataElement (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.data.DataElement)1 org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain)1 Class (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Class)1 Mapping (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.Mapping)1 PackageableRuntime (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.runtime.PackageableRuntime)1 SectionIndex (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex)1 EngineException (org.finos.legend.engine.shared.core.operational.errorManagement.EngineException)1 org.finos.legend.pure.generated (org.finos.legend.pure.generated)1 Root_meta_pure_metamodel_valuespecification_SimpleFunctionExpression_Impl (org.finos.legend.pure.generated.Root_meta_pure_metamodel_valuespecification_SimpleFunctionExpression_Impl)1 PackageableElement (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement)1