All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.aguacate.function.spi.FuncionExecutor Maven / Gradle / Ivy

package net.sf.aguacate.function.spi;

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;

public class FuncionExecutor {

	private static final Logger LOGGER = LogManager.getLogger(FuncionExecutor.class);

	private static final FunctionEvalResult DEFAULT = new FunctionEvalResult(null, null);

	private FuncionExecutor() {
	}

	public static FunctionEvalResult execute(Function[] functions, FunctionContext functionContext,
			Map context) {
		for (Function function : functions) {
			String name = function.getName();
			LOGGER.trace("trying to execute {} with: {}", name, context);
			FunctionEvalResult result = function.evaluate(functionContext, context);
			if (result.isSuccess()) {
				LOGGER.debug("succesful execution of {}", name);
				String outputName = function.getOutputName();
				if (outputName != null) {
					String[] outputContext = function.getOutputContext();
					if (outputContext != null && outputContext.length > 0) {
						Map ctx = context;
						for (String ctxName : outputContext) {
							@SuppressWarnings("unchecked")
							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 {
						context.put(outputName, result.getData());
					}
				}
			} else {
				return result;
			}
			LOGGER.trace("in subcontext: {}", context);
		}
		return DEFAULT;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy