use of org.finos.legend.pure.runtime.java.compiled.metadata.JavaMethodWithParamsSharedPureFunction in project legend-pure by finos.
the class CompiledSupport method executeFunction.
public static Object executeFunction(String uniqueFunctionId, ConcreteFunctionDefinition<?> functionDefinition, Class<?>[] paramClasses, Object[] params, ExecutionSupport es) {
SharedPureFunction<?> spf = ((CompiledExecutionSupport) es).getFunctionCache().getIfAbsentPutJavaFunctionForPureFunction(functionDefinition, () -> {
try {
Class<?> clazz = ((CompiledExecutionSupport) es).getClassLoader().loadClass(JavaPackageAndImportBuilder.rootPackage() + '.' + uniqueFunctionId);
String functionName = FunctionProcessor.functionNameToJava(functionDefinition);
Method method = getFunctionMethod(clazz, functionName, functionDefinition, paramClasses, params, es);
return new JavaMethodWithParamsSharedPureFunction(method, paramClasses, functionDefinition.getSourceInformation());
} catch (ClassNotFoundException e) {
throw new PureExecutionException("Unable to execute " + uniqueFunctionId, e);
}
});
return spf.execute(Lists.fixedSize.of(params).with(es), es);
}
use of org.finos.legend.pure.runtime.java.compiled.metadata.JavaMethodWithParamsSharedPureFunction in project legend-pure by finos.
the class Pure method findSharedPureFunction.
public static SharedPureFunction<?> findSharedPureFunction(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.Function<?> func, Bridge bridge, ExecutionSupport es) {
if (func instanceof Property) {
Type srcType = func._classifierGenericType()._typeArguments().getFirst()._rawType();
return ((CompiledExecutionSupport) es).getFunctionCache().getIfAbsentPutFunctionForClassProperty(srcType, func, ((CompiledExecutionSupport) es).getClassLoader());
}
if (func instanceof Path) {
return new PureFunction1<Object, Object>() {
@Override
public Object execute(ListIterable vars, ExecutionSupport es) {
return value(vars.getFirst(), es);
}
@Override
public Object value(Object o, ExecutionSupport es) {
RichIterable<?> result = ((Path<?, ?>) func)._path().injectInto(CompiledSupport.toPureCollection(o), (mutableList, path) -> {
if (!(path instanceof PropertyPathElement)) {
throw new PureExecutionException("Only PropertyPathElement is supported yet!");
}
return mutableList.flatCollect(instance -> {
MutableList<Object> parameters = ((PropertyPathElement) path)._parameters().collect(o1 -> o1 instanceof InstanceValue ? ((InstanceValue) o1)._values() : null, Lists.mutable.with(instance));
return CompiledSupport.toPureCollection(evaluate(es, ((PropertyPathElement) path)._property(), bridge, parameters.toArray()));
});
});
Multiplicity mult = func._classifierGenericType()._multiplicityArguments().getFirst();
return bridge.hasToOneUpperBound().apply(mult, es) ? result.getFirst() : result;
}
};
}
if (func instanceof LambdaCompiledExtended) {
return ((LambdaCompiledExtended) func).pureFunction();
}
if (func instanceof LambdaFunction) {
LambdaFunction<?> lambda = (LambdaFunction<?>) func;
if (canFindNativeOrLambdaFunction(es, func)) {
return getNativeOrLambdaFunction(es, func);
}
if (Reactivator.canReactivateWithoutJavaCompilation(lambda, es, getOpenVariables(func, bridge), bridge)) {
PureMap openVariablesMap = getOpenVariables(func, bridge);
return DynamicPureLambdaFunctionImpl.createPureLambdaFunction(lambda, openVariablesMap.getMap(), bridge);
}
return ((LambdaCompiledExtended) CompiledSupport.dynamicallyBuildLambdaFunction(func, es)).pureFunction();
}
if (func instanceof ConcreteFunctionDefinition) {
return ((CompiledExecutionSupport) es).getFunctionCache().getIfAbsentPutJavaFunctionForPureFunction(func, () -> {
try {
RichIterable<? extends VariableExpression> params = ((FunctionType) func._classifierGenericType()._typeArguments().getFirst()._rawType())._parameters();
Class<?>[] paramClasses = new Class[params.size() + 1];
int index = 0;
for (VariableExpression o : params) {
paramClasses[index] = pureTypeToJavaClassForExecution(o, bridge, es);
index++;
}
paramClasses[params.size()] = ExecutionSupport.class;
Method method = ((CompiledExecutionSupport) es).getClassLoader().loadClass(JavaPackageAndImportBuilder.rootPackage() + "." + IdBuilder.sourceToId(func.getSourceInformation())).getMethod(FunctionProcessor.functionNameToJava(func), paramClasses);
return new JavaMethodWithParamsSharedPureFunction(method, paramClasses, func.getSourceInformation());
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
MutableMap<String, SharedPureFunction<?>> functions;
try {
Class<?> myClass = ((CompiledExecutionSupport) es).getClassLoader().loadClass(JavaPackageAndImportBuilder.rootPackage() + "." + IdBuilder.sourceToId(func.getSourceInformation()));
functions = (MutableMap<String, SharedPureFunction<?>>) myClass.getDeclaredField("__functions").get(null);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
return functions.get(func.getName());
}
use of org.finos.legend.pure.runtime.java.compiled.metadata.JavaMethodWithParamsSharedPureFunction in project legend-pure by finos.
the class Pure method evaluate.
public static Object evaluate(ExecutionSupport es, org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.Function<?> func, Bridge bridge, Object... instances) {
if (func instanceof Property) {
return getSharedPureFunction(func, bridge, es).execute(FastList.newListWith(instances[0]), es);
}
if (func instanceof Path) {
return getSharedPureFunction(func, bridge, es).execute(FastList.newListWith(instances[0]), es);
}
if (func instanceof LambdaCompiledExtended) {
return getSharedPureFunction(func, bridge, es).execute(Lists.fixedSize.with(instances), es);
}
if (func instanceof ConcreteFunctionDefinition) {
JavaMethodWithParamsSharedPureFunction p = (JavaMethodWithParamsSharedPureFunction) getSharedPureFunction(func, bridge, es);
Class<?>[] paramClasses = p.getParametersTypes();
int l = paramClasses.length;
MutableList<Object> paramInstances = FastList.newList();
for (int i = 0; i < (l - 1); i++) {
if (instances[i] instanceof RichIterable && paramClasses[i] != RichIterable.class) {
paramInstances.add(CompiledSupport.toOne(((RichIterable<?>) instances[i]), null));
} else {
paramInstances.add(instances[i]);
}
}
paramInstances.add(es);
return p.execute(paramInstances, es);
}
if (func instanceof LambdaFunction) {
return getSharedPureFunction(func, bridge, es).execute(Lists.fixedSize.with(instances), es);
}
SharedPureFunction<?> reflectiveNative = getSharedPureFunction(func, bridge, es);
return reflectiveNative.execute(instances == null ? Lists.mutable.empty() : FastList.newListWith(instances), es);
}
Aggregations