123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- package holeg.model;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.List;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
- /**
- * 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 AbstractCanvasObject {
- /* Array of all consumers */
- private List<HolonElement> elements = new ArrayList<HolonElement>();
- /**
- * 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);
- }
- /**
- * Contructor of a copy of an Object.
- *
- * @param obj object to be copied
- */
- public HolonObject(HolonObject obj) {
- super(obj);
- for (HolonElement ele : obj.elements) {
- this.add(new HolonElement(ele));
- }
- }
- @Override
- public void initForReflection() {
- elements = new ArrayList<HolonElement>();
- }
- /**
- * Getter for all Elements in the HolonObject.
- *
- * @return the elements ArrayList
- */
- public Stream<HolonElement> getElements() {
- return elements.stream();
- }
- public void clearElements() {
- elements.clear();
- }
- /**
- * adds an Element to the Object.
- *
- * @param element the Element to add
- */
- public void add(HolonElement element) {
- elements.add(element);
- element.parentObject = this;
- }
- /**
- * adds an Element to the Object.
- *
- * @param element the Element to add
- */
- public void add(Collection<HolonElement> elements) {
- for (HolonElement hE : elements) {
- hE.parentObject = this;
- }
- this.elements.addAll(elements);
- }
- /**
- * remove an Element to the Object.
- *
- * @param element the Element to add
- */
- public void remove(HolonElement element) {
- elements.remove(element);
- element.parentObject = null;
- }
- public void removeElement(int index) {
- HolonElement ele = elements.remove(index);
- ele.parentObject = null;
- }
- /**
- * 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() {
- return elements.stream().filter(element -> element.getActualEnergy() < 0)
- .max((lhs, rhs) -> Float.compare(lhs.getActualEnergy(), rhs.getActualEnergy())).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() {
- return elements.stream().filter(element -> element.getActualEnergy() < 0)
- .map(element -> -element.getActualEnergy()).min((lhs, rhs) -> Float.compare(lhs, rhs)).orElse(0.0f);
- }
- /**
- * This Method returns the biggest 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 biggest consuming HolonElement or 0.
- */
- public float getMaximumConsumingElementEnergy() {
- return elements.stream().filter(element -> element.getActualEnergy() < 0)
- .map(element -> -element.getActualEnergy()).max((lhs, rhs) -> Float.compare(lhs, rhs)).orElse(0.0f);
- }
- public float getMaximumProductionPossible() {
- return elements.stream().filter(element -> element.getEnergy() > 0).map(element -> element.getEnergy())
- .reduce(0.0f, Float::sum);
- }
- public float getMaximumConsumptionPossible() {
- return elements.stream().filter(element -> element.getEnergy() < 0).map(element -> -element.getEnergy())
- .reduce(0.0f, Float::sum);
- }
- /**
- * 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() {
- return elements.stream().filter(element -> element.getActualEnergy() > 0)
- .map(element -> element.getActualEnergy()).reduce(0.0f, Float::sum);
- }
- /**
- * 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() {
- return elements.stream().filter(element -> element.getActualEnergy() < 0)
- .map(element -> -element.getActualEnergy()).reduce(0.0f, Float::sum);
- }
- /**
- * 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() {
- return (int) elements.stream().filter(element -> element.getActualEnergy() < 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() {
- return (int) elements.stream().filter(element -> element.getActualEnergy() > 0).count();
- }
- public int getNumberOfActiveElements() {
- return (int) elements.stream().filter(ele -> ele.active).count();
- }
- public int getNumberOfInActiveElements() {
- return (int) elements.stream().filter(ele -> !ele.active).count();
- }
- public int getNumberOfElements() {
- return elements.size();
- }
- /*
- * STATE
- */
- private float actualEnergy;
- private HolonObjectState state;
- private float energyFromNetwork;
- private float minimumConsumingElementEnergy;
- private float energyNeededFromNetwork;
- private float energyFromConsumingElemnets;
- private float energySelfSupplied;
- /**
- * 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 void calculateEnergy(int timestep) {
- elements.forEach(ele -> ele.calculateState(timestep));
- actualEnergy = elements.stream().map(element -> element.getActualEnergy()).reduce(0.0f, Float::sum);
- }
- public float getActualEnergy() {
- return actualEnergy;
- }
- public enum HolonObjectState {
- NO_ENERGY, NOT_SUPPLIED, SUPPLIED, PRODUCER, PARTIALLY_SUPPLIED, OVER_SUPPLIED
- }
- public HolonObjectState getState() {
- return state;
- }
- public void setState(HolonObjectState state) {
- this.state = state;
- }
- public float getEnergyFromNetwork() {
- return energyFromNetwork;
- }
- // TODO(Tom2021-01-11): Should not exist hidden or package visibility
- public void setEnergyFromNetwork(float energyFromNetwork) {
- this.energyFromNetwork = energyFromNetwork;
- }
- public float getMinimumConsumingElementEnergyState() {
- return minimumConsumingElementEnergy;
- }
- // TODO(Tom2021-01-11): Should not exist hidden or package visibility
- public void setMinimumConsumingElementEnergy(float minimumConsumingElementEnergy) {
- this.minimumConsumingElementEnergy = minimumConsumingElementEnergy;
- }
- public float getEnergyNeededFromNetwork() {
- return energyNeededFromNetwork;
- }
- // TODO(Tom2021-01-11): Should not exist hidden or package visibility
- public void setEnergyNeededFromNetwork(float energyNeededFromNetwork) {
- this.energyNeededFromNetwork = energyNeededFromNetwork;
- }
- public float getEnergyFromConsumingElemnets() {
- return energyFromConsumingElemnets;
- }
- // TODO(Tom2021-01-11): Should not exist hidden or package visibility
- public void setEnergyFromConsumingElemnets(float energyFromConsumingElemnets) {
- this.energyFromConsumingElemnets = energyFromConsumingElemnets;
- }
- public float getEnergySelfSupplied() {
- return energySelfSupplied;
- }
- // TODO(Tom2021-01-11): Should not exist hidden or package visibility
- public void setEnergySelfSupplied(float energySelfSupplied) {
- this.energySelfSupplied = energySelfSupplied;
- }
- float getCurrentEnergy() {
- return energyFromNetwork-energyNeededFromNetwork;
- }
-
- public float getSupplyBarPercentage() {
- return (getEnergyFromConsumingElemnets() > 0.001)
- ? (getEnergyFromNetwork() + this.getEnergySelfSupplied()) / getEnergyFromConsumingElemnets()
- : 1.0f;
- }
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append("[HolonObject: ").append("id=").append(getId()).append(", name=").append(name).append(", state=")
- .append(state).append(", elements=[")
- .append(getElements().map(HolonElement::getName).collect(Collectors.joining(", "))).append("]]");
- return sb.toString();
- }
-
-
-
-
- }
|