import java.lang.Integer; /** *

Discrete state for tree re-arrangement agent

*

Future work -- currently the "target topology information" * specific to "Formation Morphing Algorithm" is combined with this class. * This should not be the case. The two should be separated, and * Formation Morphing Algorithm should have its own class to store * this info. *

* * @author Michael Schuresko * @version %I%, %G% * @since 1.0 */ public class TreeAgentState implements ILogicVarBundle, IMsg { ILogicVarBundle m_algSpecificVars; /** * Gets logic vars specific to this particular algorithm * implementing the tree connectivity protocol */ public ILogicVarBundle getAlgVars() { return m_algSpecificVars; } /** * Sets logic vars specific to this particular algorithm * implementing the tree connectivity protocol */ public void setAlgVars(ILogicVarBundle algVars) { m_algSpecificVars = algVars; } /** * makes a copy and returns it */ public TreeAgentState makeCopy() { return new TreeAgentState(this); } /** * Does nothing, for the time being. */ public void init() { } /** * gets an Integer (wrapping an int) referred to by an object. * returns null if refObject is of invalid type * 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_algSpecificVars.getIntVar(refObject); } /** * Removes any values indexed by refObject * @param refObject remove values associated with this. */ public void removeVar(Object refObject) { m_algSpecificVars.removeVar(refObject); } /** * inserts an integer value (see * {@link TreeAgentState#getIntVar}). Returns false * if this insert was not permitted * @see TreeAgentState#getIntVar */ public boolean insertIntVar(Object refObject, int nValue) { return m_algSpecificVars.insertIntVar(refObject, nValue); } /** * 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) { return m_algSpecificVars.getBoolVar(refObject); } /** * inserts an boolean value (see getBoolVar). Returns fals * if this insert was not permitted * @see TreeAgentState#getBoolVar */ public boolean insertBoolVar(Object refObject, boolean bValue) { return m_algSpecificVars.insertBoolVar(refObject, bValue); } int m_nDepth, m_nParentId; int m_nProposedParent; int m_nRoundId; boolean m_bDfsFinished; int m_nFirstVisit; int m_nLastVisit; int m_nTargetDepth; int m_nParentsParent; int m_nTargNumChilds; boolean m_bParDepLess; public int getId() { return m_algSpecificVars.getId(); } public void setId(int nId) { m_algSpecificVars.setId(nId); } public boolean getDfsFinished() { return m_bDfsFinished; } public void setDfsFinished(boolean bFinished) { m_bDfsFinished = bFinished; } public int getDepthEst() { return m_nDepth; } public void updateDepthEst(TreeAgentState stateParent) { m_nDepth = stateParent.getDepthEst() +1; } /** * Not the preferred way to do this */ public void setDepthEst(int nNewDepth) { m_nDepth = nNewDepth; } public void resetDepthEst() { m_nDepth = 0; } public int getParentId() { return m_nParentId; } public void setParentId(int nParentId) { m_nParentId = nParentId; } public int getParentProp() { return m_nProposedParent; } public void setParentProp(int nParentProp) { m_nProposedParent = nParentProp; } public boolean getParDepLess() { return m_bParDepLess; } public void setParDepLess(boolean bParDepLess) { m_bParDepLess = bParDepLess; } public void setParDepLess(TreeAgentState stateParent) { if(m_nParentId == stateParent.getId()) { m_bParDepLess = (stateParent.m_nDepth < m_nDepth); } } /** * faked enum for differentiating rounds */ public static final int ROUND_ATTACH=0; /** * faked enum for differentiating rounds */ public static final int ROUND_UPDATE_DEPTH=1; /** * faked enum for differentiating rounds */ public static final int ROUND_SET_PARLESS=2; /** * faked enum for differentiating rounds */ public static final int ROUND_PROPOSE_NEW=3; /** * @param nDepth initial depth estimate (see 2006i) * @param nParentId initial parent id * @param bDfsFinished whether depth first search to determine * parent ids has finished for this node and all its children */ public TreeAgentState(int nDepth, int nParentId, boolean bDfsFinished) { m_nParentsParent = -1; m_nRoundId=0; m_nDepth = nDepth; m_nParentId = nParentId; m_algSpecificVars = new LogicVarIntLookup(); m_bDfsFinished = bDfsFinished; // now for target initialization. m_nFirstVisit = -1; m_nLastVisit = -1; m_nTargetDepth = -1; m_nTargNumChilds = -1; setId(-1); m_bParDepLess = false; m_nProposedParent = m_nParentId; } /** *

Sets depth estimate to zero.

*

Final target configuration and topology information * need to be set seperately.

* @param nParentId initial parent id * @param bDfsFinished whether depth first search to determine * parent ids has finished for this node and all its children */ public TreeAgentState(int nParentId, boolean bDfsFinished) { m_nParentsParent = -1; m_nRoundId=0; m_nDepth = 0; m_nParentId = nParentId; m_algSpecificVars = new LogicVarIntLookup(); m_bDfsFinished = bDfsFinished; // now for target initialization. m_nFirstVisit = -1; m_nLastVisit = -1; m_nTargetDepth = -1; m_nTargNumChilds = -1; m_nProposedParent = m_nParentId; m_bParDepLess = false; setId(-1); } /** *

Sets depth estimate to zero, parentId to -1, and * m_bDfsFinished to false

*

Final target configuration and topology information * need to be set seperately.

*/ public TreeAgentState() { m_nParentsParent = -1; m_nRoundId=0; m_nDepth = 0; m_nParentId = -1; m_bDfsFinished = false; m_algSpecificVars = new LogicVarIntLookup(); // now for target initialization. m_nFirstVisit = -1; m_nLastVisit = -1; m_nTargetDepth = -1; m_nTargNumChilds = -1; setId(-1); m_nProposedParent = m_nParentId; m_bParDepLess = false; } /** * Copy constructor - copies everything * target information copied as well (valid if valid in src) * @param src makes new copies of everything from this state */ public TreeAgentState(TreeAgentState src) { m_nParentsParent = src.m_nParentsParent; m_nRoundId=src.m_nRoundId; m_nDepth = src.m_nDepth; m_nParentId = src.m_nParentId; m_bDfsFinished = src.m_bDfsFinished; m_algSpecificVars = src.m_algSpecificVars.makeCopy(); // now for target initialization. m_nFirstVisit = src.m_nFirstVisit; m_nLastVisit = src.m_nLastVisit; m_nTargetDepth = src.m_nTargetDepth; m_nTargNumChilds = src.m_nTargNumChilds; setId(src.getId()); m_nProposedParent = src.m_nProposedParent; m_bParDepLess = src.m_bParDepLess; } public int getParentsPar() { return m_nParentsParent; } public void setParentsPar(int nParentsParent) { m_nParentsParent = nParentsParent; } /** * Increment round counter (e.g which round of the algorithm we're on) * This is essential for * correct negotiation of constraint tree topology re-arrangements * @return this */ public TreeAgentState incrRound() { m_nRoundId = (m_nRoundId+1)%4; return this; } /** * accessor for current round */ public int getCurrRound() { return m_nRoundId; } /** * returns depth for tree topology storage * @return depth for tree topology storage */ public int getTargetDepth() { return m_nTargetDepth; } /** * @param nDepth new target depth (do not confuse with depth estimate) * @return this */ public TreeAgentState setTargetDepth(int nDepth) { m_nTargetDepth = nDepth; return this; } /** * @param stateB state to compare to * @return whether stateB is an descendent of this in the final tree */ public boolean isAncest(TreeAgentState stateB) { return (stateB.m_nFirstVisit > m_nFirstVisit) && (stateB.m_nLastVisit <= m_nLastVisit); } /** * Sets discrete (topological) component of * target config info for this state * @param nDepthFin final depth in target config * @param nFirst used by IsAncest to check ancestor relations -- see paper * @param nLast used by IsAncest to check ancestor relations -- see paper * @return this */ public TreeAgentState setTargetConfig(int nDepthFin, int nFirst, int nLast, int nNumChilds) { m_nTargetDepth = nDepthFin; m_nFirstVisit = nFirst; m_nLastVisit = nLast; m_nTargNumChilds = nNumChilds; return this; } public int getFirstVisit() { return m_nFirstVisit; } public int getLastVisit() { return m_nLastVisit; } public int getTargDepth() { return m_nTargetDepth; } public int getTargNChildren() {return m_nTargNumChilds; } }