HolonBattery.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. //DEBUG
  134. //TODO delete Debug
  135. boolean debug = false;
  136. if(debug)
  137. {
  138. System.out.println( "Iterations:" + x);
  139. for(int i = 0; i < stateOfChargeLevels.length - 1; i++)
  140. {
  141. System.out.print( i+":"+stateOfChargeLevels[i]+ ", ");
  142. if(i%10 == 0)
  143. {
  144. System.out.println();
  145. }
  146. }
  147. System.out.println( stateOfChargeLevels.length - 1+":"+stateOfChargeLevels[stateOfChargeLevels.length - 1]);
  148. }
  149. }
  150. public float getStateOfChargeAtTimeStep(int x) {
  151. if(iterations != SingletonControl.getInstance().getControl().getModel().getIterations())
  152. {
  153. stateOfChargeLevels = new float[SingletonControl.getInstance().getControl().getModel().getIterations()];
  154. iterations = SingletonControl.getInstance().getControl().getModel().getIterations();
  155. }
  156. if(x < 0)
  157. {
  158. return initialStateOfCharge;
  159. }
  160. return stateOfChargeLevels[x];
  161. }
  162. public float getInitialStateOfCharge() {
  163. return initialStateOfCharge;
  164. }
  165. public void setInitialStateOfCharge(float initialStateOfCharge) {
  166. this.initialStateOfCharge = validStateOfCharge(initialStateOfCharge);
  167. }
  168. /** Correct if a state of charge is to big or to less
  169. * @return a valid State of charge
  170. */
  171. public float validStateOfCharge(float stateOfChargeToValid)
  172. {
  173. if(stateOfChargeToValid > capacity) //state of Charege can not more than the capacity
  174. {
  175. return capacity;
  176. }
  177. else if(stateOfChargeToValid < 0) // state of charge can not be a negativ value
  178. {
  179. return 0;
  180. }
  181. else
  182. {
  183. return stateOfChargeToValid;
  184. }
  185. }
  186. public HolonBattery makeCopy(){
  187. return new HolonBattery(this);
  188. }
  189. }