SimulationManager.java 10 KB

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