HolonElement.java 13 KB

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