SimulationManager.java 13 KB

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