View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.communication.translator;
2   
3   import java.util.ArrayList;
4   import java.util.HashMap;
5   import java.util.List;
6   import java.util.Map;
7   import java.util.logging.Logger;
8   
9   import cz.cuni.amis.pogamut.unreal.communication.messages.UnrealId;
10  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Item;
11  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.ItemMessage;
12  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.NavPoint;
13  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.NavPointMessage;
14  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.NavPointNeighbourLink;
15  import cz.cuni.amis.pogamut.ut2004.communication.translator.itemdescriptor.ItemTranslator;
16  import java.util.logging.Level;
17  
18  /**
19   * Translator context serves as the context during the FSM work. It provides respective fsm states an access
20   * to the instances of:
21   * <ul>
22   * <li>event queue - object where we're sending new world events</li>
23   * <li>item translator - an object with factories of respective items</li>
24   * <li>log</li>
25   * </ul>
26   * <p><p>
27   * WARNING: the same context is used for Bot, ControlServer as well as Observer!
28   * 
29   * @author Jimmy
30   */
31  public class TranslatorContext {
32      
33      private IWorldEventQueue events;
34      private ItemTranslator itemTranslator;
35      private Logger log;
36      
37  	private List<NavPointNeighbourLink> neighbours = new ArrayList<NavPointNeighbourLink>();
38  	private Map<UnrealId, NavPoint> navPoints = new HashMap<UnrealId, NavPoint>();
39  	private Map<UnrealId, Item> items = new HashMap<UnrealId, Item>();
40  	private Map<UnrealId, List<NavPointNeighbourLink>> links = new HashMap<UnrealId, List<NavPointNeighbourLink>>();
41      
42      public TranslatorContext(IWorldEventQueue events, ItemTranslator itemTranslator, Logger log) {
43          this.events = events;
44          this.itemTranslator = itemTranslator;
45          this.log = log;
46      }
47      
48      public void reset() {
49      	neighbours = new ArrayList<NavPointNeighbourLink>();
50      	navPoints = new HashMap<UnrealId, NavPoint>();
51      	items = new HashMap<UnrealId, Item>();
52      	links = new HashMap<UnrealId, List<NavPointNeighbourLink>>();
53      }
54      
55      public IWorldEventQueue getEventQueue() {
56          return events;
57      }
58      
59      public ItemTranslator getItemTranslator() {
60          return itemTranslator;
61      }
62      
63      public Logger getLogger() {
64      	return log;
65      }
66      
67      public List<NavPointNeighbourLink> getNeighbours() {
68      	return neighbours;
69      }
70      
71      public void setNeighbours(List<NavPointNeighbourLink> neighs) {
72      	this.neighbours  = neighs;
73      }
74      
75      public void setNavPointLinks(Map<UnrealId, List<NavPointNeighbourLink>> links) {
76      	this.links = links;
77      }
78      
79      public Map<UnrealId, List<NavPointNeighbourLink>> getNavPointLinks() {
80  		return links;
81  	}
82  
83  	public void setNavPoints(Map<UnrealId, NavPoint> navPoints) {
84      	this.navPoints = navPoints;
85      }
86      
87      public Map<UnrealId, NavPoint> getNavPoints() {
88      	return navPoints ;
89      }
90  
91  	public void setItems(Map<UnrealId, Item> items) {
92  		this.items = items;		
93  	}
94  
95  	public Map<UnrealId, Item> getItems() {
96  		return items;
97  	}
98  	
99  	/**
100 	 * Reads getNavPointsLinks() and alters navpoints incoming and outgoing edges.
101 	 * <p><p>
102 	 * Does nothing if getNavPoints() or getNavPointsLinks() returns null.
103 	 */
104 	public void processNavPointLinks() {
105 		if (getNavPoints() == null || getNavPointLinks() == null) {
106 			return;
107 		}
108 		
109 		if (getLogger().isLoggable(Level.FINE)) getLogger().fine("Processing NavPoints<->Links.");
110 		for (NavPoint navPoint : getNavPoints().values()) {
111 			navPoint.getIncomingEdges().clear();
112 			navPoint.getOutgoingEdges().clear();
113 		}
114 		for(NavPoint navPoint : getNavPoints().values()) {
115 			List<NavPointNeighbourLink> links = getNavPointLinks().get(navPoint.getId());
116 			List<NavPointNeighbourLink> fixedLinks = new ArrayList<NavPointNeighbourLink>(links.size());
117 			for (NavPointNeighbourLink link : links) {
118 				NavPoint targetNavPoint = navPoints.get(link.getId());
119 				NavPointNeighbourLink fixedLink = new NavPointNeighbourLink(link, navPoint, targetNavPoint );
120 				fixedLinks.add(fixedLink);
121 				navPoint.getOutgoingEdges().put(fixedLink.getId(), fixedLink);
122 				targetNavPoint.getIncomingEdges().put(navPoint.getId(), fixedLink);
123 			}
124 			getNavPointLinks().put(navPoint.getId(), fixedLinks);
125 		}
126 		if (getLogger().isLoggable(Level.FINE)) getLogger().fine("Processing finished.");
127 	}
128 	
129 	/**
130 	 * Interconnects instances of NavPoint and Item from getNavPoints() and getItems() map.
131 	 * <p><p>
132 	 * Note that new instances of nav points are created during this process thus
133 	 * the getNavPoints() will return a new map after this method finishes.
134 	 * <p><p>
135 	 * Does nothing if getNavPoints() or getItems() returns null.
136 	 */
137 	public void processNavPointsAndItems() {
138 		if (getItems() == null || getNavPoints() == null) {
139 			return;
140 		}
141 		
142 		if (getLogger().isLoggable(Level.FINE)) getLogger().fine("Processing NavPoints<->Items.");
143 		
144 		Map<UnrealId, Item> items = getItems();
145 		
146 		for (NavPoint navPoint : getNavPoints().values()) {
147 			if (navPoint.getItem() != null) {
148 				Item item = items.get(navPoint.getItem());
149 				if (item == null) {
150 					if (getLogger().isLoggable(Level.WARNING)) getLogger().warning("Item of id " + navPoint.getItem().getStringId() + " does not exist, referenced from navpoint " + navPoint.getId().getStringId() + ".");
151 					continue;
152 				}
153 				if (navPoint instanceof NavPointMessage) {
154 					((NavPointMessage)navPoint).setItemInstance(item);
155 				}
156 				if (item instanceof ItemMessage) {
157 					((ItemMessage)item).setNavPoint(navPoint);
158 				}
159 			}			
160 		}
161 		
162 		if (getLogger().isLoggable(Level.FINE)) getLogger().fine("Processing finished.");
163 	}
164 	
165 }