package classes; import com.google.gson.annotations.Expose; import java.awt.*; import java.util.ArrayList; import javafx.util.converter.PercentageStringConverter; /** * The class HolonObject represents any Object on the system which capability of * injecting or consuming energy on the network, for instance a house or a power * plant. * * @author Gruppe14 */ public class HolonObject extends AbstractCpsObject { @Deprecated enum State{ NO_ENERGY, NOT_SUPPLIED, SUPPLIED, PRODUCER, PARTIALLY_SUPPLIED, OVER_SUPPLIED } @Deprecated public final static int NO_ENERGY = 0; @Deprecated public final static int NOT_SUPPLIED = 1; @Deprecated public final static int SUPPLIED = 2; @Deprecated public final static int PRODUCER = 3; @Deprecated public final static int PARTIALLY_SUPPLIED = 4; @Deprecated public final static int OVER_SUPPLIED = 5; /* * Color of the actual state (red = no supplied, yellow = partially supplied * and green = supplied) */ @Expose private Color stateColor; //TODO: to draw function /* Array of all consumers */ private ArrayList elements; /* Total of consumption */ @Expose private float currentEnergy; /* Array for tracking Production */ private float[] trackingProd; /* Array for tracking Consumption */ private float[] trackingCons; /* Total Flexibility */ private float totalFlex; @Deprecated @Expose private int state = 0; /** * Energy level that was supplied by other HolonObjects in the current Calculation */ private float currentSupply; /** TODO: outsourcing * Percentage of supplied energy of the energy level needed to supply all elements */ private float suppliedPercentage; /** * Constructor Set by default the name of the object equals to the category * name, until the user changes it. * * @param objName name of the Object */ public HolonObject(String objName) { super(objName); setElements(new ArrayList<>()); setTrackingProd(new float[100]); setTrackingCons(new float[100]); } /** * Contructor of a copy of an Object. * * @param obj object to be copied */ public HolonObject(AbstractCpsObject obj) { super(obj); setElements(copyElements(((HolonObject) obj).getElements())); setTrackingProd(new float[100]); setTrackingCons(new float[100]); } /** * Getter for all Elements in the HolonObject. * * @return the elements ArrayList */ public ArrayList getElements() { return elements; } /** * Set a new ArrayList with HolonElements into the HolonObject. * * @param elements the elements to set */ public void setElements(ArrayList elements) { this.elements = elements; } /** * adds an Element to the Object. * * @param element the Element to add */ public void addElement(HolonElement element) { elements.add(element); } /** * deletes Element at a given index. * * @param idx index */ public void deleteElement(int idx) { elements.remove(idx); } /** * Calculate the maximum energy a holonObject can consume from all object that are aktive this moment; * * @return the maximal active energy possibel */ public float getMaxActiveEnergy() { float temp = 0; for (HolonElement e : getElements()) { if (e.isActive()) { temp += e.getOverallEnergy(); } } currentEnergy = temp; return temp; } /** * Getter for the current energy at a given timestep. * * @param x timestep * @return corresponding energy */ @Deprecated public float getCurrentEnergyAtTimeStepWithoutFlexiblesAndResetFlexibles(int x) { float temp = 0; for (HolonElement e : getElements()) { if (e.isActive() && !e.isFlexible()) { temp += e.getOverallEnergyAtTimeStep(x); } else if (e.isFlexible()) { e.setEnergyPerElement(0); } } currentEnergy = temp; return currentEnergy; } /** * String of all consumers in this HolonObject. * * @return all the names of this HolonObject separated by "," each object */ public String toStringElements() { String objString = "Empty"; for (HolonElement e : elements) { if (objString == "Empty") { objString = e.getEleName(); } else { objString = objString + ", " + e.getEleName(); } } return objString; } /** * Copy all Elements into a New Array. * * @param arr to copy * @return the copy of arr */ public ArrayList copyElements(ArrayList arr) { ArrayList newArr = new ArrayList<>(); for (HolonElement t : arr) { newArr.add(new HolonElement(t)); } return newArr; } /** * Get the state of the Object. * * @return state the State of the Element */ @Deprecated public int getState() { return this.state; } /** * Set the state of the Object. * * @param state boolean if the Object is fully supplied */ @Deprecated public void setState(int state) { this.state = state; switch (state) { case NO_ENERGY: stateColor = Color.WHITE; break; case NOT_SUPPLIED: stateColor = new Color(230, 120, 100); break; case SUPPLIED: stateColor = Color.GREEN; break; case PRODUCER: stateColor = Color.lightGray; break; case PARTIALLY_SUPPLIED: stateColor = Color.YELLOW; break; case OVER_SUPPLIED: // find different purple-tones at // http://www.rapidtables.com/web/color/purple-color.htm stateColor = new Color(138, 43, 226); break; } } /** * Search for the first element with the name. * * @param name name of the object to be searched * @return the searched HolonElement */ public HolonElement searchElement(String name) { HolonElement ele = null; for (HolonElement e : getElements()) { if (e.getEleName().equals(name)) { ele = e; break; } } return ele; } /** * Search for the element with the id. * * @param id id of the element to be founded * @return the element */ public HolonElement searchElementById(int id) { HolonElement ele = null; for (HolonElement e : getElements()) { if (e.getId() == id) { ele = e; break; } } return ele; } //New Methods: /** * This Method returns the smallest consuming HolonElement that is ACTIVE. * If the HolonObject has no Consumer its return null. * @param timestep is the TimeStep to compare the HolonElements. * @return The smallest consuming HolonElement or null. */ public HolonElement getMinimumConsumingElement(int timestep){ return getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) < 0) ).max((lhs,rhs) -> Float.compare(lhs.getEnergyAtTimeStep(timestep), rhs.getEnergyAtTimeStep(timestep))).orElse(null); } /** * This Method returns the smallest consuming HolonElement'Energy that is ACTIVE. * If the HolonObject has no Consumer its return 0. * @param timestep is the TimeStep to compare the HolonElements. * @return The smallest consuming HolonElement or 0. */ public float getMinimumConsumingElementEnergy(int timestep){ return getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) < 0) ).map(element -> -element.getEnergyAtTimeStep(timestep)).min((lhs,rhs) ->Float.compare(lhs, rhs)).orElse(0.0f); } /** * This Method returns the Energy of a HolonObject. Its sums all Energies from the HolonElements of the HolonObject that are ACTIVE. * If the HolonObject have no HolonElement its return 0; * Is the returned Energy negative then the HolonObject need Energy because its consuming HolonElements need more Energy then the producing HolonElements. * Is the returned Energy positive its reversed. * @param timestep is the TimeStep to compare the HolonElements. * @return The Energy of the HolonObject. */ public float getEnergyAtTimeStep(int timestep) { return getElements().stream().filter(element -> element.isActive()).map(element -> element.getEnergyAtTimeStep(timestep)).reduce(0.0f, (a, b) -> a + b); } /** * This Method returns the Energy that all HolonElements from the HolonObject produce by itself. Its sums all Energies from the HolonElements of the HolonObject that are ACTIVE and are Producer. * If the HolonObject have no HolonElement its return 0; * @param timestep is the TimeStep to compare the HolonElements. * @return The Energy of the producing HolonElements. */ public float getEnergySelfProducingFromProducingElements(int timestep) { return getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) > 0)).map(element -> element.getEnergyAtTimeStep(timestep)).reduce(0.0f, (a, b) -> a + b); } /** * This Method returns the Energy of all HolonElements from the HolonObject that are consuming. Its sums all Energies from the HolonElements of the HolonObject that are ACTIVE and are Consumer. * If the HolonObject have no HolonElement its return 0; * @param timestep is the TimeStep to compare the HolonElements. * @return The Energy of the consuming HolonElements. */ public float getEnergyNeededFromConsumingElements(int timestep) { return getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) < 0)).map(element -> -element.getEnergyAtTimeStep(timestep)).reduce(0.0f, (a, b) -> a + b); } /** * This Method calculate the amount of HolonElements that are consuming Energy and are ACTIVE. * @param timestep is the TimeStep to compare the HolonElements. * @return The amount of HolonElements that are consuming Energy. */ public int countConsumingElements(int timestep) { return (int) getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) < 0)).count(); } /** * This Method calculate the amount of HolonElements that are producing Energy and are ACTIVE. * @param timestep is the TimeStep to compare the HolonElements. * @return The amount of HolonElements that are producing Energy. */ public int countProducingElements(int timestep) { return (int) getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) > 0)).count(); } /** * Get the Color. * * @return stateColor the Color */ public Color getColor() { return stateColor; } /** * Set the State Color. * * @param color the Color */ public void setColor(Color color) { stateColor = color; } /** * Get the Array Production */ public float[] getTrackingProd() { return this.trackingProd; } /** * Set the Array Production */ public void setTrackingProd(float[] arr) { this.trackingProd = arr; } /** * Get the Array Consumption */ public float[] getTrackingCons() { return this.trackingCons; } /** * Set the Array Consumption */ public void setTrackingCons(float[] arr) { this.trackingCons = arr; } /** * Get the Array Consumption */ public float getTotalFlex() { return totalFlex; } /** * Set the Array Consumption */ public void setTotalFlex(float totalFlex) { this.totalFlex = totalFlex; } /** * Update the totalFlex */ public void updateTotalFlex() { float tempFlex = 0; for (HolonElement e : getElements()) { if (e.isFlexible()) { tempFlex += e.getFlexibleEnergyAvailablePerElement() * e.getAmount(); } } this.totalFlex = tempFlex; } /** * calculates total flexible Production */ public float getFlexProd() { float tempFlex = 0; for (HolonElement e : getElements()) { if (e.getFlexibleEnergyAvailablePerElement() > 0) { tempFlex += e.getFlexibleEnergyAvailablePerElement() * e.getAmount(); } } return tempFlex; } /** * calculates total flexible Concumption */ public float getFlexCons() { float tempFlex = 0; for (HolonElement e : getElements()) { if (e.getFlexibleEnergyAvailablePerElement() < 0) { tempFlex += e.getFlexibleEnergyAvailablePerElement() * e.getAmount(); } } return tempFlex; } /** * If the user track any HolonObject the tracking information will be * updated. (If the HolonObject enters into the untracked state, the array * will be reseted) */ public void updateTrackingInfo() { float[] tempProd = new float[100]; float[] tempCons = new float[100]; for (int i = 0; i < 100; i++) { float valueProd = 0; float valueCons = 0; for (HolonElement e : getElements()) { if (e.isActive() && e.isProducer()) { valueProd += e.getOverallEnergyAtTimeStep(i); } if (e.isActive() && e.isConsumer()) { valueCons += e.getOverallEnergyAtTimeStep(i); } } tempProd[i] = valueProd; tempCons[i] = valueCons; } this.trackingProd = tempProd; this.trackingCons = tempCons; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append("[HolonObject: "); sb.append("id=").append(id) .append(", name=").append(name) .append(", state="); switch (state) { case NO_ENERGY: sb.append("NO_ENERGY"); break; case NOT_SUPPLIED: sb.append("NOT_SUPPLIED"); break; case SUPPLIED: sb.append("SUPPLIED"); break; case PRODUCER: sb.append("PRODUCER"); break; case PARTIALLY_SUPPLIED: sb.append("PARTIALLY_SUPPLIED"); break; case OVER_SUPPLIED: sb.append("OVER_SUPPLIED"); break; } sb.append(", elements=["); for (int i = 0; i < getElements().size(); i++) { HolonElement el = getElements().get(i); if (i != 0) { sb.append(", "); } sb.append(el.getEleName()); } sb.append("]]"); return sb.toString(); } /** * @return the {@link #currentSupply} */ public float getCurrentSupply() { return currentSupply; } /** * @param currentSupply the {@link #currentSupply} to set */ public void setCurrentSupply(float currentSupply) { this.currentSupply = currentSupply; } /** * @return {@link #suppliedPercentage} */ public float getSuppliedPercentage(){ return suppliedPercentage; } }