HolonObject.java 8.2 KB

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