net.sf.aguacate.function.spi.impl.FunctionApply Maven / Gradle / Ivy
package net.sf.aguacate.function.spi.impl;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import net.sf.aguacate.function.Function;
import net.sf.aguacate.function.FunctionContext;
import net.sf.aguacate.function.FunctionEvalResult;
import net.sf.aguacate.function.spi.AbstractFunction1;
import net.sf.aguacate.util.parameter.Parameter;
/**
*
* {
* "name": "...",
* "message": "...",
* "type" : "APPLY",
* "parameters" : [
* subctx
* ],
* "validations": [
* ...
* ],
* "methods": [
* ...
* ]
* }
*
*/
public class FunctionApply extends AbstractFunction1 {
private static final Logger LOGGER = LogManager.getLogger(FunctionApply.class);
private final Function[] functions;
public FunctionApply(Collection methods, String name, String message, Parameter parameter,
List list) {
super(methods, name, message, parameter);
this.functions = list.toArray(new Function[list.size()]);
}
@SuppressWarnings("unchecked")
@Override
public FunctionEvalResult evaluate(FunctionContext functionContext, String msg, Object value) {
Map subcontext = (Map) value;
LOGGER.trace("before subcontext: {}", subcontext);
for (Function function : functions) {
FunctionEvalResult result = function.evaluate(functionContext, subcontext);
if (result.isSuccess()) {
String outputName = function.getOutputName();
if (outputName != null) {
String[] outputContext = function.getOutputContext();
if (outputContext != null && outputContext.length > 0) {
Map ctx = subcontext;
for (String ctxName : outputContext) {
Map temp = (Map) ctx.get(ctxName);
// TODO: check if temp is null
ctx = temp;
}
LOGGER.trace("working ctx: {}", ctx);
ctx.put(outputName, result.getData());
} else {
subcontext.put(outputName, result.getData());
}
}
} else {
return result;
}
LOGGER.trace("in subcontext: {}", subcontext);
}
LOGGER.trace("after subcontext: {}", subcontext);
return new FunctionEvalResult(true);
}
}