SimulationManager.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. */
  55. public void calculateStateForTimeStep(int timestep) {
  56. HashMap<CpsEdge, CableState> map = new HashMap<CpsEdge, CableState>();
  57. if(timestep > 0 && saves.containsKey(timestep-1)) //if the state before exist
  58. {
  59. //make cable hastmap
  60. DecoratedState theStateBefore = saves.get(timestep-1);
  61. //edges without HolonObjects or burned
  62. for(DecoratedCable edge : theStateBefore.getLeftOverEdges())
  63. {
  64. map.put(edge.getModel(), edge.getState());
  65. }
  66. }
  67. timeStep = timestep;
  68. ArrayList<MinimumNetwork> list = new ArrayList<MinimumNetwork>();
  69. MinimumModel minimumModel = new MinimumModel(model.getObjectsOnCanvas(), model.getEdgesOnCanvas());
  70. //set all BreakedManuel Cable Burned:
  71. for(IntermediateCableWithState cable : minimumModel.getEdgeList()) {
  72. if(cable.getModel().isBreakedManuel()) cable.setState(CableState.Burned);
  73. }
  74. //set all the state before:
  75. for(IntermediateCableWithState cable : minimumModel.getEdgeList()) {
  76. if(map.containsKey(cable.getModel())) cable.setState(map.get(cable.getModel()));
  77. }
  78. ArrayList<IntermediateCableWithState> leftOver = new ArrayList<IntermediateCableWithState>();
  79. boolean doAnotherLoop = true;
  80. while(doAnotherLoop) {
  81. doAnotherLoop = false;
  82. list = calculateNetworks(minimumModel, timestep, leftOver);
  83. for(MinimumNetwork net : list) {
  84. float energyOnCables = net.getHolonObjectList().stream().filter(object -> object.getEnergyAtTimeStep(timestep) > 0.0f).map(object -> object.getEnergyAtTimeStep(timestep)).reduce(0.0f, ((a,b) -> a + b));
  85. //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.
  86. 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);
  87. if(cable != null) {
  88. cable.setState(CableState.Burned);
  89. doAnotherLoop = true;
  90. }
  91. }
  92. }
  93. ArrayList<DecoratedNetwork> decorNetworks = new ArrayList<DecoratedNetwork>();
  94. FairnessModel actualFairnessModel = model.getFairnessModel();
  95. for (MinimumNetwork net : list) {
  96. decorNetworks.add(new DecoratedNetwork(net, timestep, actualFairnessModel));
  97. }
  98. ArrayList<DecoratedCable> leftOverDecoratedCables = new ArrayList<DecoratedCable>();
  99. for(IntermediateCableWithState cable: leftOver) {
  100. leftOverDecoratedCables.add(new DecoratedCable(cable.getModel(), cable.getState(), 0.0f));
  101. }
  102. ArrayList<DecoratedSwitch> listOfDecoratedSwitches = decorateSwitches(minimumModel, timestep);
  103. DecoratedState stateFromThisTimestep = new DecoratedState(decorNetworks, leftOverDecoratedCables, listOfDecoratedSwitches, timestep);
  104. saves.put(timestep, stateFromThisTimestep);
  105. savesVisual.put(timestep, new VisualRepresentationalState(stateFromThisTimestep, minimumModel));
  106. }
  107. /**
  108. * Decorate a switch
  109. * @param minModel
  110. * @param iteration
  111. * @return
  112. */
  113. public static ArrayList<DecoratedSwitch> decorateSwitches(MinimumModel minModel, int iteration) {
  114. ArrayList<DecoratedSwitch> aListOfDecoratedSwitches = new ArrayList<DecoratedSwitch>();
  115. for(HolonSwitch hSwitch: minModel.getSwitchList()) {
  116. aListOfDecoratedSwitches.add(new DecoratedSwitch(hSwitch, hSwitch.getState(iteration) ? SwitchState.Closed : SwitchState.Open));
  117. }
  118. return aListOfDecoratedSwitches;
  119. }
  120. /**
  121. * SubFunction to calculate the Networks from the model.
  122. * @param minModel
  123. * @param Iteration
  124. * @param leftOver
  125. * @return
  126. */
  127. ArrayList<MinimumNetwork> calculateNetworks(MinimumModel minModel, int Iteration, ArrayList<IntermediateCableWithState> leftOver){
  128. //Copy minModel ObjectList
  129. ArrayList<HolonObject> holonObjectList = new ArrayList<HolonObject>();
  130. for(HolonObject holonObject: minModel.getHolonObjectList()) {
  131. holonObjectList.add(holonObject);
  132. }
  133. //Copy minModelEdgeList
  134. ArrayList<IntermediateCableWithState> edgeList = new ArrayList<IntermediateCableWithState>();
  135. for(IntermediateCableWithState cable: minModel.getEdgeList()) {
  136. edgeList.add(cable);
  137. }
  138. ArrayList<MinimumNetwork> listOfNetworks = new ArrayList<MinimumNetwork>();
  139. while(!holonObjectList.isEmpty()) {
  140. //lookAt the first holonObject and find his neighbors
  141. HolonObject lookAtObject = holonObjectList.get(0);
  142. //delete out of list
  143. holonObjectList.remove(0);
  144. //create a new Network
  145. MinimumNetwork actualNetwork = new MinimumNetwork(new ArrayList<HolonObject>(), new ArrayList<IntermediateCableWithState>());
  146. actualNetwork.getHolonObjectList().add(lookAtObject);
  147. //create List of neighbors
  148. LinkedList<AbstractCpsObject> neighbors = new LinkedList<AbstractCpsObject>();
  149. populateListOfNeighbors(edgeList, lookAtObject, actualNetwork, neighbors);
  150. while(!neighbors.isEmpty()) {
  151. AbstractCpsObject lookAtNeighbor = neighbors.getFirst();
  152. if(lookAtNeighbor instanceof HolonObject) {
  153. actualNetwork.getHolonObjectList().add((HolonObject) lookAtNeighbor);
  154. holonObjectList.remove(lookAtNeighbor);
  155. }
  156. //When HolonSwitch Check if closed
  157. if(!(lookAtNeighbor instanceof HolonSwitch) || ((HolonSwitch)lookAtNeighbor).getState(Iteration)) {
  158. populateListOfNeighbors(edgeList, lookAtNeighbor, actualNetwork, neighbors);
  159. }
  160. neighbors.removeFirst();
  161. }
  162. listOfNetworks.add(actualNetwork);
  163. }
  164. if(leftOver!= null) {
  165. leftOver.clear();
  166. for(IntermediateCableWithState cable: edgeList) {
  167. leftOver.add(cable);
  168. }
  169. }
  170. return listOfNetworks;
  171. }
  172. /**
  173. * Adds the neighbors.
  174. * @param edgeList
  175. * @param lookAtObject
  176. * @param actualNetwork
  177. * @param neighbors
  178. */
  179. void populateListOfNeighbors(ArrayList<IntermediateCableWithState> edgeList, AbstractCpsObject lookAtObject,
  180. MinimumNetwork actualNetwork, LinkedList<AbstractCpsObject> neighbors) {
  181. ListIterator<IntermediateCableWithState> iter = edgeList.listIterator();
  182. while(iter.hasNext())
  183. {
  184. IntermediateCableWithState lookAtEdge = iter.next();
  185. if(lookAtEdge.getState() == CableState.Working && lookAtEdge.getModel().isConnectedTo(lookAtObject)) {
  186. iter.remove();
  187. actualNetwork.getEdgeList().add(lookAtEdge);
  188. //Add neighbar
  189. AbstractCpsObject edgeNeighbor;
  190. if(lookAtEdge.getModel().getA().equals(lookAtObject)) {
  191. edgeNeighbor = lookAtEdge.getModel().getB();
  192. }else {
  193. edgeNeighbor = lookAtEdge.getModel().getA();
  194. }
  195. if(!neighbors.contains(edgeNeighbor)) {
  196. neighbors.add(edgeNeighbor);
  197. }
  198. }
  199. }
  200. }
  201. /**
  202. * Get all Subnets.Not functional.
  203. *
  204. * @return all Subnets
  205. */
  206. @Deprecated
  207. public ArrayList<SubNet> getSubNets() {
  208. return new ArrayList<SubNet>();
  209. }
  210. public FlexiblePane getFlexiblePane() {
  211. return flexPane;
  212. }
  213. void setFlexiblePane(FlexiblePane fp) {
  214. flexPane = fp;
  215. }
  216. public DecoratedState getActualDecorState() {
  217. return getDecorState(timeStep);
  218. }
  219. public VisualRepresentationalState getActualVisualRepresentationalState(){
  220. return savesVisual.getOrDefault(timeStep, null);
  221. }
  222. public DecoratedState getDecorState(int timestep) {
  223. return saves.getOrDefault(timestep, null);
  224. }
  225. public VisualRepresentationalState getVisualRepresentationalState(int timestep) {
  226. return savesVisual.getOrDefault(timestep, null);
  227. }
  228. }