RandomWiggle.java 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import java.util.Collection;
  2. import java.util.Iterator;
  3. import java.util.Random;
  4. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
  5. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.NetworkManipulationAlgorithm;
  6. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
  7. /**
  8. * NetworkManipulationAlgorithm, lets the SmartDevices wiggle during the simulation.
  9. *
  10. * @author Andreas T. Meyer-Berg
  11. */
  12. public class RandomWiggle implements NetworkManipulationAlgorithm {
  13. /**
  14. * Random number generator
  15. */
  16. private Random rand = new Random();
  17. @Override
  18. public void runAlgorithm(Controller controller, long currentTimeStep) {
  19. /**
  20. * Visible devices of the model, to select one which should be moved
  21. */
  22. Collection<SmartDevice> devices = controller.getNetworkController().getVisibleSmartDevices();
  23. /**
  24. * Iterator for the devices
  25. */
  26. Iterator<SmartDevice> it = devices.iterator();
  27. /**
  28. * SmartDevice which should be moved
  29. */
  30. SmartDevice move = null;
  31. /**
  32. * Index of the randomly selected device
  33. */
  34. int deviceNumber = rand.nextInt(devices.size());
  35. for(int i = 0; i <= deviceNumber && it.hasNext(); i++){
  36. move = it.next();
  37. }
  38. /**
  39. * Move device randomly up to 5 pixels in x and y direction
  40. */
  41. if(move != null){
  42. controller.getNetworkController().moveSmartDevice(move,(int)(move.getX()+rand.nextInt(11)-5), (int)(move.getY()+rand.nextInt(11)-5), move.getZ());
  43. controller.getNetworkController().validateDevicePosition();
  44. }
  45. /**
  46. * Refresh Panel
  47. */
  48. controller.notifyObservers();
  49. }
  50. }