HolonObject.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package holeg.model;
  2. import holeg.serialize.PostDeserialize;
  3. import java.util.Collection;
  4. import java.util.HashSet;
  5. import java.util.Optional;
  6. import java.util.Set;
  7. import java.util.stream.Collectors;
  8. import java.util.stream.Stream;
  9. /**
  10. * The class HolonObject represents any Object on the system which capability of injecting or
  11. * consuming energy on the network, for instance a house or a power plant.
  12. *
  13. * @author Gruppe14
  14. */
  15. public class HolonObject extends AbstractCanvasObject implements PostDeserialize {
  16. /* Array of all consumers */
  17. private final Set<HolonElement> elements = new HashSet<>();
  18. //Calculated values
  19. private transient HolonObjectState state;
  20. private transient float actualEnergy;
  21. private transient float energyFromHolon;
  22. private transient float energyToHolon;
  23. private transient float minimumConsumingElementEnergy;
  24. private transient float energyNeededFromHolon;
  25. private transient float consumption;
  26. private transient float production;
  27. /**
  28. * Constructor Set by default the name of the object equals to the category name, until the user
  29. * changes it.
  30. *
  31. * @param objName name of the Object
  32. */
  33. public HolonObject(String objName) {
  34. super(objName);
  35. }
  36. /**
  37. * Contructor of a copy of an Object.
  38. *
  39. * @param obj object to be copied
  40. */
  41. public HolonObject(HolonObject obj) {
  42. super(obj);
  43. for (HolonElement ele : obj.elements) {
  44. this.add(new HolonElement(ele));
  45. }
  46. }
  47. @Override
  48. public AbstractCanvasObject copy() {
  49. return new HolonObject(this);
  50. }
  51. /**
  52. * Getter for all Elements in the HolonObject.
  53. *
  54. * @return the elements ArrayList
  55. */
  56. public Stream<HolonElement> elementsStream() {
  57. return elements.stream();
  58. }
  59. public void clearElements() {
  60. elements.clear();
  61. }
  62. /**
  63. * adds an Element to the Object.
  64. *
  65. * @param element the Element to add
  66. */
  67. public void add(HolonElement element) {
  68. elements.add(element);
  69. element.parentObject = this;
  70. }
  71. public void add(Collection<HolonElement> elements) {
  72. for (HolonElement hE : elements) {
  73. hE.parentObject = this;
  74. }
  75. this.elements.addAll(elements);
  76. }
  77. /**
  78. * remove an Element to the Object.
  79. *
  80. * @param element the Element to add
  81. */
  82. public void remove(HolonElement element) {
  83. elements.remove(element);
  84. element.parentObject = null;
  85. }
  86. /**
  87. * This Method returns the smallest consuming HolonElement that is ACTIVE. If the HolonObject has
  88. * no Consumer its return null.
  89. *
  90. * @return The smallest consuming HolonElement or null.
  91. */
  92. public Optional<HolonElement> getMinimumConsumingElement() {
  93. return elements.stream().filter(element -> element.getActualEnergy() < 0)
  94. .max((lhs, rhs) -> Float.compare(lhs.getActualEnergy(), rhs.getActualEnergy()));
  95. }
  96. /**
  97. * This Method returns the smallest consuming HolonElement'Energy that is ACTIVE. If the
  98. * HolonObject has no Consumer its return 0.
  99. *
  100. * @return The smallest consuming HolonElement or 0.
  101. */
  102. public float getMinimumConsumingElementEnergy() {
  103. return minimumConsumingElementEnergy;
  104. }
  105. /**
  106. * This Method returns the biggest consuming HolonElement'Energy that is ACTIVE. If the
  107. * HolonObject has no Consumer its return 0.
  108. *
  109. * @return The biggest consuming HolonElement or 0.
  110. */
  111. public float getMaximumConsumingElementEnergy() {
  112. return elements.stream().filter(element -> element.getActualEnergy() < 0)
  113. .map(element -> -element.getActualEnergy()).max(Float::compare).orElse(0.0f);
  114. }
  115. public float getMaximumProductionPossible() {
  116. return elements.stream().map(HolonElement::getEnergy).filter(energy -> energy > 0)
  117. .reduce(0.0f, Float::sum);
  118. }
  119. public float getMaximumConsumptionPossible() {
  120. return elements.stream().filter(element -> element.getEnergy() < 0)
  121. .map(element -> -element.getEnergy()).reduce(0.0f, Float::sum);
  122. }
  123. /**
  124. * This Method returns the Energy of all HolonElements from the HolonObject that are consuming.
  125. * Its sums all Energies from the HolonElements of the HolonObject that are ACTIVE and are
  126. * Consumer. If the HolonObject have no HolonElement its return 0;
  127. *
  128. * @return The Energy of the consuming HolonElements.
  129. */
  130. public float getEnergyNeededFromConsumingElements() {
  131. return consumption;
  132. }
  133. /**
  134. * This Method calculate the amount of HolonElements that are consuming Energy and are ACTIVE.
  135. *
  136. * @return The amount of HolonElements that are consuming Energy.
  137. */
  138. public int countConsumingElements() {
  139. return (int) elements.stream().filter(element -> element.getActualEnergy() < 0).count();
  140. }
  141. /**
  142. * This Method calculate the amount of HolonElements that are producing Energy and are ACTIVE.
  143. *
  144. * @return The amount of HolonElements that are producing Energy.
  145. */
  146. public int countProducingElements() {
  147. return (int) elements.stream().filter(element -> element.getActualEnergy() > 0).count();
  148. }
  149. public int getNumberOfActiveElements() {
  150. return (int) elements.stream().filter(ele -> ele.active).count();
  151. }
  152. public int getNumberOfInActiveElements() {
  153. return (int) elements.stream().filter(ele -> !ele.active).count();
  154. }
  155. public int getNumberOfElements() {
  156. return elements.size();
  157. }
  158. /**
  159. * This Method returns the Energy of a HolonObject. Its sums all Energies from the HolonElements
  160. * of the HolonObject that are ACTIVE. If the HolonObject have no HolonElement its return 0; Is
  161. * the returned Energy negative then the HolonObject need Energy because its consuming
  162. * HolonElements need more Energy then the producing HolonElements. Is the returned Energy
  163. * positive its reversed.
  164. *
  165. * @param timeStep is the TimeStep to compare the HolonElements.
  166. */
  167. public void calculateEnergy(int timeStep) {
  168. elements.forEach(ele -> ele.calculateState(timeStep));
  169. actualEnergy = elements.stream().map(HolonElement::getActualEnergy).reduce(0f, Float::sum);
  170. minimumConsumingElementEnergy = elements.stream()
  171. .filter(element -> element.getActualEnergy() < 0).map(element -> -element.getActualEnergy())
  172. .min(Float::compare).orElse(0.0f);
  173. energyNeededFromHolon = (actualEnergy >= 0) ? 0 : -actualEnergy;
  174. consumption = elements.stream().filter(element -> element.getActualEnergy() < 0)
  175. .map(element -> -element.getActualEnergy()).reduce(0.0f, Float::sum);
  176. production = elements.stream().map(HolonElement::getActualEnergy).filter(energy -> energy > 0)
  177. .reduce(0.0f, Float::sum);
  178. energyToHolon = energyFromHolon = 0;
  179. }
  180. public float getActualEnergy() {
  181. return actualEnergy;
  182. }
  183. public HolonObjectState getState() {
  184. return state;
  185. }
  186. public boolean isProducer() {
  187. return getProduction() >= getConsumption();
  188. }
  189. public boolean isConsumer() {
  190. return getConsumption() >= getProduction();
  191. }
  192. public float getEnergyFromHolon() {
  193. return energyFromHolon;
  194. }
  195. void setEnergyFromHolon(float energyFromHolon) {
  196. this.energyFromHolon = energyFromHolon;
  197. }
  198. public float getEnergyNeededFromHolon() {
  199. return energyNeededFromHolon;
  200. }
  201. public float getConsumption() {
  202. return consumption;
  203. }
  204. public float getProduction() {
  205. return production;
  206. }
  207. public float getCurrentEnergy() {
  208. return energyFromHolon - energyNeededFromHolon;
  209. }
  210. public float getSupplyBarPercentage() {
  211. return (consumption > 0.001) ? (energyFromHolon + production) / consumption : 1.0f;
  212. }
  213. public String toString() {
  214. return "[HolonObject: " + "id=" + getId() + ", name=" + name + ", state=" + state + ", pos="
  215. + position + ", elements=[" + elementsStream().map(HolonElement::getName)
  216. .collect(Collectors.joining(", ")) + "]]";
  217. }
  218. public float getEnergyToHolon() {
  219. return energyToHolon;
  220. }
  221. void setEnergyToHolon(float energyToHolon) {
  222. this.energyToHolon = energyToHolon;
  223. }
  224. public void calculateState() {
  225. if (actualEnergy > 0) {
  226. state = HolonObjectState.PRODUCER;
  227. } else if (elements.isEmpty() || consumption == 0 && production == 0) {
  228. state = HolonObjectState.NO_ENERGY;
  229. } else if (production + energyFromHolon > consumption) {
  230. state = (HolonObjectState.OVER_SUPPLIED);
  231. } else if (production + energyFromHolon == consumption) {
  232. state = (HolonObjectState.SUPPLIED);
  233. } else if (production + energyFromHolon >= minimumConsumingElementEnergy) {
  234. state = (HolonObjectState.PARTIALLY_SUPPLIED);
  235. } else {
  236. state = (HolonObjectState.NOT_SUPPLIED);
  237. }
  238. }
  239. @Override
  240. public void postDeserialize() {
  241. elements.forEach(ele -> ele.parentObject = this);
  242. }
  243. public enum HolonObjectState {
  244. NO_ENERGY, NOT_SUPPLIED, SUPPLIED, PRODUCER, PARTIALLY_SUPPLIED, OVER_SUPPLIED
  245. }
  246. }