HolonObject.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. * Getter for the current energy at a given timestep.
  147. *
  148. * @param x timestep
  149. * @return corresponding energy
  150. */
  151. public float getCurrentEnergyAtTimeStepWithoutFlexiblesAndResetFlexibles(int x) {
  152. float temp = 0;
  153. for (HolonElement e : getElements()) {
  154. if (e.isActive() && !e.isFlexible()) {
  155. temp += e.getOverallEnergyAtTimeStep(x);
  156. } else if (e.isFlexible()) {
  157. e.setEnergyPerElement(0);
  158. }
  159. }
  160. currentEnergy = temp;
  161. return currentEnergy;
  162. }
  163. /**
  164. * deletes Element at a given index.
  165. *
  166. * @param idx index
  167. */
  168. public void deleteElement(int idx) {
  169. elements.remove(idx);
  170. }
  171. /**
  172. * String of all consumers in this HolonObject.
  173. *
  174. * @return all the names of this HolonObject separated by "," each object
  175. */
  176. public String toStringElements() {
  177. String objString = "Empty";
  178. for (HolonElement e : elements) {
  179. if (objString == "Empty") {
  180. objString = e.getEleName();
  181. } else {
  182. objString = objString + ", " + e.getEleName();
  183. }
  184. }
  185. return objString;
  186. }
  187. /**
  188. * Copy all Elements into a New Array.
  189. *
  190. * @param arr to copy
  191. * @return the copy of arr
  192. */
  193. public ArrayList<HolonElement> copyElements(ArrayList<HolonElement> arr) {
  194. ArrayList<HolonElement> newArr = new ArrayList<>();
  195. for (HolonElement t : arr) {
  196. newArr.add(new HolonElement(t));
  197. }
  198. return newArr;
  199. }
  200. /**
  201. * Get the state of the Object.
  202. *
  203. * @return state the State of the Element
  204. */
  205. public int getState() {
  206. return this.state;
  207. }
  208. /**
  209. * Set the state of the Object.
  210. *
  211. * @param state boolean if the Object is fully supplied
  212. */
  213. public void setState(int state) {
  214. this.state = state;
  215. switch (state) {
  216. case NO_ENERGY:
  217. stateColor = Color.WHITE;
  218. break;
  219. case NOT_SUPPLIED:
  220. stateColor = new Color(230, 120, 100);
  221. break;
  222. case SUPPLIED:
  223. stateColor = Color.GREEN;
  224. break;
  225. case PRODUCER:
  226. stateColor = Color.lightGray;
  227. break;
  228. case PARTIALLY_SUPPLIED:
  229. stateColor = Color.YELLOW;
  230. break;
  231. case OVER_SUPPLIED:
  232. // find different purple-tones at
  233. // http://www.rapidtables.com/web/color/purple-color.htm
  234. stateColor = new Color(138, 43, 226);
  235. break;
  236. }
  237. }
  238. /**
  239. * Search for the element with the name.
  240. *
  241. * @param name 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 id of the element to be founded
  257. * @return the element
  258. */
  259. public HolonElement searchElementById(int id) {
  260. HolonElement ele = null;
  261. for (HolonElement e : getElements()) {
  262. if (e.getId() == id) {
  263. ele = e;
  264. }
  265. }
  266. return ele;
  267. }
  268. /**
  269. * Check if Partially Supplied.
  270. *
  271. * @param x current Iteration
  272. * @return boolean is partially supplied
  273. */
  274. public boolean checkIfPartiallySupplied(int x) {
  275. if (getElements().size() == 0) {
  276. return false;
  277. }
  278. float minConsum = getElements().get(0).getOverallEnergyAtTimeStep(x);
  279. float prod = 0;
  280. for (HolonElement e : getElements()) {
  281. if (e.isActive()) {
  282. float overallEnergy = e.getOverallEnergyAtTimeStep(x);
  283. if (overallEnergy > 0) {
  284. prod = prod + overallEnergy;
  285. }
  286. if (minConsum < 0 && (overallEnergy > minConsum && overallEnergy < 0)) {
  287. minConsum = overallEnergy;
  288. } else if (minConsum >= 0 && overallEnergy < minConsum) {
  289. minConsum = overallEnergy;
  290. }
  291. }
  292. }
  293. // System.out.println("minCons: " + minConsum + " prod: " + prod);
  294. if (minConsum < 0 && prod >= -minConsum) {
  295. return true;
  296. } else {
  297. return false;
  298. }
  299. }
  300. /**
  301. * Get the Color.
  302. *
  303. * @return stateColor the Color
  304. */
  305. public Color getColor() {
  306. return stateColor;
  307. }
  308. /**
  309. * Set the State Color.
  310. *
  311. * @param color the Color
  312. */
  313. public void setColor(Color color) {
  314. stateColor = color;
  315. }
  316. /**
  317. * Get the Array Production
  318. */
  319. public float[] getTrackingProd() {
  320. return this.trackingProd;
  321. }
  322. /**
  323. * Set the Array Production
  324. */
  325. public void setTrackingProd(float[] arr) {
  326. this.trackingProd = arr;
  327. }
  328. /**
  329. * Get the Array Consumption
  330. */
  331. public float[] getTrackingCons() {
  332. return this.trackingCons;
  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 getTotalFlex() {
  344. return totalFlex;
  345. }
  346. /**
  347. * Set the Array Consumption
  348. */
  349. public void setTotalFlex(float totalFlex) {
  350. this.totalFlex = totalFlex;
  351. }
  352. /**
  353. * Update the totalFlex
  354. */
  355. public void updateTotalFlex() {
  356. float tempFlex = 0;
  357. for (HolonElement e : getElements()) {
  358. if (e.isFlexible()) {
  359. tempFlex += e.getFlexibleEnergyAvailablePerElement() * e.getAmount();
  360. }
  361. }
  362. this.totalFlex = tempFlex;
  363. }
  364. /**
  365. * calculates total flexible Production
  366. */
  367. public float getFlexProd() {
  368. float tempFlex = 0;
  369. for (HolonElement e : getElements()) {
  370. if (e.getFlexibleEnergyAvailablePerElement() > 0) {
  371. tempFlex += e.getFlexibleEnergyAvailablePerElement() * e.getAmount();
  372. }
  373. }
  374. return tempFlex;
  375. }
  376. /**
  377. * calculates total flexible Concumption
  378. */
  379. public float getFlexCons() {
  380. float tempFlex = 0;
  381. for (HolonElement e : getElements()) {
  382. if (e.getFlexibleEnergyAvailablePerElement() < 0) {
  383. tempFlex += e.getFlexibleEnergyAvailablePerElement() * e.getAmount();
  384. }
  385. }
  386. return tempFlex;
  387. }
  388. /**
  389. * If the user track any HolonObject the tracking information will be
  390. * updated. (If the HolonObject enters into the untracked state, the array
  391. * will be reseted)
  392. */
  393. public void updateTrackingInfo() {
  394. float[] tempProd = new float[100];
  395. float[] tempCons = new float[100];
  396. for (int i = 0; i < 100; i++) {
  397. float valueProd = 0;
  398. float valueCons = 0;
  399. for (HolonElement e : getElements()) {
  400. if (e.isActive() && e.getSign() == '+') {
  401. valueProd = valueProd + e.getOverallEnergyAtTimeStep(i);
  402. }
  403. if (e.isActive() && e.getSign() == '-') {
  404. valueCons = valueCons + e.getOverallEnergyAtTimeStep(i);
  405. }
  406. }
  407. tempProd[i] = valueProd;
  408. tempCons[i] = valueCons;
  409. }
  410. this.trackingProd = tempProd;
  411. this.trackingCons = tempCons;
  412. }
  413. public String toString() {
  414. StringBuilder sb = new StringBuilder();
  415. sb.append("[HolonObject: ");
  416. sb.append("id=").append(id)
  417. .append(", name=").append(name)
  418. .append(", state=");
  419. switch (state) {
  420. case NO_ENERGY:
  421. sb.append("NO_ENERGY");
  422. break;
  423. case NOT_SUPPLIED:
  424. sb.append("NOT_SUPPLIED");
  425. break;
  426. case SUPPLIED:
  427. sb.append("SUPPLIED");
  428. break;
  429. case PRODUCER:
  430. sb.append("PRODUCER");
  431. break;
  432. case PARTIALLY_SUPPLIED:
  433. sb.append("PARTIALLY_SUPPLIED");
  434. break;
  435. case OVER_SUPPLIED:
  436. sb.append("OVER_SUPPLIED");
  437. break;
  438. }
  439. sb.append(", elements=[");
  440. for (int i = 0; i < getElements().size(); i++) {
  441. HolonElement el = getElements().get(i);
  442. if (i != 0) {
  443. sb.append(", ");
  444. }
  445. sb.append(el.getEleName());
  446. }
  447. sb.append("]]");
  448. return sb.toString();
  449. }
  450. }