SocialMovementGroup.java 5.6 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
/*
 * 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.groups;

import java.util.LinkedHashSet;
import java.util.Random;
Julian Zobel's avatar
Julian Zobel committed
25
import java.util.UUID;
26
27
28
29
30
31
32
33
34

import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tudarmstadt.maki.simonstrator.api.Randoms;

/**
 * This class handles the instance of a group. Each group instance has a leader which sets the behavior. All other
 * participants follow the behavior of the leader in terms of speed, velocity, heading etc.
 * 
35
 * @author Julian Zobel
36
37
38
 */
public class SocialMovementGroup {
	
Julian Zobel's avatar
Julian Zobel committed
39
40
	public final UUID groupID;
	
41
	private Random rand = Randoms.getRandom(SocialMovementGroup.class);
42
43
44
45
46
47
	private SimLocationActuator leader;
	private LinkedHashSet<SimLocationActuator> members;

	private PositionVector destination;
	private PositionVector meetingPoint;
	
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

	/**
	 * Initializes the group with a given leader.
	 * 
	 * @param SimLocationActuator New leader of the group
	 * @param int The ID of the group.
	 */
	public SocialMovementGroup(SimLocationActuator leader) {
		
		this.groupID = UUID.randomUUID();
		
		this.leader = leader;
		this.members = new LinkedHashSet<SimLocationActuator>();
		this.members.add(leader);
	}
	
64
65
66
67
68
69
	/**
	 * Initializes the group, sets the participants and groupId, finally chooses a random leader.
	 * 
	 * @param Set<SimLocationActuator> All participants of the group.
	 * @param int The ID of the group.
	 */
70
	public SocialMovementGroup(LinkedHashSet<SimLocationActuator> participants) {
Julian Zobel's avatar
Julian Zobel committed
71
72
73
		
		this.groupID = UUID.randomUUID();
		
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
		setMembers(participants);
		setRandomLeader();
	}

	/**
	 * Adds a host to the group. Manually check in {@link ModularMovementModel} class, if group size is exceeded by adding another host.
	 * 
	 * @param SimLocationActuator The host to be added.
	 */
	public void addHostToGroup(SimLocationActuator host) {
		members.add(host);
	}

	/**
	 * Removes a host from the group. If the host was the current leader, a random new leader is chosen from the remaining members.
	 * 
	 * @param SimLocationActuator The host to be removed.
	 */
	public void removeHostFromGroup(SimLocationActuator host) {
		if(members.contains(host)) {
			if(host == leader) {
				members.remove(host);
				setRandomLeader();
			}
			else {
				members.remove(host);
			}
		}
	}

	/**
	 * Sets a randomly chosen participant of the group as leader.
	 */
	private void setRandomLeader() {		
		this.leader = getRandomMember();
	}

	/**
	 * Returns a random participant of all group members. 
	 * 
	 * @return SimLocationActuator
	 */
	public SimLocationActuator getRandomMember() {
		
118
		if(members.size() > 1) {			
119
120
121
122
123
124
125
126
127
128
			int item = rand.nextInt(members.size() - 1);
			int i = 0;
			
			for(SimLocationActuator host : members) {
				if(i == item) {
					return host;
				}
				i++;
			}
		}
Julian Zobel's avatar
wip    
Julian Zobel committed
129
130
131
		else if(members.size() == 1) {
			return members.iterator().next();
		}
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
				
		return null;		
	}

	/**
	 * Returns true if this group has a specific host as member, false otherwise
	 * 
	 * @param SimLocationActuator The host to be checked.
	 * @return boolean
	 */
	public boolean hasMember(SimLocationActuator host) {
		if(members.contains(host)) {
			return true;
		}
		return false;
	}

	/**
	 * Returns the position of the group leader.
	 * 
	 * @return PositionVector The position of the group leader.
	 */
	public PositionVector getLeaderPosition() {
		return leader.getRealPosition();
	}
	
158
159
	@Override
	public String toString() {
160
		return "Social Group of Leader #" + leader.getHost().getId() + " ("+ members.size() +")";
161
162
163
		
	}
	
164
165
166
167
168
169
170
171
	// ===================
	// Getters and Setters
	// ===================

	public int getGroupSize() {
		return members.size();
	}
	
172
	public LinkedHashSet<SimLocationActuator> getMembers() {
173
		return new LinkedHashSet<SimLocationActuator>(members);
174
175
	}
	
176
	public void setMembers(LinkedHashSet<SimLocationActuator> participants) {
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
		this.members = new LinkedHashSet<>(participants);
	}
	
	public SimLocationActuator getLeader() {
		return leader;
	}
	
	public PositionVector getDestination() {
		return destination;
	}
	
	public void setDestination(PositionVector destination) {
		this.destination = destination;
	}
	
	public PositionVector getMeetingPoint() {
		return meetingPoint;
	}
	
	public void setMeetingPoint(PositionVector meetingPoint) {
		this.meetingPoint = meetingPoint;
	}
Julian Zobel's avatar
Julian Zobel committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
	
	@Override
	public boolean equals(Object obj) {
		if(!(obj instanceof SocialMovementGroup)) {
			return false;
		}
		
		SocialMovementGroup toCompare = (SocialMovementGroup) obj;
		
		if(this.groupID == toCompare.groupID) {
			if(this.getLeader() == toCompare.getLeader()) {
				return true;
			}
			else {
				System.out.println("Leader seems to be different, but still the same UUID");
				return false;
			}
		}
		else return false;		
	}
	
	@Override
	public int hashCode() {
		//FIXME hope this is correct -- should we include the leader and/or other fields as well?		
		return groupID.hashCode();
	}
225
}