HolonElement.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. }
  207. /**
  208. * Multiply the amount of gadgets, given by the user, and the
  209. * consumption/production. If the switch isWorking is turned off for on
  210. * gadget, the energy of this gadget have to be subtracted.
  211. *
  212. * @return totalEnergy (actual)
  213. */
  214. public float getTotalEnergy() {
  215. // if (flexible) {
  216. // totalEnergy = ((float) amount) * (energy + flexibleEnergyAvailable);
  217. // } else {
  218. totalEnergy = ((float) amount) * energy;
  219. // }
  220. return totalEnergy;
  221. }
  222. /**
  223. * Get the energy value at a selected time x.
  224. *
  225. * @param x int
  226. * @return energy value
  227. */
  228. public float getTotalEnergyAtTimeStep(int x) {
  229. // float result =((float) amount) * energyAt[x];
  230. // if (flexible) {
  231. // result = ((float) amount) * (energyAt[x] + flexibleEnergyAvailable);
  232. // } else {
  233. // result = ((float) amount) * energyAt[x];
  234. // }
  235. return ((float) amount) * energyAt[x];
  236. }
  237. /**
  238. * Get the symbol of the value (see variable description).
  239. *
  240. * @return the sign
  241. */
  242. public char getSign() {
  243. return sign;
  244. }
  245. /**
  246. * Set symbol of the value.
  247. *
  248. * @param energy the sign to set
  249. */
  250. public void setSign(float energy) {
  251. if (energy < 0)
  252. this.sign = '-';
  253. else
  254. this.sign = '+';
  255. }
  256. /**
  257. * Get the points (values) in the graph.
  258. *
  259. * @return the Graph Points
  260. */
  261. public LinkedList<Point> getGraphPoints() {
  262. return graphPoints;
  263. }
  264. /**
  265. * Set the points (values) in the graph.
  266. *
  267. * @param points the Graph points
  268. */
  269. public void setGraphPoints(LinkedList<Point> points) {
  270. this.graphPoints = points;
  271. }
  272. /**
  273. * Get the flexibleEnergyAvailable of an element
  274. */
  275. public float getFlexibleEnergyAvailable() {
  276. return this.flexibleEnergyAvailable;
  277. }
  278. /**
  279. * Set the flexibleEnergyAvailable of an element
  280. */
  281. public void setFlexibleEnergyAvailable(float f) {
  282. this.flexibleEnergyAvailable = f;
  283. }
  284. /**
  285. * Get the flexibleEnergyAvailable of an element
  286. */
  287. public boolean isFlexible() {
  288. return this.flexible;
  289. }
  290. /**
  291. * Set the flexibleEnergyAvailable of an element
  292. */
  293. public void setFlexible(boolean b) {
  294. this.flexible = b;
  295. // if flexibleEnergyAvailable was set to true
  296. if (b) {
  297. // set active to false
  298. this.active = false;
  299. // move energy to flexibleEnergyAvailable (becomes the possible-to-use energy)
  300. if (getEnergy() != 0) {
  301. setFlexibleEnergyAvailable(getEnergy());
  302. // and set actually used energy to zero (can be changed by algorithms, the grid itself or the user)
  303. setEnergy(0);
  304. setEnergyAt(0);
  305. }
  306. } else {
  307. // move the energy to actually used energy and set flexible amount to 0
  308. if (getFlexibleEnergyAvailable() != 0) {
  309. setEnergy(getFlexibleEnergyAvailable());
  310. setEnergyAt(getFlexibleEnergyAvailable());
  311. }
  312. setFlexibleEnergyAvailable(0);
  313. }
  314. // System.out.println("after setFlexible(" + String.valueOf(b) + "):");
  315. // System.out.println("flexible " + flexible + ", active: " + active + ", energy: " + energy
  316. // + ", flexible energy: " + flexibleEnergyAvailable + "\n");
  317. }
  318. /**
  319. * Get the Id of the selected HolonElement.
  320. *
  321. * @return id the id
  322. */
  323. public int getId() {
  324. return id;
  325. }
  326. /**
  327. * Set the ID of the HolonElement (one time only).
  328. *
  329. * @param id the id
  330. */
  331. public void setId(int id) {
  332. this.id = id;
  333. }
  334. /**
  335. * @return the saving
  336. */
  337. public Pair<String, String> getSaving() {
  338. return saving;
  339. }
  340. /**
  341. * @param saving the saving to set
  342. */
  343. public void setSaving(Pair<String, String> saving) {
  344. this.saving = saving;
  345. }
  346. }