HolonElement.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package classes;
  2. import java.awt.Point;
  3. import java.util.LinkedList;
  4. import com.google.gson.annotations.Expose;
  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. */
  12. public class HolonElement {
  13. /* Name of the gadget */
  14. @Expose
  15. private String eleName;
  16. /* Quantity */
  17. @Expose
  18. private int amount;
  19. /* Energy per gadget */
  20. @Expose
  21. private float energy;
  22. /* If the gadget is working xor not (true xor false) */
  23. @Expose
  24. private boolean active;
  25. /* Total Energy */
  26. @Expose
  27. private float totalEnergy;
  28. /* +: for Consumers and -: Producers */
  29. @Expose
  30. private char sign;
  31. /* Place where the Object is Stored */
  32. @Expose
  33. private String sav;
  34. /* Object where the Element is Stored */
  35. @Expose
  36. private String obj;
  37. /* Unique Id of the Element */
  38. @Expose
  39. private int id;
  40. /*
  41. * Energy at each point of the graph with 100 predefined points. At the
  42. * beginning, it starts with all values at energy
  43. */
  44. private float[] energyAt;
  45. // Points on the UnitGraph
  46. LinkedList<Point> graphPoints;
  47. /**
  48. * Create a new HolonElement with a user-defined name, amount of the same
  49. * element and energy per element.
  50. *
  51. * @param eleName
  52. * String
  53. * @param amount
  54. * int
  55. * @param energy
  56. * float
  57. */
  58. public HolonElement(String eleName, int amount, float energy) {
  59. setEleName(eleName);
  60. setAmount(amount);
  61. setEnergy(energy);
  62. setActive(true);
  63. setSign(energy);
  64. setEnergyAt(energy);
  65. setGraphPoints(new LinkedList<Point>());
  66. setId(IdCounterElem.nextId());
  67. }
  68. /**
  69. * Create a copy of the HolonElement given each one a new ID.
  70. *
  71. * @param element
  72. * element to copy
  73. */
  74. public HolonElement(HolonElement element) {
  75. setEleName(element.getEleName());
  76. setAmount(element.getAmount());
  77. setEnergy(element.getEnergy());
  78. setActive(element.getActive());
  79. setSign(element.getEnergy());
  80. setEnergyAt(element.getEnergy());
  81. for (int i = 0; i < energyAt.length; i++) {
  82. energyAt[i] = element.getEnergyAt()[i];
  83. }
  84. setGraphPoints(new LinkedList<Point>());
  85. for (Point p : element.getGraphPoints()) {
  86. this.graphPoints.add(new Point((int) p.getX(), (int) p.getY()));
  87. }
  88. setSav("CVS");
  89. setObj(element.getObj());
  90. setId(IdCounterElem.nextId());
  91. }
  92. /**
  93. * Get the Array of energy (100 values).
  94. *
  95. * @return energyAt Array of Floats
  96. */
  97. public float[] getEnergyAt() {
  98. return energyAt;
  99. }
  100. /**
  101. * Set energy to any value to the whole array.
  102. *
  103. * @param energy
  104. * the value
  105. */
  106. public void setEnergyAt(float energy) {
  107. this.energyAt = new float[100];
  108. for (int i = 0; i < energyAt.length; i++) {
  109. this.energyAt[i] = energy;
  110. }
  111. }
  112. /**
  113. * Set energy to any value at a given position.
  114. *
  115. * @param pos
  116. * int
  117. * @param energy
  118. * float
  119. */
  120. public void setEnergyAt(int pos, float energy) {
  121. this.energyAt[pos] = energy;
  122. }
  123. /**
  124. * Get the user-defined Name.
  125. *
  126. * @return the name String
  127. */
  128. public String getEleName() {
  129. return eleName;
  130. }
  131. /**
  132. * Set the name to any new name.
  133. *
  134. * @param name
  135. * the name to set
  136. */
  137. public void setEleName(String name) {
  138. this.eleName = name;
  139. }
  140. /**
  141. * Get the actual amount of Elements in the selected Object.
  142. *
  143. * @return the amount int
  144. */
  145. public int getAmount() {
  146. return amount;
  147. }
  148. /**
  149. * Set the amount of the Element in the selected Object.
  150. *
  151. * @param amount
  152. * the amount to set
  153. */
  154. public void setAmount(int amount) {
  155. this.amount = amount;
  156. }
  157. /**
  158. * Get the energy value of the selected Element.
  159. *
  160. * @return the energy
  161. */
  162. public float getEnergy() {
  163. return energy;
  164. }
  165. /**
  166. * Set the energy value of the selected Element.
  167. *
  168. * @param energy
  169. * the energy to set
  170. */
  171. public void setEnergy(float energy) {
  172. this.energy = energy;
  173. }
  174. /**
  175. * Get the Status of the Element (see description of variables).
  176. *
  177. * @return the active
  178. */
  179. public boolean getActive() {
  180. return active;
  181. }
  182. /**
  183. * Set the Status of the Element (see description of variables).
  184. *
  185. * @param active
  186. * the active to set
  187. */
  188. public void setActive(boolean active) {
  189. this.active = active;
  190. }
  191. /**
  192. * Multiply the amount of gadgets, given by the user, and the
  193. * consumption/production. If the switch isWorking is turned off for on
  194. * gadget, the energy of this gadget have to be subtracted.
  195. *
  196. * @return totalEnergy (actual)
  197. */
  198. public float getTotalEnergy() {
  199. totalEnergy = ((float) amount) * energy;
  200. return totalEnergy;
  201. }
  202. /**
  203. * Get the energy value at a selected time x.
  204. *
  205. * @param x
  206. * int
  207. * @return energy value
  208. */
  209. public float getTotalEnergyAtTimeStep(int x) {
  210. float result = ((float) amount) * energyAt[x];
  211. return result;
  212. }
  213. /**
  214. * Get the symbol of the value (see variable description).
  215. *
  216. * @return the sign
  217. */
  218. public char getSign() {
  219. return sign;
  220. }
  221. /**
  222. * Set symbol of the value.
  223. *
  224. * @param energy
  225. * the sign to set
  226. */
  227. public void setSign(float energy) {
  228. if (energy < 0)
  229. this.sign = '-';
  230. else
  231. this.sign = '+';
  232. }
  233. /**
  234. * @return the stored.
  235. */
  236. public String getSav() {
  237. return sav;
  238. }
  239. /**
  240. * for save purposes.
  241. *
  242. * @param sav
  243. * the stored to set
  244. */
  245. public void setSav(String sav) {
  246. this.sav = sav;
  247. }
  248. /**
  249. * Get the actual object.
  250. *
  251. * @return the obj
  252. */
  253. public String getObj() {
  254. return obj;
  255. }
  256. /**
  257. * Set the object to a new one.
  258. *
  259. * @param obj
  260. * the obj to set
  261. */
  262. public void setObj(String obj) {
  263. this.obj = obj;
  264. }
  265. /**
  266. * Get the points (values) in the graph.
  267. *
  268. * @return the Graph Points
  269. */
  270. public LinkedList<Point> getGraphPoints() {
  271. return graphPoints;
  272. }
  273. /**
  274. * Set the points (values) in the graph.
  275. *
  276. * @param points
  277. * the Graph points
  278. */
  279. public void setGraphPoints(LinkedList<Point> points) {
  280. this.graphPoints = points;
  281. }
  282. /**
  283. * Set the ID of the HolonElement (one time only).
  284. *
  285. * @param id
  286. * the id
  287. */
  288. private void setId(int id) {
  289. this.id = id;
  290. }
  291. /**
  292. * Get the Id of the selected HolonElement.
  293. *
  294. * @return id the id
  295. */
  296. public int getId() {
  297. return id;
  298. }
  299. }