SimulationManager.java 8.6 KB

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