HolonObject.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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;
  14. /* Array for tracking Production */
  15. private float[] trackingProd;
  16. /* Array for tracking Consumption */
  17. private float[] trackingCons;
  18. /* Total Flexibility */
  19. private float totalFlex;
  20. /**
  21. * Constructor Set by default the name of the object equals to the category
  22. * name, until the user changes it.
  23. *
  24. * @param objName name of the Object
  25. */
  26. public HolonObject(String objName) {
  27. super(objName);
  28. setElements(new ArrayList<>());
  29. setTrackingProd(new float[100]);
  30. setTrackingCons(new float[100]);
  31. }
  32. /**
  33. * Contructor of a copy of an Object.
  34. *
  35. * @param obj object to be copied
  36. */
  37. public HolonObject(AbstractCanvasObject obj) {
  38. super(obj);
  39. setElements(copyElements(((HolonObject) obj).getElements()));
  40. setTrackingProd(new float[100]);
  41. setTrackingCons(new float[100]);
  42. }
  43. /**
  44. * Getter for all Elements in the HolonObject.
  45. *
  46. * @return the elements ArrayList
  47. */
  48. public ArrayList<HolonElement> getElements() {
  49. return elements;
  50. }
  51. /**
  52. * Set a new ArrayList with HolonElements into the HolonObject.
  53. *
  54. * @param elements the elements to set
  55. */
  56. public void setElements(ArrayList<HolonElement> elements) {
  57. this.elements = elements;
  58. }
  59. /**
  60. * adds an Element to the Object.
  61. *
  62. * @param element the Element to add
  63. */
  64. public void addElement(HolonElement element) {
  65. elements.add(element);
  66. }
  67. /**
  68. * deletes Element at a given index.
  69. *
  70. * @param idx index
  71. */
  72. public void deleteElement(int idx) {
  73. elements.remove(idx);
  74. }
  75. /**
  76. * String of all consumers in this HolonObject.
  77. *
  78. * @return all the names of this HolonObject separated by "," each object
  79. */
  80. public String toStringElements() {
  81. String objString = "Empty";
  82. for (HolonElement e : elements) {
  83. if (objString == "Empty") {
  84. objString = e.getEleName();
  85. } else {
  86. objString = objString + ", " + e.getEleName();
  87. }
  88. }
  89. return objString;
  90. }
  91. /**
  92. * Copy all Elements into a New Array.
  93. *
  94. * @param arr to copy
  95. * @return the copy of arr
  96. */
  97. public ArrayList<HolonElement> copyElements(ArrayList<HolonElement> arr) {
  98. ArrayList<HolonElement> newArr = new ArrayList<>();
  99. for (HolonElement t : arr) {
  100. newArr.add(new HolonElement(t));
  101. }
  102. return newArr;
  103. }
  104. /**
  105. * Search for the first element with the name.
  106. *
  107. * @param name name of the object to be searched
  108. * @return the searched HolonElement
  109. */
  110. public HolonElement searchElement(String name) {
  111. HolonElement ele = null;
  112. for (HolonElement e : getElements()) {
  113. if (e.getEleName().equals(name)) {
  114. ele = e;
  115. break;
  116. }
  117. }
  118. return ele;
  119. }
  120. /**
  121. * Search for the element with the id.
  122. *
  123. * @param id id of the element to be founded
  124. * @return the element
  125. */
  126. public HolonElement searchElementById(int id) {
  127. HolonElement ele = null;
  128. for (HolonElement e : getElements()) {
  129. if (e.getId() == id) {
  130. ele = e;
  131. break;
  132. }
  133. }
  134. return ele;
  135. }
  136. //New Methods:
  137. /**
  138. * This Method returns the smallest consuming HolonElement that is ACTIVE.
  139. * If the HolonObject has no Consumer its return null.
  140. * @param timestep is the TimeStep to compare the HolonElements.
  141. * @return The smallest consuming HolonElement or null.
  142. */
  143. public HolonElement getMinimumConsumingElement(int timestep){
  144. return getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) < 0) ).max((lhs,rhs) -> Float.compare(lhs.getEnergyAtTimeStep(timestep), rhs.getEnergyAtTimeStep(timestep))).orElse(null);
  145. }
  146. /**
  147. * This Method returns the smallest consuming HolonElement'Energy that is ACTIVE.
  148. * If the HolonObject has no Consumer its return 0.
  149. * @param timestep is the TimeStep to compare the HolonElements.
  150. * @return The smallest consuming HolonElement or 0.
  151. */
  152. public float getMinimumConsumingElementEnergy(int timestep){
  153. 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);
  154. }
  155. public float getMinimumConsumingElementEnergyWithFlex(int timestep, FlexManager flexManager){
  156. 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);
  157. }
  158. /**
  159. * This Method returns the biggest consuming HolonElement'Energy that is ACTIVE.
  160. * If the HolonObject has no Consumer its return 0.
  161. * @param timestep is the TimeStep to compare the HolonElements.
  162. * @return The biggest consuming HolonElement or 0.
  163. */
  164. public float getMaximumConsumingElementEnergy(int timestep){
  165. 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);
  166. }
  167. /**
  168. * This Method returns the Energy of a HolonObject. Its sums all Energies from the HolonElements of the HolonObject that are ACTIVE.
  169. * If the HolonObject have no HolonElement its return 0;
  170. * Is the returned Energy negative then the HolonObject need Energy because its consuming HolonElements need more Energy then the producing HolonElements.
  171. * Is the returned Energy positive its reversed.
  172. * @param timestep is the TimeStep to compare the HolonElements.
  173. * @return The Energy of the HolonObject.
  174. */
  175. public float getEnergyAtTimeStep(int timestep)
  176. {
  177. return getElements().stream().filter(element -> element.isActive()).map(element -> element.getEnergyAtTimeStep(timestep)).reduce(0.0f, (a, b) -> a + b);
  178. }
  179. public float getEnergyAtTimeStepWithFlex(int timestep, FlexManager flexManager)
  180. {
  181. return getElements().stream().filter(element -> element.isOn(flexManager)).map(element -> element.getEnergyAtTimeStep(timestep)).reduce(0.0f, (a, b) -> a + b);
  182. }
  183. public float getMaximumProductionPossible(int timestep) {
  184. return elements.stream().filter(element -> element.getEnergyAtTimeStep(timestep) > 0).map(element -> element.getEnergyAtTimeStep(timestep)).reduce(0.0f, Float::sum);
  185. }
  186. public float getMaximumConsumptionPossible(int timestep) {
  187. return elements.stream().filter(element -> element.getEnergyAtTimeStep(timestep) < 0).map(element -> -element.getEnergyAtTimeStep(timestep)).reduce(0.0f, Float::sum);
  188. }
  189. /**
  190. * 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.
  191. * If the HolonObject have no HolonElement its return 0;
  192. * @param timestep is the TimeStep to compare the HolonElements.
  193. * @return The Energy of the producing HolonElements.
  194. */
  195. public float getEnergySelfProducingFromProducingElements(int timestep) {
  196. return getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) > 0)).map(element -> element.getEnergyAtTimeStep(timestep)).reduce(0.0f, (a, b) -> a + b);
  197. }
  198. public float getEnergySelfProducingFromProducingElementsWithFlex(int timestep, FlexManager flexManager) {
  199. return getElements().stream().filter(element -> element.isOn(flexManager) && (element.getEnergyAtTimeStep(timestep) > 0)).map(element -> element.getEnergyAtTimeStep(timestep)).reduce(0.0f, (a, b) -> a + b);
  200. }
  201. /**
  202. * 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.
  203. * If the HolonObject have no HolonElement its return 0;
  204. * @param timestep is the TimeStep to compare the HolonElements.
  205. * @return The Energy of the consuming HolonElements.
  206. */
  207. public float getEnergyNeededFromConsumingElements(int timestep) {
  208. return getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) < 0)).map(element -> -element.getEnergyAtTimeStep(timestep)).reduce(0.0f, (a, b) -> a + b);
  209. }
  210. public float getEnergyNeededFromConsumingElementsWithFlex(int timestep, FlexManager flexManager) {
  211. return getElements().stream().filter(element -> element.isOn(flexManager) && (element.getEnergyAtTimeStep(timestep) < 0)).map(element -> -element.getEnergyAtTimeStep(timestep)).reduce(0.0f, (a, b) -> a + b);
  212. }
  213. /**
  214. * This Method calculate the amount of HolonElements that are consuming Energy and are ACTIVE.
  215. * @param timestep is the TimeStep to compare the HolonElements.
  216. * @return The amount of HolonElements that are consuming Energy.
  217. */
  218. public int countConsumingElements(int timestep) {
  219. return (int) getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) < 0)).count();
  220. }
  221. /**
  222. * This Method calculate the amount of HolonElements that are producing Energy and are ACTIVE.
  223. * @param timestep is the TimeStep to compare the HolonElements.
  224. * @return The amount of HolonElements that are producing Energy.
  225. */
  226. public int countProducingElements(int timestep) {
  227. return (int) getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) > 0)).count();
  228. }
  229. /**
  230. * Get the Array Production
  231. */
  232. public float[] getTrackingProd() {
  233. return this.trackingProd;
  234. }
  235. /**
  236. * Set the Array Production
  237. */
  238. public void setTrackingProd(float[] arr) {
  239. this.trackingProd = arr;
  240. }
  241. /**
  242. * Get the Array Consumption
  243. */
  244. public float[] getTrackingCons() {
  245. return this.trackingCons;
  246. }
  247. /**
  248. * Set the Array Consumption
  249. */
  250. public void setTrackingCons(float[] arr) {
  251. this.trackingCons = arr;
  252. }
  253. /**
  254. * Get the Array Consumption
  255. */
  256. public float getTotalFlex() {
  257. return totalFlex;
  258. }
  259. /**
  260. * Set the Array Consumption
  261. */
  262. public void setTotalFlex(float totalFlex) {
  263. this.totalFlex = totalFlex;
  264. }
  265. /**
  266. * If the user track any HolonObject the tracking information will be
  267. * updated. (If the HolonObject enters into the untracked state, the array
  268. * will be reseted)
  269. */
  270. public void updateTrackingInfo() {
  271. float[] tempProd = new float[100];
  272. float[] tempCons = new float[100];
  273. for (int i = 0; i < 100; i++) {
  274. float valueProd = 0;
  275. float valueCons = 0;
  276. for (HolonElement e : getElements()) {
  277. if (e.isActive() && e.isProducer()) {
  278. valueProd += e.getOverallEnergyAtTimeStep(i);
  279. }
  280. if (e.isActive() && e.isConsumer()) {
  281. valueCons += e.getOverallEnergyAtTimeStep(i);
  282. }
  283. }
  284. tempProd[i] = valueProd;
  285. tempCons[i] = valueCons;
  286. }
  287. this.trackingProd = tempProd;
  288. this.trackingCons = tempCons;
  289. }
  290. public String toString() {
  291. StringBuilder sb = new StringBuilder();
  292. sb.append("[HolonObject: ");
  293. sb.append("id=").append(id)
  294. .append(", name=").append(name)
  295. .append(", state=");
  296. sb.append(", elements=[");
  297. for (int i = 0; i < getElements().size(); i++) {
  298. HolonElement el = getElements().get(i);
  299. if (i != 0) {
  300. sb.append(", ");
  301. }
  302. sb.append(el.getEleName());
  303. }
  304. sb.append("]]");
  305. return sb.toString();
  306. }
  307. @Override
  308. public HolonObject makeCopy(){
  309. return new HolonObject(this);
  310. }
  311. public int getNumberOfActiveElements() {
  312. return (int) elements.stream().filter(ele -> ele.isActive()).count();
  313. }
  314. public int getNumberOfInActiveElements() {
  315. return (int) elements.stream().filter(ele -> !ele.isActive()).count();
  316. }
  317. public int getNumberOfElements() {
  318. return (int) elements.stream().count();
  319. }
  320. }