Flexibility.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package classes;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.function.Predicate;
  5. /**
  6. * Representative of a flexibility for a HolonElement.
  7. *
  8. */
  9. public class Flexibility {
  10. /** The Name of a Flexibility.*/
  11. public String name;
  12. /** How fast in TimeSteps the Flexibility can be activated. */
  13. public int speed;
  14. /** How high the cost for a activation are. */
  15. public float cost;
  16. /** SHould this Flexibility be Offered when is constrainList is fulfilled the flexibility can be activated.*/
  17. public boolean offered;
  18. /** The Duration in TimeSteps how long the Flexibility is activated.*/
  19. private int duration;
  20. /** The Duration after a successful activation between the next possible activation.*/
  21. private int cooldown;
  22. /** The Element this flexibility is assigned.*/
  23. private HolonElement element;
  24. /** The List of Constrains the Flexibility */
  25. public List<Predicate<Flexibility>> constrainList;
  26. public Flexibility(HolonElement element){
  27. this(0 , 0.f, 1, 0, false, element);
  28. }
  29. public Flexibility(int speed, float cost, int duration, int cooldown, boolean offered, HolonElement element){
  30. this.speed = speed;
  31. this.cost = cost;
  32. setDuration(duration);
  33. setCooldown(cooldown);
  34. this.offered=offered;
  35. this.element = element;
  36. constrainList = new ArrayList<Predicate<Flexibility>>();
  37. }
  38. /** Checks if all assigned constrains are fulfilled. When no constrains assigned returns true.*/
  39. public boolean fulfillsConstrains() {
  40. return constrainList.stream().reduce(Predicate::and).orElse(f -> true).test(this);
  41. }
  42. public HolonElement getElement() {
  43. return element;
  44. }
  45. public int getDuration() {
  46. return duration;
  47. }
  48. /** Minimum duration is 1 TimeStep.*/
  49. public void setDuration(int duration) {
  50. this.duration = Math.max(1, duration);
  51. }
  52. public int getCooldown() {
  53. return cooldown;
  54. }
  55. /** No negative cooldown TimeSteps.*/
  56. public void setCooldown(int cooldown) {
  57. this.cooldown = Math.max(0, cooldown);
  58. }
  59. /** returns the total energy Amount accumulated over the TimeSteps.*/
  60. public float magnitude() {
  61. return ((float)duration) * element.getEnergyPerElement();
  62. }
  63. @Override
  64. public String toString() {
  65. return "Flexibility: " + name + " from [HolonElement: " + element.getEleName() + " ID:" + element.getId()+"]";
  66. }
  67. //Example Constrains:
  68. /** Flexibility should be offered when Element is active.*/
  69. public static Predicate<Flexibility> onConstrain = f -> f.getElement().isActive();
  70. /** Flexibility should be offered when Element is inactive.*/
  71. public static Predicate<Flexibility> offConstrain = f -> !f.getElement().isActive();
  72. }