HolonBattery.java 4.4 KB

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