Search in sources :

Example 1 with FunctionDefinition

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.FunctionDefinition in project legend-pure by finos.

the class FunctionDefinitionProcessor method processExpression.

private static void processExpression(FunctionDefinition<?> functionDefinition, Matcher matcher, ProcessorState processorState, ProcessorSupport processorSupport, int i, ValueSpecification expression) {
    processorState.resetVariables();
    PostProcessor.processElement(matcher, expression, processorState, processorSupport);
    if (expression._usageContext() == null) {
        ExpressionSequenceValueSpecificationContext usageContext = (ExpressionSequenceValueSpecificationContext) processorSupport.newAnonymousCoreInstance(null, M3Paths.ExpressionSequenceValueSpecificationContext);
        usageContext._offset(i);
        usageContext._functionDefinition(functionDefinition);
        expression._usageContext(usageContext);
    }
}
Also used : ExpressionSequenceValueSpecificationContext(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ExpressionSequenceValueSpecificationContext)

Example 2 with FunctionDefinition

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.FunctionDefinition in project legend-pure by finos.

the class GenericTypeTraceability method addTraceForFunctionDefinition.

public static void addTraceForFunctionDefinition(FunctionDefinition<?> functionDefinition, ModelRepository repository, ProcessorSupport processorSupport) {
    FunctionType functionType = (FunctionType) processorSupport.function_getFunctionType(functionDefinition);
    if (functionType._functionCoreInstance().isEmpty()) {
        functionType._functionAdd(functionDefinition);
    }
    addTraceForFunctionType(functionType, repository, processorSupport);
}
Also used : FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType)

Example 3 with FunctionDefinition

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.FunctionDefinition in project legend-pure by finos.

the class FunctionExecutionCompiled method executeFunction.

private Object executeFunction(CoreInstance functionDefinition, ListIterable<? extends CoreInstance> arguments, CompiledExecutionSupport executionSupport) {
    ProcessorSupport processorSupport = new M3ProcessorSupport(this.context, this.repository);
    Object result;
    try {
        result = this.executeFunction(functionDefinition, arguments, executionSupport, this.javaCompilerEventHandler.getJavaCompiler().getClassLoader(), processorSupport);
    } catch (PureException pe) {
        // Rethrow as is to keep the original error
        throw pe;
    } catch (Exception e) {
        StringBuilder builder = new StringBuilder("Error executing ");
        try {
            org.finos.legend.pure.m3.navigation.function.Function.print(builder, functionDefinition, processorSupport);
        } catch (Exception ignore) {
            builder = new StringBuilder("Error executing ");
            builder.append(functionDefinition);
        }
        builder.append(". ");
        if (e.getMessage() != null) {
            builder.append(e.getMessage());
        }
        throw new RuntimeException(builder.toString(), e);
    }
    if (result == null) {
        result = Lists.immutable.empty();
    }
    return result;
}
Also used : PureException(org.finos.legend.pure.m4.exception.PureException) ProcessorSupport(org.finos.legend.pure.m3.navigation.ProcessorSupport) M3ProcessorSupport(org.finos.legend.pure.m3.navigation.M3ProcessorSupport) M3ProcessorSupport(org.finos.legend.pure.m3.navigation.M3ProcessorSupport) PureExecutionStreamingException(org.finos.legend.pure.m3.exception.PureExecutionStreamingException) PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException) IOException(java.io.IOException) PureException(org.finos.legend.pure.m4.exception.PureException)

Example 4 with FunctionDefinition

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.FunctionDefinition in project legend-pure by finos.

the class FunctionExecutionCompiled method executeFunction.

