HolonObject.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. package classes;
  2. import com.google.gson.annotations.Expose;
  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 AbstractCpsObject {
  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(AbstractCpsObject 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. /**
  156. * This Method returns the biggest consuming HolonElement'Energy that is ACTIVE.
  157. * If the HolonObject has no Consumer its return 0.
  158. * @param timestep is the TimeStep to compare the HolonElements.
  159. * @return The biggest consuming HolonElement or 0.
  160. */
  161. public float getMaximumConsumingElementEnergy(int timestep){
  162. 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);
  163. }
  164. /**
  165. * This Method returns the Energy of a HolonObject. Its sums all Energies from the HolonElements of the HolonObject that are ACTIVE.
  166. * If the HolonObject have no HolonElement its return 0;
  167. * Is the returned Energy negative then the HolonObject need Energy because its consuming HolonElements need more Energy then the producing HolonElements.
  168. * Is the returned Energy positive its reversed.
  169. * @param timestep is the TimeStep to compare the HolonElements.
  170. * @return The Energy of the HolonObject.
  171. */
  172. public float getEnergyAtTimeStep(int timestep)
  173. {
  174. return getElements().stream().filter(element -> element.isActive()).map(element -> element.getEnergyAtTimeStep(timestep)).reduce(0.0f, (a, b) -> a + b);
  175. }
  176. /**
  177. * 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.
  178. * If the HolonObject have no HolonElement its return 0;
  179. * @param timestep is the TimeStep to compare the HolonElements.
  180. * @return The Energy of the producing HolonElements.
  181. */
  182. public float getEnergySelfProducingFromProducingElements(int timestep) {
  183. return getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) > 0)).map(element -> element.getEnergyAtTimeStep(timestep)).reduce(0.0f, (a, b) -> a + b);
  184. }
  185. /**
  186. * 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.
  187. * If the HolonObject have no HolonElement its return 0;
  188. * @param timestep is the TimeStep to compare the HolonElements.
  189. * @return The Energy of the consuming HolonElements.
  190. */
  191. public float getEnergyNeededFromConsumingElements(int timestep) {
  192. return getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) < 0)).map(element -> -element.getEnergyAtTimeStep(timestep)).reduce(0.0f, (a, b) -> a + b);
  193. }
  194. /**
  195. * This Method calculate the amount of HolonElements that are consuming Energy and are ACTIVE.
  196. * @param timestep is the TimeStep to compare the HolonElements.
  197. * @return The amount of HolonElements that are consuming Energy.
  198. */
  199. public int countConsumingElements(int timestep) {
  200. return (int) getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) < 0)).count();
  201. }
  202. /**
  203. * This Method calculate the amount of HolonElements that are producing Energy and are ACTIVE.
  204. * @param timestep is the TimeStep to compare the HolonElements.
  205. * @return The amount of HolonElements that are producing Energy.
  206. */
  207. public int countProducingElements(int timestep) {
  208. return (int) getElements().stream().filter(element -> element.isActive() && (element.getEnergyAtTimeStep(timestep) > 0)).count();
  209. }
  210. /**
  211. * Get the Array Production
  212. */
  213. public float[] getTrackingProd() {
  214. return this.trackingProd;
  215. }
  216. /**
  217. * Set the Array Production
  218. */
  219. public void setTrackingProd(float[] arr) {
  220. this.trackingProd = arr;
  221. }
  222. /**
  223. * Get the Array Consumption
  224. */
  225. public float[] getTrackingCons() {
  226. return this.trackingCons;
  227. }
  228. /**
  229. * Set the Array Consumption
  230. */
  231. public void setTrackingCons(float[] arr) {
  232. this.trackingCons = arr;
  233. }
  234. /**
  235. * Get the Array Consumption
  236. */
  237. public float getTotalFlex() {
  238. return totalFlex;
  239. }
  240. /**
  241. * Set the Array Consumption
  242. */
  243. public void setTotalFlex(float totalFlex) {
  244. this.totalFlex = totalFlex;
  245. }
  246. /**
  247. * If the user track any HolonObject the tracking information will be
  248. * updated. (If the HolonObject enters into the untracked state, the array
  249. * will be reseted)
  250. */
  251. public void updateTrackingInfo() {
  252. float[] tempProd = new float[100];
  253. float[] tempCons = new float[100];
  254. for (int i = 0; i < 100; i++) {
  255. float valueProd = 0;
  256. float valueCons = 0;
  257. for (HolonElement e : getElements()) {
  258. if (e.isActive() && e.isProducer()) {
  259. valueProd += e.getOverallEnergyAtTimeStep(i);
  260. }
  261. if (e.isActive() && e.isConsumer()) {
  262. valueCons += e.getOverallEnergyAtTimeStep(i);
  263. }
  264. }
  265. tempProd[i] = valueProd;
  266. tempCons[i] = valueCons;
  267. }
  268. this.trackingProd = tempProd;
  269. this.trackingCons = tempCons;
  270. }
  271. public String toString() {
  272. StringBuilder sb = new StringBuilder();
  273. sb.append("[HolonObject: ");
  274. sb.append("id=").append(id)
  275. .append(", name=").append(name)
  276. .append(", state=");
  277. sb.append(", elements=[");
  278. for (int i = 0; i < getElements().size(); i++) {
  279. HolonElement el = getElements().get(i);
  280. if (i != 0) {
  281. sb.append(", ");
  282. }
  283. sb.append(el.getEleName());
  284. }
  285. sb.append("]]");
  286. return sb.toString();
  287. }
  288. @Override
  289. public HolonObject makeCopy(){
  290. return new HolonObject(this);
  291. }
  292. public int getNumberOfActiveElements() {
  293. return (int) elements.stream().filter(ele -> ele.isActive()).count();
  294. }
  295. public int getNumberOfInActiveElements() {
  296. return (int) elements.stream().filter(ele -> !ele.isActive()).count();
  297. }
  298. public int getNumberOfElements() {
  299. return (int) elements.stream().count();
  300. }
  301. }