HolonObject.java 8.6 KB

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