SimulationManager.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package ui.controller;
  2. import algo.StorageElement;
  3. import classes.*;
  4. import classes.comparator.EnergyMinToMaxComparator;
  5. import classes.comparator.MinEnergyComparator;
  6. import classes.comparator.WeakestBattery;
  7. import ui.model.IntermediateCableWithState;
  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. FlexManager newFlexManager = new FlexManager(model, timestep, savesFlexManger.getOrDefault(timestep-1, null));
  62. //Actual ordered not delete:
  63. FlexManager flexManagerFromActual = savesFlexManger.getOrDefault(timestep, null);
  64. if(flexManagerFromActual != null) {
  65. newFlexManager.orderFlexFromList(flexManagerFromActual.getAllFlexesOrderedThisTimeStep());
  66. }
  67. savesFlexManger.put(timestep, newFlexManager);
  68. HashMap<CpsEdge, CableState> map = new HashMap<CpsEdge, CableState>();
  69. if(timestep > 0 && saves.containsKey(timestep-1)) //if the state before exist
  70. {
  71. //make cable hastmap
  72. DecoratedState theStateBefore = saves.get(timestep-1);
  73. //edges without HolonObjects or burned
  74. for(DecoratedCable edge : theStateBefore.getLeftOverEdges())
  75. {
  76. map.put(edge.getModel(), edge.getState());
  77. }
  78. }
  79. timeStep = timestep;
  80. ArrayList<MinimumNetwork> list = new ArrayList<MinimumNetwork>();
  81. MinimumModel minimumModel = new MinimumModel(model.getObjectsOnCanvas(), model.getEdgesOnCanvas());
  82. //set all BreakedManuel Cable Burned:
  83. for(IntermediateCableWithState cable : minimumModel.getEdgeList()) {
  84. if(cable.getModel().isBreakedManuel()) cable.setState(CableState.Burned);
  85. }
  86. //set all the state before:
  87. for(IntermediateCableWithState cable : minimumModel.getEdgeList()) {
  88. if(map.containsKey(cable.getModel())) cable.setState(map.get(cable.getModel()));
  89. }
  90. ArrayList<IntermediateCableWithState> leftOver = new ArrayList<IntermediateCableWithState>();
  91. boolean doAnotherLoop = true;
  92. while(doAnotherLoop) {
  93. doAnotherLoop = false;
  94. list = calculateNetworks(minimumModel, timestep, leftOver);
  95. for(MinimumNetwork net : list) {
  96. float energyOnCables = net.getHolonObjectList()
  97. .stream()
  98. .filter(object -> object.getEnergyAtTimeStepWithFlex(timestep, newFlexManager) > 0.0f)
  99. .map(object -> object.getEnergyAtTimeStepWithFlex(timestep, newFlexManager))
  100. .reduce(0.0f, ((a,b) -> a + b));
  101. //calculation for storage
  102. net.getHolonObjectList()
  103. .stream()
  104. .flatMap(holonObject -> holonObject.getElements().stream())
  105. .map(ele -> {
  106. if (ele instanceof StorageElement) {
  107. return (StorageElement) ele;
  108. }else {
  109. return null;
  110. }
  111. })
  112. .filter(ele -> ele != null)
  113. .filter(ele -> (ele.getStatus().equals(StorageElement.Mode.EMIT)
  114. || (ele.getStatus().equals(StorageElement.Mode.COLLECT))))
  115. .forEach(obj -> obj.chargeCalc());
  116. //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.
  117. 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);
  118. if(cable != null) {
  119. cable.setState(CableState.Burned);
  120. doAnotherLoop = true;
  121. }
  122. }
  123. }
  124. ArrayList<DecoratedNetwork> decorNetworks = new ArrayList<DecoratedNetwork>();
  125. FairnessModel actualFairnessModel = model.getFairnessModel();
  126. for (MinimumNetwork net : list) {
  127. decorNetworks.add(new DecoratedNetwork(net, timestep, actualFairnessModel, newFlexManager));
  128. }
  129. ArrayList<DecoratedCable> leftOverDecoratedCables = new ArrayList<DecoratedCable>();
  130. for(IntermediateCableWithState cable: leftOver) {
  131. leftOverDecoratedCables.add(new DecoratedCable(cable.getModel(), cable.getState(), 0.0f));
  132. }
  133. ArrayList<DecoratedSwitch> listOfDecoratedSwitches = decorateSwitches(minimumModel, timestep);
  134. DecoratedState stateFromThisTimestep = new DecoratedState(decorNetworks, leftOverDecoratedCables, listOfDecoratedSwitches, flexManagerFromActual, timestep);
  135. saves.put(timestep, stateFromThisTimestep);
  136. if(updateVisual)savesVisual.put(timestep, new VisualRepresentationalState(stateFromThisTimestep, minimumModel));
  137. }
  138. /**
  139. * Decorate a switch
  140. * @param minModel
  141. * @param iteration
  142. * @return
  143. */
  144. public static ArrayList<DecoratedSwitch> decorateSwitches(MinimumModel minModel, int iteration) {
  145. ArrayList<DecoratedSwitch> aListOfDecoratedSwitches = new ArrayList<DecoratedSwitch>();
  146. for(HolonSwitch hSwitch: minModel.getSwitchList()) {
  147. aListOfDecoratedSwitches.add(new DecoratedSwitch(hSwitch, hSwitch.getState(iteration) ? SwitchState.Closed : SwitchState.Open));
  148. }
  149. return aListOfDecoratedSwitches;
  150. }
  151. /**
  152. * SubFunction to calculate the Networks from the model.
  153. * @param minModel
  154. * @param Iteration
  155. * @param leftOver
  156. * @return
  157. */
  158. ArrayList<MinimumNetwork> calculateNetworks(MinimumModel minModel, int Iteration, ArrayList<IntermediateCableWithState> leftOver){
  159. //Copy minModel ObjectList
  160. ArrayList<HolonObject> holonObjectList = new ArrayList<HolonObject>();
  161. for(HolonObject holonObject: minModel.getHolonObjectList()) {
  162. holonObjectList.add(holonObject);
  163. }
  164. //Copy minModelEdgeList
  165. ArrayList<IntermediateCableWithState> edgeList = new ArrayList<IntermediateCableWithState>();
  166. for(IntermediateCableWithState cable: minModel.getEdgeList()) {
  167. edgeList.add(cable);
  168. }
  169. ArrayList<MinimumNetwork> listOfNetworks = new ArrayList<MinimumNetwork>();
  170. while(!holonObjectList.isEmpty()) {
  171. //lookAt the first holonObject and find his neighbors
  172. HolonObject lookAtObject = holonObjectList.get(0);
  173. //delete out of list
  174. holonObjectList.remove(0);
  175. //create a new Network
  176. MinimumNetwork actualNetwork = new MinimumNetwork(new ArrayList<HolonObject>(), new ArrayList<IntermediateCableWithState>());
  177. actualNetwork.getHolonObjectList().add(lookAtObject);
  178. //create List of neighbors
  179. LinkedList<AbstractCpsObject> neighbors = new LinkedList<AbstractCpsObject>();
  180. populateListOfNeighbors(edgeList, lookAtObject, actualNetwork, neighbors);
  181. while(!neighbors.isEmpty()) {
  182. AbstractCpsObject lookAtNeighbor = neighbors.getFirst();
  183. if(lookAtNeighbor instanceof HolonObject) {
  184. actualNetwork.getHolonObjectList().add((HolonObject) lookAtNeighbor);
  185. holonObjectList.remove(lookAtNeighbor);
  186. }
  187. //When HolonSwitch Check if closed
  188. if(!(lookAtNeighbor instanceof HolonSwitch) || ((HolonSwitch)lookAtNeighbor).getState(Iteration)) {
  189. populateListOfNeighbors(edgeList, lookAtNeighbor, actualNetwork, neighbors);
  190. }
  191. neighbors.removeFirst();
  192. }
  193. listOfNetworks.add(actualNetwork);
  194. }
  195. if(leftOver!= null) {
  196. leftOver.clear();
  197. for(IntermediateCableWithState cable: edgeList) {
  198. leftOver.add(cable);
  199. }
  200. }
  201. return listOfNetworks;
  202. }
  203. /**
  204. * Adds the neighbors.
  205. * @param edgeList
  206. * @param lookAtObject
  207. * @param actualNetwork
  208. * @param neighbors
  209. */
  210. void populateListOfNeighbors(ArrayList<IntermediateCableWithState> edgeList, AbstractCpsObject lookAtObject,
  211. MinimumNetwork actualNetwork, LinkedList<AbstractCpsObject> neighbors) {
  212. ListIterator<IntermediateCableWithState> iter = edgeList.listIterator();
  213. while(iter.hasNext())
  214. {
  215. IntermediateCableWithState lookAtEdge = iter.next();
  216. if(lookAtEdge.getState() == CableState.Working && lookAtEdge.getModel().isConnectedTo(lookAtObject)) {
  217. iter.remove();
  218. actualNetwork.getEdgeList().add(lookAtEdge);
  219. //Add neighbar
  220. AbstractCpsObject edgeNeighbor;
  221. if(lookAtEdge.getModel().getA().equals(lookAtObject)) {
  222. edgeNeighbor = lookAtEdge.getModel().getB();
  223. }else {
  224. edgeNeighbor = lookAtEdge.getModel().getA();
  225. }
  226. if(!neighbors.contains(edgeNeighbor)) {
  227. neighbors.add(edgeNeighbor);
  228. }
  229. }
  230. }
  231. }
  232. /**
  233. * Get all Subnets.Not functional.
  234. *
  235. * @return all Subnets
  236. */
  237. @Deprecated
  238. public ArrayList<SubNet> getSubNets() {
  239. return new ArrayList<SubNet>();
  240. }
  241. public FlexiblePane getFlexiblePane() {
  242. return flexPane;
  243. }
  244. void setFlexiblePane(FlexiblePane fp) {
  245. flexPane = fp;
  246. }
  247. public DecoratedState getActualDecorState() {
  248. return getDecorState(timeStep);
  249. }
  250. public VisualRepresentationalState getActualVisualRepresentationalState(){
  251. return savesVisual.getOrDefault(timeStep, null);
  252. }
  253. public FlexManager getActualFlexManager() {
  254. return savesFlexManger.getOrDefault(timeStep, null);
  255. }
  256. public void resetFlexManager(){
  257. savesFlexManger.clear();
  258. }
  259. public void resetFlexManagerForTimeStep(int timestep) {
  260. FlexManager newFlexManager = new FlexManager(model, timestep, savesFlexManger.getOrDefault(timestep-1, null));
  261. savesFlexManger.put(timestep, newFlexManager);
  262. }
  263. public DecoratedState getDecorState(int timestep) {
  264. return saves.getOrDefault(timestep, null);
  265. }
  266. public VisualRepresentationalState getVisualRepresentationalState(int timestep) {
  267. return savesVisual.getOrDefault(timestep, null);
  268. }
  269. }