SimulationManager.java 11 KB

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