HolonElement.java 13 KB

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