HolonObject.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package model;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. /**
  5. * The class HolonObject represents any Object on the system which capability of
  6. * injecting or consuming energy on the network, for instance a house or a power
  7. * plant.
  8. *
  9. * @author Gruppe14
  10. */
  11. public class HolonObject extends AbstractCanvasObject {
  12. /* Array of all consumers */
  13. private List<HolonElement> elements = new ArrayList<HolonElement>();
  14. /**
  15. * Constructor Set by default the name of the object equals to the category
  16. * name, until the user changes it.
  17. *
  18. * @param objName name of the Object
  19. */
  20. public HolonObject(String objName) {
  21. super(objName);
  22. }
  23. /**
  24. * Contructor of a copy of an Object.
  25. *
  26. * @param obj object to be copied
  27. */
  28. public HolonObject(HolonObject obj) {
  29. super(obj);
  30. for(HolonElement ele : obj.elements) {
  31. this.addElement(new HolonElement(ele));
  32. }
  33. }
  34. @Override
  35. public void initForReflection() {
  36. super.initForReflection();
  37. elements = new ArrayList<HolonElement>();
  38. }
  39. /**
  40. * Getter for all Elements in the HolonObject.
  41. *
  42. * @return the elements ArrayList
  43. */
  44. public List<HolonElement> getElements() {
  45. return elements;
  46. }
  47. public void setElements(List<HolonElement> list) {
  48. this.elements = list;
  49. }
  50. /**
  51. * adds an Element to the Object.
  52. *
  53. * @param element the Element to add
  54. */
  55. public void addElement(HolonElement element) {
  56. elements.add(element);
  57. element.parentObject = this;
  58. }
  59. /**
  60. * remove an Element to the Object.
  61. *
  62. * @param element the Element to add
  63. */
  64. public void removeElement(HolonElement element) {
  65. elements.remove(element);
  66. element.parentObject = null;
  67. }
  68. public void removeElement(int index) {
  69. HolonElement ele = elements.remove(index);
  70. ele.parentObject = null;
  71. }
  72. /*
  73. * STATE
  74. */
  75. private float actualEnergy;
  76. /**
  77. * This Method returns the Energy of a HolonObject. Its sums all Energies from the HolonElements of the HolonObject that are ACTIVE.
  78. * If the HolonObject have no HolonElement its return 0;
  79. * Is the returned Energy negative then the HolonObject need Energy because its consuming HolonElements need more Energy then the producing HolonElements.
  80. * Is the returned Energy positive its reversed.
  81. * @param timestep is the TimeStep to compare the HolonElements.
  82. * @return The Energy of the HolonObject.
  83. */
  84. public void calculateState(int timestep)
  85. {
  86. elements.forEach(ele -> ele.calculateState(timestep));
  87. actualEnergy = elements.stream().map(element -> element.getActualEnergy()).reduce(0.0f, Float::sum);
  88. }
  89. public float getActualEnergy()
  90. {
  91. return actualEnergy;
  92. }
  93. //New Methods:
  94. /**
  95. * This Method returns the smallest consuming HolonElement that is ACTIVE.
  96. * If the HolonObject has no Consumer its return null.
  97. * @param timestep is the TimeStep to compare the HolonElements.
  98. * @return The smallest consuming HolonElement or null.
  99. */
  100. public HolonElement getMinimumConsumingElement(){
  101. return elements.stream().filter(element -> element.getActualEnergy() < 0).max((lhs,rhs) -> Float.compare(lhs.getActualEnergy(), rhs.getActualEnergy())).orElse(null);
  102. }
  103. /**
  104. * This Method returns the smallest consuming HolonElement'Energy that is ACTIVE.
  105. * If the HolonObject has no Consumer its return 0.
  106. * @param timestep is the TimeStep to compare the HolonElements.
  107. * @return The smallest consuming HolonElement or 0.
  108. */
  109. public float getMinimumConsumingElementEnergy(){
  110. return elements.stream().filter(element -> element.getActualEnergy() < 0 ).map(element -> -element.getActualEnergy()).min((lhs,rhs) ->Float.compare(lhs, rhs)).orElse(0.0f);
  111. }
  112. /**
  113. * This Method returns the biggest consuming HolonElement'Energy that is ACTIVE.
  114. * If the HolonObject has no Consumer its return 0.
  115. * @param timestep is the TimeStep to compare the HolonElements.
  116. * @return The biggest consuming HolonElement or 0.
  117. */
  118. public float getMaximumConsumingElementEnergy(){
  119. return elements.stream().filter(element -> element.getActualEnergy() < 0 ).map(element -> -element.getActualEnergy()).max((lhs,rhs) ->Float.compare(lhs, rhs)).orElse(0.0f);
  120. }
  121. public float getMaximumProductionPossible() {
  122. return elements.stream().filter(element -> element.getEnergy() > 0).map(element -> element.getEnergy()).reduce(0.0f, Float::sum);
  123. }
  124. public float getMaximumConsumptionPossible() {
  125. return elements.stream().filter(element -> element.getEnergy() < 0).map(element -> -element.getEnergy()).reduce(0.0f, Float::sum);
  126. }
  127. /**
  128. * 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.
  129. * If the HolonObject have no HolonElement its return 0;
  130. * @param timestep is the TimeStep to compare the HolonElements.
  131. * @return The Energy of the producing HolonElements.
  132. */
  133. public float getEnergySelfProducingFromProducingElements() {
  134. return elements.stream().filter(element -> element.getActualEnergy() > 0).map(element -> element.getActualEnergy()).reduce(0.0f, Float::sum);
  135. }
  136. /**
  137. * 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.
  138. * If the HolonObject have no HolonElement its return 0;
  139. * @param timestep is the TimeStep to compare the HolonElements.
  140. * @return The Energy of the consuming HolonElements.
  141. */
  142. public float getEnergyNeededFromConsumingElements() {
  143. return elements.stream().filter(element -> element.getActualEnergy() < 0).map(element -> -element.getActualEnergy()).reduce(0.0f, Float::sum);
  144. }
  145. /**
  146. * This Method calculate the amount of HolonElements that are consuming Energy and are ACTIVE.
  147. * @param timestep is the TimeStep to compare the HolonElements.
  148. * @return The amount of HolonElements that are consuming Energy.
  149. */
  150. public int countConsumingElements() {
  151. return (int) elements.stream().filter(element -> element.getActualEnergy() < 0).count();
  152. }
  153. /**
  154. * This Method calculate the amount of HolonElements that are producing Energy and are ACTIVE.
  155. * @param timestep is the TimeStep to compare the HolonElements.
  156. * @return The amount of HolonElements that are producing Energy.
  157. */
  158. public int countProducingElements() {
  159. return (int) elements.stream().filter(element -> element.getActualEnergy() > 0).count();
  160. }
  161. public String toString() {
  162. StringBuilder sb = new StringBuilder();
  163. sb.append("[HolonObject: ");
  164. sb.append("id=").append(getId())
  165. .append(", name=").append(name)
  166. .append(", state=");
  167. sb.append(", elements=[");
  168. for (int i = 0; i < elements.size(); i++) {
  169. HolonElement el = elements.get(i);
  170. if (i != 0) {
  171. sb.append(", ");
  172. }
  173. sb.append(el.getName());
  174. }
  175. sb.append("]]");
  176. return sb.toString();
  177. }
  178. public int getNumberOfActiveElements() {
  179. return (int) elements.stream().filter(ele -> ele.active).count();
  180. }
  181. public int getNumberOfInActiveElements() {
  182. return (int) elements.stream().filter(ele -> !ele.active).count();
  183. }
  184. public int getNumberOfElements() {
  185. return elements.size();
  186. }
  187. }