HolonObject.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. package classes;
  2. import java.awt.Color;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import com.google.gson.annotations.Expose;
  6. import ui.controller.MultiPurposeController;
  7. /**
  8. * The class HolonObject represents any Object on the system which capability of
  9. * injecting or consuming energy on the network, for instance a house or a power
  10. * plant.
  11. *
  12. * @author Gruppe14
  13. *
  14. */
  15. public class HolonObject extends AbstractCpsObject {
  16. /*
  17. * Color of the actual state (red = no supplied, yellow = partially supplied
  18. * and green = supplied)
  19. */
  20. @Expose
  21. private Color stateColor;
  22. /* Array of all consumers */
  23. private ArrayList<HolonElement> elements;
  24. /* Total of consumption */
  25. @Expose
  26. private float currentEnergy;
  27. /* Array for tracking Production */
  28. private float[] trackingProd;
  29. /* Array for tracking Consumption */
  30. private float[] trackingCons;
  31. /* Total Flexibility */
  32. private float totalFlex;
  33. /*
  34. * 0 = no energy, 1 = not supplied, 2 = supplied, 3 producer, 4 Partially
  35. * Supplied
  36. */
  37. public final static int NO_ENERGY = 0;
  38. public final static int NOT_SUPPLIED = 1;
  39. public final static int SUPPLIED = 2;
  40. public final static int PRODUCER = 3;
  41. public final static int PARTIALLY_SUPPLIED = 4;
  42. @Expose
  43. private int state = 0;
  44. /**
  45. * Constructor Set by default the name of the object equals to the category
  46. * name, until the user changes it.
  47. *
  48. * @param objName
  49. * name of the Object
  50. */
  51. public HolonObject(String objName) {
  52. super(objName);
  53. setElements(new ArrayList<HolonElement>());
  54. setState();
  55. setTrackingProd(new float[100]);
  56. setTrackingCons(new float[100]);
  57. }
  58. /**
  59. * Contructor of a copy of an Object.
  60. *
  61. * @param obj
  62. * object to be copied
  63. */
  64. public HolonObject(AbstractCpsObject obj) {
  65. super(obj);
  66. setElements(copyElements(((HolonObject) obj).getElements()));
  67. setState();
  68. setTrackingProd(new float[100]);
  69. setTrackingCons(new float[100]);
  70. }
  71. /**
  72. * sets the State, wether object is a producer, zero Energy, supplied or
  73. * not.
  74. */
  75. public void setState() {
  76. if (getCurrentEnergy() > 0) {
  77. setState(3);
  78. stateColor = Color.lightGray;
  79. } else {
  80. if (getCurrentEnergy() == 0) {
  81. setState(0);
  82. stateColor = Color.WHITE;
  83. } else {
  84. if (checkIfPartiallySupplied(0)) {
  85. stateColor = Color.yellow;
  86. } else {
  87. stateColor = new Color(230, 120, 100);
  88. }
  89. }
  90. }
  91. }
  92. /**
  93. * Getter for all Elements in the HolonObject.
  94. *
  95. * @return the elements ArrayList
  96. */
  97. public ArrayList<HolonElement> getElements() {
  98. return elements;
  99. }
  100. /**
  101. * Set a new ArrayList with HolonElements into the HolonObject.
  102. *
  103. * @param elements
  104. * the elements to set
  105. */
  106. public void setElements(ArrayList<HolonElement> elements) {
  107. this.elements = elements;
  108. }
  109. /**
  110. * adds an Element to the Object.
  111. *
  112. * @param element
  113. * the Element to add
  114. */
  115. public void addElements(HolonElement element) {
  116. elements.add(element);
  117. }
  118. /**
  119. * Doesn't take into account which timestep is watched, calculates the max
  120. * values.
  121. *
  122. * @return the currentEnergy
  123. */
  124. public float getCurrentEnergy() {
  125. float temp = 0;
  126. for (HolonElement e : getElements()) {
  127. if (e.getActive()) {
  128. temp = temp + e.getTotalEnergy();
  129. }
  130. }
  131. currentEnergy = temp;
  132. return currentEnergy;
  133. }
  134. /**
  135. * Getter for the current energy at a given timestep.
  136. *
  137. * @param x
  138. * timestep
  139. * @return corresponding energy
  140. */
  141. public float getCurrentEnergyAtTimeStep(int x) {
  142. float temp = 0;
  143. for (HolonElement e : getElements()) {
  144. if (e.getActive()) {
  145. temp = temp + e.getTotalEnergyAtTimeStep(x);
  146. }
  147. }
  148. currentEnergy = temp;
  149. return currentEnergy;
  150. }
  151. /**
  152. * deletes Element at a given index.
  153. *
  154. * @param idx
  155. * index
  156. */
  157. public void deleteElement(int idx) {
  158. elements.remove(idx);
  159. }
  160. /**
  161. * String of all consumers in this HolonObject.
  162. *
  163. * @return all the names of this HolonObject separated by "," each object
  164. */
  165. public String toStringElements() {
  166. String objString = "Empty";
  167. for (HolonElement e : elements) {
  168. if (objString == "Empty") {
  169. objString = e.getEleName();
  170. } else {
  171. objString = objString + ", " + e.getEleName();
  172. }
  173. }
  174. return objString;
  175. }
  176. /**
  177. * Copy all Elements into a New Array.
  178. *
  179. * @param arr
  180. * to copy
  181. * @return the copy of arr
  182. */
  183. public ArrayList<HolonElement> copyElements(ArrayList<HolonElement> arr) {
  184. ArrayList<HolonElement> newArr = new ArrayList<>();
  185. for (HolonElement t : arr) {
  186. newArr.add(new HolonElement(t));
  187. }
  188. return newArr;
  189. }
  190. /**
  191. * Get the state of the Object.
  192. *
  193. * @return state the State of the Element
  194. */
  195. public int getState() {
  196. return this.state;
  197. }
  198. /**
  199. * Set the state of the Object.
  200. *
  201. * @param state
  202. * boolean if the Object is fully supplied
  203. */
  204. public void setState(int state) {
  205. this.state = state;
  206. switch (state) {
  207. case 0:
  208. stateColor = Color.WHITE;
  209. break;
  210. case 1:
  211. stateColor = new Color(230, 120, 100);
  212. break;
  213. case 2:
  214. stateColor = Color.GREEN;
  215. break;
  216. case 3:
  217. stateColor = Color.lightGray;
  218. break;
  219. case 4:
  220. stateColor = Color.YELLOW;
  221. }
  222. }
  223. /**
  224. * Search for the element with the name.
  225. *
  226. * @param name
  227. * name of the object to be searched
  228. * @return the searched HolonElement
  229. */
  230. public HolonElement searchElement(String name) {
  231. HolonElement ele = null;
  232. for (HolonElement e : getElements()) {
  233. if (e.getEleName().equals(name)) {
  234. ele = e;
  235. }
  236. }
  237. return ele;
  238. }
  239. /**
  240. * Search for the element with the id.
  241. *
  242. * @param id
  243. * id of the element to be founded
  244. * @return the element
  245. */
  246. public HolonElement searchElementById(int id) {
  247. HolonElement ele = null;
  248. for (HolonElement e : getElements()) {
  249. if (e.getId() == id) {
  250. ele = e;
  251. }
  252. }
  253. return ele;
  254. }
  255. /**
  256. * Check if Partially Supplied.
  257. *
  258. * @param x
  259. * current Iteration
  260. * @return boolean is partially supplied
  261. */
  262. public boolean checkIfPartiallySupplied(int x) {
  263. if (getElements().size() == 0) {
  264. return false;
  265. }
  266. float minConsum = getElements().get(0).getTotalEnergyAtTimeStep(x);
  267. float prod = 0;
  268. for (HolonElement e : getElements()) {
  269. if (e.getActive()) {
  270. if (e.getTotalEnergyAtTimeStep(x) > 0) {
  271. prod = prod + e.getTotalEnergyAtTimeStep(x);
  272. }
  273. if (minConsum < 0 && (e.getTotalEnergyAtTimeStep(x) > minConsum && e.getTotalEnergyAtTimeStep(x) < 0)) {
  274. minConsum = e.getTotalEnergyAtTimeStep(x);
  275. } else if (minConsum >= 0 && e.getTotalEnergyAtTimeStep(x) < minConsum) {
  276. minConsum = e.getTotalEnergyAtTimeStep(x);
  277. }
  278. }
  279. }
  280. // System.out.println("minCons: " + minConsum + " prod: " + prod);
  281. if (minConsum < 0 && prod >= -minConsum) {
  282. return true;
  283. } else {
  284. return false;
  285. }
  286. }
  287. /**
  288. * Set the State Color.
  289. *
  290. * @param color
  291. * the Color
  292. */
  293. public void setColor(Color color) {
  294. stateColor = color;
  295. }
  296. /**
  297. * Get the Color.
  298. *
  299. * @return stateColor the Color
  300. */
  301. public Color getColor() {
  302. return stateColor;
  303. }
  304. /**
  305. * Set the Array Production
  306. */
  307. public void setTrackingProd(float[] arr) {
  308. this.trackingProd = arr;
  309. }
  310. /**
  311. * Get the Array Production
  312. */
  313. public float[] getTrackingProd() {
  314. return this.trackingProd;
  315. }
  316. /**
  317. * Set the Array Consumption
  318. */
  319. public void setTrackingCons(float[] arr) {
  320. this.trackingCons = arr;
  321. }
  322. /**
  323. * Get the Array Consumption
  324. */
  325. public float[] getTrackingCons() {
  326. return this.trackingCons;
  327. }
  328. /**
  329. * Get the Array Consumption
  330. */
  331. public float getTotalFlex() {
  332. return totalFlex;
  333. }
  334. /**
  335. * Set the Array Consumption
  336. */
  337. public void setTotalFlex(float totalFlex) {
  338. this.totalFlex = totalFlex;
  339. }
  340. /**
  341. * Update the totalFlex
  342. */
  343. public void updateTotalFlex() {
  344. float tempFlex = 0;
  345. for (HolonElement e : getElements()) {
  346. if (e.getActiveFlex()) {
  347. tempFlex += e.getFlexibility() * e.getAmount();
  348. }
  349. }
  350. this.totalFlex = tempFlex;
  351. }
  352. /**
  353. * calculates total flexible Production
  354. */
  355. public float getFlexProd() {
  356. float tempFlex = 0;
  357. for (HolonElement e : getElements()) {
  358. if (e.getFlexibility() > 0) {
  359. tempFlex += e.getFlexibility();
  360. }
  361. }
  362. return tempFlex;
  363. }
  364. /**
  365. * calculates total flexible Concumption
  366. */
  367. public float getFlexCons() {
  368. float tempFlex = 0;
  369. for (HolonElement e : getElements()) {
  370. if (e.getFlexibility() < 0) {
  371. tempFlex += e.getFlexibility();
  372. }
  373. }
  374. return tempFlex;
  375. }
  376. /**
  377. * If the user track any HolonObject the tracking information will be
  378. * updated. (If the HolonObject enters into the untracked state, the array
  379. * will be reseted)
  380. */
  381. public void updateTrackingInfo() {
  382. float[] tempProd = new float[100];
  383. float[] tempCons = new float[100];
  384. for (int i = 0; i < 100; i++) {
  385. float valueProd = 0;
  386. float valueCons = 0;
  387. for (HolonElement e : getElements()) {
  388. if (e.getActive() && e.getSign() == '+') {
  389. valueProd = valueProd + e.getTotalEnergyAtTimeStep(i);
  390. }
  391. if (e.getActive() && e.getSign() == '-') {
  392. valueCons = valueCons + e.getTotalEnergyAtTimeStep(i);
  393. }
  394. }
  395. tempProd[i] = valueProd;
  396. tempCons[i] = valueCons;
  397. }
  398. this.trackingProd = tempProd;
  399. this.trackingCons = tempCons;
  400. }
  401. }