Flexibility.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package model;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.function.Predicate;
  5. import com.google.gson.annotations.Expose;
  6. /**
  7. * Representative of a flexibility for a HolonElement.
  8. *
  9. */
  10. public class Flexibility {
  11. /*
  12. * MODEL
  13. */
  14. /** The owner of the Flexibility.*/
  15. private HolonElement element;
  16. /** The Name of a Flexibility.*/
  17. @Expose
  18. public String name;
  19. /** How fast in TimeSteps the Flexibility can be activated. */
  20. @Expose
  21. public int speed;
  22. /** How high the cost for a activation are. */
  23. @Expose
  24. public float cost;
  25. /** SHould this Flexibility be Offered when is constrainList is fulfilled the flexibility can be activated.*/
  26. @Expose
  27. public boolean offered;
  28. /** The Duration in TimeSteps how long the Flexibility is activated.*/
  29. @Expose
  30. private int duration;
  31. /** The Duration after a successful activation between the next possible activation.*/
  32. @Expose
  33. private int cooldown;
  34. /** The Element this flexibility is assigned.*/
  35. /** The List of Constrains the Flexibility */
  36. @Expose
  37. public List<Constrain> constrainList;
  38. public Flexibility(HolonElement element){
  39. this(0 , 0.f, 1, 0, false, element);
  40. }
  41. public Flexibility(int speed, float cost, int duration, int cooldown, boolean offered, HolonElement element){
  42. this.speed = speed;
  43. this.cost = cost;
  44. setDuration(duration);
  45. setCooldown(cooldown);
  46. this.offered=offered;
  47. this.element = element;
  48. constrainList = new ArrayList<Constrain>();
  49. }
  50. public HolonElement getElement() {
  51. return element;
  52. }
  53. public void setElement(HolonElement element) {
  54. this.element = element;
  55. }
  56. public int getDuration() {
  57. return duration;
  58. }
  59. /** Minimum duration is 1 TimeStep.*/
  60. public void setDuration(int duration) {
  61. this.duration = Math.max(1, duration);
  62. }
  63. public int getCooldown() {
  64. return cooldown;
  65. }
  66. /** No negative cooldown TimeSteps.*/
  67. public void setCooldown(int cooldown) {
  68. this.cooldown = Math.max(0, cooldown);
  69. }
  70. /** returns the total energy Amount accumulated over the TimeSteps.*/
  71. public float magnitude() {
  72. return ((float)duration) * element.getEnergy();
  73. }
  74. public float energyReleased(){
  75. return (constrainList.stream().map(constrain -> constrain.getName()).anyMatch(name -> name.equals("onConstrain"))?-1.0f:1.0f) * element.getEnergy();
  76. }
  77. public boolean isPositive() {
  78. return energyReleased() >= 0;
  79. }
  80. public boolean isNegative() {
  81. return energyReleased() < 0;
  82. }
  83. public int getSpeed() {
  84. return speed;
  85. }
  86. public void setSpeed(int speed) {
  87. this.speed = speed;
  88. }
  89. @Override
  90. public String toString() {
  91. return "Flexibility: " + name + " from [HolonElement: " + element.getName() + " ID:" + element.getId() +"]";
  92. }
  93. public static enum FlexState{
  94. IN_USE, ON_COOLDOWN, OFFERED, NOT_OFFERED, UNAVAILABLE
  95. }
  96. /*
  97. * STATE
  98. */
  99. private FlexState state = FlexState.OFFERED;
  100. private int timestep;
  101. private int activationTime = -1;
  102. private int durationEndTime = -1;
  103. private int coolDownEndTime = -1;
  104. //TODO(Tom2021-12-1) public -> package
  105. public void calculateState(int timestep) {
  106. this.timestep = timestep;
  107. state = revalidateState();
  108. }
  109. //TODO(Tom2021-12-1) public -> package
  110. public boolean order() {
  111. if(canOrder()) {
  112. state = FlexState.IN_USE;
  113. durationEndTime = timestep + this.getDuration();
  114. coolDownEndTime = durationEndTime + this.getCooldown();
  115. activationTime = timestep;
  116. return true;
  117. }
  118. return false;
  119. }
  120. //TODO(Tom2021-12-1) public -> package
  121. public boolean cancel() {
  122. if(activationTime == timestep) {
  123. state=FlexState.OFFERED;
  124. durationEndTime = -1;
  125. coolDownEndTime = -1;
  126. return true;
  127. }
  128. return false;
  129. }
  130. public boolean canActivate() {
  131. return remainingTimeTillActivation() == 0;
  132. }
  133. public boolean canOrder() {
  134. boolean flexIsOffered = state.equals(FlexState.OFFERED);
  135. return flexIsOffered && !element.isFlexActive() ;
  136. }
  137. public FlexState getState() {
  138. return state;
  139. }
  140. public int remainingTimeTillActivation() {
  141. return Math.max(0, coolDownEndTime - timestep);
  142. }
  143. public int remainingDuration() {
  144. return Math.max(0, durationEndTime - timestep);
  145. }
  146. /** Checks if all assigned constrains are fulfilled. When no constrains assigned returns true.*/
  147. private boolean fulfillsConstrains() {
  148. return constrainList.stream().map(constrain -> constrain.getConstrainFunction()).reduce(Predicate::and).orElse(f -> true).test(this);
  149. }
  150. private FlexState revalidateState() {
  151. if(canActivate()) {
  152. if (!this.fulfillsConstrains() || element.isFlexActive()) {
  153. return FlexState.UNAVAILABLE;
  154. }else if (this.offered){
  155. return FlexState.OFFERED;
  156. }
  157. return FlexState.NOT_OFFERED;
  158. }
  159. else if(remainingDuration()== 0) {
  160. return FlexState.ON_COOLDOWN;
  161. }
  162. else {
  163. return FlexState.IN_USE;
  164. }
  165. }
  166. }