|
@@ -1,24 +1,23 @@
|
|
|
-package ui.controller;
|
|
|
-
|
|
|
-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;
|
|
|
+package holeg.ui.controller;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.LinkedList;
|
|
|
import java.util.ListIterator;
|
|
|
import java.util.Optional;
|
|
|
+import java.util.logging.Logger;
|
|
|
|
|
|
-import model.*;
|
|
|
-import model.Edge.EdgeState;
|
|
|
-
|
|
|
+import holeg.model.*;
|
|
|
+import holeg.model.Edge.EdgeState;
|
|
|
+import holeg.ui.model.DecoratedNetwork;
|
|
|
+import holeg.ui.model.DecoratedState;
|
|
|
+import holeg.ui.model.DecoratedSwitch;
|
|
|
+import holeg.ui.model.MinimumModel;
|
|
|
+import holeg.ui.model.MinimumNetwork;
|
|
|
+import holeg.ui.model.Model;
|
|
|
+import holeg.ui.model.VisualRepresentationalState;
|
|
|
+import holeg.ui.model.DecoratedSwitch.SwitchState;
|
|
|
+import holeg.ui.model.Model.FairnessModel;
|
|
|
|
|
|
|
|
|
* Controller for Simulation.
|
|
@@ -26,6 +25,7 @@ import model.Edge.EdgeState;
|
|
|
* @author Gruppe14
|
|
|
*/
|
|
|
public class SimulationManager {
|
|
|
+ private static final Logger LOGGER = Logger.getLogger(SimulationManager.class.getName());
|
|
|
private Model model;
|
|
|
private HashMap<Integer, VisualRepresentationalState> savesVisual = new HashMap<Integer, VisualRepresentationalState>();
|
|
|
private int timeStep;
|
|
@@ -34,134 +34,145 @@ public class SimulationManager {
|
|
|
|
|
|
* Constructor.
|
|
|
*
|
|
|
- * @param m
|
|
|
- * Model
|
|
|
+ * @param m Model
|
|
|
*/
|
|
|
public SimulationManager(Model m) {
|
|
|
+ LOGGER.fine("Construct SimulationManager");
|
|
|
model = m;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
|
|
|
- * calculates the flow of the edges and the supply for objects and consider old timesteps for burned cables.
|
|
|
+ * calculates the flow of the edges and the supply for objects and consider old
|
|
|
+ * timesteps for burned cables.
|
|
|
*
|
|
|
- * @param timestep
|
|
|
- * current Iteration
|
|
|
- * @param updateVisual
|
|
|
- * Determine if the Visuals should also be calculated
|
|
|
+ * @param timestep current Iteration
|
|
|
+ * @param updateVisual Determine if the Visuals should also be calculated
|
|
|
*/
|
|
|
public void calculateStateForTimeStep(int timestep, boolean updateVisual) {
|
|
|
+ LOGGER.fine("Calculate");
|
|
|
timeStep = timestep;
|
|
|
- ArrayList<MinimumNetwork> list = new ArrayList<MinimumNetwork>();
|
|
|
- MinimumModel minimumModel = new MinimumModel(model.getObjectsOnCanvas(), model.getEdgesOnCanvas(), model.getActualTimeStep());
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ ArrayList<MinimumNetwork> list = new ArrayList<MinimumNetwork>();
|
|
|
+ MinimumModel minimumModel = new MinimumModel(model.getObjectsOnCanvas(), model.getEdgesOnCanvas(),
|
|
|
+ model.getActualTimeStep());
|
|
|
ArrayList<Edge> leftOver = new ArrayList<Edge>();
|
|
|
|
|
|
boolean doAnotherLoop = true;
|
|
|
- while(doAnotherLoop) {
|
|
|
+ do {
|
|
|
doAnotherLoop = false;
|
|
|
list = calculateNetworks(minimumModel, timestep, leftOver);
|
|
|
- for(MinimumNetwork net : list) {
|
|
|
- float energyOnCables = net.getHolonObjectList().stream().map(object -> object.getActualEnergy()).filter(energy -> energy > 0.0f).reduce(0.0f, (Float::sum));
|
|
|
-
|
|
|
- Edge cable = net.getEdgeList().stream().filter(aCable -> energyOnCables > aCable.maxCapacity && !aCable.isUnlimitedCapacity()).max((lhs,rhs) -> Float.compare(lhs.getEnergyFromConneted(), rhs.getEnergyFromConneted())).orElse(null);
|
|
|
- if(cable != null) {
|
|
|
+ for (MinimumNetwork net : list) {
|
|
|
+ float energyOnCables = net.getHolonObjectList().stream().map(object -> object.getActualEnergy())
|
|
|
+ .filter(energy -> energy > 0.0f).reduce(0.0f, (Float::sum));
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Edge cable = net.getEdgeList().stream()
|
|
|
+ .filter(aCable -> energyOnCables > aCable.maxCapacity && !aCable.isUnlimitedCapacity())
|
|
|
+ .max((lhs, rhs) -> Float.compare(lhs.getEnergyFromConneted(), rhs.getEnergyFromConneted()))
|
|
|
+ .orElse(null);
|
|
|
+ if (cable != null) {
|
|
|
cable.setState(EdgeState.Burned);
|
|
|
doAnotherLoop = true;
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
+ } while (doAnotherLoop);
|
|
|
ArrayList<DecoratedNetwork> decorNetworks = new ArrayList<DecoratedNetwork>();
|
|
|
FairnessModel actualFairnessModel = model.getFairnessModel();
|
|
|
for (MinimumNetwork net : list) {
|
|
|
decorNetworks.add(new DecoratedNetwork(net, timestep, actualFairnessModel));
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- for(Edge cable: leftOver) {
|
|
|
+ for (Edge cable : leftOver) {
|
|
|
cable.setActualFlow(0.0f);
|
|
|
}
|
|
|
ArrayList<DecoratedSwitch> listOfDecoratedSwitches = decorateSwitches(minimumModel, timestep);
|
|
|
- DecoratedState stateFromThisTimestep = new DecoratedState(decorNetworks, leftOver, listOfDecoratedSwitches, timestep);
|
|
|
- if(updateVisual)savesVisual.put(timestep, new VisualRepresentationalState(stateFromThisTimestep, minimumModel));
|
|
|
- actualDecorState = Optional.of(stateFromThisTimestep);
|
|
|
+ DecoratedState stateFromThisTimestep = new DecoratedState(decorNetworks, leftOver, listOfDecoratedSwitches,
|
|
|
+ timestep);
|
|
|
+ if (updateVisual)
|
|
|
+ savesVisual.put(timestep, new VisualRepresentationalState(stateFromThisTimestep, minimumModel));
|
|
|
+ actualDecorState = Optional.of(stateFromThisTimestep);
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ LOGGER.finer("Simulation: " + (end - start) + "ms");
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
* Decorate a switch
|
|
|
+ *
|
|
|
* @param minModel
|
|
|
* @param iteration
|
|
|
* @return
|
|
|
*/
|
|
|
public static ArrayList<DecoratedSwitch> decorateSwitches(MinimumModel minModel, int iteration) {
|
|
|
- ArrayList<DecoratedSwitch> aListOfDecoratedSwitches = new ArrayList<DecoratedSwitch>();
|
|
|
- for(HolonSwitch hSwitch: minModel.getSwitchList()) {
|
|
|
- aListOfDecoratedSwitches.add(new DecoratedSwitch(hSwitch, hSwitch.getState(iteration) ? SwitchState.Closed : SwitchState.Open));
|
|
|
+ ArrayList<DecoratedSwitch> aListOfDecoratedSwitches = new ArrayList<DecoratedSwitch>();
|
|
|
+ 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<MinimumNetwork> calculateNetworks(MinimumModel minModel, int Iteration, ArrayList<Edge> leftOver){
|
|
|
-
|
|
|
+ ArrayList<MinimumNetwork> calculateNetworks(MinimumModel minModel, int Iteration, ArrayList<Edge> leftOver) {
|
|
|
+
|
|
|
ArrayList<HolonObject> holonObjectList = new ArrayList<HolonObject>();
|
|
|
- for(HolonObject holonObject: minModel.getHolonObjectList()) {
|
|
|
+ for (HolonObject holonObject : minModel.getHolonObjectList()) {
|
|
|
holonObjectList.add(holonObject);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
ArrayList<Edge> edgeList = new ArrayList<Edge>();
|
|
|
- for(Edge cable: minModel.getEdgeList()) {
|
|
|
+ for (Edge cable : minModel.getEdgeList()) {
|
|
|
edgeList.add(cable);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
ArrayList<MinimumNetwork> listOfNetworks = new ArrayList<MinimumNetwork>();
|
|
|
- while(!holonObjectList.isEmpty()) {
|
|
|
-
|
|
|
+ while (!holonObjectList.isEmpty()) {
|
|
|
+
|
|
|
HolonObject lookAtObject = holonObjectList.get(0);
|
|
|
-
|
|
|
+
|
|
|
holonObjectList.remove(0);
|
|
|
-
|
|
|
+
|
|
|
MinimumNetwork actualNetwork = new MinimumNetwork(new ArrayList<HolonObject>(), new ArrayList<Edge>());
|
|
|
actualNetwork.getHolonObjectList().add(lookAtObject);
|
|
|
-
|
|
|
+
|
|
|
LinkedList<AbstractCanvasObject> neighbors = new LinkedList<AbstractCanvasObject>();
|
|
|
populateListOfNeighbors(edgeList, lookAtObject, actualNetwork, neighbors);
|
|
|
- while(!neighbors.isEmpty()) {
|
|
|
+ while (!neighbors.isEmpty()) {
|
|
|
AbstractCanvasObject lookAtNeighbor = neighbors.getFirst();
|
|
|
- if(lookAtNeighbor instanceof HolonObject hO) {
|
|
|
+ if (lookAtNeighbor instanceof HolonObject hO) {
|
|
|
actualNetwork.getHolonObjectList().add(hO);
|
|
|
holonObjectList.remove(lookAtNeighbor);
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
actualNetwork.getNodeAndSwitches().add(lookAtNeighbor);
|
|
|
}
|
|
|
-
|
|
|
- if(!(lookAtNeighbor instanceof HolonSwitch sw) || sw.getState(Iteration)) {
|
|
|
+
|
|
|
+ if (!(lookAtNeighbor instanceof HolonSwitch sw) || sw.getState(Iteration)) {
|
|
|
populateListOfNeighbors(edgeList, lookAtNeighbor, actualNetwork, neighbors);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
neighbors.removeFirst();
|
|
|
}
|
|
|
listOfNetworks.add(actualNetwork);
|
|
|
- }
|
|
|
- if(leftOver!= null) {
|
|
|
+ }
|
|
|
+ if (leftOver != null) {
|
|
|
leftOver.clear();
|
|
|
- for(Edge cable: edgeList) {
|
|
|
+ for (Edge cable : edgeList) {
|
|
|
leftOver.add(cable);
|
|
|
}
|
|
|
}
|
|
|
return listOfNetworks;
|
|
|
}
|
|
|
+
|
|
|
|
|
|
* Adds the neighbors.
|
|
|
+ *
|
|
|
* @param edgeList
|
|
|
* @param lookAtObject
|
|
|
* @param actualNetwork
|
|
@@ -170,37 +181,37 @@ public class SimulationManager {
|
|
|
void populateListOfNeighbors(ArrayList<Edge> edgeList, AbstractCanvasObject lookAtObject,
|
|
|
MinimumNetwork actualNetwork, LinkedList<AbstractCanvasObject> neighbors) {
|
|
|
ListIterator<Edge> iter = edgeList.listIterator();
|
|
|
- while(iter.hasNext())
|
|
|
- {
|
|
|
+ while (iter.hasNext()) {
|
|
|
Edge lookAtEdge = iter.next();
|
|
|
- if(lookAtEdge.getState() == EdgeState.Working && lookAtEdge.isConnectedTo(lookAtObject)) {
|
|
|
+ if (lookAtEdge.getState() == EdgeState.Working && lookAtEdge.isConnectedTo(lookAtObject)) {
|
|
|
iter.remove();
|
|
|
actualNetwork.getEdgeList().add(lookAtEdge);
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
AbstractCanvasObject edgeNeighbor;
|
|
|
- if(lookAtEdge.getA().equals(lookAtObject)) {
|
|
|
+ if (lookAtEdge.getA().equals(lookAtObject)) {
|
|
|
edgeNeighbor = lookAtEdge.getB();
|
|
|
-
|
|
|
- }else {
|
|
|
+
|
|
|
+ } else {
|
|
|
edgeNeighbor = lookAtEdge.getA();
|
|
|
}
|
|
|
- if(!neighbors.contains(edgeNeighbor)) {
|
|
|
+ if (!neighbors.contains(edgeNeighbor)) {
|
|
|
neighbors.add(edgeNeighbor);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public Optional<DecoratedState> getActualDecorState() {
|
|
|
return actualDecorState;
|
|
|
}
|
|
|
- public Optional<VisualRepresentationalState> getActualVisualRepresentationalState(){
|
|
|
+
|
|
|
+ public Optional<VisualRepresentationalState> getActualVisualRepresentationalState() {
|
|
|
return Optional.ofNullable(savesVisual.get(timeStep));
|
|
|
}
|
|
|
+
|
|
|
public Optional<VisualRepresentationalState> getVisualRepresentationalState(int timestep) {
|
|
|
return Optional.ofNullable(savesVisual.get(timestep));
|
|
|
}
|
|
|
|
|
|
-
|
|
|
}
|