package eu.dnetlib.dlms.jdbc;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import eu.dnetlib.dlms.jdbc.server.ParamInfo;
import eu.dnetlib.dlms.jdbc.server.Variable;

/**
 * The context for an AbstractDOLExecutor. An executor needs to know the map of variables, the map of ParamInfo and
 * other context info needed for the execution of ASTNodes.
 * 
 * @author alessia
 * 
 */
public class ExecutorContext implements ContextInterface {

	/** Variables declared in this context. */
	private Map<String, Variable<?>> variables = new HashMap<String, Variable<?>>();
	/** Parameters to be used for the execution of ASTNodes. */
	private Map<String, ParamInfo> parameters = new HashMap<String, ParamInfo>();
	/**
	 * Map containing useful properties for the execution of ASTNode. See I*executor implementations for the values used
	 * in this map.
	 */
	private Map<String, String> context = new HashMap<String, String>();
	/**
	 * Identifiers of the sets that are currently involved in the navigation, that is the sets that are to be used as
	 * source sets for the next query step. For example: A/B -- involvedSets contains the id of the set which is target
	 * of the relation labeled B. A/(B|C) -- involvedSets contains the identifier(s) of set(s) target for both B and C
	 * relations.
	 */
	private Set<Long> involvedSets = new HashSet<Long>();

	/** Constructor. */
	protected ExecutorContext() {
		super();
	}

	/**
	 * Copy constructor.
	 * 
	 * @param context
	 *            ExecutorContext instance to copy
	 */
	public ExecutorContext(final ExecutorContext context) {
		if (context.getVariables() != null)
			this.variables = new HashMap<String, Variable<?>>(context.getVariables());
		if (context.getParameters() != null)
			this.parameters = new HashMap<String, ParamInfo>(context.getParameters());
		if (context.getContext() != null)
			this.context = new HashMap<String, String>(context.getContext());
		if (context.getInvolvedSets() != null)
			this.involvedSets = new HashSet<Long>(context.getInvolvedSets());
	}

	/**
	 * Constructor.
	 * 
	 * @param variables
	 *            variables map of the context
	 * @param parameters
	 *            parameters map of the context
	 * @param context
	 *            context map of the context
	 */
	public ExecutorContext(final Map<String, Variable<?>> variables, final Map<String, ParamInfo> parameters, final Map<String, String> context) {
		super();
		this.variables = variables;
		this.parameters = parameters;
		this.context = context;
	}

	public void setParameters(final Map<String, ParamInfo> parameters) {
		this.parameters = parameters;
	}

	public Map<String, ParamInfo> getParameters() {
		return this.parameters;
	}

	public Map<String, Variable<?>> getVariables() {
		return this.variables;
	}

	public void setVariables(final Map<String, Variable<?>> variables) {
		this.variables = variables;
	}

	public Map<String, String> getContext() {
		return this.context;
	}

	public void setContext(final Map<String, String> context) {
		this.context = context;
	}

	public void setInvolvedSets(final Set<Long> involvedSets) {
		this.involvedSets = involvedSets;
	}

	public Set<Long> getInvolvedSets() {
		return this.involvedSets;
	}

}
