HolonElement.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 and energyPerElement.
  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. public HolonElement(String eleName, int amount, float energy) {
  72. this(eleName, amount, energy, IdCounterElem.nextId(),null);//TODO: This is just for the old tests...
  73. }
  74. /**
  75. * same as standard constructor, but with already given id (so the counter is not increased twice)
  76. */
  77. public HolonElement(String eleName, int amount, float energy, int id, Model model){
  78. setLocalPeriod(model==null?UnitGraph.STANDARD_GRAPH_ACCURACY:model.getGraphIterations());
  79. setStretching(IGraphedElement.STRETCH_BY_DEFAULT);
  80. setEleName(eleName);
  81. setAmount(amount);
  82. setEnergyPerElement(energy);
  83. setActive(true);
  84. setSign(energy);
  85. setAvailableEnergyPerElementAt(energy);
  86. setGraphPoints(new LinkedList<>());
  87. setId(id);
  88. setFlexibleEnergyAvailable(0);
  89. setFlexible(false);
  90. }
  91. /**
  92. * Create a copy of the HolonElement given each one a new ID.
  93. *
  94. * @param element element to copy
  95. */
  96. public HolonElement(HolonElement element) {
  97. setLocalPeriod(element.getLocalPeriod());
  98. setStretching(element.isStretching());
  99. setEleName(element.getEleName());
  100. setLocalPeriod(element.getLocalPeriod());
  101. setAmount(element.getAmount());
  102. setEnergyPerElement(element.getEnergyPerElement());
  103. setActive(element.isActive());
  104. setSign(element.getEnergyPerElement());
  105. setAvailableEnergyPerElementAt(element.getEnergyPerElement());
  106. for (int i = 0; i < availableEnergyPerElementAt.length; i++) {
  107. availableEnergyPerElementAt[i] = element.getAvailableEnergyAt(i);
  108. }
  109. setGraphPoints(new LinkedList<>());
  110. for (Point p : element.getGraphPoints()) {
  111. this.graphPoints.add(new Point((int) p.getX(), (int) p.getY()));
  112. }
  113. setSaving(null);
  114. setId(IdCounterElem.nextId());
  115. setFlexibleEnergyAvailable(0);
  116. setFlexible(false);
  117. }
  118. public String getObjName() {
  119. return objName;
  120. }
  121. public void setObjName(String objName) {
  122. this.objName = objName;
  123. }
  124. /*
  125. * Get the Array of energyPerElement (100 values).
  126. *
  127. * @return availableEnergyPerElementAt Array of Floats
  128. */
  129. /*public float[] getAvailableEnergyPerElementAt() {
  130. return availableEnergyPerElementAt;
  131. }*/
  132. /**
  133. * Set energyPerElement to any value to the whole array.
  134. *
  135. * @param energy the value
  136. */
  137. public void setAvailableEnergyPerElementAt(float energy) {
  138. this.availableEnergyPerElementAt = new float[100];
  139. for (int i = 0; i < availableEnergyPerElementAt.length; i++) {
  140. this.availableEnergyPerElementAt[i] = energy;
  141. }
  142. }
  143. /**
  144. * Get the energyPerElement currently available
  145. */
  146. public float getAvailableEnergyAt(int timestep) {
  147. return this.availableEnergyPerElementAt[UnitGraph.getEffectiveIndex(this, timestep)];
  148. }
  149. /**
  150. * Set energyPerElement to any value at a given position.
  151. *
  152. * @param pos int
  153. * @param energyPerElement float
  154. */
  155. public void setAvailableEnergyPerElementAt(int pos, float energyPerElement) {
  156. this.availableEnergyPerElementAt[pos] = energyPerElement;
  157. }
  158. /**
  159. * Get the user-defined Name.
  160. *
  161. * @return the name String
  162. */
  163. public String getEleName() {
  164. return eleName;
  165. }
  166. /**
  167. * Set the name to any new name.
  168. *
  169. * @param name the name to set
  170. */
  171. public void setEleName(String name) {
  172. this.eleName = name;
  173. }
  174. /**
  175. * Get the actual amount of Elements in the selected Object.
  176. *
  177. * @return the amount int
  178. */
  179. public int getAmount() {
  180. return amount;
  181. }
  182. /**
  183. * Set the amount of the Element in the selected Object.
  184. *
  185. * @param amount the amount to set
  186. */
  187. public void setAmount(int amount) {
  188. this.amount = amount;
  189. }
  190. /**
  191. * Get the energyPerElement value of the selected Element.
  192. *
  193. * @return the energyPerElement
  194. */
  195. public float getEnergyPerElement() {
  196. return energyPerElement;
  197. }
  198. /**
  199. * Set the energyPerElement value of the selected Element.
  200. *
  201. * @param energyPerElement the energyPerElement to set
  202. */
  203. public void setEnergyPerElement(float energyPerElement) {
  204. this.energyPerElement = energyPerElement;
  205. setSign(energyPerElement);
  206. }
  207. /**
  208. * Get the Status of the Element (see description of variables).
  209. *
  210. * @return the active
  211. */
  212. public boolean isActive() {
  213. return active;
  214. }
  215. /**
  216. * Set the Status of the Element (see description of variables).
  217. *
  218. * @param active the active to set
  219. */
  220. public void setActive(boolean active) {
  221. this.active = active;
  222. }
  223. /**
  224. * Multiply the amount of gadgets, given by the user, and the
  225. * consumption/production. If the switch isWorking is turned off for on
  226. * gadget, the energyPerElement of this gadget have to be subtracted.
  227. *
  228. * @return totalEnergy (actual)
  229. */
  230. public float getOverallEnergy() {
  231. float totalEnergy = ((float) amount) * energyPerElement;
  232. return totalEnergy;
  233. }
  234. /**
  235. * Get the energyPerElement value at a selected time x.
  236. *
  237. * @param x int
  238. * @return energyPerElement value
  239. */
  240. public float getOverallEnergyAtTimeStep(int x) {
  241. if (flexible) {
  242. return ((float) amount) * energyPerElement;
  243. } else {
  244. return ((float) amount) * availableEnergyPerElementAt[UnitGraph.getEffectiveIndex(this, x)];
  245. }
  246. }
  247. /**
  248. * Get the symbol of the value (see variable description).
  249. *
  250. * @return the sign
  251. */
  252. public char getSign() {
  253. return sign;
  254. }
  255. /**
  256. * Set symbol of the value.
  257. *
  258. * @param energy the sign to set
  259. */
  260. public void setSign(float energy) {
  261. if (energy < 0)
  262. this.sign = '-';
  263. else
  264. this.sign = '+';
  265. }
  266. /**
  267. * Get the points (values) in the graph.
  268. *
  269. * @return the Graph Points
  270. */
  271. public LinkedList<Point> getGraphPoints() {
  272. return graphPoints;
  273. }
  274. /**
  275. * Set the points (values) in the graph.
  276. *
  277. * @param points the Graph points
  278. */
  279. public void setGraphPoints(LinkedList<Point> points) {
  280. this.graphPoints = points;
  281. }
  282. /**
  283. * Get the flexibleEnergyAvailable of an element
  284. */
  285. public float getFlexibleEnergyAvailablePerElement() {
  286. return this.flexibleEnergyAvailable;
  287. }
  288. /**
  289. * Set the flexibleEnergyAvailable of an element
  290. */
  291. public void setFlexibleEnergyAvailable(float f) {
  292. this.flexibleEnergyAvailable = f;
  293. }
  294. /**
  295. * Get the flexibleEnergyAvailable of an element
  296. */
  297. public boolean isFlexible() {
  298. return this.flexible;
  299. }
  300. /**
  301. * Set the flexibleEnergyAvailable of an element, ~switches energyPerElement and flexible energyPerElement
  302. */
  303. public void setFlexible(boolean b) {
  304. // if flexibleEnergyAvailable was set to true
  305. if (b && !this.flexible) {
  306. this.flexible = b;
  307. // move energyPerElement to flexibleEnergyAvailable (becomes the possible-to-use energyPerElement)
  308. if (getEnergyPerElement() != 0) {
  309. setFlexibleEnergyAvailable(getEnergyPerElement());
  310. setEnergyPerElement(0);
  311. }
  312. } else if (!b && this.flexible) {
  313. this.flexible = b;
  314. // move the energyPerElement to actually used energyPerElement and set flexible amount to 0
  315. if (getFlexibleEnergyAvailablePerElement() != 0) {
  316. setEnergyPerElement(getFlexibleEnergyAvailablePerElement());
  317. }
  318. setFlexibleEnergyAvailable(0);
  319. }
  320. }
  321. /**
  322. * Get the Id of the selected HolonElement.
  323. *
  324. * @return id the id
  325. */
  326. public int getId() {
  327. return id;
  328. }
  329. /**
  330. * Set the ID of the HolonElement (one time only).
  331. *
  332. * @param id the id
  333. */
  334. public void setId(int id) {
  335. this.id = id;
  336. }
  337. /**
  338. * @return the saving
  339. */
  340. public Pair<String, String> getSaving() {
  341. return saving;
  342. }
  343. /**
  344. * @param saving the saving to set
  345. */
  346. public void setSaving(Pair<String, String> saving) {
  347. this.saving = saving;
  348. }
  349. public String toString() {
  350. StringBuilder sb = new StringBuilder();
  351. sb.append("[HolonElement: ");
  352. sb.append("id=").append(id)
  353. .append(", eleName=").append(eleName)
  354. .append(", amount=").append(amount)
  355. .append(", active=").append(active)
  356. .append(", flexible=").append(flexible)
  357. .append(", energyPerElement used=").append(energyPerElement)
  358. .append(", flexible energyPerElement available=").append(flexibleEnergyAvailable);
  359. sb.append("]");
  360. return sb.toString();
  361. }
  362. @Override
  363. public void setLocalPeriod(int period) {
  364. localPeriod=period;
  365. }
  366. @Override
  367. public int getLocalPeriod() {
  368. return localPeriod;
  369. }
  370. @Override
  371. public boolean isStretching() {
  372. return stretch;
  373. }
  374. @Override
  375. public void setStretching(boolean stretch) {
  376. this.stretch=stretch;
  377. }
  378. }