SimulationManager.java 7.0 KB

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