HolonObject.java 11 KB

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