1 package cz.cuni.amis.pogamut.ut2004.communication.translator.observer.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.ut2004.communication.messages.gbinfomessages.Item; 14 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.ItemListEnd; 15 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.ItemListStart; 16 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.NavPoint; 17 import cz.cuni.amis.pogamut.ut2004.communication.translator.TranslatorContext; 18 import cz.cuni.amis.pogamut.ut2004.communication.translator.observer.support.ObserverListState; 19 import cz.cuni.amis.pogamut.ut2004.communication.translator.shared.events.MapPointListObtained; 20 import java.util.logging.Level; 21 22 @FSMState( 23 map={ 24 @FSMTransition( 25 state=ObserverRunningState.class, 26 symbol={ItemListEnd.class}, 27 transition={} 28 ) 29 } 30 ) 31 public class ItemListState extends ObserverListState<Item, TranslatorContext> { 32 33 public ItemListState() { 34 super(ItemListStart.class, Item.class, ItemListEnd.class); 35 } 36 37 @Override 38 public void stateLeaving(TranslatorContext translatorContext, 39 IFSMState<InfoMessage, TranslatorContext> toState, InfoMessage symbol) { 40 long simTime = 41 (symbol instanceof IWorldChangeEvent ? ((IWorldChangeEvent)symbol).getSimTime() : 0); 42 processItems(translatorContext.getNavPoints(), getList(), translatorContext, simTime); 43 newList(); 44 } 45 46 private void processItems(Map<UnrealId, NavPoint> origNavPoints, List<Item> list, TranslatorContext context, long simTime) { 47 Map<UnrealId, Item> items = new HashMap<UnrealId, Item>(); 48 49 for (Item item : list) { 50 items.put(item.getId(), item); 51 } 52 53 context.setItems(items); 54 55 context.processNavPointsAndItems(); 56 57 if (context.getNavPoints() != null && context.getNavPoints().size() > 0) { 58 if (context.getLogger().isLoggable(Level.FINE)) context.getLogger().fine("Pushing NavPoint events."); 59 context.getEventQueue().pushEvent(context.getNavPoints().values().toArray(new IWorldChangeEvent[0])); 60 if (context.getLogger().isLoggable(Level.FINE)) context.getLogger().fine("Pushing Item events."); 61 context.getEventQueue().pushEvent(context.getItems().values().toArray(new IWorldChangeEvent[0])); 62 context.getEventQueue().pushEvent(new MapPointListObtained(context.getNavPoints(), context.getItems(), simTime)); 63 } else { 64 if (context.getLogger().isLoggable(Level.FINE)) context.getLogger().fine("Pushing Item events."); 65 context.getEventQueue().pushEvent(context.getItems().values().toArray(new IWorldChangeEvent[0])); 66 } 67 } 68 }