package classes; import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; /** * Representative of a flexibility for a HolonElement. * */ public class Flexibility { /** How fast in TimeSteps the Flexibility can be activated. */ public int speed; /** How high the cost for a activation are. */ public float cost; /** SHould this Flexibility be Offered when is constrainList is fulfilled the flexibility can be activated.*/ public boolean offered; /** The Duration in TimeSteps how long the Flexibility is activated.*/ private int duration; /** The Duration after a successful activation between the next possible activation.*/ private int cooldown; /** The Element this flexibility is assigned.*/ private HolonElement element; /** The List of Constrains the Flexibility */ public List> constrainList; public Flexibility(HolonElement element){ this(0 , 0.f, 1, 0, false, element); } public Flexibility(int speed, float cost, int duration, int cooldown, boolean offered, HolonElement element){ this.speed = speed; this.cost = cost; setDuration(duration); setCooldown(cooldown); this.offered=offered; this.element = element; constrainList = new ArrayList>(); } /** Checks if all assigned constrains are fulfilled. When no constrains assigned returns true.*/ public boolean fulfillsConstrains() { return constrainList.stream().reduce(Predicate::and).orElse(f -> true).test(this); } public HolonElement getElement() { return element; } public int getDuration() { return duration; } /** Minimum duration is 1 TimeStep.*/ public void setDuration(int duration) { this.duration = Math.max(1, duration); } public int getCooldown() { return cooldown; } /** No negative cooldown TimeSteps.*/ public void setCooldown(int cooldown) { this.cooldown = Math.max(0, cooldown); } /** returns the total energy Amount accumulated over the TimeSteps.*/ public float magnitude() { return ((float)duration) * element.getEnergyPerElement(); } //Example Constrains: /** Flexibility should be offered when Element is active.*/ public Predicate onConstrain = f -> f.getElement().isActive(); /** Flexibility should be offered when Element is inactive.*/ public Predicate offConstrain = f -> !f.getElement().isActive(); }