Manipulation_RandomMove.java 1.7 KB

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