HolonObject.java 8.6 KB

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