HolonElement.java 6.7 KB

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