View Javadoc

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   * Wrapper for a FloyWarschallMap shared by multiple agents.
20   * 
21   * @author M.P. Korstanje
22   */
23  public class SharedFloydWarshallMap extends SensorModule<IGhostAgent> implements IPathPlanner<NavPoint> {
24  
25  	/**
26  	 * Prohibited edges.
27  	 */
28  	protected int badEdgeFlag = 0;
29  
30  	/**
31  	 * Our map
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  	 * Transforms a list of navpoints owned by the (bot owning the) shared map
61  	 * to navpoints that we own. This is important with regards to visibility.
62  	 * 
63  	 * @param shared
64  	 * @return
65  	 */
66  	private List<NavPoint> clean(List<NavPoint> shared) {
67  		
68  		//Empty path.
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  	 * Transforms a navpoint owned by the (bot owning the) shared map to
84  	 * navpoints that we own. This is important with regards to visibility.
85  	 * 
86  	 * @param shared
87  	 * @return
88  	 */
89  	private NavPoint clean(NavPoint shared) {
90  		return worldView.get(shared.getId(), NavPoint.class);
91  	}
92  
93  	/**
94  	 * Returns path between navpoints 'from' -> 'to'. The path begins with
95  	 * 'from' and ends with 'to'. If such path does not exist, it returns
96  	 * zero-sized path.
97  	 * <p>
98  	 * <p>
99  	 * Throws exception if object is disabled, see
100 	 * {@link FloydWarshallMap#setEnabled(boolean)}. Note that the object
101 	 * is enabled by default.
102 	 * 
103 	 * @param from
104 	 * @param to
105 	 * @return
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 	 * Whether navpoint 'to' is reachable from the navpoint 'from'.
114 	 * <p>
115 	 * <p>
116 	 * Throws exception if object is disabled, see
117 	 * {@link FloydWarshallMap#setEnabled(boolean)}. Note that the object
118 	 * is enabled by default.
119 	 * 
120 	 * @param from
121 	 * @param to
122 	 * @return
123 	 */
124 	public boolean reachable(NavPoint from, NavPoint to) {
125 		return sharedMap.reachable(from, to);
126 	}
127 
128 	/**
129 	 * Calculate's distance between two nav points (using pathfinding).
130 	 * <p>
131 	 * <p>
132 	 * Throws exception if object is disabled, see
133 	 * {@link FloydWarshallMap#setEnabled(boolean)}. Note that the object
134 	 * is enabled by default.
135 	 * 
136 	 * @return Distance or POSITIVE_INFINITY if there's no path.
137 	 */
138 	public float getDistance(NavPoint from, NavPoint to) {
139 		return sharedMap.getDistance(from, to);
140 	}
141 
142 	/**
143 	 * Returns path between navpoints 'from' -> 'to'. The path begins with
144 	 * 'from' and ends with 'to'. If such path doesn't exist, returns null.
145 	 * <p>
146 	 * <p>
147 	 * Throws exception if object is disabled, see
148 	 * {@link FloydWarshallMap#setEnabled(boolean)}. Note that the object
149 	 * is enabled by default.
150 	 * 
151 	 * @param from
152 	 * @param to
153 	 * @return
154 	 */
155 	public List<NavPoint> getPath(NavPoint from, NavPoint to) {
156 		return clean(sharedMap.getPath(from, to));
157 	}
158 
159 	/**
160 	 * Checks whether the edge is usable.
161 	 * 
162 	 * @param from
163 	 *            Starting nav point.
164 	 * @param edge
165 	 *            NeighNav object representing the edge.
166 	 * @return boolean
167 	 */
168 	public boolean checkLink(NavPointNeighbourLink edge) {
169 		return sharedMap.checkLink(edge);
170 
171 	}
172 
173 	/**
174 	 * Hook where to perform clean up of data structures of the module.
175 	 */
176 	@Override
177 	protected void cleanUp() {	
178 		super.cleanUp();
179 		sharedMap = null;
180 	}
181 }