ModularMovementModel.java 10.2 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
/*
 * 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.modularosm;

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

32
33
import javax.swing.JComponent;

34
import de.tud.kom.p2psim.api.scenario.ConfigurationException;
35
import de.tud.kom.p2psim.api.topology.Topology;
36
import de.tud.kom.p2psim.api.topology.movement.MovementModel;
37
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
38
import de.tud.kom.p2psim.api.topology.movement.local.LocalMovementStrategy;
39
import de.tud.kom.p2psim.api.topology.placement.PlacementModel;
40
41
import de.tud.kom.p2psim.impl.simengine.Simulator;
import de.tud.kom.p2psim.impl.topology.PositionVector;
42
import de.tud.kom.p2psim.impl.topology.TopologyFactory;
43
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.IAttractionGenerator;
44
import de.tud.kom.p2psim.impl.topology.movement.modularosm.mapvisualization.IMapVisualization;
45
46
47
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;
48
import de.tudarmstadt.maki.simonstrator.api.Binder;
49
50
51
import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.EventHandler;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
52
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.AttractionPoint;
53
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.LocationActuator;
54
55
56

/**
 * Modular Movement Model uses different models/strategies to create a movement
57
 * model. In this implementation, it has 3 different models/strategies.
58
59
 * <p>
 * M0: AttractionGenerator -> Generates the {@link AttractionPoint}s and place
60
61
 * them on the map. The {@link AttractionPoint}s can't be moved, because they
 * are static POIs from real-world data!
62
 * <p>
63
64
 * M1: A general {@link MovementModel} is not used, because we use static
 * attraction points.
65
66
67
68
69
70
71
72
73
74
75
76
 * <p>
 * M2: The {@link TransitionStrategy}! It takes the Hosts, which should be moved
 * around, but calculates only the assignment to the {@link AttractionPoint}s.
 * It doesn't move the Hosts! It will be only assignment a new AttractionPoint!
 * 
 * <p>
 * M3: The {@link LocalMovementStrategy} is responsible for the movement of the
 * Hosts. It moves the hosts to the assigned AttractionPoint, and if the
 * AttractionPoint has moved, then will be followed. The
 * {@link LocalMovementStrategy} will be called from the
 * {@link ModularMovementModel} to do a Movement!
 * <p>
77
 * This class contains all three components and manage the data exchange.
78
79
80
81
82
83
84
85
86
87
 * Additionally it contains an periodic operation, which handle the movement of
 * all hosts. This mean, that it will be call the {@link LocalMovementStrategy}
 * with the destination. Please take care, that the handling of the movement of
 * the AttractionPoints will be handled by the movement model in M1! <br>
 * Further it contains an offset for every Host, which will be added to the
 * destination point (AttractionPoint), so that not all hosts, which are
 * assigned to one {@link AttractionPoint}, lies on the same point.<br>
 * 
 * 
 * 
88
 * @author Martin Hellwig, Christoph Muenker
89
 * @version 1.0, 07.07.2015
90
91
92
93
94
95
 */
public class ModularMovementModel implements MovementModel, EventHandler {

	private final int EVENT_MOVE = 1;

	private final int EVENT_INIT = 2;
96

97
98
99
100
101
102
103
	protected PositionVector worldDimensions;

	protected ITransitionStrategy transition;

	protected IAttractionGenerator attractionGenerator;

	protected LocalMovementStrategy localMovementStrategy;
104

105
	protected IMapVisualization mapVisualization;
106

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

109
	private Map<SimLocationActuator, PositionVector> offsetPosition = new LinkedHashMap<SimLocationActuator, PositionVector>();
110
111
112
113
114
115
116
117

	private boolean initialized = false;

	private long timeBetweenMoveOperation = Simulator.SECOND_UNIT;

	private Random rand;

	public ModularMovementModel() {
118
119
		this.worldDimensions = Binder.getComponentOrNull(Topology.class)
				.getWorldDimensions();
120
121
122
123
124
125
126
127
128
129
130
		this.rand = Randoms.getRandom(ModularMovementModel.class);

		// 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() {
131

132
133
134
135
136
		if (!initialized) {			
			VisualizationInjector.injectComponent(new ModularMovementModelViz(this));
			if (mapVisualization != null) {
				VisualizationInjector.injectComponent(mapVisualization);
			}
137
138
139
140

			checkConfiguration();

			// setWayPointModel
141
142
143
144
			localMovementStrategy.setObstacleModel(Binder
					.getComponentOrNull(Topology.class).getObstacleModel());
			localMovementStrategy.setWaypointModel(Binder
					.getComponentOrNull(Topology.class).getWaypointModel());
145

146
147
			List<AttractionPoint> attractionPoints = attractionGenerator
					.getAttractionPoints();
148
			transition.setAttractionPoints(attractionPoints);
149
150
151

			// This adds the mobile hosts (smartphones/users) to the transition
			// strategy
152
			for (SimLocationActuator ms : moveableHosts) {
153
154
155
156
157
158
159
160
161
162
163
				transition.addComponent(ms);
			}

			setTimeBetweenMoveOperations(timeBetweenMoveOperation);

			// initial move
			move();

			initialized = true;
		}
	}
164

165
166
167
168
169
170
171
172
	/**
	 * This default implementation relies on {@link PlacementModel}s to be
	 * configured in the {@link TopologyFactory}
	 */
	@Override
	public void placeComponent(SimLocationActuator actuator) {
		// not supported
	}
173

174
	@Override
175
176
177
178
179
180
181
	public void changeTargetLocation(SimLocationActuator actuator, AttractionPoint ap) {
		transition.updateTargetAttractionPoint(actuator, ap);
	}
	
