HolonObject.java 9.7 KB

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