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