use of org.finos.legend.pure.m3.coreinstance.meta.pure.router.RoutedValueSpecification in project legend-pure by finos.
the class Reactivator method reactivateWithoutJavaCompilationImpl.
private static Object reactivateWithoutJavaCompilationImpl(final ValueSpecification valueSpecification, final PureMap lambdaOpenVariablesMap, final ExecutionSupport es, final boolean atRoot, Bridge bridge) {
if (valueSpecification instanceof RoutedValueSpecification) {
return reactivateWithoutJavaCompilationImpl(((RoutedValueSpecification) valueSpecification)._value(), lambdaOpenVariablesMap, es, atRoot, bridge);
} else if (valueSpecification instanceof InstanceValue) {
InstanceValue iv = (InstanceValue) valueSpecification;
RichIterable<?> result = iv._values().flatCollect(new org.eclipse.collections.api.block.function.Function<Object, RichIterable<Object>>() {
@Override
public RichIterable<Object> valueOf(Object value) {
Object result;
if (value instanceof ValueSpecification) {
result = reactivateWithoutJavaCompilationImpl((ValueSpecification) value, lambdaOpenVariablesMap, es, false, bridge);
} else if ((!lambdaOpenVariablesMap.getMap().isEmpty()) && value instanceof KeyExpression) {
KeyExpression ke = (KeyExpression) value;
CompiledProcessorSupport processorSupport = ((CompiledExecutionSupport) es).getProcessorSupport();
Object key = reactivateWithoutJavaCompilationImpl(ke._key(), lambdaOpenVariablesMap, es, false, bridge);
InstanceValue rKey = (InstanceValue) ((CompiledExecutionSupport) es).getProcessorSupport().newCoreInstance("key", M3Paths.InstanceValue, null);
rKey._values(key instanceof RichIterable ? (RichIterable) key : Lists.immutable.with(key));
Object expression = reactivateWithoutJavaCompilationImpl(ke._expression(), lambdaOpenVariablesMap, es, false, bridge);
InstanceValue rExpression = (InstanceValue) ((CompiledExecutionSupport) es).getProcessorSupport().newCoreInstance("key", M3Paths.InstanceValue, null);
rExpression._values(expression instanceof RichIterable ? (RichIterable) expression : Lists.immutable.with(expression));
KeyExpression rKe = ke.copy();
rKe._key(rKey);
rKe._expression(rExpression);
result = rKe;
} else if (value instanceof LambdaFunction) {
LambdaFunction lambdaFunction = (LambdaFunction) value;
UnifiedMap openVariables = UnifiedMap.newMap();
for (Object entry : lambdaOpenVariablesMap.getMap().keyValuesView()) {
Pair<String, org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.List> pair = (Pair<String, org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.List>) entry;
openVariables.put(pair.getOne(), pair.getTwo()._values());
}
for (Object entry : Pure.getOpenVariables(lambdaFunction, bridge).getMap().keyValuesView()) {
Pair<String, org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.List> pair = (Pair<String, org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.List>) entry;
openVariables.put(pair.getOne(), pair.getTwo()._values());
}
result = bridge.lambdaBuilder().value().lambdaFunction(lambdaFunction).pureFunction(DynamicPureLambdaFunctionImpl.createPureLambdaFunction(lambdaFunction, openVariables, bridge));
} else {
result = value;
}
return CompiledSupport.toPureCollection(result);
}
});
return (result != null) && (result.size() == 1) ? result.getFirst() : result;
} else if (valueSpecification instanceof SimpleFunctionExpression) {
SimpleFunctionExpression sfe = (SimpleFunctionExpression) valueSpecification;
Function func = (Function) sfe._func();
if (!canReactivateWithoutJavaCompilationImpl(func, es, atRoot, lambdaOpenVariablesMap, bridge)) {
throw new PureDynamicReactivateException(valueSpecification.getSourceInformation(), "Can not reactivate function, unexpected:" + func._name());
}
RichIterable<?> paramValues = sfe._parametersValues().collect(new org.eclipse.collections.api.block.function.Function<Object, Object>() {
@Override
public Object valueOf(Object value) {
if (value instanceof ValueSpecification) {
Object newValue = reactivateWithoutJavaCompilationImpl((ValueSpecification) value, lambdaOpenVariablesMap, es, false, bridge);
if (newValue instanceof RichIterable) {
return newValue;
} else {
return Lists.fixedSize.of(newValue);
}
} else {
return value;
}
}
});
MutableList<Object> vars = Lists.mutable.withAll(paramValues);
if (sfe._func()._name().equals("new_Class_1__String_1__KeyExpression_MANY__T_1_")) {
// Have to get the first param from the generic type
vars.set(0, Lists.fixedSize.of(sfe._genericType()._rawType()));
} else if (sfe._func()._name().equals("cast_Any_m__T_1__T_m_")) {
// Have to get the second param from the generic type
vars.set(1, Lists.fixedSize.of(sfe._genericType()));
}
return Pure._evaluateToMany(es, bridge, func, vars);
} else if (valueSpecification instanceof VariableExpression) {
String varName = ((VariableExpression) valueSpecification)._name();
if (!lambdaOpenVariablesMap.getMap().containsKey(varName)) {
throw new PureDynamicReactivateException("Attempt to use out of scope variable: " + varName);
}
Object result = lambdaOpenVariablesMap.getMap().get(varName);
return ((org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.List<Object>) result)._values();
} else {
throw new PureDynamicReactivateException(valueSpecification.getSourceInformation(), "Unexpected type to dynamically reactivate: " + valueSpecification.getClass().getName());
}
}
Aggregations