View Javadoc

1   package cz.cuni.amis.pogamut.udk.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.udk.communication.messages.gbinfomessages.NavPoint;
13  import cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.NavPointListEnd;
14  import cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.NavPointListStart;
15  import cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.NavPointNeighbourLink;
16  import cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.NavPointNeighbourLinkEnd;
17  import cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.NavPointNeighbourLinkStart;
18  import cz.cuni.amis.pogamut.udk.communication.translator.TranslatorContext;
19  import cz.cuni.amis.pogamut.udk.communication.translator.TranslatorMessages;
20  import cz.cuni.amis.pogamut.udk.communication.translator.UnexpectedMessageException;
21  import cz.cuni.amis.pogamut.udk.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  		context.processNavPointLinks();
79  		
80  		// leaving the state, mark we haven't ever begun
81  		navPoints = new HashMap<UnrealId, NavPoint>(navPoints.size() + 20);
82  		neighbours = new HashMap<UnrealId, List<NavPointNeighbourLink>>(neighbours.size() + 20);
83  		begun = false;
84  	}
85  
86  	@Override
87  	public void stateSymbol(TranslatorContext context, InfoMessage symbol) {
88  		if (!symbol.getClass().equals(NavPoint.class)) throw new UnexpectedMessageException(TranslatorMessages.unexpectedMessage(this, symbol, NavPoint.class), context.getLogger(), this);
89  		currentNavPoint = (NavPoint)symbol;	
90  		context.getEventQueue().pushEvent(currentNavPoint);
91  	}
92  
93  	@Override
94  	public void init(TranslatorContext context) {
95  	}
96  
97  	@Override
98  	public void restart(TranslatorContext context) {
99  		currentNavPoint = null;
100 		begun = false;
101 		navPoints = new HashMap<UnrealId, NavPoint>(navPoints.size() + 20);
102 		neighbours = new HashMap<UnrealId, List<NavPointNeighbourLink>>(neighbours.size() + 20);
103 	}
104 	
105 }