HolonObject.java 8.3 KB

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