StreetMovement.java 11.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 * Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
 * 
 * PeerfactSim.KOM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * PeerfactSim.KOM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package de.tud.kom.p2psim.impl.topology.movement;

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
import java.util.Vector;

import javax.swing.JComponent;

import com.graphhopper.GHRequest;
import com.graphhopper.GHResponse;
import com.graphhopper.util.PointList;
import com.graphhopper.util.shapes.GHPoint;
import com.graphhopper.util.shapes.GHPoint3D;

import de.tud.kom.p2psim.api.scenario.ConfigurationException;
import de.tud.kom.p2psim.api.topology.Topology;
import de.tud.kom.p2psim.api.topology.TopologyComponent;
import de.tud.kom.p2psim.api.topology.movement.MovementModel;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tud.kom.p2psim.api.topology.movement.local.LocalMovementStrategy;
import de.tud.kom.p2psim.api.topology.placement.PlacementModel;
import de.tud.kom.p2psim.impl.simengine.Simulator;
import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.TopologyFactory;
import de.tud.kom.p2psim.impl.topology.movement.local.RealWorldMovementPoints;
import de.tud.kom.p2psim.impl.topology.movement.modular.ModularMovementModelViz;
import de.tud.kom.p2psim.impl.topology.movement.modular.transition.TransitionStrategy;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.GPSCalculation;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.AttractionPoint;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.IAttractionGenerator;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.mapvisualization.IMapVisualization;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.transition.ITransitionStrategy;
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView.VisualizationInjector;
import de.tud.kom.p2psim.impl.util.Either;
import de.tud.kom.p2psim.impl.util.Left;
import de.tudarmstadt.maki.simonstrator.api.Binder;
import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.EventHandler;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;

/**
 * 
Julian Zobel's avatar
Julian Zobel committed
71
72
73
74
 * 
 * 
 * @author Julian Zobel
 * 
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
 * 
 */
public class StreetMovement implements MovementModel, EventHandler {

	private final int EVENT_MOVE = 1;

	private final int EVENT_INIT = 2;

	protected PositionVector worldDimensions;

	private Set<SimLocationActuator> moveableHosts = new LinkedHashSet<SimLocationActuator>();

	private Map<SimLocationActuator, PositionVector> positions = new LinkedHashMap<SimLocationActuator, PositionVector>();
	
	private Map<SimLocationActuator, AttractionPoint> attractionOfClients = new LinkedHashMap<SimLocationActuator, AttractionPoint>();
	
	List<AttractionPoint> attractionPoints = new LinkedList<AttractionPoint>();
	
	private boolean initialized = false;

	private long timeBetweenMoveOperation = Simulator.SECOND_UNIT;

	private Random rand;

	private double latLeft; //Values from -90 to 90; always smaller than latRight
	private double latRight; //Values from -90 to 90
	private double lonLeft; //Values from -180 to 180; Always smaller than lonRight
	private double lonRight; //Values from -180 to 180
	
	
	public StreetMovement() {
		this.worldDimensions = Binder.getComponentOrNull(Topology.class)
				.getWorldDimensions();
		this.rand = Randoms.getRandom(StreetMovement.class);
		
		latLeft = GPSCalculation.getLatLower();
		latRight = GPSCalculation.getLatUpper();
		lonLeft = GPSCalculation.getLonLeft();
		lonRight = GPSCalculation.getLonRight();
		
		System.out.println("Scheduling first event for Init");
		
		// scheduling initalization!
		Event.scheduleImmediately(this, null, EVENT_INIT);
	}

	/**
	 * This Method will be not called from the Components. So we call this
	 * manually!
	 */
	public void initialize() {

Julian Zobel's avatar
Julian Zobel committed
127
		if (!initialized) {	
128
129
			setTimeBetweenMoveOperations(timeBetweenMoveOperation);

130
131
			for (SimLocationActuator ms : moveableHosts) {				
				initializeStartingPosition(ms);							
132
133
134
135
136
137
138
139
			}			
			
			// initial move
			move();

			initialized = true;
		}
	}
Julian Zobel's avatar
Julian Zobel committed
140
141
142
143
144
145
146
147
148
	
