SimulationManager.java 11 KB

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