ConfigurableGridPositionDistribution.java 4.97 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
/*
 * 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.placement;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.BitSet;
import java.util.List;
import java.util.Random;
import java.util.Vector;

import de.tud.kom.p2psim.api.topology.TopologyComponent;
import de.tud.kom.p2psim.api.topology.placement.PlacementModel;
33
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
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
71
72
73
74
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import de.tudarmstadt.maki.simonstrator.api.Randoms;

/**
 * A configurable grid where occupied positions in the grid are configured via a
 * simple matrix. Scales with the world-size.
 * 
 * Format of the matrix: lines with 0,1,0,0,0,1\n0,1,...
 * 
 * where 1 means: position a node and 0 means: empty cell.
 * 
 * 
 * @author Bjoern Richerzhagen
 * @version 1.0, Sep 19, 2013
 */
public class ConfigurableGridPositionDistribution implements PlacementModel {

	private int numberOfComponents = 0;

	private boolean randInCell = true;

	private final String SEP = ",";

	private final String OCCUPIED = "1";

	private final String EMPTY = "0";

	private int placedComponents = 0;

	private int cols = -1;

	private int rows = -1;

	private BitSet occupiedBits = new BitSet();

	private PositionVector worldDimensions = null;

	private Random random = Randoms
			.getRandom(ConfigurableGridPositionDistribution.class);

	private List<PositionVector> positions = new Vector<PositionVector>();

	@Override
	public void addComponent(TopologyComponent comp) {
		if (worldDimensions == null) {
			worldDimensions = comp.getTopology().getWorldDimensions();
		}
		numberOfComponents++;
		if (numberOfComponents == positions.size() + 1) {
			System.err
					.println("WARNING: Your specified positioning-topology does not contain enough places for the number of components in your scenario!");
		}
	}

	@Override
	public PositionVector place(TopologyComponent comp) {
		if (positions.isEmpty()) {
			calcPositions2D();
		}
		PositionVector pos = positions.get(placedComponents);
		placedComponents = (placedComponents + 1) % positions.size();
		return pos;
	}

	private void calcPositions2D() {
		assert rows > 0 && cols > 0;
		// float ratio = (float) (worldDimensions.getX() /
		// worldDimensions.getY());
		// float ratio_1 = 1 / ratio;
		// int anz_x = ((int) Math.sqrt(ratio * cols * rows)) + 1;
		int anz_x = cols;
		// int anz_y = ((int) Math.sqrt(ratio_1 * cols * rows)) + 1;
		int anz_y = rows;
		int dist_x = (int) worldDimensions.getX() / anz_x;
		int dist_y = (int) worldDimensions.getY() / anz_y;
		for (int y = 0; y < anz_y; y++) {
			for (int x = 0; x < anz_x; x++) {
				if (occupiedBits.get(y * cols + x)) {
					// valid
					double xcenter = x * dist_x + dist_x / 2;
					double ycenter = y * dist_y + dist_y / 2;
					if (randInCell) {
						xcenter += random.nextDouble() * dist_x / 4 - dist_x
								/ 8;
						ycenter += random.nextDouble() * dist_y / 4 - dist_y
								/ 8;
					}
					PositionVector vec = new PositionVector(xcenter, ycenter);
					positions.add(vec);
				}
			}
		}
	}

	/**
	 * Allow a random offset between the grid points and the actual node
	 * position.
	 * 
	 * @param randomOffset
	 *            default: true
	 */
	public void setRandomOffset(boolean randomOffset) {
		this.randInCell = randomOffset;
	}

	/**
	 * The given file is parsed.
	 * 
	 * @param filename
	 */
	public void setPlacementFile(String file) {
		BufferedReader csv = null;
		try {
			csv = new BufferedReader(new FileReader(file));

			while (csv.ready()) {
				String line = csv.readLine();

				if (line.startsWith("#")) {
					continue;
				}

				if (line.indexOf(SEP) > -1) {
					String[] parts = line.split(SEP);
					if (cols == -1) {
						cols = parts.length;
					}
					assert cols == parts.length;

					if (rows == -1) {
						rows = 0;
					}
					
					int actCol = 0;
					for (String part : parts) {
						if (part.equals(OCCUPIED)) {
							occupiedBits.set(rows * cols + actCol);
						} else if (part.equals(EMPTY)) {
							// ok.
						} else {
							throw new AssertionError("Unknown entry " + part);
						}
						actCol++;
					}
					rows++;

				}
			}

		} catch (Exception e) {
			throw new AssertionError("Positioning-Input failed!");
		} finally {
			if (csv != null) {
				try {
					csv.close();
				} catch (IOException e) {
					//
				}
			}
		}
	}

}