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