View Javadoc

1   package cz.cuni.amis.pogamut.udk.communication.translator.bot.state;
2   
3   import java.util.HashSet;
4   import java.util.Set;
5   
6   import cz.cuni.amis.fsm.FSMState;
7   import cz.cuni.amis.fsm.FSMTransition;
8   import cz.cuni.amis.fsm.IFSMState;
9   import cz.cuni.amis.pogamut.base.communication.messages.InfoMessage;
10  import cz.cuni.amis.pogamut.base.communication.translator.event.IWorldChangeEvent;
11  import cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.BotKilled;
12  import cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.PathListStart;
13  import cz.cuni.amis.pogamut.udk.communication.translator.TranslatorContext;
14  import cz.cuni.amis.pogamut.udk.communication.translator.TranslatorMessages;
15  import cz.cuni.amis.pogamut.udk.communication.translator.UnexpectedMessageException;
16  import cz.cuni.amis.pogamut.udk.communication.translator.bot.support.AbstractBotFSMState;
17  import cz.cuni.amis.pogamut.udk.communication.worldview.objects.IGBViewable;
18  
19  /**
20   * This class implements a batch handling logic.
21   * @author Jimmy
22   */
23  @FSMState(map = { 
24  					@FSMTransition(
25  							state = BotDeadState.class, 
26  							symbol = {BotKilled.class }, 
27  							transition = {}
28  					),
29                      @FSMTransition(
30                          state = PathAcceptState.class,
31  						symbol = {PathListStart.class },
32  						transition = {}
33                      )
34  				}
35  )
36  public class BotAliveState extends AbstractBotFSMState<InfoMessage, TranslatorContext> {
37  
38  	/**
39  	 * Last batch of the objects we have seen in previous moment.
40  	 */
41  	private Set<IGBViewable> lastBatch = new HashSet<IGBViewable>();
42  	
43  	/**
44  	 * Current objects we have in the field of view.
45  	 */
46      private Set<IGBViewable> currentBatch = new HashSet<IGBViewable>();
47  
48      public void stateEntering(TranslatorContext context, IFSMState<InfoMessage, TranslatorContext> arg1, InfoMessage arg2) {
49      }
50  
51      public void stateSymbol(TranslatorContext context, InfoMessage obj) {
52  /*        if (obj instanceof IGBViewable) {
53              currentBatch.add((IGBViewable) obj);
54              if (!lastBatch.contains(obj) || obj instanceof Player) {
55              	if (!(obj instanceof IWorldChangeEvent)) throw new UnexpectedMessageException(TranslatorMessages.messageNotWorldEvent(this, obj), context.getLogger(), this);
56              	context.getEventQueue().pushEvent((IWorldChangeEvent)obj);
57              }
58          } else if (obj instanceof EndMessage) {
59              // we've got end of the batch message!
60  
61              // remove all message from last batch that is in current batch (we
62              // can still see them)
63              lastBatch.removeAll(currentBatch);
64  
65              // for each object that disappeared from the field of view
66              for (IGBViewable object : lastBatch) {
67                  // generate disappear event
68                  context.getEventQueue().pushEvent(object.createDisappearEvent());
69              }
70  
71              // write current batch as the last one
72              lastBatch = currentBatch;
73  
74              // create new batch
75              currentBatch = new HashSet<IGBViewable>(lastBatch.size() + 10);
76  
77              // push EndMessage event
78              if (!(obj instanceof IWorldChangeEvent)) throw new UnexpectedMessageException(TranslatorMessages.messageNotWorldEvent(this, obj), context.getLogger(), this);
79              context.getEventQueue().pushEvent((IWorldChangeEvent) obj);
80          } else if (obj instanceof IWorldChangeEvent) {
81              context.getEventQueue().pushEvent((IWorldChangeEvent) obj);
82          } else {
83          	throw new UnexpectedMessageException(TranslatorMessages.messageNotWorldEvent(this, obj), context.getLogger(), this);
84          }
85  */
86      	
87          context.getEventQueue().pushEvent((IWorldChangeEvent) obj);
88      }
89  
90      public void stateLeaving(TranslatorContext context, IFSMState<InfoMessage, TranslatorContext> arg1, InfoMessage symbol) {
91      	if (symbol instanceof BotKilled) {
92              // the bot has been killed! 
93              // for each object that disappeared from the field of view
94      		/*lastBatch.removeAll(currentBatch);
95              for (IGBViewable object : lastBatch) {
96                  // generate disappear event
97                  context.getEventQueue().pushEvent(object.createDisappearEvent());
98              }
99              lastBatch.clear();
100             for (IGBViewable object : currentBatch) {
101                 // generate disappear event
102                 context.getEventQueue().pushEvent(object.createDisappearEvent());
103             }
104             currentBatch.clear();
105             */
106              // push BotKilled event
107             if (!(symbol instanceof IWorldChangeEvent)) throw new UnexpectedMessageException(TranslatorMessages.messageNotWorldEvent(this, symbol), context.getLogger(), this);
108             context.getEventQueue().pushEvent((IWorldChangeEvent) symbol);
109     	}
110     }
111 
112     public void init(TranslatorContext arg0) {        
113     }
114 
115     public void restart(TranslatorContext arg0) {
116     	lastBatch.clear();
117     	currentBatch.clear();
118     }
119     
120 }