Przeglądaj źródła

SaveTheState v0.1

tolatesry 6 lat temu
rodzic
commit
ff857e306a

+ 65 - 15
src/classes/HolonBattery.java

@@ -1,12 +1,17 @@
 package classes;
+import ui.model.Model;
+import ui.controller.*;
 
 public class HolonBattery extends AbstractCpsObject{
 	
 	private float inRatio;
 	private float outRatio;
 	private float capacity;
+	private float initialStateOfCharge;
 	private float stateOfCharge;
 	private float newStateOfCharge;
+	private float[] stateOfChargeLevels;
+	private float iterations = 0;
 	public enum State{STANDBY, COLLECT, EMIT};
 	private State state;
 	/** Constructor for a unique ID.
@@ -20,6 +25,7 @@ public class HolonBattery extends AbstractCpsObject{
 		capacity = 0;
 		stateOfCharge = 0;
 		newStateOfCharge = 0;
+		initialStateOfCharge = 0;
 		setState(State.STANDBY);
 	}
 	/** Constructor to Copy a Battery
@@ -34,21 +40,14 @@ public class HolonBattery extends AbstractCpsObject{
 		setCapacity(((HolonBattery) obj).getCapacity());
 		setStateOfCharge(((HolonBattery) obj).getStateOfCharge());
 		setNewStateOfCharge(((HolonBattery) obj).getStateOfCharge());
+		setInitialStateOfCharge(((HolonBattery) obj).getStateOfCharge());
 		setState(State.STANDBY);
 	}
 	public float getStateOfCharge() {
 		return stateOfCharge;
 	}
 	public void setStateOfCharge(float stateOfCharge) {
-		if(stateOfCharge > capacity) //state of Charege can not more than the capacity
-		{
-			stateOfCharge = capacity;
-		}
-		else if(stateOfCharge < 0) // state of charge can not be a negativ value
-		{
-			stateOfCharge = 0;
-		}
-		this.stateOfCharge = stateOfCharge;
+		this.stateOfCharge = validStateOfCharge(stateOfCharge);
 	}
 	public float getCapacity() {
 	
@@ -75,17 +74,17 @@ public class HolonBattery extends AbstractCpsObject{
 			return inRatio;
 	}
 	//For Calculations
-	public float getIN()
+	public float getInAtTimeStep(int x)
 	{
-		if(getCapacity() - getStateOfCharge() < inRatio)
-			return getCapacity() - getStateOfCharge();
+		if(getCapacity() - getStateOfChargeAtTimeStep(x) < inRatio)
+			return getCapacity() - getStateOfChargeAtTimeStep(x);
 		else
 			return inRatio;
 	}
-	public float getOUT()
+	public float getOutAtTimeStep(int x)
 	{
-		if(getStateOfCharge() < outRatio)
-			return getStateOfCharge();
+		if(getStateOfChargeAtTimeStep(x) < outRatio)
+			return getStateOfChargeAtTimeStep(x);
 		else
 			return outRatio;
 	}
@@ -148,4 +147,55 @@ public class HolonBattery extends AbstractCpsObject{
 	public void setNewStateOfCharge(float newStateOfCharge) {
 		this.newStateOfCharge = newStateOfCharge;
 	}
+	public void setStateOfChargeAtTimeStep(float newStateOfCharge, int x) {
+		if(iterations != SingletonControl.getInstance().getControl().getModel().getIterations())
+		{
+			stateOfChargeLevels = new float[SingletonControl.getInstance().getControl().getModel().getIterations()];
+			iterations = SingletonControl.getInstance().getControl().getModel().getIterations();
+		}
+		System.out.println( "Iterations:" + x);
+	
+		stateOfChargeLevels[x] = validStateOfCharge(newStateOfCharge);
+		
+		for(int i = 0; i < stateOfChargeLevels.length; i++)
+		{
+			System.out.println( i+":"+stateOfChargeLevels[i]+ ",");
+		}
+	}
+	public float getStateOfChargeAtTimeStep(int x) {
+		if(iterations != SingletonControl.getInstance().getControl().getModel().getIterations())
+		{
+			stateOfChargeLevels = new float[SingletonControl.getInstance().getControl().getModel().getIterations()];
+			iterations = SingletonControl.getInstance().getControl().getModel().getIterations();
+		}
+		if(x < 0)
+		{
+			return initialStateOfCharge;
+		}
+		return stateOfChargeLevels[x];
+	}
+	public float getInitialStateOfCharge() {
+		return initialStateOfCharge;
+	}
+	public void setInitialStateOfCharge(float initialStateOfCharge) {
+		this.initialStateOfCharge = validStateOfCharge(initialStateOfCharge);
+	}
+	/** Correct if a state of charge is to big or to less
+	 * @return a valid State of charge
+	 */
+	public float validStateOfCharge(float stateOfChargeToValid)
+	{
+		if(stateOfChargeToValid > capacity) //state of Charege can not more than the capacity
+		{
+			return capacity;
+		}
+		else if(stateOfChargeToValid < 0) // state of charge can not be a negativ value
+		{
+			return 0;
+		}
+		else
+		{
+			return stateOfChargeToValid;
+		}
+	}
 }

+ 9 - 6
src/ui/controller/SimulationManager.java

