Search in sources :

Example 1 with Metadata

use of org.finos.legend.pure.runtime.java.compiled.metadata.Metadata 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)

Aggregations

RichIterable (org.eclipse.collections.api.RichIterable)1 MutableList (org.eclipse.collections.api.list.MutableList)1 PureExecutionException (org.finos.legend.pure.m3.exception.PureExecutionException)1 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)1 Metadata (org.finos.legend.pure.runtime.java.compiled.metadata.Metadata)1