AbstractAttractionBasedAssignmentStrategy.java 4.35 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
/*
 * 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.transition;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
28

29
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
Julian Zobel's avatar
wip    
Julian Zobel committed
30
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.IAttractionGenerator;
31
32
33
34
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.AttractionPoint;

/**
Julian Zobel's avatar
wip    
Julian Zobel committed
35
 * Abstract base class for Transition Strategies ({@link IAttractionAssigmentStrategy}s) using {@link AttractionPoint}s.
36
37
38
39
 * 
 * @author Julian Zobel
 * @version 1.0, 23.01.2019
 */
40
public abstract class AbstractAttractionBasedAssignmentStrategy implements IAttractionAssigmentStrategy {
41

42
	protected Random rnd = Randoms.getRandom(AbstractAttractionBasedAssignmentStrategy.class);
43
		
44
45
46
47
	protected Map<SimLocationActuator, AttractionPoint> assignments = new LinkedHashMap<>();
	
	protected Map<SimLocationActuator, AttractionPoint> lastAssignments = new LinkedHashMap<>();
	
48
	private List<AttractionAssignmentListener> listeners = new LinkedList<>();
49
	
50
51
	protected long defaultPauseTimeMin = 0;	
	protected long defaultPauseTimeMax = 0;
52
53
54
	
	protected final static int EVENT_PAUSE_ENDED = 1;
	
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
	@Override
	public AttractionPoint getAssignment(SimLocationActuator comp) {
		return assignments.get(comp);
	}
	
	@Override
	public void addAttractionAssignmentListener(
			AttractionAssignmentListener listener) {
		listeners.add(listener);
	}
	
	@Override
	public void removeAttractionAssignmentListener(
			AttractionAssignmentListener listener) {
		listeners.remove(listener);
	}

72
73
74
75
76
77
	/**
	 * Notify all listeners of an updated attraction point assignment for the given component
	 * 
	 * @param comp
	 * @param attractionPoint
	 */
78
79
80
81
82
83
84
85
86
87
88
89
90
91
	protected void notifyListenersOfAssignmentUpdate(SimLocationActuator comp,
			AttractionPoint attractionPoint) {
		listeners.forEach(listener -> listener.updatedAttractionAssignment(comp, attractionPoint));
	}
	
	@Override
	public void updateTargetAttractionPoint(SimLocationActuator comp,
			AttractionPoint attractionPoint) {
		if(assignments.containsKey(comp)) {
			this.lastAssignments.put(comp, this.assignments.remove(comp));
		}
		this.assignments.put(comp, attractionPoint);
		notifyListenersOfAssignmentUpdate(comp, attractionPoint);	
	}
92
	
93
	private long getRandomUniformDistributionPauseTime(AttractionPoint attractionPoint) {
94
95
96
97
98
99
100
101
		
		// if attraction point has own pause time use it, otherwise use default values
		if(attractionPoint.hasPauseTime()) {
			return (long) (rnd.nextDouble() * (attractionPoint.getPauseTimeMax() - attractionPoint.getPauseTimeMin())) + attractionPoint.getPauseTimeMin();
		}
		else {
			return (long) (rnd.nextDouble() * (defaultPauseTimeMax - defaultPauseTimeMin)) + defaultPauseTimeMin;
		}		
102
103
	}
	
104
105
	protected long getPauseTime(AttractionPoint attractionPoint) {
		return getRandomUniformDistributionPauseTime(attractionPoint);
106
	}	
Julian Zobel's avatar
wip    
Julian Zobel committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
	
	protected AttractionPoint getNewAttractionPointAssignment(SimLocationActuator component) {		
		double score = rnd.nextDouble();		
		List<AttractionPoint> candidates = new LinkedList<>();
		for (AttractionPoint ap : IAttractionGenerator.attractionPoints) {
			
			if(ap == null) {
				continue;
			}
			
			if (ap.getWeight() >= score) {
				if(lastAssignments.get(component) == null || !ap.equals(lastAssignments.get(component))) {
					candidates.add(ap);
				}				
			}
		}
		
		if (candidates.isEmpty()) {
			candidates.addAll(IAttractionGenerator.attractionPoints);
		}
		
		AttractionPoint assignment = candidates.get(rnd.nextInt(candidates.size()));		
		return assignment;		
	}  
131
}