@@ -151,16 +151,18 @@ public class SimulationManager {
 				singleSubNet.getBatteries().sort(new WeakestBattery());//Sort all batteries by the Value of ther StateOfCharge/Capasity
 				for(HolonBattery hB : singleSubNet.getBatteries())
 				{
-					float energyToCollect = hB.getIN();
+					float energyToCollect = hB.getInAtTimeStep(x);
 					if(currentProduction >= energyToCollect)
 					{
 						//TODO: change StateofCharge soc = soc + energyToCollect
 						hB.setNewStateOfCharge(hB.getStateOfCharge() + energyToCollect);
+						hB.setStateOfChargeAtTimeStep(hB.getStateOfChargeAtTimeStep(x-1) + energyToCollect, x);
 						currentProduction -= energyToCollect;
 					}else
 					{
 						//TODO: change StateofCharge soc = soc + currentProduction
 						hB.setNewStateOfCharge(hB.getStateOfCharge() + currentProduction);
+						hB.setStateOfChargeAtTimeStep(hB.getStateOfChargeAtTimeStep(x-1) + currentProduction, x);
 						currentProduction = 0;
 						//no break must be calculatet for all break; //because no more energy
 					}
@@ -187,7 +189,7 @@ public class SimulationManager {
 			{
 				
 				//Check all Battries what they can provide
-				if(energySurplus + GetOutAllBatteries(singleSubNet.getBatteries()) >= 0)
+				if(energySurplus + GetOutAllBatteries(singleSubNet.getBatteries(), x) >= 0)
 				{
 					singleSubNet.getBatteries().sort(new WeakestBattery());//.reverse();
 					Collections.reverse(singleSubNet.getBatteries()); //most supplyed first
@@ -195,7 +197,7 @@ public class SimulationManager {
 					for(HolonBattery hB : singleSubNet.getBatteries())
 					{
 						float neededEnergyFromBattery = currentProduction + consumption; //Energy is negativ
-						float maxEnergyAvailable = hB.getOUT(); //energy is positiv
+						float maxEnergyAvailable = hB.getOutAtTimeStep(x); //energy is positiv
 						if(maxEnergyAvailable >= -neededEnergyFromBattery)
 						{
 							//TODO: change StateofCharge soc = soc - -neededEnergyFromBattery
@@ -227,7 +229,7 @@ public class SimulationManager {
 					//Get all Energy out of battries as possible
 					for(HolonBattery hB : singleSubNet.getBatteries())
 					{
-						float maxEnergyAvailable = hB.getOUT(); //energy is positiv
+						float maxEnergyAvailable = hB.getOutAtTimeStep(x); //energy is positiv
 						//TODO: change StateofCharge soc = soc - maxEnergyAvailable
 						hB.setNewStateOfCharge(hB.getStateOfCharge() - maxEnergyAvailable);
 						currentProduction += maxEnergyAvailable;
@@ -431,15 +433,16 @@ public class SimulationManager {
 	/**
 	 * add all battries.getOut() from a list of battries and return them
 	 * @param aL a List of HolonBattries likely from subnet.getBatteries()
+	 * @param x TODO
 	 * @return 
 	 * 
 	 */
-	private float GetOutAllBatteries(ArrayList<HolonBattery> aL)
+	private float GetOutAllBatteries(ArrayList<HolonBattery> aL, int x)
 	{
 		float OutEnergy = 0;
 		for(HolonBattery hB : aL)
 		{
-			OutEnergy += hB.getOUT();
+			OutEnergy += hB.getOutAtTimeStep(x);
 		}
 		return OutEnergy;
 	}

+ 21 - 0
src/ui/controller/SingletonControl.java

@@ -0,0 +1,21 @@
+package ui.controller;
+
+
+public class SingletonControl {
+	  private Control control;
+	  private static final class InstanceHolder {
+	    static final SingletonControl INSTANCE = new SingletonControl();
+	  }
+	  private SingletonControl () {}
+	  public static SingletonControl getInstance () {
+	    return InstanceHolder.INSTANCE;
+	  }
+	  public Control getControl()
+	  {
+		return control;
+	  }
+	  public void setControl(Control control)
+	  {
+		this.control = control;
+	  }
+}

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

@@ -339,7 +339,7 @@ public class UpdateController {
 				deleteRows(model.getMultiTable());
 				Object[] numInRatio = {"InRatio:", ((HolonBattery)obj).getInRatio() };
 				Object[] numOutRatio = {"OutRatio:", ((HolonBattery)obj).getOutRatio() };
-				Object[] numSOC_Capasity = {"State of charge:", Float.toString(((HolonBattery)obj).getStateOfCharge()) + "/" + Float.toString(((HolonBattery)obj).getCapacity()) };
+				Object[] numSOC_Capasity = {"State of charge:", Float.toString(((HolonBattery)obj).getStateOfChargeAtTimeStep(model.getCurIteration()-1)) + "/" + Float.toString(((HolonBattery)obj).getCapacity()) };
 				model.getPropertyTable().addRow(numInRatio);
 				model.getPropertyTable().addRow(numOutRatio);
 				model.getPropertyTable().addRow(numSOC_Capasity);

+ 1 - 1
src/ui/view/AbstractCanvas.java

@@ -197,7 +197,7 @@ public abstract class AbstractCanvas extends JPanel {
 					return;
 				}
 				// get supplied status
-				percentage = hB.getNewStateOfCharge() / hB.getCapacity();
+				percentage = hB.getStateOfChargeAtTimeStep(model.getCurIteration()-1) / hB.getCapacity();
 				//Color lerping
 //				float lerp(float point1, float point2, float alpha)
 //				{

+ 2 - 0
src/ui/view/Main.java

@@ -1,6 +1,7 @@
 package ui.view;
 
 import ui.controller.Control;
+import ui.controller.SingletonControl;
 import ui.model.Model;
 
 import javax.swing.*;
@@ -41,6 +42,7 @@ public class Main {
             try {
                 Model model = new Model();
                 Control control = new Control(model);
+                SingletonControl.getInstance().setControl(control);
                 GUI view = new GUI(control);
 
                 view.getFrmCyberPhysical().setVisible(true);