SimulationManager.java 12 KB

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