HolonElement.java 12 KB

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