RandomPathMovement.java 3.93 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
/*
 * Copyright (c) 2005-2011 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.Map;
import java.util.Set;

import de.tud.kom.p2psim.api.topology.movement.MovementInformation;
import de.tud.kom.p2psim.api.topology.movement.MovementSupported;
29
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
30
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
31
32
33
34
35
36
37
38
39
40
import de.tudarmstadt.maki.simonstrator.api.Time;

/**
 * A Random-Path Movement
 * 
 * @author Bjoern Richerzhagen
 * @version 1.0, mm/dd/2011
 */
public class RandomPathMovement extends AbstractMovementModel {

41
	private Map<SimLocationActuator, RandomPathMovementInfo> stateInfo;
42
43
44
45
46
47
48
49

	/**
	 * Maximal time a component walks into one direction
	 */
	public int maxStepsInOneDirection = 500;

	public RandomPathMovement() {
		super();
50
		stateInfo = new LinkedHashMap<SimLocationActuator, RandomPathMovement.RandomPathMovementInfo>();
51
52
53
	}

	@Override
54
	public void addComponent(SimLocationActuator component) {
55
56
57
58
59
60
61
62
63
64
		super.addComponent(component);
		stateInfo.put(component, new RandomPathMovementInfo());
	}

	public void setMaxStepsInOneDirection(int maxStepsInOneDirection) {
		this.maxStepsInOneDirection = maxStepsInOneDirection;
	}

	@Override
	public void move() {
65
66
		Set<SimLocationActuator> comps = getComponents();
		for (SimLocationActuator comp : comps) {
67
68
69
70
71
72
			PositionVector pos = comp.getRealPosition();
			RandomPathMovementInfo info = stateInfo.get(comp);
			if (info.getRemainingSteps() == 0 || info.getDelta() == null) {
				// assign new delta and new steps!
				assignNewMovementInfo(comp);
			}
73
			updatePosition(comp, pos.plus(info.getDelta()));
74
75
76
77
			info.setRemainingSteps(info.getRemainingSteps() - 1);
		}
	}

78
	protected void assignNewMovementInfo(SimLocationActuator comp) {
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
		RandomPathMovementInfo info = stateInfo.get(comp);

		PositionVector actPos = comp.getRealPosition();
		PositionVector delta = null;
		int steps = 1;
		PositionVector targetPos;

		do {
			steps = getRandomInt(
					1,
					(int) (maxStepsInOneDirection * Time.SECOND / getTimeBetweenMoveOperations())); // converge
			delta = getRandomDeltaWithinSpeed(actPos,
					comp.getMinMovementSpeed(),
					comp.getMaxMovementSpeed());
			double[] target = new double[comp.getRealPosition()
					.getDimensions()];
			for (int i = 0; i < target.length; i++) {
				target[i] = actPos.getEntry(i) + delta.getEntry(i) * steps;
			}
			targetPos = new PositionVector(target);
		} while (!isValidPosition(targetPos));

		info.setDelta(delta);
		info.setRemainingSteps(steps);
		// System.out.println(info);
	}

	/**
	 * Stores persistent Information on the Devices Path
	 * 
	 * @author Bjoern Richerzhagen
	 * @version 1.0, 05/10/2011
	 */
	protected class RandomPathMovementInfo implements MovementInformation {

		private int remainingSteps = 0;

		private PositionVector delta;

		public void setDelta(PositionVector delta) {
			this.delta = delta;
		}

		public PositionVector getDelta() {
			return delta;
		}

		public void setRemainingSteps(int steps) {
			this.remainingSteps = steps;
		}

		public int getRemainingSteps() {
			return remainingSteps;
		}

		@Override
		public String toString() {
			return "MovementInfo: d=" + delta.toString() + " steps="
					+ remainingSteps;
		}
	}
}