	/**
	 * Assign the given node a random attraction point and place it 
	 * on a randomly selected position on the line between 
	 * the assigned attraction point and another adjacent attraction point.  
	 * 
	 * @param ms
	 */
	private void initializeStartingPosition(SimLocationActuator ms) {		
149
150
151
152
153
154
		// set an initial attraction point
		int index = rand.nextInt(attractionPoints.size());
		AttractionPoint a = attractionPoints.get(index);		
		attractionOfClients.put(ms, a);	
		
		AttractionPoint b = this.returnNextOrLastAttractionPoint(index);
Julian Zobel's avatar
Julian Zobel committed
155
				
156
157
158
159
160
161
162
		PositionVector startingPosition = this.returnRandomPositionBetweenPoints(a,b);
		
		ms.updateCurrentLocation(startingPosition.getLongitude(), startingPosition.getLatitude());			
		
		positions.put(ms, ms.getRealPosition());	
		
	}
Julian Zobel's avatar
Julian Zobel committed
163
164
165
166
167
168
169
170
	
	/**
	 * Returns a randomly selected point on the line between the two given attraction points 
	 * 
	 * @param a Attraction Point A
	 * @param b Attraction Point B
	 * @return PositionVector of a randomly selected point on the connecting line of the attraction points
	 */
171
172
173
174
	private PositionVector returnRandomPositionBetweenPoints(AttractionPoint a,
			AttractionPoint b) {		
		double longMin, longMax, latMin, latMax, longNew, latNew;
		
Julian Zobel's avatar
Julian Zobel committed
175
		// Points have different longitude, so only search for random value for longitude
176
177
178
179
		if(a.getRealPosition().getLongitude() != b.getRealPosition().getLongitude()) {
			if(a.getRealPosition().getLongitude() < b.getRealPosition().getLongitude()) {
				longMin = a.getRealPosition().getLongitude();
				longMax = b.getRealPosition().getLongitude();
Julian Zobel's avatar
Julian Zobel committed
180
			} else {
181
182
183
184
185
186
187
188
189
190
191
192
				longMin = b.getRealPosition().getLongitude();
				longMax = a.getRealPosition().getLongitude();
			}
			
			do {
				longNew = rand.nextDouble()*longMax;				
			} while(longNew < longMin);
			
			assert longNew > longMin && longNew <= longMax;
			return new PositionVector(longNew, a.getRealPosition().getLatitude());
		}
		
Julian Zobel's avatar
Julian Zobel committed
193
194
		// Points have different latitude, so only search for random value for latitude
		if(a.getRealPosition().getLatitude() != b.getRealPosition().getLatitude()) {		
195
196
197
			if(a.getRealPosition().getLatitude() < b.getRealPosition().getLatitude()) {
				latMin = a.getRealPosition().getLatitude();
				latMax = b.getRealPosition().getLatitude();
Julian Zobel's avatar
Julian Zobel committed
198
			} else {
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
				latMin = b.getRealPosition().getLatitude();
				latMax = a.getRealPosition().getLatitude();
			}		
			
			do{
				latNew = rand.nextDouble()*latMax;				
			} while(latNew < latMin);
			
			assert latNew > latMin && latNew <= latMax;
			return new PositionVector(a.getRealPosition().getLongitude(), latNew);
		}
		
		return null;		
	}

214
215
216
217
218
219
	/**
	 * This default implementation relies on {@link PlacementModel}s to be
	 * configured in the {@link TopologyFactory}
	 */
	@Override
	public void placeComponent(SimLocationActuator actuator) {
220
		// not used here i guess
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
	}

	@Override
	public void changeTargetLocation(SimLocationActuator actuator,
			double longitude, double latitude) {
		/*
		 * Set a new target AP for the current actuator
		 */
		attractionOfClients.put(actuator, new AttractionPoint((int) longitude, (int) latitude, ""));
	}
	

	@Override
	public void addComponent(SimLocationActuator comp) {		
		moveableHosts.add(comp);		
		positions.put(comp, comp.getRealPosition());		
	}

