import java.util.Collection;
import java.util.Iterator;
import java.util.HashMap;
public class SimpAgentTreeTopo implements ISimpAgent {
/**
* Some design needs to happen here.
* Need to figure out how to send different things for sent messages
* than for states, despite the fact that the holder class in each
* case is of type TreeConstraintState
* Also : Need to send desired start number to children (different
* for each child), total descendants to parent, and store correct
* start num and num descendants.
*
*
Design
* Needs to store map from child to start number.
* Needs to set own start number to that send by parent.
* Needs to send child start number to appropriate child.
*
* @author mds
*
*/
public static class TreeTopoInfo implements IMsg {
int m_nId;
int m_nNumDesc;
int m_nStart;
HashMap m_mapStartId;
public TreeTopoInfo() {
m_nNumDesc = 1; // 1 for myself first visit, 1 for myself last visit
m_mapStartId = new HashMap(17);
}
public TreeTopoInfo(TreeTopoInfo src) {
m_nId = src.m_nId;
m_nNumDesc = src.m_nNumDesc;
m_nStart = src.m_nStart;
m_mapStartId = new HashMap(src.m_mapStartId.size());
Iterator > iter =
src.m_mapStartId.entrySet().iterator();
while(iter.hasNext()) {
java.util.Map.Entry currEntry = iter.next();
m_mapStartId.put(currEntry.getKey(), currEntry.getValue());
}
}
public TreeTopoInfo makeCopy() {
return new TreeTopoInfo(this);
}
public int getId() { return m_nId; }
public void setId(int nId) { m_nId = nId; }
public int getStart() { return m_nStart; }
public void setStart(int nStart) { m_nStart = nStart; }
public int getNumDesc() { return m_nNumDesc;}
public void setNumDesc(int nNumDesc) { m_nNumDesc = nNumDesc; }
public int incrDesc(int nIncrBy) { m_nNumDesc += nIncrBy; return m_nNumDesc; }
public void handleParent(TreeTopoInfo infParent) {
if(infParent == null) {
return;
}
if(infParent.getId() == getId()) {
m_nStart = 0;
return;
}
if(infParent.m_mapStartId.containsKey(m_nId)) {
m_nStart = infParent.m_mapStartId.get(m_nId) + infParent.getStart();
return;
}
m_nStart = infParent.getStart()+infParent.getNumDesc(); // just a guess
}
public void handleChild(TreeTopoInfo infChild) {
if(infChild == null) {
return;
}
if(infChild.getId() == getId()) {
return;
}
if(m_mapStartId.containsKey(infChild.getId())) {
return;
}
int nDescs = infChild.getNumDesc();
m_mapStartId.put(infChild.getId(), m_nNumDesc);
m_nNumDesc += nDescs+1;
}
public boolean isDesc(TreeConstraintState compare) {
TreeTopoInfo infoCompare = getInfo(compare);
return isDesc(infoCompare);
}
public boolean isDesc(TreeTopoInfo compare) {
if(compare == null) {
return true;
}
if(getStart() < compare.getStart() &&
getStart() + getNumDesc() > compare.getStart()) {
return true;
}
if(getStart() < compare.getStart() +compare.getNumDesc() &&
getStart() + getNumDesc() >
compare.getStart() + compare.getNumDesc()) {
return true;
}
return false;
}
}
public static TreeTopoInfo getInfo(TreeConstraintState state) {
IMsg infoUncast = state.getMsgAddition(TreeTopoInfo.class);
if(infoUncast == null) {
return null;
}
return (TreeTopoInfo)infoUncast;
}
public TreeConstraintState getMsg(TreeConstraintState varsPrev, int idDst) {
// TODO Auto-generated method stub
// still don't need to fill this in -- dangerous to skip it, but don't
// really need it.
return null;
}
public TreeConstraintState stateNext(TreeConstraintState varsPrev,
Collection bagMsg) {
// TODO Auto-generated method stub
IFuncObj funcChildren =
new TreeConstraintAgent.SelectMsgWithParent(varsPrev.getId());
IFuncObj funcParent =
new TreeConstraintAgent.SelectMsgMatchingId(varsPrev.getParentId());
Iterator childIter =
AgentMsgHelpers.getFilteredMsgs(funcChildren, bagMsg);
Iterator parentIter =
AgentMsgHelpers.getFilteredMsgs(funcParent, bagMsg);
TreeTopoInfo myInfo = getInfo(varsPrev);
myInfo = new TreeTopoInfo();
myInfo.setId(varsPrev.getId());
while(parentIter.hasNext()) {
TreeConstraintState currState = parentIter.next();
TreeTopoInfo currVars = getInfo(currState);
myInfo.handleParent(currVars);
}
while(childIter.hasNext()) {
TreeConstraintState currState = childIter.next();
TreeTopoInfo currVars = getInfo(currState);
myInfo.handleChild(currVars);
}
TreeConstraintState stateNext = new TreeConstraintState(varsPrev);
stateNext.setMsgAddition(TreeTopoInfo.class, myInfo);
return stateNext;
}
}