HolonElement.java 10 KB

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