HolonObject.java 9.1 KB

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