SocialMovementGroup.java 5.21 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
/*
 * 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;
import java.util.Set;
Julian Zobel's avatar
Julian Zobel committed
26
import java.util.UUID;
27
28
29
30
31
32
33
34
35
36
37
38
39
40

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.
 * 
 * @author Marcel Verst
 * @version 1.0, 22.11.2018
 */
public class SocialMovementGroup {
	
Julian Zobel's avatar
Julian Zobel committed
41
42
	public final UUID groupID;
	
43
44
45
46
47
48
49
50
51
52
53
54
	private SimLocationActuator leader;
	private LinkedHashSet<SimLocationActuator> members;

	private PositionVector destination;
	private PositionVector meetingPoint;
	
	/**
	 * 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.
	 */
Julian Zobel's avatar
Julian Zobel committed
55
56
57
58
	public SocialMovementGroup(Set<SimLocationActuator> participants) {
		
		this.groupID = UUID.randomUUID();
		
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
		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() {
		
Julian Zobel's avatar
wip    
Julian Zobel committed
103
		if(members.size() > 1) {
104
105
106
107
108
109
110
111
112
113
114
			Random rand = Randoms.getRandom(SocialMovementGroup.class);
			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
115
116
117
		else if(members.size() == 1) {
			return members.iterator().next();
		}
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
				
		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();
	}
	
144
145
146
147
148
149
	@Override
	public String toString() {
		return "Social Group of #" + leader.getHost().getId() + " ("+ members.size() +")";
		
	}
	
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
	// ===================
	// Getters and Setters
	// ===================

	public int getGroupSize() {
		return members.size();
	}
	
	public Set<SimLocationActuator> getMembers() {
		return members;
	}
	
	public void setMembers(Set<SimLocationActuator> participants) {
		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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
	
	@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();
	}
211
}