import java.lang.Integer; import java.lang.Boolean; import java.util.HashMap; /** *

* Implementation of ILogicVarBundle that just wraps an int map. *

*

* ILogicVarBundle is the interface for * the discrete component of robotic agent state. * This is the component of DiscreteAgentState that gets fed back * into the agent for the next round of *

* @author Michael Schuresko * @version %I%, %G% * @since 1.0 */ public class LogicVarIntLookup implements ILogicVarBundle { HashMap m_boolMap; HashMap m_intMap; int m_nId; public LogicVarIntLookup() { m_nId = -1; m_boolMap = new HashMap(); m_intMap = new HashMap(); } public LogicVarIntLookup(LogicVarIntLookup src) { copyFrom(src); } /** * just a little thing to help out derived classes. * @param src */ protected void copyFrom(LogicVarIntLookup src) { m_nId = src.m_nId; m_boolMap = new HashMap(src.m_boolMap); m_intMap = new HashMap(src.m_intMap); } /** * makes a copy and returns it */ public ILogicVarBundle makeCopy() { return (ILogicVarBundle)(new LogicVarIntLookup(this)); } /** * Does nothing, for the time being. */ public void init() { // do nothing for the time being } /** * Gets unique Id of this agent */ public int getId() { return m_nId; } /** * Sets unique Id of this agent */ public void setId(int nId) { m_nId = nId; } /** * gets an Integer (wrapping an int) referred to by an object. * returns null if refObject is of invalid type * (in this case the correct type is Integer) * or indexes an invalid logic var. * * Note: The original specification only allowed boolean * variables in this place. Obviously some applications may * want to use collections of boolean variables to represent * integers over a finite fixed range, or rationals with * finite fixed precision over a finite fixed range. * This function is merely provided as a shortcut towards this end. */ public Integer getIntVar(Object refObject) { return m_intMap.get(refObject); } /** * Removes any values indexed by refObject * @param refObject remove values associated with this. */ public void removeVar(Object refObject) { if(m_intMap.get(refObject) != null) { m_intMap.put((Integer)refObject, null); } if(m_boolMap.get(refObject) != null) { m_boolMap.put((Integer)refObject, null); } } /** * inserts an integer value (see getIntvar). Returns fals * if this insert was not permitted * @see LogicVarIntLookup#getIntVar */ public boolean insertIntVar(Object refObject, int nValue) { try { Integer intRef = (Integer)refObject; if(intRef == null) { return false; } m_intMap.put(intRef, new Integer(nValue)); return true; } catch(ClassCastException e) { return false; } } /** * gets a Boolean (wrapping a boolean) referred to by an object. * returns null if refObject is of invalid type * or indexes an invalid logic var. */ public Boolean getBoolVar(Object refObject) { Integer intRef = (Integer)refObject; if(intRef == null) { return null; } return (Boolean)(m_boolMap.get(refObject)); } /** * inserts an boolean value (see getBoolVar). Returns fals * if this insert was not permitted * @see LogicVarIntLookup#getBoolVar */ public boolean insertBoolVar(Object refObject, boolean bValue) { try { Integer intRef = (Integer)refObject; if(intRef == null) { return false; } m_boolMap.put(intRef, new Boolean(bValue)); return true; } catch(ClassCastException e) { return false; } } }