	@Override
	public AttractionPoint getTargetLocation(SimLocationActuator actuator) {
		return transition.getAssignments().get(actuator);
182
	}
183
184
185
186
187
188
	
	@Override
	public Set<AttractionPoint> getAllAttractionPoints()
			throws UnsupportedOperationException {
		return transition.getAllAttractionPoints();
	}
189

190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
	private void checkConfiguration() {
		if (localMovementStrategy == null) {
			throw new ConfigurationException(
					"LocalMovementStrategy is missing in ModularMovementModel!");
		}
		if (transition == null) {
			throw new ConfigurationException(
					"TransitionStrategy is missing in ModularMovementModel!");
		}
		if (attractionGenerator == null) {
			throw new ConfigurationException(
					"AttractionGenerator is missing in ModularMovementModel!");
		}
	}

	@Override
206
	public void addComponent(SimLocationActuator comp) {
207
208
209
		moveableHosts.add(comp);
		offsetPosition.put(comp, randomOffsetVector());
	}
210
211
212
213
	
	public Set<SimLocationActuator> getAllLocationActuators() {
		return moveableHosts;
	}
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232

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

	private PositionVector randomOffsetVector() {
		double x = rand.nextGaussian() * 6;
		double y = rand.nextGaussian() * 6;

		return new PositionVector(x, y);
	}

	protected void move() {
233
		Map<SimLocationActuator, AttractionPoint> assigns = transition
234
				.getAssignments();
235
		for (Entry<SimLocationActuator, AttractionPoint> entry : assigns
236
				.entrySet()) {
237
			SimLocationActuator ms = entry.getKey();
238
			PositionVector attractionCenter = (PositionVector) entry.getValue();
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
			PositionVector destination = new PositionVector(attractionCenter);
			destination.add(offsetPosition.get(ms));

			doLocalMovement(ms, destination);

		}

		Event.scheduleWithDelay(timeBetweenMoveOperation, this, null,
				EVENT_MOVE);
	}

	/**
	 * 
	 * Ask the local movement strategy for the next position. It may return the
	 * next position or a boolean with true to notify the movement model that it
	 * can't get any closer to the current way point.
	 * 
	 * @param ms
	 * @param destination
	 */
259
	private void doLocalMovement(SimLocationActuator ms,
260
261
262
263
264
			PositionVector destination) {

		Either<PositionVector, Boolean> either = localMovementStrategy
				.nextPosition(ms, destination);
		if (either.hasLeft()) {
265
			ms.updateCurrentLocation(either.getLeft());
266
267
268
			/*
			 * Check for negative or out of bound coordinates!
			 */
269
270
271
272
273
274
275
276
			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();
Björn Richerzhagen's avatar
Björn Richerzhagen committed
277
278
		} else {
			transition.reachedAttractionPoint(ms);
279
280
281
		}
	}

282
283
	public void setIAttractionGenerator(
			IAttractionGenerator attractionGenerator) {
284
285
286
287
288
289
290
291
292
293
294
		this.attractionGenerator = attractionGenerator;
	}

	public void setLocalMovementStrategy(
			LocalMovementStrategy localMovementStrategy) {
		this.localMovementStrategy = localMovementStrategy;
	}

	public void setITransitionStrategy(ITransitionStrategy transition) {
		this.transition = transition;
	}
295

296
297
298
	public void setIMapVisualization(IMapVisualization mapVisualization) {
		this.mapVisualization = mapVisualization;
	}
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313

	@Override
	public void eventOccurred(Object content, int type) {
		if (type == EVENT_INIT) {
			initialize();
		} else if (type == EVENT_MOVE) {
			move();
		}
	}

	/**
	 * Only for visualization!
	 * 
	 * @return
	 */
314
315
316
	public List<AttractionPoint> getAttractionPoints() {
		return new Vector<AttractionPoint>(transition.getAllAttractionPoints());
	}
317
}