001 /**
002 * @author paulk, Jul 16, 2002
003 */
004
005 package toolbus.atom.msg;
006
007 import java.util.List;
008 import toolbus.TBTermFactory;
009 import toolbus.atom.Atom;
010 import toolbus.exceptions.ToolBusException;
011 import toolbus.matching.MatchStore;
012 import toolbus.parsercup.PositionInformation;
013 import toolbus.process.ProcessExpression;
014 import toolbus.process.ProcessInstance;
015 import aterm.ATerm;
016
017 public class SndMsg extends MsgAtom{
018 private int index;
019
020 public SndMsg(ATerm msg, TBTermFactory tbfactory, PositionInformation posInfo){
021 super(msg, tbfactory, posInfo);
022
023 index = 0;
024 }
025
026 public ProcessExpression copy(){
027 Atom a = new SndMsg(msg, tbfactory, getPosInfo());
028 a.copyAtomAttributes(this);
029 return a;
030 }
031
032 private boolean matchesPartner(RecMsg recMsg){
033 return tbfactory.match(recMsg.msg, recMsg.getEnv(), msg, getEnv());
034 }
035
036 public boolean execute() throws ToolBusException{
037 if(isEnabled()){
038 MatchStore matchStore = getToolBus().getMatchStore();
039 List<RecMsg> msgPartners = matchStore.getPossibleMessagePartners(msg);
040
041 int size = msgPartners.size();
042
043 if(size == 0) return false;
044
045 for(int i = (index++ % size), left = size; left > 0; i = ((i + 1) % size), left--){
046 RecMsg recMsg = msgPartners.get(i);
047 ProcessInstance pb = recMsg.getProcess();
048
049 if(pb.contains(recMsg) && recMsg.isEnabled() && matchesPartner(recMsg)){
050 //LoggerFactory.log("unknown", " " + this, IToolBusLoggerConstants.MESSAGES);
051 //LoggerFactory.log("unknown", "<=> " + b, IToolBusLoggerConstants.MESSAGES);
052 pb.gotoNextStateAndActivate(recMsg);
053 return true;
054 }
055 }
056 }
057 return false;
058 }
059
060 public ProcessInstance[] debugExecute() throws ToolBusException{
061 if(isEnabled()){
062 MatchStore matchStore = getToolBus().getMatchStore();
063 List<RecMsg> msgPartners = matchStore.getPossibleMessagePartners(msg);
064
065 int size = msgPartners.size();
066
067 if(size == 0) return null;
068
069 for(int i = (index++ % size), left = size; left > 0; i = ((i + 1) % size), left--){
070 RecMsg recMsg = msgPartners.get(i);
071 ProcessInstance pb = recMsg.getProcess();
072
073 if(pb.contains(recMsg) && recMsg.isEnabled() && matchesPartner(recMsg)){
074 //LoggerFactory.log("unknown", " " + this, IToolBusLoggerConstants.MESSAGES);
075 //LoggerFactory.log("unknown", "<=> " + b, IToolBusLoggerConstants.MESSAGES);
076 pb.gotoNextStateAndActivate(recMsg);
077 return new ProcessInstance[]{pb};
078 }
079 }
080 }
081 return null;
082 }
083 }