1 package cz.cuni.amis.pogamut.udk.communication.translator.server.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.base.communication.translator.event.IWorldChangeEvent;
12 import cz.cuni.amis.pogamut.unreal.communication.messages.UnrealId;
13 import cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.NavPoint;
14 import cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.NavPointListEnd;
15 import cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.NavPointListStart;
16 import cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.NavPointNeighbourLink;
17 import cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.NavPointNeighbourLinkEnd;
18 import cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.NavPointNeighbourLinkStart;
19 import cz.cuni.amis.pogamut.udk.communication.translator.TranslatorContext;
20 import cz.cuni.amis.pogamut.udk.communication.translator.TranslatorMessages;
21 import cz.cuni.amis.pogamut.udk.communication.translator.UnexpectedMessageException;
22 import cz.cuni.amis.pogamut.udk.communication.translator.server.support.AbstractServerFSMState;
23 import cz.cuni.amis.pogamut.udk.communication.translator.shared.events.MapPointListObtained;
24
25 import java.util.logging.Level;
26
27
28
29
30
31
32
33
34
35
36
37 @FSMState(map={@FSMTransition(
38 state = ServerRunningState.class,
39 symbol={NavPointListEnd.class},
40 transition={}),
41 @FSMTransition(
42 state = NavPointNeighboursState.class,
43 symbol = { NavPointNeighbourLinkStart.class },
44 transition = { }
45 )
46 }
47 )
48 public class NavPointListState extends AbstractServerFSMState<InfoMessage, TranslatorContext> {
49
50 private boolean begun = false;
51
52 private Map<UnrealId, NavPoint> navPoints = new HashMap<UnrealId, NavPoint>();
53
54 private Map<UnrealId, List<NavPointNeighbourLink>> neighbours = new HashMap<UnrealId, List<NavPointNeighbourLink>>();
55
56 private NavPoint currentNavPoint = null;
57
58 public NavPointListState() {
59 }
60
61 @Override
62 public void stateEntering(TranslatorContext context, IFSMState<InfoMessage, TranslatorContext> fromState, InfoMessage symbol) {
63 if (!begun) {
64 if (!symbol.getClass().equals(NavPointListStart.class)) throw new UnexpectedMessageException(TranslatorMessages.unexpectedMessage(this, symbol, NavPointListStart.class), context.getLogger(), this);
65 begun = true;
66 return;
67 }
68 if (!symbol.getClass().equals(NavPointNeighbourLinkEnd.class)) throw new UnexpectedMessageException(TranslatorMessages.unexpectedMessage(this, symbol, NavPointNeighbourLinkEnd.class), context.getLogger(), this);
69 navPoints.put(currentNavPoint.getId(), currentNavPoint);
70 neighbours.put(currentNavPoint.getId(), context.getNeighbours());
71 }
72
73 @Override
74 public void stateLeaving(TranslatorContext context,
75 IFSMState<InfoMessage, TranslatorContext> toState, InfoMessage symbol) {
76 if (symbol.getClass().equals(NavPointNeighbourLinkStart.class)) return;
77 if (!symbol.getClass().equals(NavPointListEnd.class)) throw new UnexpectedMessageException(TranslatorMessages.unexpectedMessage(this, symbol, NavPointListEnd.class), context.getLogger(), this);
78
79
80 context.setNavPoints(navPoints);
81 context.setNavPointLinks(neighbours);
82 context.processNavPointLinks();
83
84 if (context.getItems() != null && context.getItems().size() > 0) {
85 context.processNavPointsAndItems();
86 if (context.getLogger().isLoggable(Level.FINE)) context.getLogger().fine("Pushing NavPoint events.");
87 context.getEventQueue().pushEvent(context.getNavPoints().values().toArray(new IWorldChangeEvent[0]));
88 if (context.getLogger().isLoggable(Level.FINE)) context.getLogger().fine("Pushing Item events.");
89 context.getEventQueue().pushEvent(context.getItems().values().toArray(new IWorldChangeEvent[0]));
90 context.getEventQueue().pushEvent(new MapPointListObtained(context.getNavPoints(), context.getItems()));
91 } else {
92 if (context.getLogger().isLoggable(Level.FINE)) context.getLogger().fine("Pushing NavPoint events.");
93 context.getEventQueue().pushEvent(context.getNavPoints().values().toArray(new IWorldChangeEvent[0]));
94 }
95
96
97 navPoints = new HashMap<UnrealId, NavPoint>(navPoints.size() + 20);
98 neighbours = new HashMap<UnrealId, List<NavPointNeighbourLink>>(neighbours.size() + 20);
99 begun = false;
100 }
101
102 @Override
103 protected void innerStateSymbol(TranslatorContext context, InfoMessage symbol) {
104 if (!symbol.getClass().equals(NavPoint.class)) throw new UnexpectedMessageException(TranslatorMessages.unexpectedMessage(this, symbol, NavPoint.class), context.getLogger(), this);
105 currentNavPoint = (NavPoint)symbol;
106 }
107
108 @Override
109 public void init(TranslatorContext context) {
110 }
111
112 @Override
113 public void restart(TranslatorContext context) {
114 currentNavPoint = null;
115 begun = false;
116 navPoints = new HashMap<UnrealId, NavPoint>(navPoints.size() + 20);
117 neighbours = new HashMap<UnrealId, List<NavPointNeighbourLink>>(neighbours.size() + 20);
118 }
119
120 }