HolonObject.java 12 KB

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