Browse Source

Cables are working now!

Tom Troppmann 5 years ago
parent
commit
1810b9b708

+ 1 - 1
src/ui/controller/CalculataModel.java

@@ -8,7 +8,7 @@ import classes.AbstractCpsObject;
 import classes.HolonObject;
 import classes.HolonSwitch;
 import ui.model.CableWithState;
-import ui.model.CableWithState.CableState;
+import ui.model.DecoratedCable.CableState;
 import ui.model.DecoratedSwitch;
 import ui.model.DecoratedSwitch.SwitchState;
 import ui.model.MinimumModel;

+ 28 - 22
src/ui/controller/SimulationManager.java

@@ -6,7 +6,7 @@ import classes.comparator.MinEnergyComparator;
 import classes.comparator.WeakestBattery;
 import ui.model.CableWithState;
 import ui.model.DecoratedCable;
-import ui.model.CableWithState.CableState;
+import ui.model.DecoratedCable.CableState;
 import ui.model.DecoratedNetwork;
 import ui.model.DecoratedState;
 import ui.model.DecoratedSwitch;
@@ -32,13 +32,9 @@ public class SimulationManager {
 	int global = 0;
 	private Model model;
 	private ArrayList<AbstractCpsObject> objectsToHandle;
-	// private ArrayList<CpsEdge> allConnections;
 	private ArrayList<SubNet> subNets;
 	private ArrayList<CpsEdge> brokenEdges;
-	
-	private ArrayList<DecoratedNetwork> decorNetworks = new ArrayList<DecoratedNetwork>();
-	private DecoratedState decorState;
-	private MinimumModel minimumModel = null;
+	private HashMap<Integer, DecoratedState> saves = new HashMap<Integer, DecoratedState>();
 	private MyCanvas canvas;
 	private int timeStep;
 	private HashMap<Integer, Float> tagTable = new HashMap<>();
@@ -78,14 +74,25 @@ public class SimulationManager {
 	 *            current Iteration
 	 */
 	void calculateStateForTimeStep(int timestep) {
-		System.out.println("Calculate Timestep: " + timestep);
+		boolean theStateBeforeExist= (timestep > 0 && saves.containsKey(timestep-1));
+		
+		System.out.println("Calculate Timestep: " + timestep + (theStateBeforeExist ? "  StateBeforeExist": ""));
+		HashMap<CpsEdge, CableState> map = new HashMap<CpsEdge, CableState>();
+		if(theStateBeforeExist)
+		{
+			//make cable hastmap
+			DecoratedState theStateBefore = saves.get(timestep-1);
+			for(DecoratedCable edge : theStateBefore.getLeftOverEdges())
+			{
+				map.put(edge.getModel(), edge.getState());
+			}
+		}
 		timeStep = timestep;
-		decorNetworks.clear();
 		ArrayList<MinimumNetwork> list =  new ArrayList<MinimumNetwork>();
-		minimumModel = new MinimumModel(model.getObjectsOnCanvas(), model.getEdgesOnCanvas());
+		MinimumModel minimumModel = new MinimumModel(model.getObjectsOnCanvas(), model.getEdgesOnCanvas());
 		//set all working:
 		for(CableWithState cable : minimumModel.getEdgeList()) {
-			cable.setState(CableState.Working);
+			if(map.containsKey(cable.getModel())) cable.setState(map.get(cable.getModel()));
 		}
 		ArrayList<CableWithState> leftOver = new ArrayList<CableWithState>();
 		boolean doAnotherLoop = true;
@@ -102,17 +109,17 @@ public class SimulationManager {
 				}
 			}
 		}
-		
+		ArrayList<DecoratedNetwork> decorNetworks = new ArrayList<DecoratedNetwork>();
 		for (MinimumNetwork net : list) {
 			decorNetworks.add(new DecoratedNetwork(net, timestep));
 		}
 		ArrayList<DecoratedCable> leftOverDecoratedCables = new ArrayList<DecoratedCable>();
 		
 		for(CableWithState cable: leftOver) {
-			leftOverDecoratedCables.add(new DecoratedCable(cable.getModel(), CableWithState.convertCableStateToDecoratedCableState(cable.getState()), 0.0f));
+			leftOverDecoratedCables.add(new DecoratedCable(cable.getModel(), cable.getState(), 0.0f));
 		}
 		ArrayList<DecoratedSwitch> listOfDecoratedSwitches = CalculataModel.decorateSwitches(minimumModel, timestep);
-		decorState = new DecoratedState(decorNetworks, leftOverDecoratedCables, listOfDecoratedSwitches);
+		saves.put(timestep, new DecoratedState(decorNetworks, leftOverDecoratedCables, listOfDecoratedSwitches, minimumModel.getNodeList() , timestep));
 		canvas.repaint();
 //		for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
 //		    System.out.println(ste);
@@ -1028,17 +1035,16 @@ public class SimulationManager {
 		flexPane = fp;
 	}
 
-	public ArrayList<DecoratedNetwork> getDecorNetworks() {
-		return decorNetworks;
+	
+	public DecoratedState getActualDecorStateWithOffSet(int offSet) {
+		return getDecorState(timeStep + offSet);
 	}
-
-	public MinimumModel getMinimumModel() {
-		return minimumModel;
+	public DecoratedState getActualDecorState() {
+		return getDecorState(timeStep);
 	}
-
-	public DecoratedState getDecorState() {
-		return decorState;
+	
+	public DecoratedState getDecorState(int timestep) {
+		return saves.getOrDefault(timestep, null);
 	}
 
-
 }

+ 1 - 18
src/ui/model/CableWithState.java

@@ -2,7 +2,7 @@ package ui.model;
 
 import classes.CpsEdge;
 import classes.HolonObject;
-import ui.model.DecoratedCable.DecoratedCableState;
+import ui.model.DecoratedCable.CableState;
 
 /**
  * Intermediate to calculate/simulate the burning of Cables.
@@ -10,9 +10,6 @@ import ui.model.DecoratedCable.DecoratedCableState;
  * @see DecoratedCable
  */
 public class CableWithState {
-	public enum CableState{
-		Undefined, Working, Burned
-	}
 	private CableState state;
 	private CpsEdge model;
 	public CableWithState(CpsEdge model,CableState state)
@@ -37,18 +34,4 @@ public class CableWithState {
 		return energy;
 	}
 	
-	//to be swapped with on CableState system 
-	public static DecoratedCableState convertCableStateToDecoratedCableState(CableState toConvert) {
-		switch(toConvert) {
-		case Burned:
-			return DecoratedCableState.Burned;
-		case Working:
-			return DecoratedCableState.Working;
-		case Undefined:
-			return DecoratedCableState.Burned;
-		default:
-			return DecoratedCableState.Burned;
-		
-		}
-	}
 }

+ 4 - 4
src/ui/model/DecoratedCable.java

@@ -3,13 +3,13 @@ package ui.model;
 import classes.CpsEdge;
 
 public class DecoratedCable {
-	public enum DecoratedCableState{
+	public enum CableState{
 		Working, Burned
 	}
 	private CpsEdge model;
-	private DecoratedCableState state;
+	private CableState state;
 	private float flowEnergy;
-	public DecoratedCable(CpsEdge edge, DecoratedCableState state, float flowEnergy){
+	public DecoratedCable(CpsEdge edge, CableState state, float flowEnergy){
 		this.model = edge;
 		this.state = state;
 		this.flowEnergy = flowEnergy;
@@ -17,7 +17,7 @@ public class DecoratedCable {
 	public CpsEdge getModel() {
 		return model;
 	}
-	public DecoratedCableState getState() {
+	public CableState getState() {
 		return state;
 	}
 	public float getFlowEnergy() {

+ 2 - 3
src/ui/model/DecoratedNetwork.java

@@ -4,8 +4,7 @@ import java.util.ArrayList;
 
 import classes.CpsEdge;
 import classes.HolonObject;
-import ui.model.CableWithState.CableState;
-import ui.model.DecoratedCable.DecoratedCableState;
+import ui.model.DecoratedCable.CableState;
 import ui.model.DecoratedHolonObject.HolonObjectState;
 
 public class DecoratedNetwork {
@@ -58,7 +57,7 @@ public class DecoratedNetwork {
 		float energyToSupplyInTheNetwork = supplierList.stream().map(supplier -> supplier.getEnergyToSupplyNetwork() - supplier.getEnergySupplied()).reduce( 0.0f, (a, b) -> a + b);
 		//DecoratedCables
 		for(CableWithState edge: minimumNetwork.getEdgeList()) {		
-			decoratedCableList.add(new DecoratedCable(edge.getModel(), CableWithState.convertCableStateToDecoratedCableState(edge.getState()), (edge.getState() == CableState.Working) ? energyToSupplyInTheNetwork : 0.0f));
+			decoratedCableList.add(new DecoratedCable(edge.getModel(), edge.getState(), (edge.getState() == CableState.Working) ? energyToSupplyInTheNetwork : 0.0f));
 		}
 		
 		outerLoop:

+ 13 - 1
src/ui/model/DecoratedState.java

@@ -2,14 +2,20 @@ package ui.model;
 
 import java.util.ArrayList;
 
+import classes.CpsNode;
+
 public class DecoratedState {
+	int timestepOfState;
 	ArrayList<DecoratedNetwork> networkList = new ArrayList<DecoratedNetwork>();
 	ArrayList<DecoratedCable> leftOverEdges = new ArrayList<DecoratedCable>();
 	ArrayList<DecoratedSwitch> decoratedSwitches = new ArrayList<DecoratedSwitch>();
-	public DecoratedState(ArrayList<DecoratedNetwork> networkList, ArrayList<DecoratedCable> leftOverEdges, ArrayList<DecoratedSwitch> decoratedSwitches){
+	ArrayList<CpsNode> nodes = new ArrayList<CpsNode>();
+	public DecoratedState(ArrayList<DecoratedNetwork> networkList, ArrayList<DecoratedCable> leftOverEdges, ArrayList<DecoratedSwitch> decoratedSwitches,ArrayList<CpsNode> nodes, int timestepOfState){
 		this.networkList = networkList;
 		this.leftOverEdges = leftOverEdges;
 		this.decoratedSwitches = decoratedSwitches;
+		this.nodes = nodes;
+		this.timestepOfState = timestepOfState;
 	}
 	public ArrayList<DecoratedNetwork> getNetworkList() {
 		return networkList;
@@ -20,5 +26,11 @@ public class DecoratedState {
 	public ArrayList<DecoratedSwitch> getDecoratedSwitches() {
 		return decoratedSwitches;
 	}
+	public int getTimestepOfState() {
+		return timestepOfState;
+	}
+	public ArrayList<CpsNode> getNodeList() {
+		return nodes;
+	}
 
 }

+ 3 - 3
src/ui/model/MinimumModel.java

@@ -7,8 +7,8 @@ import classes.CpsEdge;
 import classes.CpsNode;
 import classes.HolonObject;
 import classes.HolonSwitch;
-import ui.model.CableWithState.CableState;
-import ui.model.DecoratedCable.DecoratedCableState;
+import ui.model.DecoratedCable.CableState;
+
 
 public class MinimumModel {
 	private ArrayList<HolonObject> holonObjectList = new ArrayList<HolonObject>();
@@ -23,7 +23,7 @@ public class MinimumModel {
 			else if (aCps instanceof HolonSwitch) switchList.add((HolonSwitch) aCps);
 		}
 		for (CpsEdge edge : edgeList) {
-			this.cableList.add(new CableWithState(edge, CableState.Undefined));
+			this.cableList.add(new CableWithState(edge, CableState.Working));
 		}
 	}
 	

+ 67 - 9
src/ui/view/MyCanvas.java

@@ -1,6 +1,7 @@
 package ui.view;
 
 import classes.*;
+import javafx.scene.input.KeyCode;
 
 import com.google.gson.JsonParseException;
 
@@ -23,6 +24,9 @@ import javax.swing.*;
 
 import java.awt.*;
 import java.awt.datatransfer.UnsupportedFlavorException;
+import java.awt.event.ActionEvent;
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 import java.awt.event.MouseMotionListener;
@@ -40,7 +44,7 @@ public class MyCanvas extends AbstractCanvas implements MouseListener,
 		MouseMotionListener {
 
 	private static final long serialVersionUID = 1L;
-
+    private int displayOther = 0;
 	/**
 	 * Constructor.
 	 *
@@ -55,13 +59,13 @@ public class MyCanvas extends AbstractCanvas implements MouseListener,
 		this.controller = control;
 		this.model = mod;
 		scalediv20 = model.getScale() / 20;
-
+		
 		showedInformation[0] = true;
 		showedInformation[1] = true;
 		showedInformation[3] = false;
 		showedInformation[4] = true;
 		control.setMaxCapacity(10000);
-
+		setKeyBindings();
 		popmenu.add(itemCut);
 		popmenu.add(itemCopy);
 		popmenu.add(itemPaste);
@@ -400,11 +404,16 @@ public class MyCanvas extends AbstractCanvas implements MouseListener,
 		g.setColor(statecolor);
 		g.fillRect(pos.x - controller.getScaleDiv2(), pos.y - controller.getScaleDiv2(), controller.getScale(), controller.getScale());
 		drawCanvasObject(g, decoratedHolonObject.getModel().getImage(), pos);
-		
+	}
+	private void drawCanvasObjectString(Graphics2D g, Position posOfCanvasObject, float energy) {
+		g.setColor(Color.BLACK);
+		g.setFont(new Font("TimesNewRoman", Font.PLAIN, (int) (controller.getScale() / 4f) )); 
+		g.drawString((energy > 0)? "+" + Float.toString(energy): Float.toString(energy), posOfCanvasObject.x - controller.getScaleDiv2(), posOfCanvasObject.y - controller.getScaleDiv2() - 1);
 	}
 	private void paintConsumer(Graphics2D g, Consumer con){
 		paintCanvasObject(g, con);
 		paintSupplyBar(g,con.getSupplyBarPercentage(), getStateColor(con.getState()), con.getModel().getPosition());
+		drawCanvasObjectString(g, con.getModel().getPosition(), con.getEnergyNeededFromNetwork());
 	}
 	private void drawCanvasObject(Graphics2D g, String Image, Position pos) {
 		g.drawImage(Util.loadImage(Image) , 
@@ -433,6 +442,7 @@ public class MyCanvas extends AbstractCanvas implements MouseListener,
 		}
 		g.drawLine(start.x, start.y, end.x, end.y);
 		Position middle = new Position((start.x + end.x) / 2, (start.y + end.y) / 2);
+		g.setFont(new Font("TimesRoman", Font.PLAIN, Math.max((int) (controller.getScale() / 3.5f), 10) )); 
 		g.drawString(currentEnergy + "/" + capacity , middle.x, middle.y);
 	}
 	private void paintSwitch(Graphics2D g, DecoratedSwitch dSwitch)
@@ -450,7 +460,7 @@ public class MyCanvas extends AbstractCanvas implements MouseListener,
 		g.setColor(Color.BLACK);
 		g.setStroke(new BasicStroke(1));
 		g.drawRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, barWidth - 1 , barHeight);
-		g.setFont(new Font("TimesRoman", Font.PLAIN, (int) (barHeight * 1.5) - 2));
+		g.setFont(new Font("TimesNewRoman", Font.PLAIN, (int) (barHeight * 1.5) - 2)); 
 		String percentageString = (Math.round((percentage * 100))) + "%";
 		int stringWidth = (int) g.getFontMetrics().getStringBounds(percentageString, g).getWidth();
 		if(percentage > 1.0f) g.setColor(Color.WHITE); //Just to see better on purple
@@ -482,11 +492,14 @@ public class MyCanvas extends AbstractCanvas implements MouseListener,
 			g2d.drawLine(tempCps.getPosition().x, tempCps.getPosition().y, x, y);
 		}
 		//<--
+		//timstep:
+		g.setFont(new Font("TimesNewRoman", Font.PLAIN, Math.max((int) (controller.getScale() / 3.5f), 10) )); 
+		g.drawString("DEBUG Timestep:" + controller.getSimManager().getActualDecorState().getTimestepOfState() + ((displayOther != 0) ? " " + ((displayOther > 0) ? "+" + displayOther: displayOther): "") , 10, 10);
 		g2d.setColor(Color.BLACK);
-		for(DecoratedCable cable : controller.getSimManager().getDecorState().getLeftOverEdges()) {
+		for(DecoratedCable cable : controller.getSimManager().getActualDecorStateWithOffSet(displayOther).getLeftOverEdges()) {
 			paintCable(g2d, cable);
 		}
-		for(DecoratedNetwork network : controller.getSimManager().getDecorState().getNetworkList()) {
+		for(DecoratedNetwork network : controller.getSimManager().getActualDecorStateWithOffSet(displayOther).getNetworkList()) {
 			for(DecoratedCable cable : network.getDecoratedCableList()) {
 				paintCable(g2d, cable);
 			}
@@ -504,10 +517,11 @@ public class MyCanvas extends AbstractCanvas implements MouseListener,
 			}
 		}
 		
-		for(DecoratedSwitch dSwitch : controller.getSimManager().getDecorState().getDecoratedSwitches()) {
+		for(DecoratedSwitch dSwitch : controller.getSimManager().getActualDecorStateWithOffSet(displayOther).getDecoratedSwitches()) {
 			paintSwitch(g2d, dSwitch);
 		}
-		for(CpsNode node : controller.getSimManager().getMinimumModel().getNodeList()) {
+		//should be in DecorState
+		for(CpsNode node : controller.getSimManager().getActualDecorStateWithOffSet(displayOther).getNodeList()) {
 			drawCanvasObject(g2d, "/Images/node_selected.png" , node.getPosition());
 		}
 		//oldCode 
@@ -1178,4 +1192,48 @@ public class MyCanvas extends AbstractCanvas implements MouseListener,
 			}
 		}
 	}
+	
+	private void setKeyBindings() {
+	      ActionMap actionMap = getActionMap();
+	      int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
+	      InputMap inputMap = getInputMap(condition );
+
+	      String vkLeft = "VK_UP";
+	      String vkRight = "VK_DOWN";
+	      inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), vkLeft);
+	      inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), vkRight);
+
+	      actionMap.put(vkLeft, new KeyAction(vkLeft));
+	      actionMap.put(vkRight, new KeyAction(vkRight));
+
+	   }
+
+
+	   @SuppressWarnings("serial")
+	private class KeyAction extends AbstractAction {
+	      public KeyAction(String actionCommand) {
+	         putValue(ACTION_COMMAND_KEY, actionCommand);
+	      }
+
+		@Override
+		public void actionPerformed(ActionEvent actionEvent) {
+			// TODO Auto-generated method stub
+			System.out.println(actionEvent.getActionCommand() + " pressed");
+			switch(actionEvent.getActionCommand()) {
+			case "VK_UP":
+				System.out.println("increase");
+				if(displayOther < 0)displayOther++;
+				repaint();
+				break;
+			case "VK_DOWN":
+				System.out.println("decrease");
+				displayOther--;
+				repaint();
+				break;
+			default:
+				break;
+			}
+		}
+	   }
+	   
 }

+ 2 - 1
src/ui/view/Outliner.java

@@ -26,6 +26,7 @@ import ui.controller.UpdateController;
 import ui.model.CableWithState;
 import ui.model.Consumer;
 import ui.model.Consumer.SupplierListEntry;
+import ui.model.DecoratedCable.CableState;
 import ui.model.DecoratedHolonObject;
 import ui.model.DecoratedHolonObject.HolonObjectState;
 import ui.model.DecoratedNetwork;
@@ -33,7 +34,7 @@ import ui.model.MinimumModel;
 import ui.model.MinimumNetwork;
 import ui.model.Model;
 import ui.model.Supplier;
-import ui.model.CableWithState.CableState;
+