SimulationManager.java 8.7 KB

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