SimulationManager.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package ui.controller;
  2. import classes.*;
  3. import ui.model.IntermediateCableWithState;
  4. import ui.model.DecoratedCable;
  5. import ui.model.DecoratedCable.CableState;
  6. import ui.model.DecoratedSwitch.SwitchState;
  7. import ui.model.DecoratedNetwork;
  8. import ui.model.DecoratedState;
  9. import ui.model.DecoratedSwitch;
  10. import ui.model.MinimumModel;
  11. import ui.model.MinimumNetwork;
  12. import ui.model.Model;
  13. import ui.model.Model.FairnessModel;
  14. import ui.model.VisualRepresentationalState;
  15. import java.util.ArrayList;
  16. import java.util.HashMap;
  17. import java.util.LinkedList;
  18. import java.util.ListIterator;
  19. /**
  20. * Controller for Simulation.
  21. *
  22. * @author Gruppe14
  23. */
  24. public class SimulationManager {
  25. int global = 0;
  26. private Model model;
  27. private HashMap<Integer, DecoratedState> saves = new HashMap<Integer, DecoratedState>();
  28. private HashMap<Integer, VisualRepresentationalState> savesVisual = new HashMap<Integer, VisualRepresentationalState>();
  29. private HashMap<Integer, FlexManager> savesFlexManger = new HashMap<Integer, FlexManager>();
  30. private int timeStep;
  31. /**
  32. * Constructor.
  33. *
  34. * @param m
  35. * Model
  36. */
  37. public SimulationManager(Model m) {
  38. model = m;
  39. }
  40. /**
  41. * calculates the flow of the edges and the supply for objects and consider old timesteps for burned cables.
  42. *
  43. * @param timestep
  44. * current Iteration
  45. * @param updateVisual TODO
  46. */
  47. public void calculateStateForTimeStep(int timestep, boolean updateVisual) {
  48. boolean doesFlexManagerExist = savesFlexManger.containsKey(timestep);
  49. boolean createNew = updateVisual || !doesFlexManagerExist;
  50. FlexManager newFlexManager;
  51. if(createNew) {
  52. newFlexManager = new FlexManager(model, timestep, savesFlexManger.get(timestep-1));
  53. if(doesFlexManagerExist) newFlexManager.orderFlexFromList(savesFlexManger.get(timestep).getAllFlexesOrderedThisTimeStep());
  54. savesFlexManger.put(timestep, newFlexManager);
  55. }else {
  56. newFlexManager = savesFlexManger.get(timestep);
  57. }
  58. HashMap<Edge, CableState> map = new HashMap<Edge, CableState>();
  59. if(timestep > 0 && saves.containsKey(timestep-1)) //if the state before exist
  60. {
  61. //make cable hastmap
  62. DecoratedState theStateBefore = saves.get(timestep-1);
  63. //edges without HolonObjects or burned
  64. for(DecoratedCable edge : theStateBefore.getLeftOverEdges())
  65. {
  66. map.put(edge.getModel(), edge.getState());
  67. }
  68. }
  69. timeStep = timestep;
  70. ArrayList<MinimumNetwork> list = new ArrayList<MinimumNetwork>();
  71. MinimumModel minimumModel = new MinimumModel(model.getObjectsOnCanvas(), model.getEdgesOnCanvas());
  72. //set all BreakedManuel Cable Burned:
  73. for(IntermediateCableWithState cable : minimumModel.getEdgeList()) {
  74. if(cable.getModel().isBreakedManuel()) cable.setState(CableState.Burned);
  75. }
  76. //set all the state before:
  77. for(IntermediateCableWithState cable : minimumModel.getEdgeList()) {
  78. if(map.containsKey(cable.getModel())) cable.setState(map.get(cable.getModel()));
  79. }
  80. ArrayList<IntermediateCableWithState> leftOver = new ArrayList<IntermediateCableWithState>();
  81. boolean doAnotherLoop = true;
  82. while(doAnotherLoop) {
  83. doAnotherLoop = false;
  84. list = calculateNetworks(minimumModel, timestep, leftOver);
  85. for(MinimumNetwork net : list) {
  86. float energyOnCables = net.getHolonObjectList().stream().filter(object -> object.getEnergyAtTimeStepWithFlex(timestep, newFlexManager) > 0.0f).map(object -> object.getEnergyAtTimeStepWithFlex(timestep, newFlexManager)).reduce(0.0f, ((a,b) -> a + b));
  87. //find the cable with the energy supplied from his two connected objects are the biggest, from all cables that the network give more energy than the cablecapacity.
  88. IntermediateCableWithState cable = net.getEdgeList().stream().filter(aCable -> energyOnCables > aCable.getModel().getCapacity() && !aCable.getModel().isUnlimitedCapacity()).max((lhs,rhs) -> Float.compare(lhs.getEnergyFromConnetedAtTimestep(timestep, newFlexManager), rhs.getEnergyFromConnetedAtTimestep(timestep, newFlexManager))).orElse(null);
  89. if(cable != null) {
  90. cable.setState(CableState.Burned);
  91. doAnotherLoop = true;
  92. }
  93. }
  94. }
  95. ArrayList<DecoratedNetwork> decorNetworks = new ArrayList<DecoratedNetwork>();
  96. FairnessModel actualFairnessModel = model.getFairnessModel();
  97. for (MinimumNetwork net : list) {
  98. decorNetworks.add(new DecoratedNetwork(net, timestep, actualFairnessModel, newFlexManager));
  99. }
  100. ArrayList<DecoratedCable> leftOverDecoratedCables = new ArrayList<DecoratedCable>();
  101. for(IntermediateCableWithState cable: leftOver) {
  102. leftOverDecoratedCables.add(new DecoratedCable(cable.getModel(), cable.getState(), 0.0f));
  103. }
  104. ArrayList<DecoratedSwitch> listOfDecoratedSwitches = decorateSwitches(minimumModel, timestep);
  105. DecoratedState stateFromThisTimestep = new DecoratedState(decorNetworks, leftOverDecoratedCables, listOfDecoratedSwitches, newFlexManager, timestep);
  106. saves.put(timestep, stateFromThisTimestep);
  107. if(updateVisual)savesVisual.put(timestep, new VisualRepresentationalState(stateFromThisTimestep, minimumModel));
  108. }
  109. /**
  110. * Decorate a switch
  111. * @param minModel
  112. * @param iteration
  113. * @return
  114. */
  115. public static ArrayList<DecoratedSwitch> decorateSwitches(MinimumModel minModel, int iteration) {
  116. ArrayList<DecoratedSwitch> aListOfDecoratedSwitches = new ArrayList<DecoratedSwitch>();
  117. for(HolonSwitch hSwitch: minModel.getSwitchList()) {
  118. aListOfDecoratedSwitches.add(new DecoratedSwitch(hSwitch, hSwitch.getState(iteration) ? SwitchState.Closed : SwitchState.Open));
  119. }
  120. return aListOfDecoratedSwitches;
  121. }
  122. /**
  123. * SubFunction to calculate the Networks from the model.
  124. * @param minModel
  125. * @param Iteration
  126. * @param leftOver
  127. * @return
  128. */
  129. ArrayList<MinimumNetwork> calculateNetworks(MinimumModel minModel, int Iteration, ArrayList<IntermediateCableWithState> leftOver){
  130. //Copy minModel ObjectList
  131. ArrayList<HolonObject> holonObjectList = new ArrayList<HolonObject>();
  132. for(HolonObject holonObject: minModel.getHolonObjectList()) {
  133. holonObjectList.add(holonObject);
  134. }
  135. //Copy minModelEdgeList
  136. ArrayList<IntermediateCableWithState> edgeList = new ArrayList<IntermediateCableWithState>();
  137. for(IntermediateCableWithState cable: minModel.getEdgeList()) {
  138. edgeList.add(cable);
  139. }
  140. ArrayList<MinimumNetwork> listOfNetworks = new ArrayList<MinimumNetwork>();
  141. while(!holonObjectList.isEmpty()) {
  142. //lookAt the first holonObject and find his neighbors
  143. HolonObject lookAtObject = holonObjectList.get(0);
  144. //delete out of list
  145. holonObjectList.remove(0);
  146. //create a new Network
  147. MinimumNetwork actualNetwork = new MinimumNetwork(new ArrayList<HolonObject>(), new ArrayList<IntermediateCableWithState>());
  148. actualNetwork.getHolonObjectList().add(lookAtObject);
  149. //create List of neighbors
  150. LinkedList<AbstractCanvasObject> neighbors = new LinkedList<AbstractCanvasObject>();
  151. populateListOfNeighbors(edgeList, lookAtObject, actualNetwork, neighbors);
  152. while(!neighbors.isEmpty()) {
  153. AbstractCanvasObject lookAtNeighbor = neighbors.getFirst();
  154. if(lookAtNeighbor instanceof HolonObject) {
  155. actualNetwork.getHolonObjectList().add((HolonObject) lookAtNeighbor);
  156. holonObjectList.remove(lookAtNeighbor);
  157. }else {
  158. actualNetwork.getNodeAndSwitches().add(lookAtNeighbor);
  159. }
  160. //When HolonSwitch Check if closed
  161. if(!(lookAtNeighbor instanceof HolonSwitch) || ((HolonSwitch)lookAtNeighbor).getState(Iteration)) {
  162. populateListOfNeighbors(edgeList, lookAtNeighbor, actualNetwork, neighbors);
  163. }
  164. neighbors.removeFirst();
  165. }
  166. listOfNetworks.add(actualNetwork);
  167. }
  168. if(leftOver!= null) {
  169. leftOver.clear();
  170. for(IntermediateCableWithState cable: edgeList) {
  171. leftOver.add(cable);
  172. }
  173. }
  174. return listOfNetworks;
  175. }
  176. /**
  177. * Adds the neighbors.
  178. * @param edgeList
  179. * @param lookAtObject
  180. * @param actualNetwork
  181. * @param neighbors
  182. */
  183. void populateListOfNeighbors(ArrayList<IntermediateCableWithState> edgeList, AbstractCanvasObject lookAtObject,
  184. MinimumNetwork actualNetwork, LinkedList<AbstractCanvasObject> neighbors) {
  185. ListIterator<IntermediateCableWithState> iter = edgeList.listIterator();
  186. while(iter.hasNext())
  187. {
  188. IntermediateCableWithState lookAtEdge = iter.next();
  189. if(lookAtEdge.getState() == CableState.Working && lookAtEdge.getModel().isConnectedTo(lookAtObject)) {
  190. iter.remove();
  191. actualNetwork.getEdgeList().add(lookAtEdge);
  192. //Add neighbar
  193. AbstractCanvasObject edgeNeighbor;
  194. if(lookAtEdge.getModel().getA().equals(lookAtObject)) {
  195. edgeNeighbor = lookAtEdge.getModel().getB();
  196. }else {
  197. edgeNeighbor = lookAtEdge.getModel().getA();
  198. }
  199. if(!neighbors.contains(edgeNeighbor)) {
  200. neighbors.add(edgeNeighbor);
  201. }
  202. }
  203. }
  204. }
  205. public DecoratedState getActualDecorState() {
  206. return getDecorState(timeStep);
  207. }
  208. public VisualRepresentationalState getActualVisualRepresentationalState(){
  209. return savesVisual.getOrDefault(timeStep, null);
  210. }
  211. public FlexManager getActualFlexManager() {
  212. return savesFlexManger.getOrDefault(timeStep, null);
  213. }
  214. public void resetFlexManager(){
  215. savesFlexManger.clear();
  216. }
  217. public void resetFlexManagerForTimeStep(int timestep) {
  218. savesFlexManger.get(timestep).reset();
  219. }
  220. public DecoratedState getDecorState(int timestep) {
  221. return saves.get(timestep);
  222. }
  223. public VisualRepresentationalState getVisualRepresentationalState(int timestep) {
  224. return savesVisual.getOrDefault(timestep, null);
  225. }
  226. }