Flexibility.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package classes;
  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. /** The Name of a Flexibility.*/
  12. @Expose
  13. public String name;
  14. /** How fast in TimeSteps the Flexibility can be activated. */
  15. @Expose
  16. public int speed;
  17. /** How high the cost for a activation are. */
  18. @Expose
  19. public float cost;
  20. /** SHould this Flexibility be Offered when is constrainList is fulfilled the flexibility can be activated.*/
  21. @Expose
  22. public boolean offered;
  23. /** The Duration in TimeSteps how long the Flexibility is activated.*/
  24. @Expose
  25. private int duration;
  26. /** The Duration after a successful activation between the next possible activation.*/
  27. @Expose
  28. private int cooldown;
  29. /** The Element this flexibility is assigned.*/
  30. private HolonElement element;
  31. /** The List of Constrains the Flexibility */
  32. @Expose
  33. public List<Constrain> constrainList;
  34. public Flexibility(HolonElement element){
  35. this(0 , 0.f, 1, 0, false, element);
  36. }
  37. public Flexibility(int speed, float cost, int duration, int cooldown, boolean offered, HolonElement element){
  38. this.speed = speed;
  39. this.cost = cost;
  40. setDuration(duration);
  41. setCooldown(cooldown);
  42. this.offered=offered;
  43. this.element = element;
  44. constrainList = new ArrayList<Constrain>();
  45. }
  46. /** Checks if all assigned constrains are fulfilled. When no constrains assigned returns true.*/
  47. public boolean fulfillsConstrains() {
  48. //System.out.println("Fix me when other is fixed");
  49. return constrainList.stream().map(constrain -> constrain.getConstrainFunction()).reduce(Predicate::and).orElse(f -> true).test(this);
  50. }
  51. public HolonElement getElement() {
  52. return element;
  53. }
  54. public void setElement(HolonElement element) {
  55. this.element = element;
  56. }
  57. public int getDuration() {
  58. return duration;
  59. }
  60. /** Minimum duration is 1 TimeStep.*/
  61. public void setDuration(int duration) {
  62. this.duration = Math.max(1, duration);
  63. }
  64. public int getCooldown() {
  65. return cooldown;
  66. }
  67. /** No negative cooldown TimeSteps.*/
  68. public void setCooldown(int cooldown) {
  69. this.cooldown = Math.max(0, cooldown);
  70. }
  71. /** returns the total energy Amount accumulated over the TimeSteps.*/
  72. public float magnitude() {
  73. return ((float)duration) * element.getEnergyPerElement() * element.getAmount();
  74. }
  75. public float bringtmir(){
  76. //System.out.println("Name:" + element.getEleName() + " BringtMir:" + (-element.getEnergyPerElement() * element.getAmount()));
  77. return (constrainList.stream().map(constrain -> constrain.getName()).anyMatch(name -> name.equals("onConstrain"))?-1.0f:1.0f) * element.getEnergyPerElement() * element.getAmount();
  78. }
  79. public boolean isPositive() {
  80. return bringtmir() >= 0;
  81. }
  82. public boolean isNegative() {
  83. return bringtmir() < 0;
  84. }
  85. public int getSpeed() {
  86. return speed;
  87. }
  88. public void setSpeed(int speed) {
  89. this.speed = speed;
  90. }
  91. @Override
  92. public String toString() {
  93. return "Flexibility: " + name + " from [HolonElement: " + element.getEleName() + " ID:" + element.getId()+"]";
  94. }
  95. }