	@Override
	public void setTimeBetweenMoveOperations(long time) {
		if (time > 0) {
			this.timeBetweenMoveOperation = time;
		} else {
			throw new ConfigurationException(
					"time is negative for the Move Operations");
		}
	}

Julian Zobel's avatar
Julian Zobel committed
249
250
251
252
	/**
	 * Move all nodes towards their assigned attraction point
	 */
	protected void move() {		
253
254
255
256
257
258
259
		for (Entry<SimLocationActuator, AttractionPoint> entry : attractionOfClients.entrySet()) {
			
			SimLocationActuator ms = entry.getKey();
			PositionVector attractionCenter = entry.getValue()
					.getRealPosition();
			PositionVector destination = new PositionVector(attractionCenter);

Julian Zobel's avatar
Julian Zobel committed
260
			if (destination.distanceTo(ms.getRealPosition()) > ms.getMovementSpeed()) {				
261
262
				PositionVector newPosition = ms.getRealPosition().moveStep(destination, ms.getMovementSpeed());
				ms.updateCurrentLocation(newPosition.getLongitude(), newPosition.getLatitude());
Julian Zobel's avatar
Julian Zobel committed
263
				positions.put(ms, newPosition);				
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
			} else {
				assignNewAttractionPoint(ms);
			}			
			
			/*
			 * Check for negative or out of bound coordinates!
			 */
			assert ms.getRealPosition().getX() >= 0.0
					&& ms.getRealPosition().getX() <= Binder
							.getComponentOrNull(Topology.class)
							.getWorldDimensions().getX();
			assert ms.getRealPosition().getY() >= 0.0
					&& ms.getRealPosition().getY() <= Binder
							.getComponentOrNull(Topology.class)
							.getWorldDimensions().getY();
		}

		Event.scheduleWithDelay(timeBetweenMoveOperation, this, null,
				EVENT_MOVE);
	}
	
Julian Zobel's avatar
Julian Zobel committed
285
286
287
288
289
	/**
	 * Assign the node a random attraction point, that is adjacent to the last one
	 * 
	 * @param ms The given node
	 */
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
	private void assignNewAttractionPoint(SimLocationActuator ms) {
		
		if(attractionOfClients.containsKey(ms)) {
			boolean updownrand = rand.nextBoolean();
			
			int index = attractionPoints.indexOf(attractionOfClients.get(ms));
			
			if(updownrand) {
				index = ((index + 1) % attractionPoints.size());
			} else {				
				index = ((index - 1 + attractionPoints.size()) % attractionPoints.size());
			}
			
			attractionOfClients.put(ms, attractionPoints.get(index));		
		} else {
			attractionOfClients.put(ms, attractionPoints.get(rand.nextInt(attractionPoints.size())));	
		}
	}
308
	
Julian Zobel's avatar
Julian Zobel committed
309
310
311
312
313
314
	/**
	 * Returns the next or last attraction point of the list to the given list index 
	 * 
	 * @param index Index of the attraction point, for which another attraction point should be returned
	 * @return Attraction Point
	 */
315
316
317
318
319
320
321
322
323
324
325
	private AttractionPoint returnNextOrLastAttractionPoint(int index) {
		boolean updownrand = rand.nextBoolean();
				
		if(updownrand) {
			index = ((index + 1) % attractionPoints.size());
		} else {				
			index = ((index - 1 + attractionPoints.size()) % attractionPoints.size());
		}
		
		return attractionPoints.get(index);
	}
326
327
328
329
330
331
332
333
334

	@Override
	public void eventOccurred(Object content, int type) {
		if (type == EVENT_INIT) {
			initialize();
		} else if (type == EVENT_MOVE) {
			move();
		}
	}
Julian Zobel's avatar
Julian Zobel committed
335
	
336
337
338
339
340
341
342
343
	protected List<AttractionPoint> getAttractionPoints() {
		return attractionPoints;
	}
	
	public void setAttractionPoint(AttractionPoint point) {
		this.attractionPoints.add(point);
	}
}