HolonElement.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 flexibility
  28. */
  29. @Expose
  30. private boolean flexible;
  31. /*
  32. * Flexibility (meaning the actual
  33. */
  34. @Expose
  35. private float flexibility;
  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. setFlexibleEnergyAmount(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. setFlexibleEnergyAmount(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. setFlexibleEnergyAmount(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. // TODO: anpassen
  216. if (flexible) {
  217. totalEnergy = ((float) amount) * (energy + flexibility);
  218. } else {
  219. totalEnergy = ((float) amount) * energy;
  220. }
  221. return totalEnergy;
  222. }
  223. /**
  224. * Get the energy value at a selected time x.
  225. *
  226. * @param x int
  227. * @return energy value
  228. */
  229. public float getTotalEnergyAtTimeStep(int x) {
  230. float result;
  231. if (flexible) {
  232. result = ((float) amount) * (energyAt[x] + flexibility);
  233. } else {
  234. result = ((float) amount) * energyAt[x];
  235. }
  236. return result;
  237. }
  238. /**
  239. * Get the symbol of the value (see variable description).
  240. *
  241. * @return the sign
  242. */
  243. public char getSign() {
  244. return sign;
  245. }
  246. /**
  247. * Set symbol of the value.
  248. *
  249. * @param energy the sign to set
  250. */
  251. public void setSign(float energy) {
  252. if (energy < 0)
  253. this.sign = '-';
  254. else
  255. this.sign = '+';
  256. }
  257. /**
  258. * Get the points (values) in the graph.
  259. *
  260. * @return the Graph Points
  261. */
  262. public LinkedList<Point> getGraphPoints() {
  263. return graphPoints;
  264. }
  265. /**
  266. * Set the points (values) in the graph.
  267. *
  268. * @param points the Graph points
  269. */
  270. public void setGraphPoints(LinkedList<Point> points) {
  271. this.graphPoints = points;
  272. }
  273. /**
  274. * Get the flexibility of an element
  275. */
  276. public float getFlexibleEnergyAmount() {
  277. return this.flexibility;
  278. }
  279. /**
  280. * Set the flexibility of an element
  281. */
  282. public void setFlexibleEnergyAmount(float f) {
  283. this.flexibility = f;
  284. }
  285. /**
  286. * Get the flexibility of an element
  287. */
  288. public boolean isFlexible() {
  289. return this.flexible;
  290. }
  291. /**
  292. * Set the flexibility of an element
  293. */
  294. public void setFlexible(boolean b) {
  295. this.flexible = b;
  296. // if flexibility was set to true
  297. if (b) {
  298. // set active to false
  299. this.active = false;
  300. // move energy to flexibility (becomes the possible-to-use energy)
  301. setFlexibleEnergyAmount(getEnergy());
  302. // and set actually used energy to zero (can be changed by algorithms, the grid itself or the user)
  303. setEnergy(0);
  304. } else {
  305. // move the energy to actually used energy and set flexible amount to 0
  306. setEnergy(getFlexibleEnergyAmount());
  307. setFlexibleEnergyAmount(0);
  308. }
  309. }
  310. /**
  311. * Get the Id of the selected HolonElement.
  312. *
  313. * @return id the id
  314. */
  315. public int getId() {
  316. return id;
  317. }
  318. /**
  319. * Set the ID of the HolonElement (one time only).
  320. *
  321. * @param id the id
  322. */
  323. public void setId(int id) {
  324. this.id = id;
  325. }
  326. /**
  327. * @return the saving
  328. */
  329. public Pair<String, String> getSaving() {
  330. return saving;
  331. }
  332. /**
  333. * @param saving the saving to set
  334. */
  335. public void setSaving(Pair<String, String> saving) {
  336. this.saving = saving;
  337. }
  338. }