View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.communication.translator.bot.state;
2   
3   import java.util.HashMap;
4   import java.util.List;
5   import java.util.Map;
6   
7   import cz.cuni.amis.fsm.FSMState;
8   import cz.cuni.amis.fsm.FSMTransition;
9   import cz.cuni.amis.fsm.IFSMState;
10  import cz.cuni.amis.pogamut.base.communication.messages.InfoMessage;
11  import cz.cuni.amis.pogamut.unreal.communication.messages.UnrealId;
12  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.NavPoint;
13  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.NavPointListEnd;
14  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.NavPointListStart;
15  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.NavPointNeighbourLink;
16  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.NavPointNeighbourLinkEnd;
17  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.NavPointNeighbourLinkStart;
18  import cz.cuni.amis.pogamut.ut2004.communication.translator.TranslatorContext;
19  import cz.cuni.amis.pogamut.ut2004.communication.translator.TranslatorMessages;
20  import cz.cuni.amis.pogamut.ut2004.communication.translator.UnexpectedMessageException;
21  import cz.cuni.amis.pogamut.ut2004.communication.translator.bot.support.AbstractBotFSMState;
22  
23  /**
24   * Takes care of the navpoint list. It stores them inside a List object and when END message comes it sends
25   * them to the world view via <b>NavPointListObtained</b> event, note that before we send the nav points we're doing
26   * the preprocessing of those navpoints to interconnect them with links (NavPointNeighbourLink) filling
27   * respective fileds of incomingEdges and outgoingEdges.
28   * <p><p>
29   * The processing of navpoints and it's links is done via TranslatorContext.processNavPointLinks().
30   * 
31   * @author Jimmy
32   */
33  @FSMState(map={@FSMTransition(
34  					state=HandshakeControllerState.class, 
35  					symbol={NavPointListEnd.class}, 
36  					transition={}),
37  			   @FSMTransition(
38  					state = NavPointNeighboursState.class, 
39  					symbol = { NavPointNeighbourLinkStart.class }, 
40  					transition = { }
41  			   )	
42  		  }
43  )
44  public class NavPointListState extends AbstractBotFSMState<InfoMessage, TranslatorContext> {
45  	
46  	private boolean begun = false;
47  	
48  	private Map<UnrealId, NavPoint> navPoints = new HashMap<UnrealId, NavPoint>();
49  	
50  	private Map<UnrealId, List<NavPointNeighbourLink>> neighbours = new HashMap<UnrealId, List<NavPointNeighbourLink>>();
51  	
52  	private NavPoint currentNavPoint = null;
53  
54  	public NavPointListState() {
55  	}
56  	
57  	@Override
58  	public void stateEntering(TranslatorContext context, IFSMState<InfoMessage, TranslatorContext> fromState, InfoMessage symbol) {
59  		if (!begun) {
60  			if (!symbol.getClass().equals(NavPointListStart.class)) throw new UnexpectedMessageException(TranslatorMessages.unexpectedMessage(this, symbol, NavPointListStart.class), context.getLogger(), this);
61  			begun = true;
62  			return;
63  		}		
64  		if (!symbol.getClass().equals(NavPointNeighbourLinkEnd.class)) throw new UnexpectedMessageException(TranslatorMessages.unexpectedMessage(this, symbol, NavPointNeighbourLinkEnd.class), context.getLogger(), this);
65  		navPoints.put(currentNavPoint.getId(), currentNavPoint);
66  		neighbours.put(currentNavPoint.getId(), context.getNeighbours());
67  	}
68  	
69  	@Override
70  	public void stateLeaving(TranslatorContext context,
71  			IFSMState<InfoMessage, TranslatorContext> toState, InfoMessage symbol) {
72  		if (symbol.getClass().equals(NavPointNeighbourLinkStart.class)) return;	
73  		if (!symbol.getClass().equals(NavPointListEnd.class)) throw new UnexpectedMessageException(TranslatorMessages.unexpectedMessage(this, symbol, NavPointListEnd.class), context.getLogger(), this);
74  		
75  		// we've received all the navpoints we could!		
76  		context.setNavPoints(navPoints);
77  		context.setNavPointLinks(neighbours);
78  		
79  		// leaving the state, mark we haven't ever begun
80  		navPoints = new HashMap<UnrealId, NavPoint>(navPoints.size() + 20);
81  		neighbours = new HashMap<UnrealId, List<NavPointNeighbourLink>>(neighbours.size() + 20);
82  		begun = false;
83  	}
84  
85  	@Override
86  	public void stateSymbol(TranslatorContext context, InfoMessage symbol) {
87  		if (!symbol.getClass().equals(NavPoint.class)) {
88  			if (!NavPoint.class.isAssignableFrom(symbol.getClass())) {
89  				throw new UnexpectedMessageException(TranslatorMessages.unexpectedMessage(this, symbol, NavPoint.class), context.getLogger(), this);
90  			}
91  		}
92  		currentNavPoint = (NavPoint)symbol;
93  		// DO NOT PUSH - LEAVE IT TO {@link HandshakeEndTransition} that will process it all in one step and pushes all in one step...
94  		//context.getEventQueue().pushEvent(currentNavPoint);
95  	}
96  
97  	@Override
98  	public void init(TranslatorContext context) {
99  	}
100 
101 	@Override
102 	public void restart(TranslatorContext context) {
103 		currentNavPoint = null;
104 		begun = false;
105 		navPoints = new HashMap<UnrealId, NavPoint>(navPoints.size() + 20);
106 		neighbours = new HashMap<UnrealId, List<NavPointNeighbourLink>>(neighbours.size() + 20);
107 	}
108 	
109 }