HolonElement.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. package classes;
  2. import com.google.gson.annotations.Expose;
  3. import java.awt.*;
  4. import java.util.LinkedList;
  5. /**
  6. * The class "HolonElement" represents any possible element that can be added to
  7. * a CpsObject (such as TV (consumer) or any energy source/producer).
  8. *
  9. * @author Gruppe14
  10. */
  11. public class HolonElement {
  12. // Points on the UnitGraph
  13. LinkedList<Point> graphPoints;
  14. /* Name of the gadget */
  15. @Expose
  16. private String eleName;
  17. /* Quantity */
  18. @Expose
  19. private int amount;
  20. /* Currently used energy (- indicates consuming of energy, + indicates producing energy)*/
  21. @Expose
  22. private float energy;
  23. /* Whether the gadget is active or not (currently uses/poduces the energy in energy) */
  24. @Expose
  25. private boolean active;
  26. /*
  27. * Gives us whether this element is flexible and can flexibly use any part of the energy in flexibleEnergyAvailable
  28. */
  29. @Expose
  30. private boolean flexible;
  31. /*
  32. * Flexibility (meaning the actual
  33. */
  34. @Expose
  35. private float flexibleEnergyAvailable;
  36. /* Total Energy */
  37. @Expose
  38. private float totalEnergy;
  39. /* -: for Consumers and +: Producers */
  40. @Expose
  41. private char sign;
  42. /* Place where the Object is Stored */
  43. @Expose
  44. private Pair<String, String> saving;
  45. /*
  46. * ID
  47. */
  48. @Expose
  49. private int id;
  50. /*
  51. * Energy at each point of the graph with 100 predefined points. At the
  52. * beginning, it starts with all values at energy
  53. */
  54. private float[] energyAt;
  55. /**
  56. * Create a new HolonElement with a user-defined name, amount of the same
  57. * element and energy per element.
  58. *
  59. * @param eleName String
  60. * @param amount int
  61. * @param energy float
  62. */
  63. public HolonElement(String eleName, int amount, float energy) {
  64. setEleName(eleName);
  65. setAmount(amount);
  66. setEnergy(energy);
  67. setActive(true);
  68. setSign(energy);
  69. setEnergyAt(energy);
  70. setGraphPoints(new LinkedList<>());
  71. setId(IdCounterElem.nextId());
  72. setFlexibleEnergyAvailable(0);
  73. setFlexible(false);
  74. }
  75. /**
  76. * same as standard constructor, but with already given id (so the counter is not increased twice)
  77. */
  78. public HolonElement(String eleName, int amount, float energy, int id) {
  79. setEleName(eleName);
  80. setAmount(amount);
  81. setEnergy(energy);
  82. setActive(true);
  83. setSign(energy);
  84. setEnergyAt(energy);
  85. setGraphPoints(new LinkedList<>());
  86. setId(id);
  87. setFlexibleEnergyAvailable(0);
  88. setFlexible(false);
  89. }
  90. /**
  91. * Create a copy of the HolonElement given each one a new ID.
  92. *
  93. * @param element element to copy
  94. */
  95. public HolonElement(HolonElement element) {
  96. setEleName(element.getEleName());
  97. setAmount(element.getAmount());
  98. setEnergy(element.getEnergy());
  99. setActive(element.isActive());
  100. setSign(element.getEnergy());
  101. setEnergyAt(element.getEnergy());
  102. for (int i = 0; i < energyAt.length; i++) {
  103. energyAt[i] = element.getEnergyAt()[i];
  104. }
  105. setGraphPoints(new LinkedList<>());
  106. for (Point p : element.getGraphPoints()) {
  107. this.graphPoints.add(new Point((int) p.getX(), (int) p.getY()));
  108. }
  109. setSaving(null);
  110. setId(IdCounterElem.nextId());
  111. setFlexibleEnergyAvailable(0);
  112. setFlexible(false);
  113. }
  114. /**
  115. * Get the Array of energy (100 values).
  116. *
  117. * @return energyAt Array of Floats
  118. */
  119. public float[] getEnergyAt() {
  120. return energyAt;
  121. }
  122. /**
  123. * Set energy to any value to the whole array.
  124. *
  125. * @param energy the value
  126. */
  127. public void setEnergyAt(float energy) {
  128. this.energyAt = new float[100];
  129. for (int i = 0; i < energyAt.length; i++) {
  130. this.energyAt[i] = energy;
  131. }
  132. }
  133. /**
  134. * Set energy to any value at a given position.
  135. *
  136. * @param pos int
  137. * @param energy float
  138. */
  139. public void setEnergyAt(int pos, float energy) {
  140. this.energyAt[pos] = energy;
  141. }
  142. /**
  143. * Get the user-defined Name.
  144. *
  145. * @return the name String
  146. */
  147. public String getEleName() {
  148. return eleName;
  149. }
  150. /**
  151. * Set the name to any new name.
  152. *
  153. * @param name the name to set
  154. */
  155. public void setEleName(String name) {
  156. this.eleName = name;
  157. }
  158. /**
  159. * Get the actual amount of Elements in the selected Object.
  160. *
  161. * @return the amount int
  162. */
  163. public int getAmount() {
  164. return amount;
  165. }
  166. /**
  167. * Set the amount of the Element in the selected Object.
  168. *
  169. * @param amount the amount to set
  170. */
  171. public void setAmount(int amount) {
  172. this.amount = amount;
  173. }
  174. /**
  175. * Get the energy value of the selected Element.
  176. *
  177. * @return the energy
  178. */
  179. public float getEnergy() {
  180. return energy;
  181. }
  182. /**
  183. * Set the energy value of the selected Element.
  184. *
  185. * @param energy the energy to set
  186. */
  187. public void setEnergy(float energy) {
  188. this.energy = energy;
  189. setSign(energy);
  190. }
  191. /**
  192. * Get the Status of the Element (see description of variables).
  193. *
  194. * @return the active
  195. */
  196. public boolean isActive() {
  197. return active;
  198. }
  199. /**
  200. * Set the Status of the Element (see description of variables).
  201. *
  202. * @param active the active to set
  203. */
  204. public void setActive(boolean active) {
  205. this.active = active;
  206. if (!active && flexible) {
  207. setEnergy(0);
  208. setEnergyAt(0);
  209. }
  210. }
  211. /**
  212. * Multiply the amount of gadgets, given by the user, and the
  213. * consumption/production. If the switch isWorking is turned off for on
  214. * gadget, the energy of this gadget have to be subtracted.
  215. *
  216. * @return totalEnergy (actual)
  217. */
  218. public float getTotalEnergy() {
  219. // if (flexible) {
  220. // totalEnergy = ((float) amount) * (energy + flexibleEnergyAvailable);
  221. // } else {
  222. totalEnergy = ((float) amount) * energy;
  223. // }
  224. return totalEnergy;
  225. }
  226. /**
  227. * Get the energy value at a selected time x.
  228. *
  229. * @param x int
  230. * @return energy value
  231. */
  232. public float getTotalEnergyAtTimeStep(int x) {
  233. // float result =((float) amount) * energyAt[x];
  234. // if (flexible) {
  235. // result = ((float) amount) * (energyAt[x] + flexibleEnergyAvailable);
  236. // } else {
  237. // result = ((float) amount) * energyAt[x];
  238. // }
  239. return ((float) amount) * energyAt[x];
  240. }
  241. /**
  242. * Get the symbol of the value (see variable description).
  243. *
  244. * @return the sign
  245. */
  246. public char getSign() {
  247. return sign;
  248. }
  249. /**
  250. * Set symbol of the value.
  251. *
  252. * @param energy the sign to set
  253. */
  254. public void setSign(float energy) {
  255. if (energy < 0)
  256. this.sign = '-';
  257. else
  258. this.sign = '+';
  259. }
  260. /**
  261. * Get the points (values) in the graph.
  262. *
  263. * @return the Graph Points
  264. */
  265. public LinkedList<Point> getGraphPoints() {
  266. return graphPoints;
  267. }
  268. /**
  269. * Set the points (values) in the graph.
  270. *
  271. * @param points the Graph points
  272. */
  273. public void setGraphPoints(LinkedList<Point> points) {
  274. this.graphPoints = points;
  275. }
  276. /**
  277. * Get the flexibleEnergyAvailable of an element
  278. */
  279. public float getFlexibleEnergyAvailable() {
  280. return this.flexibleEnergyAvailable;
  281. }
  282. /**
  283. * Set the flexibleEnergyAvailable of an element
  284. */
  285. public void setFlexibleEnergyAvailable(float f) {
  286. this.flexibleEnergyAvailable = f;
  287. }
  288. /**
  289. * Get the flexibleEnergyAvailable of an element
  290. */
  291. public boolean isFlexible() {
  292. return this.flexible;
  293. }
  294. /**
  295. * Set the flexibleEnergyAvailable of an element
  296. */
  297. public void setFlexible(boolean b) {
  298. this.flexible = b;
  299. // if flexibleEnergyAvailable was set to true
  300. if (b) {
  301. // set active to false
  302. this.active = false;
  303. // move energy to flexibleEnergyAvailable (becomes the possible-to-use energy)
  304. if (getEnergy() != 0) {
  305. setFlexibleEnergyAvailable(getEnergy());
  306. // and set actually used energy to zero (can be changed by algorithms, the grid itself or the user)
  307. setEnergy(0);
  308. setEnergyAt(0);
  309. }
  310. } else {
  311. // move the energy to actually used energy and set flexible amount to 0
  312. if (getFlexibleEnergyAvailable() != 0) {
  313. setEnergy(getFlexibleEnergyAvailable());
  314. setEnergyAt(getEnergy());
  315. }
  316. setFlexibleEnergyAvailable(0);
  317. }
  318. // System.out.println("after setFlexible(" + String.valueOf(b) + "):");
  319. // System.out.println("flexible " + flexible + ", active: " + active + ", energy: " + energy
  320. // + ", flexible energy: " + flexibleEnergyAvailable + "\n");
  321. }
  322. /**
  323. * Get the Id of the selected HolonElement.
  324. *
  325. * @return id the id
  326. */
  327. public int getId() {
  328. return id;
  329. }
  330. /**
  331. * Set the ID of the HolonElement (one time only).
  332. *
  333. * @param id the id
  334. */
  335. public void setId(int id) {
  336. this.id = id;
  337. }
  338. /**
  339. * @return the saving
  340. */
  341. public Pair<String, String> getSaving() {
  342. return saving;
  343. }
  344. /**
  345. * @param saving the saving to set
  346. */
  347. public void setSaving(Pair<String, String> saving) {
  348. this.saving = saving;
  349. }
  350. public String toString() {
  351. StringBuilder sb = new StringBuilder();
  352. sb.append("[HolonElement: ");
  353. sb.append("id=").append(id).append(", eleName=").append(eleName).append(", amount=").append(amount)
  354. .append(", active=").append(active).append(", flexible=").append(flexible)
  355. .append(", energy used=").append(energy).append(", flexible energy available=").append(flexibleEnergyAvailable);
  356. sb.append("]");
  357. return sb.toString();
  358. }
  359. }