/* * 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 . * */ package de.tud.kom.p2psim.impl.topology.movement.modularosm.transition; import java.util.Collection; import java.util.Collections; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Random; import java.util.Set; import de.tud.kom.p2psim.api.scenario.ConfigurationException; import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator; import de.tudarmstadt.maki.simonstrator.api.Randoms; import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.AttractionPoint; /** * Abstract base class for Transition Strategies ({@link ITransitionStrategy}s) using {@link AttractionPoint}s. * * @author Julian Zobel * @version 1.0, 23.01.2019 */ public abstract class AbstractAttractionBasedTransitionStrategy implements ITransitionStrategy { protected Random rnd = Randoms.getRandom(AbstractAttractionBasedTransitionStrategy.class); protected Set attractionPoints = new LinkedHashSet<>(); protected Map assignments = new LinkedHashMap<>(); protected Map lastAssignments = new LinkedHashMap<>(); private List listeners = new LinkedList<>(); private long pauseTimeMin = 0; private long pauseTimeMax = 0; protected final static int EVENT_PAUSE_ENDED = 1; @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); } @Override public void setAttractionPoints( Collection attractionPoints) { this.attractionPoints.clear(); this.attractionPoints.addAll(attractionPoints); } @Override public Set getAllAttractionPoints() { return Collections.unmodifiableSet(attractionPoints); } /** * Notify all listeners of an updated attraction point assignment for the given component * * @param comp * @param attractionPoint */ 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); } protected long getPauseTime() { return (long) (rnd.nextDouble() * (pauseTimeMax-pauseTimeMin)) + pauseTimeMin; } public void setMinPauseTime(long minPauseTime) { if (minPauseTime < 0) { throw new ConfigurationException( "MinPauseTime should be >= 0!"); } this.pauseTimeMin = minPauseTime; } public void setMaxPauseTime(long maxPauseTime) { if (maxPauseTime < 0) { throw new ConfigurationException( "MaxPauseTime should be >= 0!"); } this.pauseTimeMax = maxPauseTime; } }