123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- package model;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * 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.addElement(new HolonElement(ele));
- }
- }
- @Override
- public void initForReflection() {
- super.initForReflection();
- elements = new ArrayList<HolonElement>();
- }
-
-
-
- /**
- * Getter for all Elements in the HolonObject.
- *
- * @return the elements ArrayList
- */
- public List<HolonElement> getElements() {
- return elements;
- }
-
- public void setElements(List<HolonElement> list) {
- this.elements = list;
- }
-
-
-
- /**
- * adds an Element to the Object.
- *
- * @param element the Element to add
- */
- public void addElement(HolonElement element) {
- elements.add(element);
- element.parentObject = this;
- }
-
- /**
- * remove an Element to the Object.
- *
- * @param element the Element to add
- */
- public void removeElement(HolonElement element) {
- elements.remove(element);
- element.parentObject = null;
- }
- public void removeElement(int index) {
- HolonElement ele = elements.remove(index);
- ele.parentObject = null;
- }
-
- /*
- * STATE
- */
-
-
- private float actualEnergy;
-
-
- /**
- * 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 calculateState(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;
- }
-
-
-
- //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(){
- 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 String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append("[HolonObject: ");
- sb.append("id=").append(getId())
- .append(", name=").append(name)
- .append(", state=");
- sb.append(", elements=[");
- for (int i = 0; i < elements.size(); i++) {
- HolonElement el = elements.get(i);
- if (i != 0) {
- sb.append(", ");
- }
- sb.append(el.getName());
- }
- sb.append("]]");
- return sb.toString();
- }
-
- 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();
- }
-
- }
|