1 package nl.tudelft.goal.ut2004.floydwarshall;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.logging.Level;
6 import java.util.logging.Logger;
7
8 import cz.cuni.amis.pogamut.base.agent.IGhostAgent;
9 import cz.cuni.amis.pogamut.base.agent.module.SensorModule;
10 import cz.cuni.amis.pogamut.base.agent.navigation.IPathFuture;
11 import cz.cuni.amis.pogamut.base.agent.navigation.IPathPlanner;
12 import cz.cuni.amis.pogamut.base.agent.navigation.impl.PrecomputedPathFuture;
13 import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEventListener;
14 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.NavPoint;
15 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.NavPointNeighbourLink;
16 import cz.cuni.amis.pogamut.ut2004.communication.translator.shared.events.MapPointListObtained;
17 import cz.cuni.amis.utils.collections.MyCollections;
18
19
20
21
22
23
24 public class SharedFloydWarshallMap extends SensorModule<IGhostAgent> implements IPathPlanner<NavPoint> {
25
26
27
28
29 protected int badEdgeFlag = 0;
30
31
32
33
34 protected FloydWarshallMap sharedMap = null;
35
36 private IWorldEventListener<MapPointListObtained> mapListener = new IWorldEventListener<MapPointListObtained>() {
37
38 @Override
39 public void notify(MapPointListObtained event) {
40 if (log.isLoggable(Level.INFO))
41 log.info("Map point list obtained.");
42 sharedMap = FloydWarshallMapCache.getInstance().createMap(event, badEdgeFlag, log);
43 }
44 };
45
46 public SharedFloydWarshallMap(IGhostAgent bot) {
47 this(bot, null);
48 }
49
50 public SharedFloydWarshallMap(IGhostAgent bot, Logger log) {
51 this(bot, FloydWarshallMap.BAD_EDGE_FLAG, log);
52 }
53
54 public SharedFloydWarshallMap(IGhostAgent bot, int badEdgeFlag, Logger log) {
55 super(bot, log);
56 this.badEdgeFlag = badEdgeFlag;
57 worldView.addEventListener(MapPointListObtained.class, mapListener);
58 }
59
60
61
62
63
64
65
66
67 private List<NavPoint> clean(List<NavPoint> shared) {
68
69
70 if(shared == null){
71 return null;
72 }
73
74 List<NavPoint> clean = new ArrayList<NavPoint>(shared.size());
75
76 for (NavPoint navpoint : shared) {
77 clean.add(clean(navpoint));
78 }
79
80 return clean;
81 }
82
83
84
85
86
87
88
89
90 private NavPoint clean(NavPoint shared) {
91 return worldView.get(shared.getId(), NavPoint.class);
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 @Override
109 public IPathFuture<NavPoint> computePath(NavPoint from, NavPoint to) {
110 return new PrecomputedPathFuture<NavPoint>(from, to, getPath(from, to));
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124
125 public boolean reachable(NavPoint from, NavPoint to) {
126 return sharedMap.reachable(from, to);
127 }
128
129
130
131
132
133
134
135
136
137
138
139 public float getDistance(NavPoint from, NavPoint to) {
140 return sharedMap.getDistance(from, to);
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 public List<NavPoint> getPath(NavPoint from, NavPoint to) {
157 return clean(sharedMap.getPath(from, to));
158 }
159
160
161
162
163
164
165
166
167
168
169 public boolean checkLink(NavPointNeighbourLink edge) {
170 return sharedMap.checkLink(edge);
171
172 }
173
174
175
176
177 @Override
178 protected void cleanUp() {
179 super.cleanUp();
180 sharedMap = null;
181 }
182
183 public void refreshPathMatrix() {
184 List<NavPoint> navPoints = MyCollections.asList(agent.getWorldView().getAll(NavPoint.class).values());
185 sharedMap.refreshPathMatrix(navPoints);
186 }
187 }