SimulationManager.java 9.1 KB

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