123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- package holeg.model;
- import com.google.gson.annotations.Expose;
- import com.google.gson.annotations.SerializedName;
- import holeg.interfaces.TimelineDependent;
- import holeg.model.Flexibility.FlexState;
- import holeg.ui.controller.IndexTranslator;
- import holeg.ui.model.IdCounter;
- import holeg.ui.model.IdCounter.CounterType;
- import holeg.utility.Vector2Float;
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.ListIterator;
- import java.util.AbstractMap.SimpleEntry;
- import java.util.logging.Logger;
- /**
- * The class "HolonElement" represents any possible element that can be added to
- * a CpsObject (such as TV (consumer) or any energyPerElement source/producer).
- *
- * @author Gruppe14
- */
- public class HolonElement implements TimelineDependent{
- private static final Logger log = Logger.getLogger(HolonElement.class.getName());
- /*
- * MODEL
- */
- /** Owner of the Element */
- public HolonObject parentObject;
- /** Points of new TestGraph
- * Represent the Graph
- * the X component from a Point is period from 0..1
- * the Y component from a Point is the percentage from 0..1
- * */
- private LinkedList<Vector2Float> graphPoints;
- /** Name of the gadget, e.g. TV */
- @Expose
- @SerializedName(value = "name", alternate = "eleName")
- private String name;
- /** Amount of same elements */
- @Expose
- @SerializedName(value = "energy", alternate = "energyPerElement")
- private float energy;
- /** Whether the gadget is active or not (currently uses/produces the energy in energyPerElement) */
- @Expose
- public boolean active;
- /** Gives us whether this element is flexible and can flexibly use any part of the energy in flexibleEnergyAvailable */
-
-
-
-
- public enum Priority {
- Low, Medium, High, Essential
- }
-
- @Expose
- public Priority priority = Priority.Low;
-
- /** Place where the Object is Stored */
- @Expose
- private SimpleEntry<String, String> saving;
- /** ID */
- @Expose
- private int id;
-
-
- public java.util.List<Flexibility> flexList = new ArrayList<Flexibility>();
-
- /*
- * Energy at each point of the graph with 100 predefined points. At the
- * beginning, it starts with all values at energyPerElement.
- * If switched to flexible, this represents the maximum of usable energy
- */
- private float[] curveSample;
-
- @Expose
- private int localPeriod;
- @Expose
- private boolean isUsingLocalPeriod;
- /**
- * Create a new HolonElement with a user-defined name, amount of the same
- * element, energyPerElement and corresponding model.
- *
- * @param eleName String
- * @param amount int
- * @param energy float
- * @param model Model
- */
- //TODO(Tom2021-12-20): constructor should be remade
- public HolonElement(HolonObject parentObject, String eleName, float energy) {
-
- this(parentObject, eleName, energy, IdCounter.nextId(CounterType.Element));
- }
-
- /**
- * same as standard constructor, but with already given id (so the counter is not increased twice)
- */
- public HolonElement(HolonObject parentObject, String eleName, float energy, int id){
- this.parentObject = parentObject;
- setUseLocalPeriod(false);
- setName(eleName);
- setEnergy(energy);
- this.active = true;
- setGraphPoints(new LinkedList<>());
- initGraphPoints();
- sampleGraph();
- this.id = id;
- }
- /**
- * Create a copy of the HolonElement given each one a new ID.
- *
- * @param element element to copy
- */
- public HolonElement(HolonElement element) {
- this.parentObject = element.parentObject;
- this.priority = element.getPriority();
- setLocalPeriod(element.getLocalPeriod());
- setUseLocalPeriod(element.isUsingLocalPeriod());
- setName(element.getName());
- setLocalPeriod(element.getLocalPeriod());
- setEnergy(element.getEnergy());
- this.active = element.active;
- setGraphPoints(new LinkedList<>());
- for (Vector2Float p : element.getGraphPoints()) {
- this.graphPoints.add(new Vector2Float(p));
- }
- sampleGraph();
- setSaving(null);
- this.id = IdCounter.nextId(CounterType.Element);
- }
-
- public int getId() {
- return id;
- }
-
- /**
- * Get the user-defined Name.
- *
- * @return the name String
- */
- public String getName() {
- return name;
- }
- /**
- * Set the name to any new name.
- *
- * @param name the name to set
- */
- public void setName(String name) {
- this.name = name;
- }
- /**
- * Get the energyPerElement value of the selected Element.
- *
- * @return the energyPerElement
- */
- public float getEnergy() {
- return energy;
- }
-
- /**
- * Set the energyPerElement value of the selected Element.
- *
- * @param energyPerElement the energyPerElement to set
- */
- public void setEnergy(float energyPerElement) {
- log.finest(this.energy + " -> " + energyPerElement);
- this.energy = energyPerElement;
- }
-
-
- /**
- * Check the HolonElemnet is a Producer
- * @return true when the energy used be each element is higher then 0
- */
- public boolean isProducer()
- {
- return (energy > 0);
- }
-
- /**
- * Check the HolonElemnet is a Consumer
- * @return true when the energy used be each element is lower then 0
- */
- public boolean isConsumer()
- {
- return (energy < 0);
- }
-
-
-
- public Priority getPriority() {
- return priority;
- }
-
-
- public void setPriority(Priority priority) {
- this.priority = priority;
- }
- /**
- * @return the saving
- */
- public SimpleEntry<String, String> getSaving() {
- return saving;
- }
- /**
- * @param saving the saving to set
- */
- public void setSaving(SimpleEntry<String, String> saving) {
- this.saving = saving;
- }
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append("[HolonElement: ");
- sb.append("id=").append(id)
- .append(", eleName=").append(name)
- .append(", parentName=").append(parentObject.getName())
- .append(", active=").append(active)
- .append(", energyPerElement used=").append(energy);
- sb.append("]");
- return sb.toString();
- }
- /**
- * Initialize the {@link HolonElement#graphPoints} List with the normal 2 Points at 100%.
- */
- private void initGraphPoints()
- {
- graphPoints.clear();
- graphPoints.add(new Vector2Float(0f,1.0f));
- graphPoints.add(new Vector2Float(1f,1.0f));
- }
- /**
- * Getter for the graphPoint List.
- * @return {@link HolonElement#graphPoints}
- */
- public LinkedList<Vector2Float> getGraphPoints() {
- return graphPoints;
- }
- /**
- * Setter for the graphPoint List.
- * @param testGraphPoints is new {@link HolonElement#graphPoints}
- */
- public void setGraphPoints(LinkedList<Vector2Float> graphPoints) {
- this.graphPoints = graphPoints;
- }
-
-
-
- //interfaces.GraphEditable
- @Override
- public GraphType getGraphType() {
- return GraphType.doubleGraph;
- }
- @Override
- public LinkedList<Vector2Float> getStateGraph() {
- return getGraphPoints();
- }
-
- @Override
- public void sampleGraph() {
- curveSample = sampleGraph(100);
- }
- @Override
- public void reset() {
- initGraphPoints();
- sampleGraph();
- }
- /**
- * Generate out of the Graph Points a array of floats that represent the Curve at each sample position. The Values are in the Range [0,1].
- * e.g. 0.0 represent: "0%" , 0.34 represent: 34% 1.0 represent: "100%"
- * @param sampleLength amount of samplePositions. The positions are equidistant on the Range[0,1].
- * @return the float array of samplepoints.
- */
- private float[] sampleGraph(int sampleLength)
- {
- ListIterator<Vector2Float> iter = this.graphPoints.listIterator();
- Vector2Float before = iter.next();
- Vector2Float after = iter.next();
- float [] sampleCurve = new float[sampleLength];
- for(int i = 0; i<sampleLength ; i++)
- {
- double graphX = (double)i / (double) (sampleLength - 1); //from 0.0 to 1.0
- if(graphX > after.x)
- {
- before = after;
- after = iter.next();
- }
- //t to determine how many percentage the graphX is to the next Point needed to calc Bezier
- //inverseLerp(valueBetween, min, max) (valueBetween - min) / (max - min)
- // e.g. old.x = 0.4, actual.x = 0.8 and graphX = 0.6 then t is 0.5
- double t = (after.x -before.x > 0)? (graphX - before.x) / (after.x -before.x) : 0.0;
- sampleCurve[i] = (float) getYBetweenTwoPoints(t, before, after);
- }
- return sampleCurve;
- }
-
- /**
- * Helper method for {@link HolonElement#sampleGraph(int)}.
- * <p>
- * Its get the start and Endposition and calculate the Points in between for the Bezi�r Curve.
- * Then its get the Y Value a.k.a. the percentage from the curve at the X value t.
- * @param t is in Range [0,1] and represent how much the X value is traverse along the Curve between the two Points.
- * @param start is the start Point of the Curve.
- * @param end is the end Point of the Curve.
- * @return the percentage from the Curve at the X Value based on t.
- */
- private double getYBetweenTwoPoints(double t, Vector2Float start, Vector2Float end) {
-
- float mitte = (start.x + end.x)* 0.5f;
- Vector2Float bezier = getBezierPoint(t, start, new Vector2Float(mitte, start.y), new Vector2Float(mitte, end.y), end);
- return bezier.y;
- }
- /**
- * Helper method for {@link HolonElement#getYBetweenTwoPoints(double, Vector2Float, Vector2Float)}.
- * <p>
- * A Method for a normal Cubic Bezier Curve. A Cubic Bezier curve has four control points.
- * @param t is in Range [0,1] how much it traverse along the curve.
- * @param p0 StartPoint
- * @param p1 ControlPoint
- * @param p2 ControlPoint
- * @param p3 EndPoint
- * @return the BezierPosition at t.
- */
- private Vector2Float getBezierPoint(double t, Vector2Float p0, Vector2Float p1,Vector2Float p2,Vector2Float p3) {
- /*
- * Calculate Bezi�r:
- * B(t) = (1-t)^3 * P0 + 3*(1-t)^2 * t * P1 + 3*(1-t)*t^2 * P2 + t^3 * P3 , 0 < t < 1
- *
- * Source: //http://www.theappguruz.com/blog/bezier-curve-in-games
- */
- Vector2Float bezier = new Vector2Float();
- double OneSubT = 1-t;
- double OneSubT2 = Math.pow(OneSubT, 2);
- double OneSubT3 = Math.pow(OneSubT, 3);
- double t2 = Math.pow(t , 2);
- double t3 = Math.pow(t , 3);
-
- bezier.x = (float)(OneSubT3 * p0.x + 3 * OneSubT2 * t * p1.x + 3 * OneSubT * t2 * p2.x + t3 * p3.x);
- bezier.y = (float)(OneSubT3 * p0.y + 3 * OneSubT2 * t * p1.y + 3 * OneSubT * t2 * p2.y + t3 * p3.y);
- return bezier;
-
- }
- //interfaces.LocalMode
- @Override
- public void setLocalPeriod(int period) {
- localPeriod=period;
- }
-
- @Override
- public int getLocalPeriod() {
- return localPeriod;
- }
-
- @Override
- public boolean isUsingLocalPeriod() {
- return isUsingLocalPeriod;
- }
-
- @Override
- public void setUseLocalPeriod(boolean state) {
- this.isUsingLocalPeriod=state;
- }
-
- /*
- * STATE
- */
-
- private float actualEnergy = 0;
- //TODO(Tom2021-12-1): public -> package
- public void calculateState(int timestep) {
- flexList.forEach(flex -> flex.calculateState(timestep));
- float energyWhenActive = energy * this.curveSample[IndexTranslator.getEffectiveIndex(this, timestep)];
- actualEnergy = isOn() ? energyWhenActive : 0;
- }
- /**
- * Get the energyPerElement currently(at given time step) available
- */
- public float calculateExpectedEnergyAtTimeStep(int timestep) {
- float energyWhenActive = energy * this.curveSample[IndexTranslator.getEffectiveIndex(this, timestep)];
- return active ? energyWhenActive : 0;
- }
-
- public float getActualEnergy() {
- return actualEnergy;
- }
- public boolean isOn() {
- //return isFlexActive()?!active:active;
- //Bool logic XOR
- return isFlexActive() ^ active;
- }
- public boolean isFlexActive() {
- return flexList.stream().anyMatch(flex -> flex.getState() == FlexState.IN_USE || flex.getState() == FlexState.ON_COOLDOWN);
- }
-
- }
|