HolonBattery.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package classes;
  2. import ui.model.Model;
  3. import com.google.gson.annotations.Expose;
  4. import ui.controller.*;
  5. public class HolonBattery extends AbstractCpsObject{
  6. @Expose
  7. private float inRatio;
  8. @Expose
  9. private float outRatio;
  10. @Expose
  11. private float capacity;
  12. @Expose
  13. private float initialStateOfCharge;
  14. @Expose
  15. private float[] stateOfChargeLevels;
  16. @Expose
  17. private float iterations = 0;
  18. public enum State{STANDBY, COLLECT, EMIT};
  19. private State state;
  20. /** Constructor for a unique ID.
  21. * @param ObjName
  22. */
  23. public HolonBattery(String ObjName)
  24. {
  25. super(ObjName);
  26. inRatio = 1000.0f;
  27. outRatio = 1000.0f;
  28. capacity = 20000.0f;
  29. initialStateOfCharge = 0;
  30. setState(State.STANDBY);
  31. }
  32. /** Constructor to Copy a Battery
  33. * @param obj Object to copy.
  34. */
  35. public HolonBattery(AbstractCpsObject obj)
  36. {
  37. super(obj);
  38. super.setName(obj.getName());
  39. setInRatio(((HolonBattery) obj).getInRatio());
  40. setOutRatio(((HolonBattery) obj).getOutRatio());
  41. setCapacity(((HolonBattery) obj).getCapacity());
  42. setInitialStateOfCharge(((HolonBattery) obj).getInitialStateOfCharge());
  43. setState(State.STANDBY);
  44. }
  45. public float getCapacity() {
  46. return capacity;
  47. }
  48. public void setCapacity(float capacity) {
  49. if(capacity < 0) //capasity can not be negative
  50. {
  51. capacity = 0;
  52. }
  53. this.capacity = capacity;
  54. }
  55. public float getOutRatio() {
  56. return outRatio;
  57. }
  58. public void setOutRatio(float outRatio) {
  59. if(outRatio < 0) //
  60. {
  61. outRatio = 0;
  62. }
  63. this.outRatio = outRatio;
  64. }
  65. public float getInRatio() {
  66. return inRatio;
  67. }
  68. //For Calculations
  69. public float getInAtTimeStep(int x)
  70. {
  71. if(getCapacity() - getStateOfChargeAtTimeStep(x) < inRatio)
  72. return getCapacity() - getStateOfChargeAtTimeStep(x);
  73. else
  74. return inRatio;
  75. }
  76. public float getOutAtTimeStep(int x)
  77. {
  78. if(getStateOfChargeAtTimeStep(x) < outRatio)
  79. return getStateOfChargeAtTimeStep(x);
  80. else
  81. return outRatio;
  82. }
  83. public void setInRatio(float inRatio) {
  84. if(inRatio < 0)
  85. {
  86. inRatio = 0;
  87. }
  88. this.inRatio = inRatio;
  89. }
  90. public State getState() {
  91. return state;
  92. }
  93. public void setState(State state) {
  94. this.state = state;
  95. }
  96. /**
  97. * @return The String that is over the Battery in the Canvas if COLLECT the
  98. * input if EMIT the output of the Battery
  99. */
  100. public String getCanvasBatteryString(int x)
  101. {
  102. x--;
  103. float stateOfCharge1 = getStateOfChargeAtTimeStep(x-1);
  104. float newStateOfCharge1 = getStateOfChargeAtTimeStep(x);
  105. if(newStateOfCharge1 > stateOfCharge1)
  106. {
  107. return "-" + Float.toString(newStateOfCharge1 - stateOfCharge1);
  108. }
  109. else if (newStateOfCharge1 < stateOfCharge1)
  110. {
  111. return "+" +Float.toString(-(newStateOfCharge1 - stateOfCharge1)); // '"-" +' not needed because negative
  112. }else
  113. {
  114. return "0";
  115. }
  116. }
  117. public String toString()
  118. {
  119. return "HolonBattery" + this.getId();
  120. /*
  121. return "HolonBattery ID:" + this.getId() + " State:" + getState().name()
  122. + " InRatio:" + getInRatio() + " OutRatio:" + getOutRatio()
  123. + " Akku: " + getStateOfChargeAtTimeStep(SingletonControl.getInstance().getControl().getModel().getCurIteration()) + "/" + getCapacity();
  124. */
  125. }
  126. public void setStateOfChargeAtTimeStep(float newStateOfCharge, int x) {
  127. if(iterations != SingletonControl.getInstance().getControl().getModel().getIterations())
  128. {
  129. stateOfChargeLevels = new float[SingletonControl.getInstance().getControl().getModel().getIterations()];
  130. iterations = SingletonControl.getInstance().getControl().getModel().getIterations();
  131. }
  132. stateOfChargeLevels[x] = validStateOfCharge(newStateOfCharge);
  133. }
  134. public float getStateOfChargeAtTimeStep(int x) {
  135. if(iterations != SingletonControl.getInstance().getControl().getModel().getIterations())
  136. {
  137. stateOfChargeLevels = new float[SingletonControl.getInstance().getControl().getModel().getIterations()];
  138. iterations = SingletonControl.getInstance().getControl().getModel().getIterations();
  139. }
  140. if(x < 0)
  141. {
  142. return initialStateOfCharge;
  143. }
  144. return stateOfChargeLevels[x];
  145. }
  146. public float getInitialStateOfCharge() {
  147. return initialStateOfCharge;
  148. }
  149. public void setInitialStateOfCharge(float initialStateOfCharge) {
  150. this.initialStateOfCharge = validStateOfCharge(initialStateOfCharge);
  151. }
  152. /** Correct if a state of charge is to big or to less
  153. * @return a valid State of charge
  154. */
  155. public float validStateOfCharge(float stateOfChargeToValid)
  156. {
  157. if(stateOfChargeToValid > capacity) //state of Charege can not more than the capacity
  158. {
  159. return capacity;
  160. }
  161. else if(stateOfChargeToValid < 0) // state of charge can not be a negativ value
  162. {
  163. return 0;
  164. }
  165. else
  166. {
  167. return stateOfChargeToValid;
  168. }
  169. }
  170. public String getImageBattery() {
  171. //HardCoded Image selection
  172. float actualStateOfCharge = getStateOfChargeAtTimeStep(SingletonControl.getInstance().getControl().getModel().getCurIteration() - 1 );
  173. int whichOne = (int)(actualStateOfCharge/capacity * 4);
  174. String battery = "/Images/battery"+ whichOne + ".png";
  175. setImage(battery);
  176. return battery;
  177. }
  178. @Override
  179. public HolonBattery makeCopy(){
  180. return new HolonBattery(this);
  181. }
  182. }