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;
}
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);
// -----------------------------------------------
}
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);
}
}
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);
}
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;
}
Aggregations