Flexibility.java 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. /** How fast in TimeSteps the Flexibility can be activated. */
  11. public int speed;
  12. /** How high the cost for a activation are. */
  13. public float cost;
  14. /** SHould this Flexibility be Offered when is constrainList is fulfilled the flexibility can be activated.*/
  15. public boolean offered;
  16. /** The Duration in TimeSteps how long the Flexibility is activated.*/
  17. private int duration;
  18. /** The Duration after a successful activation between the next possible activation.*/
  19. private int cooldown;
  20. /** The Element this flexibility is assigned.*/
  21. private HolonElement element;
  22. /** The List of Constrains the Flexibility */
  23. public List<Predicate<Flexibility>> constrainList;
  24. public Flexibility(HolonElement element){
  25. this(0 , 0.f, 1, 0, false, element);
  26. }
  27. public Flexibility(int speed, float cost, int duration, int cooldown, boolean offered, HolonElement element){
  28. this.speed = speed;
  29. this.cost = cost;
  30. setDuration(duration);
  31. setCooldown(cooldown);
  32. this.offered=offered;
  33. this.element = element;
  34. constrainList = new ArrayList<Predicate<Flexibility>>();
  35. }
  36. /** Checks if all assigned constrains are fulfilled. When no constrains assigned returns true.*/
  37. public boolean fulfillsConstrains() {
  38. return constrainList.stream().reduce(Predicate::and).orElse(f -> true).test(this);
  39. }
  40. public HolonElement getElement() {
  41. return element;
  42. }
  43. public int getDuration() {
  44. return duration;
  45. }
  46. /** Minimum duration is 1 TimeStep.*/
  47. public void setDuration(int duration) {
  48. this.duration = Math.max(1, duration);
  49. }
  50. public int getCooldown() {
  51. return cooldown;
  52. }
  53. /** No negative cooldown TimeSteps.*/
  54. public void setCooldown(int cooldown) {
  55. this.cooldown = Math.max(0, cooldown);
  56. }
  57. /** returns the total energy Amount accumulated over the TimeSteps.*/
  58. public float magnitude() {
  59. return ((float)duration) * element.getEnergyPerElement();
  60. }
  61. //Example Constrains:
  62. /** Flexibility should be offered when Element is active.*/
  63. public Predicate<Flexibility> onConstrain = f -> f.getElement().isActive();
  64. /** Flexibility should be offered when Element is inactive.*/
  65. public Predicate<Flexibility> offConstrain = f -> !f.getElement().isActive();
  66. }