HolonObject.java 9.1 KB

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