SimulationManager.java 10 KB

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