SimulationManager.java 11 KB

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