1 package cz.cuni.amis.pogamut.udk.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.udk.communication.messages.gbinfomessages.Item;
11 import cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.NavPoint;
12 import cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.NavPointNeighbourLink;
13 import cz.cuni.amis.pogamut.udk.communication.translator.itemdescriptor.ItemTranslator;
14
15 import java.util.logging.Level;
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public class TranslatorContext {
31
32 private IWorldEventQueue events;
33 private ItemTranslator itemTranslator;
34 private Logger log;
35
36 private List<NavPointNeighbourLink> neighbours = new ArrayList<NavPointNeighbourLink>();
37 private Map<UnrealId, NavPoint> navPoints = new HashMap<UnrealId, NavPoint>();
38 private Map<UnrealId, Item> items = new HashMap<UnrealId, Item>();
39 private Map<UnrealId, List<NavPointNeighbourLink>> links = new HashMap<UnrealId, List<NavPointNeighbourLink>>();
40
41 public TranslatorContext(IWorldEventQueue events, ItemTranslator itemTranslator, Logger log) {
42 this.events = events;
43 this.itemTranslator = itemTranslator;
44 this.log = log;
45 }
46
47 public void reset() {
48 neighbours = new ArrayList<NavPointNeighbourLink>();
49 navPoints = new HashMap<UnrealId, NavPoint>();
50 items = new HashMap<UnrealId, Item>();
51 links = new HashMap<UnrealId, List<NavPointNeighbourLink>>();
52 }
53
54 public IWorldEventQueue getEventQueue() {
55 return events;
56 }
57
58 public ItemTranslator getItemTranslator() {
59 return itemTranslator;
60 }
61
62 public Logger getLogger() {
63 return log;
64 }
65
66 public List<NavPointNeighbourLink> getNeighbours() {
67 return neighbours;
68 }
69
70 public void setNeighbours(List<NavPointNeighbourLink> neighs) {
71 this.neighbours = neighs;
72 }
73
74 public void setNavPointLinks(Map<UnrealId, List<NavPointNeighbourLink>> links) {
75 this.links = links;
76 }
77
78 public Map<UnrealId, List<NavPointNeighbourLink>> getNavPointLinks() {
79 return links;
80 }
81
82 public void setNavPoints(Map<UnrealId, NavPoint> navPoints) {
83 this.navPoints = navPoints;
84 }
85
86 public Map<UnrealId, NavPoint> getNavPoints() {
87 return navPoints ;
88 }
89
90 public void setItems(Map<UnrealId, Item> items) {
91 this.items = items;
92 }
93
94 public Map<UnrealId, Item> getItems() {
95 return items;
96 }
97
98
99
100
101
102
103 public void processNavPointLinks() {
104 if (getNavPoints() == null || getNavPointLinks() == null) {
105 return;
106 }
107
108 if (getLogger().isLoggable(Level.FINE)) getLogger().fine("Processing NavPoints<->Links.");
109 for (NavPoint navPoint : getNavPoints().values()) {
110 navPoint.getIncomingEdges().clear();
111 navPoint.getOutgoingEdges().clear();
112 }
113 for(NavPoint navPoint : getNavPoints().values()) {
114 List<NavPointNeighbourLink> links = getNavPointLinks().get(navPoint.getId());
115 List<NavPointNeighbourLink> fixedLinks = new ArrayList<NavPointNeighbourLink>(links.size());
116 for (NavPointNeighbourLink link : links) {
117 NavPoint targetNavPoint = navPoints.get(link.getId());
118 NavPointNeighbourLink fixedLink = new NavPointNeighbourLink(link, navPoint, targetNavPoint );
119 fixedLinks.add(fixedLink);
120 navPoint.getOutgoingEdges().put(fixedLink.getId(), fixedLink);
121 targetNavPoint.getIncomingEdges().put(navPoint.getId(), fixedLink);
122 }
123 getNavPointLinks().put(navPoint.getId(), fixedLinks);
124 }
125 if (getLogger().isLoggable(Level.FINE)) getLogger().fine("Processing finished.");
126 }
127
128
129
130
131
132
133
134
135
136 public void processNavPointsAndItems() {
137 if (getItems() == null || getNavPoints() == null) {
138 return;
139 }
140
141 if (getLogger().isLoggable(Level.FINE)) getLogger().fine("Processing NavPoints<->Items.");
142
143 Map<UnrealId, Item> items = getItems();
144
145 for (NavPoint navPoint : getNavPoints().values()) {
146 if (navPoint.getItem() != null) {
147 Item item = items.get(navPoint.getItem());
148 if (item == null) {
149 if (getLogger().isLoggable(Level.WARNING)) getLogger().warning("Item of id " + navPoint.getItem().getStringId() + " does not exist, referenced from navpoint " + navPoint.getId().getStringId() + ".");
150 continue;
151 }
152 navPoint.setItem(item);
153 item.setNavPoint(navPoint);
154 }
155 }
156
157 if (getLogger().isLoggable(Level.FINE)) getLogger().fine("Processing finished.");
158 }
159
160 }