HolonElement.java 12 KB

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