SimulationManager.java 10 KB

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