HolonElement.java 11 KB

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