package edu.umich.soar.hpp;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Variable whose values can be represented with
 * a discrete conditional-probability table (CPT).
 * 
 * For convenience, supports a default value for
 * CPT fall-through.
 */
public class HPPMultiVariable extends HPPVariable {
	
	/**
	 * Supports read-only access to the default value
	 * 
	 * @return default value if no CPT match
	 */
	public String getDefaultValue() {
		return myDefaultValue;
	}
	
	/**
	 * Supports read-only access to the CPT [via entries]
	 * 
	 * @return iterator to CPT entry list
	 */
	public Iterator<HPPCPTEntry> getCPT() {
		return myCPT.iterator();
	}
	
	/**
	 * Supports appending to the CPT
	 * 
	 * @param newEntry entry to add to the CPT
	 */
	public void addCPTEntry(HPPCPTEntry newEntry) {
		myCPT.add(newEntry);
	}
	
	/**
	 * For convenience, supports appending multiple entries to the CPT 
	 * 
	 * @param cpt array of entries to add to the CPT
	 */
	protected void addCPTEntries(HPPCPTEntry[] cpt) {
		if (cpt != null) {
			for (HPPCPTEntry e : cpt) {
				addCPTEntry(e);
			}
		}
	}

	/**
	 * Basic constructor - empty CPT
	 * 
	 * @param name variable name
	 * @param defaultValue default value, or null for none
	 */
	public HPPMultiVariable(String name, String defaultValue) {
		super(name);
		
		myDefaultValue = defaultValue;
		myCPT = new LinkedList<HPPCPTEntry>();
	}
	
	/**
	 * Advanced constructor to initialize the CPT
	 * 
	 * @param name variable name
	 * @param defaultValue default value, or null fo rnone
	 * @param cpt initial cpt entries
	 */
	public HPPMultiVariable(String name, String defaultValue, HPPCPTEntry[] cpt) {
		this(name, defaultValue);
		addCPTEntries(cpt);
	}	
	
	/* (non-Javadoc)
	 * @see edu.umich.soar.hpp.HPPVariable#getType()
	 */
	public String getType() {
		return "multi";
	}
	
	private final String myDefaultValue;
	private final List<HPPCPTEntry> myCPT;
}
