HolonElement.java 13 KB

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