HolonObject.java 12 KB

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