HolonElement.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 Pair<String, String> saving;
  34. /*
  35. * ID
  36. */
  37. @Expose
  38. private int id;
  39. /*
  40. * Flexibility
  41. */
  42. @Expose
  43. private float flexibility;
  44. /*
  45. * Active the flexibility field
  46. */
  47. @Expose
  48. private boolean activeFlex;
  49. /*
  50. * Energy at each point of the graph with 100 predefined points. At the
  51. * beginning, it starts with all values at energy
  52. */
  53. private float[] energyAt;
  54. // Points on the UnitGraph
  55. LinkedList<Point> graphPoints;
  56. /**
  57. * Create a new HolonElement with a user-defined name, amount of the same
  58. * element and energy per element.
  59. *
  60. * @param eleName
  61. * String
  62. * @param amount
  63. * int
  64. * @param energy
  65. * float
  66. */
  67. public HolonElement(String eleName, int amount, float energy) {
  68. setEleName(eleName);
  69. setAmount(amount);
  70. setEnergy(energy);
  71. setActive(true);
  72. setSign(energy);
  73. setEnergyAt(energy);
  74. setGraphPoints(new LinkedList<Point>());
  75. setId(IdCounterElem.nextId());
  76. System.out.println(this.getId());
  77. setFlexibility(0);
  78. setActiveFlex(false);
  79. }
  80. /**
  81. * Create a copy of the HolonElement given each one a new ID.
  82. *
  83. * @param element
  84. * element to copy
  85. */
  86. public HolonElement(HolonElement element) {
  87. setEleName(element.getEleName());
  88. setAmount(element.getAmount());
  89. setEnergy(element.getEnergy());
  90. setActive(element.getActive());
  91. setSign(element.getEnergy());
  92. setEnergyAt(element.getEnergy());
  93. for (int i = 0; i < energyAt.length; i++) {
  94. energyAt[i] = element.getEnergyAt()[i];
  95. }
  96. setGraphPoints(new LinkedList<Point>());
  97. for (Point p : element.getGraphPoints()) {
  98. this.graphPoints.add(new Point((int) p.getX(), (int) p.getY()));
  99. }
  100. setSaving(null);
  101. setId(IdCounterElem.nextId());
  102. setFlexibility(0);
  103. setActiveFlex(false);
  104. }
  105. /**
  106. * Get the Array of energy (100 values).
  107. *
  108. * @return energyAt Array of Floats
  109. */
  110. public float[] getEnergyAt() {
  111. return energyAt;
  112. }
  113. /**
  114. * Set energy to any value to the whole array.
  115. *
  116. * @param energy
  117. * the value
  118. */
  119. public void setEnergyAt(float energy) {
  120. this.energyAt = new float[100];
  121. for (int i = 0; i < energyAt.length; i++) {
  122. this.energyAt[i] = energy;
  123. }
  124. }
  125. /**
  126. * Set energy to any value at a given position.
  127. *
  128. * @param pos
  129. * int
  130. * @param energy
  131. * float
  132. */
  133. public void setEnergyAt(int pos, float energy) {
  134. this.energyAt[pos] = energy;
  135. }
  136. /**
  137. * Get the user-defined Name.
  138. *
  139. * @return the name String
  140. */
  141. public String getEleName() {
  142. return eleName;
  143. }
  144. /**
  145. * Set the name to any new name.
  146. *
  147. * @param name
  148. * the name to set
  149. */
  150. public void setEleName(String name) {
  151. this.eleName = name;
  152. }
  153. /**
  154. * Get the actual amount of Elements in the selected Object.
  155. *
  156. * @return the amount int
  157. */
  158. public int getAmount() {
  159. return amount;
  160. }
  161. /**
  162. * Set the amount of the Element in the selected Object.
  163. *
  164. * @param amount
  165. * the amount to set
  166. */
  167. public void setAmount(int amount) {
  168. this.amount = amount;
  169. }
  170. /**
  171. * Get the energy value of the selected Element.
  172. *
  173. * @return the energy
  174. */
  175. public float getEnergy() {
  176. return energy;
  177. }
  178. /**
  179. * Set the energy value of the selected Element.
  180. *
  181. * @param energy
  182. * the energy to set
  183. */
  184. public void setEnergy(float energy) {
  185. this.energy = energy;
  186. }
  187. /**
  188. * Get the Status of the Element (see description of variables).
  189. *
  190. * @return the active
  191. */
  192. public boolean getActive() {
  193. return active;
  194. }
  195. /**
  196. * Set the Status of the Element (see description of variables).
  197. *
  198. * @param active
  199. * the active to set
  200. */
  201. public void setActive(boolean active) {
  202. this.active = active;
  203. }
  204. /**
  205. * Multiply the amount of gadgets, given by the user, and the
  206. * consumption/production. If the switch isWorking is turned off for on
  207. * gadget, the energy of this gadget have to be subtracted.
  208. *
  209. * @return totalEnergy (actual)
  210. */
  211. public float getTotalEnergy() {
  212. if (activeFlex) {
  213. totalEnergy = ((float) amount) * (energy + flexibility);
  214. } else {
  215. totalEnergy = ((float) amount) * energy;
  216. }
  217. return totalEnergy;
  218. }
  219. /**
  220. * Get the energy value at a selected time x.
  221. *
  222. * @param x
  223. * int
  224. * @return energy value
  225. */
  226. public float getTotalEnergyAtTimeStep(int x) {
  227. float result = 0;
  228. if (activeFlex) {
  229. result = ((float) amount) * (energyAt[x] + flexibility);
  230. } else {
  231. result = ((float) amount) * energyAt[x];
  232. }
  233. return result;
  234. }
  235. /**
  236. * Get the symbol of the value (see variable description).
  237. *
  238. * @return the sign
  239. */
  240. public char getSign() {
  241. return sign;
  242. }
  243. /**
  244. * Set symbol of the value.
  245. *
  246. * @param energy
  247. * the sign to set
  248. */
  249. public void setSign(float energy) {
  250. if (energy < 0)
  251. this.sign = '-';
  252. else
  253. this.sign = '+';
  254. }
  255. /**
  256. * Get the points (values) in the graph.
  257. *
  258. * @return the Graph Points
  259. */
  260. public LinkedList<Point> getGraphPoints() {
  261. return graphPoints;
  262. }
  263. /**
  264. * Set the points (values) in the graph.
  265. *
  266. * @param points
  267. * the Graph points
  268. */
  269. public void setGraphPoints(LinkedList<Point> points) {
  270. this.graphPoints = points;
  271. }
  272. /**
  273. * Set the flexibility of an element
  274. */
  275. public void setFlexibility(float f) {
  276. this.flexibility = f;
  277. }
  278. /**
  279. * Get the flexibility of an element
  280. */
  281. public float getFlexibility() {
  282. return this.flexibility;
  283. }
  284. /**
  285. * Set the flexibility of an element
  286. */
  287. public void setActiveFlex(boolean b) {
  288. this.activeFlex = b;
  289. }
  290. /**
  291. * Get the flexibility of an element
  292. */
  293. public boolean getActiveFlex() {
  294. return this.activeFlex;
  295. }
  296. /**
  297. * Set the ID of the HolonElement (one time only).
  298. *
  299. * @param id
  300. * the id
  301. */
  302. public void setId(int id) {
  303. this.id = id;
  304. }
  305. /**
  306. * Get the Id of the selected HolonElement.
  307. *
  308. * @return id the id
  309. */
  310. public int getId() {
  311. return id;
  312. }
  313. /**
  314. * @return the saving
  315. */
  316. public Pair<String, String> getSaving() {
  317. return saving;
  318. }
  319. /**
  320. * @param saving
  321. * the saving to set
  322. */
  323. public void setSaving(Pair<String, String> saving) {
  324. this.saving = saving;
  325. }
  326. }