private Object executeFunction(CoreInstance functionDefinition, ListIterable<? extends CoreInstance> coreInstances, CompiledExecutionSupport executionSupport, ClassLoader cl, ProcessorSupport processorSupport) {
    // Manage Parameters ----------------------------
    ListIterable<? extends CoreInstance> parameters = Instance.getValueForMetaPropertyToManyResolved(processorSupport.function_getFunctionType(functionDefinition), M3Properties.parameters, processorSupport);
    Class[] paramClasses = new Class[parameters.size()];
    Object[] params = new Object[parameters.size()];
    Metadata metamodel = this.metadataCompilerEventHandler.getMetadata();
    int i = 0;
    if (parameters.size() != coreInstances.size()) {
        StringBuilder builder = new StringBuilder();
        org.finos.legend.pure.m3.navigation.function.Function.print(builder, functionDefinition, processorSupport);
        String message = "Error executing the function:" + builder + ". Mismatch between the number of function parameters (" + parameters.size() + ") and the number of supplied arguments (" + coreInstances.size() + ")";
        throw new PureExecutionException(message);
    }
    for (CoreInstance param : parameters) {
        Object val = GraphSerializer.valueSpecToJavaObject(coreInstances.get(i), this.context, this.getProcessorSupport(), metamodel);
        CoreInstance paramMult = Instance.getValueForMetaPropertyToOneResolved(param, M3Properties.multiplicity, processorSupport);
        if (Multiplicity.isToOne(paramMult, true)) {
            String t = TypeProcessor.typeToJavaPrimitiveSingle(Instance.getValueForMetaPropertyToOneResolved(param, M3Properties.genericType, processorSupport), processorSupport);
            paramClasses[i] = CompiledSupport.convertFunctionTypeStringToClass(t, cl);
            if (val instanceof MutableList) {
                MutableList valList = (MutableList) val;
                if (valList.size() != 1) {
                    throw new RuntimeException("Expected exactly one value, found " + valList.size());
                }
                val = valList.get(0);
            }
        } else if (Multiplicity.isToOne(paramMult, false)) {
            String className = TypeProcessor.typeToJavaObjectSingle(Instance.getValueForMetaPropertyToOneResolved(param, M3Properties.genericType, processorSupport), false, processorSupport);
            paramClasses[i] = CompiledSupport.loadClass(className, cl);
            if (val instanceof MutableList) {
                MutableList valList = (MutableList) val;
                switch(valList.size()) {
                    case 0:
                        {
                            val = null;
                            break;
                        }
                    case 1:
                        {
                            val = valList.get(0);
                            break;
                        }
                    default:
                        {
                            throw new RuntimeException("Expected at most one value, found " + valList.size());
                        }
                }
            }
        } else {
            paramClasses[i] = RichIterable.class;
            if (!(val instanceof MutableList)) {
                val = FastList.newListWith(val);
            }
        }
        params[i] = val;
        i++;
    }
    return CompiledSupport.executeFunction(functionDefinition, paramClasses, params, executionSupport);
// -----------------------------------------------
}
Also used : PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException) Metadata(org.finos.legend.pure.runtime.java.compiled.metadata.Metadata) RichIterable(org.eclipse.collections.api.RichIterable) MutableList(org.eclipse.collections.api.list.MutableList) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance)

Example 5 with FunctionDefinition

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.FunctionDefinition in project legend-pure by finos.

the class FunctionDefinitionProcessor method findReturnTypesForLambda.

private static void findReturnTypesForLambda(FunctionDefinition<?> function, FunctionType functionType, ProcessorSupport processorSupport) throws PureCompilationException {
    ValueSpecification lastExpression = function._expressionSequence().toList().getLast();
    if (functionType._returnType() == null) {
        GenericType lastExpressionGenericType = lastExpression._genericType();
        if (lastExpressionGenericType == null) {
            throw new PureCompilationException(lastExpression.getSourceInformation(), "Final expression has no generic type");
        }
        GenericType lambdaReturnType = (GenericType) org.finos.legend.pure.m3.navigation.generictype.GenericType.copyGenericTypeAsInferredGenericType(lastExpressionGenericType, function.getSourceInformation(), processorSupport);
        if (function._classifierGenericType() != null && function._classifierGenericType()._typeArguments() != null && function._classifierGenericType()._typeArguments().size() > 0 && function._classifierGenericType()._typeArguments().toList().getFirst()._rawTypeCoreInstance() != null) {
            ((FunctionType) ImportStub.withImportStubByPass(function._classifierGenericType()._typeArguments().toList().getFirst()._rawTypeCoreInstance(), processorSupport))._returnType(lambdaReturnType);
        }
    }
    if (functionType._returnMultiplicity() == null) {
        Multiplicity lastExpressionMultiplicity = lastExpression._multiplicity();
        if (lastExpressionMultiplicity == null) {
            throw new PureCompilationException(lastExpression.getSourceInformation(), "Final expression has no multiplicity");
        }
        if (function._classifierGenericType() != null && function._classifierGenericType()._typeArguments() != null && function._classifierGenericType()._typeArguments().size() > 0 && function._classifierGenericType()._typeArguments().toList().getFirst()._rawTypeCoreInstance() != null) {
            ((FunctionType) ImportStub.withImportStubByPass(function._classifierGenericType()._typeArguments().toList().getFirst()._rawTypeCoreInstance(), processorSupport))._returnMultiplicity(lastExpressionMultiplicity);
        }
    }
}
Also used : GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) Multiplicity(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Aggregations

ValueSpecification (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification)12 FunctionType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType)8 GenericType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType)8 PureExecutionException (org.finos.legend.pure.m3.exception.PureExecutionException)6 ProcessorSupport (org.finos.legend.pure.m3.navigation.ProcessorSupport)5 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)5 FunctionDefinition (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.FunctionDefinition)4 Multiplicity (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity)4 PureCompilationException (org.finos.legend.pure.m4.exception.PureCompilationException)4 VariableExpression (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression)3 PureException (org.finos.legend.pure.m4.exception.PureException)3 IOException (java.io.IOException)2 RichIterable (org.eclipse.collections.api.RichIterable)2 Function (org.eclipse.collections.api.block.function.Function)2 MutableList (org.eclipse.collections.api.list.MutableList)2 UnbindState (org.finos.legend.pure.m3.compiler.unload.unbind.UnbindState)2 Constraint (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.constraint.Constraint)2 LambdaFunction (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction)2 NewPropertyRouteNode (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.treepath.NewPropertyRouteNode)2 Type (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type)2