package ui.controller; import classes.*; import ui.model.IntermediateCableWithState; import ui.model.DecoratedCable; import ui.model.DecoratedCable.CableState; import ui.model.DecoratedSwitch.SwitchState; import ui.model.DecoratedNetwork; import ui.model.DecoratedState; import ui.model.DecoratedSwitch; import ui.model.MinimumModel; import ui.model.MinimumNetwork; import ui.model.Model; import ui.model.Model.FairnessModel; import ui.model.VisualRepresentationalState; import java.lang.ModuleLayer.Controller; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; /** * Controller for Simulation. * * @author Gruppe14 */ public class SimulationManager { int global = 0; private Model model; private HashMap saves = new HashMap(); private HashMap savesVisual = new HashMap(); private HashMap savesFlexManger = new HashMap(); private int timeStep; /** * Constructor. * * @param m * Model */ public SimulationManager(Model m) { model = m; } /** * calculates the flow of the edges and the supply for objects and consider old timesteps for burned cables. * * @param timeStep * current Iteration * @param updateVisual TODO */ public void calculateStateForTimeStep(int timeStep, boolean updateVisual) { this.timeStep = timeStep; //compute state of each holon this.model.getStateHolon().holonControlUnit.getStateEstimator().computeState(timeStep); boolean doesFlexManagerExist = savesFlexManger.containsKey(timeStep); boolean createNew = updateVisual || !doesFlexManagerExist; FlexManager newFlexManager; if(createNew) { newFlexManager = new FlexManager(model, timeStep, savesFlexManger.get(timeStep-1)); if(doesFlexManagerExist) newFlexManager.orderFlexFromList(savesFlexManger.get(timeStep).getAllFlexesOrderedThisTimeStep()); savesFlexManger.put(timeStep, newFlexManager); }else { newFlexManager = savesFlexManger.get(timeStep); } HashMap map = new HashMap(); if(timeStep > 0 && saves.containsKey(timeStep-1)) //if the state before exist { //make cable hastmap DecoratedState theStateBefore = saves.get(timeStep-1); //edges without HolonObjects or burned for(DecoratedCable edge : theStateBefore.getLeftOverEdges()) { map.put(edge.getModel(), edge.getState()); } } ArrayList list = new ArrayList(); MinimumModel minimumModel = new MinimumModel(model.getObjectsOnCanvas(), model.getEdgesOnCanvas()); //for each holarchy get minimum model ArrayList independentHolarchies = new ArrayList(); for(Holon h : model.getStateHolon().childHolons) { independentHolarchies.add(h.getMinimumModel()); } //set all edges that are not connecting holarchies unnused HashSet e = new HashSet(); for(MinimumModel minimumModel2 : independentHolarchies) { for(IntermediateCableWithState i : minimumModel2.getEdgeList()) { e.add(i.getModel()); } } for(IntermediateCableWithState cable : minimumModel.getEdgeList()) { if(!e.contains(cable.getModel())) { cable.setState(CableState.Unused); } } //set all BreakedManuel Cable Burned: for(IntermediateCableWithState cable : minimumModel.getEdgeList()) { if(cable.getModel().isBreakedManuel()) cable.setState(CableState.Burned); } //set all the state before: for(IntermediateCableWithState cable : minimumModel.getEdgeList()) { if(map.containsKey(cable.getModel())) cable.setState(map.get(cable.getModel())); } ArrayList leftOver = new ArrayList(); boolean doAnotherLoop = true; while(doAnotherLoop) { doAnotherLoop = false; list = calculateNetworks(minimumModel, timeStep, leftOver); for(MinimumNetwork net : list) { // 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)); 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)); //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. 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); if(cable != null) { cable.setState(CableState.Burned); doAnotherLoop = true; } } } //Create lookUpTableForHolonObjetcs HashMap holonObjectNetworkTable = new HashMap(); ArrayList decorNetworks = new ArrayList(); FairnessModel actualFairnessModel = model.getFairnessModel(); for (MinimumNetwork net : list) { DecoratedNetwork decNetwork = new DecoratedNetwork(net, timeStep, actualFairnessModel, newFlexManager); decorNetworks.add(decNetwork); for(HolonObject obj : net.getHolonObjectList()) { holonObjectNetworkTable.put(obj, decNetwork); } } ArrayList leftOverDecoratedCables = new ArrayList(); for(IntermediateCableWithState cable: leftOver) { leftOverDecoratedCables.add(new DecoratedCable(cable.getModel(), cable.getState(), 0.0f)); } ArrayList listOfDecoratedSwitches = decorateSwitches(minimumModel, timeStep); DecoratedState stateFromThisTimestep = new DecoratedState(decorNetworks, leftOverDecoratedCables, listOfDecoratedSwitches, newFlexManager, timeStep, holonObjectNetworkTable); saves.put(timeStep, stateFromThisTimestep); if(updateVisual)savesVisual.put(timeStep, new VisualRepresentationalState(stateFromThisTimestep, minimumModel)); //Check Holarchy and split Holons if no physical connection is present. List holonList = model.getStateHolon().childHolons; for(int i = 0; i < holonList.size(); i++) { holonList.get(i).checkRepairHolarchy(holonObjectNetworkTable, model.getStateHolon()); } } /** * Decorate a switch * @param minModel * @param iteration * @return */ public static ArrayList decorateSwitches(MinimumModel minModel, int iteration) { ArrayList aListOfDecoratedSwitches = new ArrayList(); for(HolonSwitch hSwitch: minModel.getSwitchList()) { aListOfDecoratedSwitches.add(new DecoratedSwitch(hSwitch, hSwitch.getState(iteration) ? SwitchState.Closed : SwitchState.Open)); } return aListOfDecoratedSwitches; } /** * SubFunction to calculate the Networks from the model. * @param minModel * @param Iteration * @param leftOver * @return */ ArrayList calculateNetworks(MinimumModel minModel, int Iteration, ArrayList leftOver){ //Copy minModel ObjectList ArrayList holonObjectList = new ArrayList(); for(HolonObject holonObject: minModel.getHolonObjectList()) { holonObjectList.add(holonObject); } //Copy minModelEdgeList ArrayList edgeList = new ArrayList(); for(IntermediateCableWithState cable: minModel.getEdgeList()) { edgeList.add(cable); } ArrayList listOfNetworks = new ArrayList(); while(!holonObjectList.isEmpty()) { //lookAt the first holonObject and find his neighbors HolonObject lookAtObject = holonObjectList.get(0); //delete out of list holonObjectList.remove(0); //create a new Network MinimumNetwork actualNetwork = new MinimumNetwork(new ArrayList(), new ArrayList()); actualNetwork.getHolonObjectList().add(lookAtObject); //create List of neighbors LinkedList neighbors = new LinkedList(); populateListOfNeighbors(edgeList, lookAtObject, actualNetwork, neighbors); while(!neighbors.isEmpty()) { AbstractCanvasObject lookAtNeighbor = neighbors.getFirst(); if(lookAtNeighbor instanceof HolonObject) { actualNetwork.getHolonObjectList().add((HolonObject) lookAtNeighbor); holonObjectList.remove(lookAtNeighbor); }else { actualNetwork.getNodeAndSwitches().add(lookAtNeighbor); } //When HolonSwitch Check if closed if(!(lookAtNeighbor instanceof HolonSwitch) || ((HolonSwitch)lookAtNeighbor).getState(Iteration)) { populateListOfNeighbors(edgeList, lookAtNeighbor, actualNetwork, neighbors); } neighbors.removeFirst(); } listOfNetworks.add(actualNetwork); } if(leftOver!= null) { leftOver.clear(); for(IntermediateCableWithState cable: edgeList) { leftOver.add(cable); } } return listOfNetworks; } /** * Adds the neighbors. * @param edgeList * @param lookAtObject * @param actualNetwork * @param neighbors */ void populateListOfNeighbors(ArrayList edgeList, AbstractCanvasObject lookAtObject, MinimumNetwork actualNetwork, LinkedList neighbors) { ListIterator iter = edgeList.listIterator(); while(iter.hasNext()) { IntermediateCableWithState lookAtEdge = iter.next(); if(lookAtEdge.getState() == CableState.Working && lookAtEdge.getModel().isConnectedTo(lookAtObject)) { iter.remove(); actualNetwork.getEdgeList().add(lookAtEdge); //Add neighbar AbstractCanvasObject edgeNeighbor; if(lookAtEdge.getModel().getA().equals(lookAtObject)) { edgeNeighbor = lookAtEdge.getModel().getB(); }else { edgeNeighbor = lookAtEdge.getModel().getA(); } if(!neighbors.contains(edgeNeighbor)) { neighbors.add(edgeNeighbor); } } } } public DecoratedState getActualDecorState() { return getDecorState(timeStep); } public VisualRepresentationalState getActualVisualRepresentationalState(){ return savesVisual.getOrDefault(timeStep, null); } public FlexManager getActualFlexManager() { return savesFlexManger.getOrDefault(timeStep, null); } public void resetFlexManager(){ savesFlexManger.clear(); } public void resetFlexManagerForTimeStep(int timestep) { savesFlexManger.get(timestep).reset(); } public DecoratedState getDecorState(int timestep) { return saves.get(timestep); } public VisualRepresentationalState getVisualRepresentationalState(int timestep) { return savesVisual.getOrDefault(timestep, null); } }