Search in sources :

Example 1 with ExecutionSupport

use of org.finos.legend.pure.m3.execution.ExecutionSupport 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 2 with ExecutionSupport

use of org.finos.legend.pure.m3.execution.ExecutionSupport 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 3 with ExecutionSupport

use of org.finos.legend.pure.m3.execution.ExecutionSupport in project legend-pure by finos.

the class FunctionExecutionInterpreted method start.

@Override
public CoreInstance start(CoreInstance function, ListIterable<? extends CoreInstance> arguments) {
    this.cancelExecution.set(false);
    Exception isException = null;
    ExecutionSupport executionSupport = new ExecutionSupport();
    try {
        CoreInstance result = this.executeFunction(false, FunctionCoreInstanceWrapper.toFunction(function), arguments, new Stack<MutableMap<String, CoreInstance>>(), new Stack<MutableMap<String, CoreInstance>>(), VariableContext.newVariableContext(), null, VoidProfiler.VOID_PROFILER, new InstantiationContext(), executionSupport);
        return result;
    } catch (Exception ex) {
        isException = ex;
        throw ex;
    } finally {
        executionSupport.executionEnd(isException);
    }
}
Also used : InstantiationContext(org.finos.legend.pure.runtime.java.interpreted.natives.core.InstantiationContext) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) MutableMap(org.eclipse.collections.api.map.MutableMap) PureAssertFailException(org.finos.legend.pure.m3.exception.PureAssertFailException) VariableNameConflictException(org.finos.legend.pure.runtime.java.interpreted.VariableContext.VariableNameConflictException) PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException) IOException(java.io.IOException) PureException(org.finos.legend.pure.m4.exception.PureException)

Example 4 with ExecutionSupport

use of org.finos.legend.pure.m3.execution.ExecutionSupport in project legend-pure by finos.

the class Tag method execute.

@Override
public CoreInstance execute(ListIterable<? extends CoreInstance> params, Stack<MutableMap<String, CoreInstance>> resolvedTypeParameters, Stack<MutableMap<String, CoreInstance>> resolvedMultiplicityParameters, VariableContext variableContext, CoreInstance functionExpressionToUseInStack, Profiler profiler, InstantiationContext instantiationContext, ExecutionSupport executionSupport, Context context, ProcessorSupport processorSupport) throws PureExecutionException {
    String tagName = Instance.getValueForMetaPropertyToOneResolved(params.get(1), M3Properties.values, processorSupport).getName();
    CoreInstance profile = Instance.getValueForMetaPropertyToOneResolved(params.get(0), M3Properties.values, processorSupport);
    CoreInstance tag = Profile.findTag(profile, tagName);
    if (tag == null) {
        throw new PureExecutionException(functionExpressionToUseInStack.getSourceInformation(), "The tag '" + tagName + "' can't be found in the profile '" + profile.getName() + "'");
    }
    return ValueSpecificationBootstrap.wrapValueSpecification(tag, ValueSpecification.isExecutable(params.get(0), processorSupport), processorSupport);
}
Also used : PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance)

Example 5 with ExecutionSupport

use of org.finos.legend.pure.m3.execution.ExecutionSupport in project legend-pure by finos.

the class ToMultiplicity method execute.

@Override
public CoreInstance execute(ListIterable<? extends CoreInstance> params, Stack<MutableMap<String, CoreInstance>> resolvedTypeParameters, Stack<MutableMap<String, CoreInstance>> resolvedMultiplicityParameters, VariableContext variableContext, CoreInstance functionExpressionToUseInStack, Profiler profiler, InstantiationContext instantiationContext, ExecutionSupport executionSupport, Context context, ProcessorSupport processorSupport) throws PureExecutionException {
    CoreInstance returnMultiplicity = getReturnMultiplicity(processorSupport);
    CoreInstance param = params.get(0);
    if (Multiplicity.multiplicitiesEqual(returnMultiplicity, Instance.getValueForMetaPropertyToOneResolved(param, M3Properties.multiplicity, processorSupport))) {
        return param;
    }
    ListIterable<? extends CoreInstance> values = Instance.getValueForMetaPropertyToManyResolved(param, M3Properties.values, processorSupport);
    if (!Multiplicity.isValid(returnMultiplicity, values.size())) {
        throw new PureExecutionException(functionExpressionToUseInStack.getSourceInformation(), "Cannot cast a collection of size " + values.size() + " to multiplicity " + Multiplicity.print(returnMultiplicity));
    }
    CoreInstance result = this.repository.newAnonymousCoreInstance(param.getSourceInformation(), param.getClassifier());
    Instance.addValueToProperty(result, M3Properties.genericType, Instance.getValueForMetaPropertyToOneResolved(param, M3Properties.genericType, processorSupport), processorSupport);
    Instance.setValuesForProperty(result, M3Properties.values, values, processorSupport);
    Instance.addValueToProperty(result, M3Properties.multiplicity, returnMultiplicity, processorSupport);
    return result;
}
Also used : PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance)

Aggregations

PureExecutionException (org.finos.legend.pure.m3.exception.PureExecutionException)56 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)55 RichIterable (org.eclipse.collections.api.RichIterable)13 CompiledExecutionSupport (org.finos.legend.pure.runtime.java.compiled.execution.CompiledExecutionSupport)12 ProcessorSupport (org.finos.legend.pure.m3.navigation.ProcessorSupport)11 ListIterable (org.eclipse.collections.api.list.ListIterable)10 MutableMap (org.eclipse.collections.api.map.MutableMap)10 PureException (org.finos.legend.pure.m4.exception.PureException)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 MutableList (org.eclipse.collections.api.list.MutableList)7 Method (java.lang.reflect.Method)6 Function (org.eclipse.collections.api.block.function.Function)6 FunctionType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType)6 SourceInformation (org.finos.legend.pure.m4.coreinstance.SourceInformation)6 IOException (java.io.IOException)5 Property (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property)5 ExecutionSupport (org.finos.legend.pure.m3.execution.ExecutionSupport)5 JSONObject (org.json.simple.JSONObject)5 TimeoutException (java.util.concurrent.TimeoutException)4 Pair (org.eclipse.collections.api.tuple.Pair)4