HolonObject.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. package classes;
  2. import ui.controller.FlexManager;
  3. import java.util.ArrayList;
  4. /**
  5. * The class HolonObject represents any Object on the system which capability of
  6. * injecting or consuming energy on the network, for instance a house or a power
  7. * plant.
  8. *
  9. * @author Gruppe14
  10. */
  11. public class HolonObject extends AbstractCanvasObject {
  12. /* Array of all consumers */
  13. private ArrayList<HolonElement> elements = new ArrayList<HolonElement>();
  14. public Holon holon;
  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(AbstractCanvasObject obj) {
  30. super(obj);
  31. setElements(copyElements(((HolonObject) obj).getElements()));
  32. }
  33. /**
  34. * Getter for all Elements in the HolonObject.
  35. *
  36. * @return the elements ArrayList
  37. */
  38. public ArrayList<HolonElement> getElements() {
  39. return elements;
  40. }
  41. /**
  42. * Set a new ArrayList with HolonElements into the HolonObject.
  43. *
  44. * @param elements the elements to set
  45. */
  46. public void setElements(ArrayList<HolonElement> elements) {
  47. this.elements = elements;
  48. }
  49. /**
  50. * adds an Element to the Object.
  51. *
  52. * @param element the Element to add
  53. */
  54. public void addElement(HolonElement element) {
  55. elements.add(element);
  56. }
  57. /**
  58. * deletes Element at a given index.
  59. *
  60. * @param idx index
  61. */
  62. public void deleteElement(int idx) {
  63. elements.remove(idx);
  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. * Copy all Elements into a New Array.
  83. *
  84. * @param arr to copy
  85. * @return the copy of arr
  86. */
  87. public ArrayList<HolonElement> copyElements(ArrayList<HolonElement> arr) {
  88. ArrayList<HolonElement> newArr = new ArrayList<>();
  89. for (HolonElement t : arr) {
  90. newArr.add(new HolonElement(t));
  91. }
  92. return newArr;
  93. }
  94. /**
  95. * Search for the first element with the name.
  96. *
  97. * @param name name of the object to be searched
  98. * @return the searched HolonElement
  99. */
  100. public HolonElement searchElement(String name) {
  101. HolonElement ele = null;
  102. for (HolonElement e : getElements()) {
  103. if (e.getName().equals(name)) {
  104. ele = e;
  105. break;
  106. }
  107. }
  108. return ele;
  109. }
  110. /**
  111. * Search for the element with the id.
  112. *
  113. * @param id id of the element to be founded
  114. * @return the element
  115. */
  116. public HolonElement searchElementById(int id) {
  117. HolonElement ele = null;
  118. for (HolonElement e : getElements()) {
  119. if (e.getId() == id) {
  120. ele = e;
  121. break;
  122. }
  123. }
  124. return ele;
  125. }
  126. //New Methods:
  127. /**
  128. * This Method returns the smallest consuming HolonElement that is ACTIVE.
  129. * If the HolonObject has no Consumer its return null.
  130. * @param timestep is the TimeStep to compare the HolonElements.
  131. * @return The smallest consuming HolonElement or null.
  132. */
  133. public HolonElement getMinimumConsumingElement(int timestep){
  134. return getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) < 0) ).max((lhs,rhs) -> Float.compare(lhs.getEnergyAtTimeStep(timestep), rhs.getEnergyAtTimeStep(timestep))).orElse(null);
  135. }
  136. /**
  137. * This Method returns the smallest consuming HolonElement'Energy that is ACTIVE.
  138. * If the HolonObject has no Consumer its return 0.
  139. * @param timestep is the TimeStep to compare the HolonElements.
  140. * @return The smallest consuming HolonElement or 0.
  141. */
  142. public float getMinimumConsumingElementEnergy(int timestep){
  143. 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);
  144. }
  145. public float getMinimumConsumingElementEnergyWithFlex(int timestep, FlexManager flexManager){
  146. 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);
  147. }
  148. public float getMinimumConsumingElementEnergyFlex(int timeStep) {
  149. return getElements().stream()
  150. .filter(element -> element.isOn(this.holon.holonControlUnit.getFlexMan().getFlexManager(timeStep))
  151. && (element.getEnergyAtTimeStep(timeStep) < 0))
  152. .map(element -> -element.getEnergyAtTimeStep(timeStep))
  153. .min((lhs,rhs) ->Float.compare(lhs, rhs))
  154. .orElse(0.0f);
  155. }
  156. /**
  157. * This Method returns the biggest consuming HolonElement'Energy that is ACTIVE.
  158. * If the HolonObject has no Consumer its return 0.
  159. * @param timestep is the TimeStep to compare the HolonElements.
  160. * @return The biggest consuming HolonElement or 0.
  161. */
  162. public float getMaximumConsumingElementEnergy(int timestep){
  163. 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);
  164. }
  165. /**
  166. * This Method returns the Energy of a HolonObject. Its sums all Energies from the HolonElements of the HolonObject that are ACTIVE.
  167. * If the HolonObject have no HolonElement its return 0;
  168. * Is the returned Energy negative then the HolonObject need Energy because its consuming HolonElements need more Energy then the producing HolonElements.
  169. * Is the returned Energy positive its reversed.
  170. * @param timestep is the TimeStep to compare the HolonElements.
  171. * @return The Energy of the HolonObject.
  172. */
  173. public float getEnergyAtTimeStep(int timestep)
  174. {
  175. return getElements().stream().filter(element -> element.isActive()).map(element -> element.getEnergyAtTimeStep(timestep)).reduce(0.0f, (a, b) -> a + b);
  176. }
  177. public float getEnergyAtTimeStepWithFlex(int timestep, FlexManager flexManager)
  178. {
  179. return getElements().stream().filter(element -> element.isOn(flexManager)).map(element -> element.getEnergyAtTimeStep(timestep)).reduce(0.0f, (a, b) -> a + b);
  180. }
  181. public float getEnergyAtTimeStepFlex(int timeStep) {
  182. return getElements().stream()
  183. .filter(element -> element.isOn(this.holon.holonControlUnit.getFlexMan().getFlexManager(timeStep)))
  184. .map(element -> element.getEnergyAtTimeStep(timeStep))
  185. .reduce(0.0f, (a, b) -> a + b);
  186. }
  187. public float getMaximumProductionPossible(int timestep) {
  188. return elements.stream().filter(element -> element.getEnergyAtTimeStep(timestep) > 0).map(element -> element.getEnergyAtTimeStep(timestep)).reduce(0.0f, Float::sum);
  189. }
  190. public float getMaximumConsumptionPossible(int timestep) {
  191. return elements.stream().filter(element -> element.getEnergyAtTimeStep(timestep) < 0).map(element -> -element.getEnergyAtTimeStep(timestep)).reduce(0.0f, Float::sum);
  192. }
  193. /**
  194. * 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.
  195. * If the HolonObject have no HolonElement its return 0;
  196. * @param timestep is the TimeStep to compare the HolonElements.
  197. * @return The Energy of the producing HolonElements.
  198. */
  199. public float getEnergySelfProducingFromProducingElements(int timestep) {
  200. return getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) > 0)).map(element -> element.getEnergyAtTimeStep(timestep)).reduce(0.0f, (a, b) -> a + b);
  201. }
  202. public float getEnergySelfProducingFromProducingElementsWithFlex(int timestep, FlexManager flexManager) {
  203. return getElements().stream().filter(element -> element.isOn(flexManager) && (element.getEnergyAtTimeStep(timestep) > 0)).map(element -> element.getEnergyAtTimeStep(timestep)).reduce(0.0f, (a, b) -> a + b);
  204. }
  205. public float getEnergySelfProducingFromProducingElementsFlex(int timestep) {
  206. return getElements().stream()
  207. .filter(element -> element.isOn(this.holon.holonControlUnit.getFlexMan().getFlexManager(timestep))
  208. && (element.getEnergyAtTimeStep(timestep) > 0)).map(element -> element.getEnergyAtTimeStep(timestep))
  209. .reduce(0.0f, (a, b) -> a + b);
  210. }
  211. /**
  212. * 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.
  213. * If the HolonObject have no HolonElement its return 0;
  214. * @param timestep is the TimeStep to compare the HolonElements.
  215. * @return The Energy of the consuming HolonElements.
  216. */
  217. public float getEnergyNeededFromConsumingElements(int timestep) {
  218. return getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) < 0)).map(element -> -element.getEnergyAtTimeStep(timestep)).reduce(0.0f, (a, b) -> a + b);
  219. }
  220. public float getEnergyNeededFromConsumingElementsWithFlex(int timestep, FlexManager flexManager) {
  221. return getElements().stream().filter(element -> element.isOn(flexManager) && (element.getEnergyAtTimeStep(timestep) < 0)).map(element -> -element.getEnergyAtTimeStep(timestep)).reduce(0.0f, (a, b) -> a + b);
  222. }
  223. public float getEnergyNeededFromConsumingElementsFlex(int timestep) {
  224. return getElements().stream()
  225. .filter(element -> element.isOn(this.holon.holonControlUnit.getFlexMan().getFlexManager(timestep))
  226. && (element.getEnergyAtTimeStep(timestep) < 0)).map(element -> -element.getEnergyAtTimeStep(timestep))
  227. .reduce(0.0f, (a, b) -> a + b);
  228. }
  229. /**
  230. * This Method calculate the amount of HolonElements that are consuming Energy and are ACTIVE.
  231. * @param timestep is the TimeStep to compare the HolonElements.
  232. * @return The amount of HolonElements that are consuming Energy.
  233. */
  234. public int countConsumingElements(int timestep) {
  235. return (int) getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) < 0)).count();
  236. }
  237. /**
  238. * This Method calculate the amount of HolonElements that are producing Energy and are ACTIVE.
  239. * @param timestep is the TimeStep to compare the HolonElements.
  240. * @return The amount of HolonElements that are producing Energy.
  241. */
  242. public int countProducingElements(int timestep) {
  243. return (int) getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) > 0)).count();
  244. }
  245. public String toString() {
  246. StringBuilder sb = new StringBuilder();
  247. sb.append("[HolonObject: ");
  248. sb.append("id=").append(id)
  249. .append(", name=").append(name)
  250. .append(", state=");
  251. sb.append(", elements=[");
  252. for (int i = 0; i < getElements().size(); i++) {
  253. HolonElement el = getElements().get(i);
  254. if (i != 0) {
  255. sb.append(", ");
  256. }
  257. sb.append(el.getName());
  258. }
  259. sb.append("]]");
  260. return sb.toString();
  261. }
  262. @Override
  263. public HolonObject makeCopy(){
  264. return new HolonObject(this);
  265. }
  266. public int getNumberOfActiveElements() {
  267. return (int) elements.stream().filter(ele -> ele.isActive()).count();
  268. }
  269. public int getNumberOfInActiveElements() {
  270. return (int) elements.stream().filter(ele -> !ele.isActive()).count();
  271. }
  272. public int getNumberOfElements() {
  273. return (int) elements.stream().count();
  274. }
  275. }