123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- package classes;
- import com.google.gson.annotations.Expose;
- import java.awt.*;
- import java.util.LinkedList;
- /**
- * The class "HolonElement" represents any possible element that can be added to
- * a CpsObject (such as TV (consumer) or any energy source/producer).
- *
- * @author Gruppe14
- */
- public class HolonElement {
- // Points on the UnitGraph
- LinkedList<Point> graphPoints;
- /* Name of the gadget */
- @Expose
- private String eleName;
- /* Quantity */
- @Expose
- private int amount;
- /* Currently used energy (- indicates consuming of energy, + indicates producing energy)*/
- @Expose
- private float energy;
- /* Whether the gadget is active or not (currently uses/poduces the energy in energy) */
- @Expose
- private boolean active;
- /*
- * Gives us whether this element is flexible and can flexibly use any part of the energy in flexibleEnergyAvailable
- */
- @Expose
- private boolean flexible;
- /*
- * Flexibility (meaning the actual
- */
- @Expose
- private float flexibleEnergyAvailable;
- /* Total Energy */
- @Expose
- private float totalEnergy;
- /* -: for Consumers and +: Producers */
- @Expose
- private char sign;
- /* Place where the Object is Stored */
- @Expose
- private Pair<String, String> saving;
- /*
- * ID
- */
- @Expose
- private int id;
- /*
- * Energy at each point of the graph with 100 predefined points. At the
- * beginning, it starts with all values at energy
- */
- private float[] energyAt;
- /**
- * Create a new HolonElement with a user-defined name, amount of the same
- * element and energy per element.
- *
- * @param eleName String
- * @param amount int
- * @param energy float
- */
- public HolonElement(String eleName, int amount, float energy) {
- setEleName(eleName);
- setAmount(amount);
- setEnergy(energy);
- setActive(true);
- setSign(energy);
- setEnergyAt(energy);
- setGraphPoints(new LinkedList<>());
- setId(IdCounterElem.nextId());
- setFlexibleEnergyAvailable(0);
- setFlexible(false);
- }
- /**
- * same as standard constructor, but with already given id (so the counter is not increased twice)
- */
- public HolonElement(String eleName, int amount, float energy, int id) {
- setEleName(eleName);
- setAmount(amount);
- setEnergy(energy);
- setActive(true);
- setSign(energy);
- setEnergyAt(energy);
- setGraphPoints(new LinkedList<>());
- setId(id);
- setFlexibleEnergyAvailable(0);
- setFlexible(false);
- }
- /**
- * Create a copy of the HolonElement given each one a new ID.
- *
- * @param element element to copy
- */
- public HolonElement(HolonElement element) {
- setEleName(element.getEleName());
- setAmount(element.getAmount());
- setEnergy(element.getEnergy());
- setActive(element.isActive());
- setSign(element.getEnergy());
- setEnergyAt(element.getEnergy());
- for (int i = 0; i < energyAt.length; i++) {
- energyAt[i] = element.getEnergyAt()[i];
- }
- setGraphPoints(new LinkedList<>());
- for (Point p : element.getGraphPoints()) {
- this.graphPoints.add(new Point((int) p.getX(), (int) p.getY()));
- }
- setSaving(null);
- setId(IdCounterElem.nextId());
- setFlexibleEnergyAvailable(0);
- setFlexible(false);
- }
- /**
- * Get the Array of energy (100 values).
- *
- * @return energyAt Array of Floats
- */
- public float[] getEnergyAt() {
- return energyAt;
- }
- /**
- * Set energy to any value to the whole array.
- *
- * @param energy the value
- */
- public void setEnergyAt(float energy) {
- this.energyAt = new float[100];
- for (int i = 0; i < energyAt.length; i++) {
- this.energyAt[i] = energy;
- }
- }
- /**
- * Set energy to any value at a given position.
- *
- * @param pos int
- * @param energy float
- */
- public void setEnergyAt(int pos, float energy) {
- this.energyAt[pos] = energy;
- }
- /**
- * Get the user-defined Name.
- *
- * @return the name String
- */
- public String getEleName() {
- return eleName;
- }
- /**
- * Set the name to any new name.
- *
- * @param name the name to set
- */
- public void setEleName(String name) {
- this.eleName = name;
- }
- /**
- * Get the actual amount of Elements in the selected Object.
- *
- * @return the amount int
- */
- public int getAmount() {
- return amount;
- }
- /**
- * Set the amount of the Element in the selected Object.
- *
- * @param amount the amount to set
- */
- public void setAmount(int amount) {
- this.amount = amount;
- }
- /**
- * Get the energy value of the selected Element.
- *
- * @return the energy
- */
- public float getEnergy() {
- return energy;
- }
- /**
- * Set the energy value of the selected Element.
- *
- * @param energy the energy to set
- */
- public void setEnergy(float energy) {
- this.energy = energy;
- setSign(energy);
- }
- /**
- * Get the Status of the Element (see description of variables).
- *
- * @return the active
- */
- public boolean isActive() {
- return active;
- }
- /**
- * Set the Status of the Element (see description of variables).
- *
- * @param active the active to set
- */
- public void setActive(boolean active) {
- this.active = active;
- if (!active && flexible) {
- setEnergy(0);
- setEnergyAt(0);
- }
- }
- /**
- * Multiply the amount of gadgets, given by the user, and the
- * consumption/production. If the switch isWorking is turned off for on
- * gadget, the energy of this gadget have to be subtracted.
- *
- * @return totalEnergy (actual)
- */
- public float getTotalEnergy() {
- // if (flexible) {
- // totalEnergy = ((float) amount) * (energy + flexibleEnergyAvailable);
- // } else {
- totalEnergy = ((float) amount) * energy;
- // }
- return totalEnergy;
- }
- /**
- * Get the energy value at a selected time x.
- *
- * @param x int
- * @return energy value
- */
- public float getTotalEnergyAtTimeStep(int x) {
- // float result =((float) amount) * energyAt[x];
- // if (flexible) {
- // result = ((float) amount) * (energyAt[x] + flexibleEnergyAvailable);
- // } else {
- // result = ((float) amount) * energyAt[x];
- // }
- return ((float) amount) * energyAt[x];
- }
- /**
- * Get the symbol of the value (see variable description).
- *
- * @return the sign
- */
- public char getSign() {
- return sign;
- }
- /**
- * Set symbol of the value.
- *
- * @param energy the sign to set
- */
- public void setSign(float energy) {
- if (energy < 0)
- this.sign = '-';
- else
- this.sign = '+';
- }
- /**
- * Get the points (values) in the graph.
- *
- * @return the Graph Points
- */
- public LinkedList<Point> getGraphPoints() {
- return graphPoints;
- }
- /**
- * Set the points (values) in the graph.
- *
- * @param points the Graph points
- */
- public void setGraphPoints(LinkedList<Point> points) {
- this.graphPoints = points;
- }
- /**
- * Get the flexibleEnergyAvailable of an element
- */
- public float getFlexibleEnergyAvailable() {
- return this.flexibleEnergyAvailable;
- }
- /**
- * Set the flexibleEnergyAvailable of an element
- */
- public void setFlexibleEnergyAvailable(float f) {
- this.flexibleEnergyAvailable = f;
- }
- /**
- * Get the flexibleEnergyAvailable of an element
- */
- public boolean isFlexible() {
- return this.flexible;
- }
- /**
- * Set the flexibleEnergyAvailable of an element
- */
- public void setFlexible(boolean b) {
- this.flexible = b;
- // if flexibleEnergyAvailable was set to true
- if (b) {
- // set active to false
- this.active = false;
- // move energy to flexibleEnergyAvailable (becomes the possible-to-use energy)
- if (getEnergy() != 0) {
- setFlexibleEnergyAvailable(getEnergy());
- // and set actually used energy to zero (can be changed by algorithms, the grid itself or the user)
- setEnergy(0);
- setEnergyAt(0);
- }
- } else {
- // move the energy to actually used energy and set flexible amount to 0
- if (getFlexibleEnergyAvailable() != 0) {
- setEnergy(getFlexibleEnergyAvailable());
- setEnergyAt(getEnergy());
- }
- setFlexibleEnergyAvailable(0);
- }
- // System.out.println("after setFlexible(" + String.valueOf(b) + "):");
- // System.out.println("flexible " + flexible + ", active: " + active + ", energy: " + energy
- // + ", flexible energy: " + flexibleEnergyAvailable + "\n");
- }
- /**
- * Get the Id of the selected HolonElement.
- *
- * @return id the id
- */
- public int getId() {
- return id;
- }
- /**
- * Set the ID of the HolonElement (one time only).
- *
- * @param id the id
- */
- public void setId(int id) {
- this.id = id;
- }
- /**
- * @return the saving
- */
- public Pair<String, String> getSaving() {
- return saving;
- }
- /**
- * @param saving the saving to set
- */
- public void setSaving(Pair<String, String> saving) {
- this.saving = saving;
- }
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append("[HolonElement: ");
- sb.append("id=").append(id).append(", eleName=").append(eleName).append(", amount=").append(amount)
- .append(", active=").append(active).append(", flexible=").append(flexible)
- .append(", energy used=").append(energy).append(", flexible energy available=").append(flexibleEnergyAvailable);
- sb.append("]");
- return sb.toString();
- }
- }
|