HolonElement.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. package classes;
  2. import com.google.gson.annotations.Expose;
  3. import interfaces.GraphEditable;
  4. import interfaces.LocalMode;
  5. import ui.model.Model;
  6. import ui.view.IndexTranslator;
  7. import java.awt.*;
  8. import java.awt.geom.Point2D;
  9. import java.awt.geom.Point2D.Double;
  10. import java.util.LinkedList;
  11. import java.util.ListIterator;
  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 LocalMode, GraphEditable{
  19. /** Points of new TestGraph
  20. * Represent the Graph
  21. * the X component from a Point is period from 0..1
  22. * the Y component from a Point is the percentage from 0..1
  23. * */
  24. private LinkedList<Point2D.Double> graphPoints;
  25. /** Name of the object, e.g. House, 1 */
  26. @Expose
  27. private String objName;
  28. /** Name of the gadget, e.g. TV */
  29. @Expose
  30. private String eleName;
  31. /** Amount of same elements */
  32. @Expose
  33. private int amount;
  34. /** Currently used energy per element (- indicates consumation of energy, + indicates production of energy)*/
  35. @Expose
  36. private float energyPerElement;
  37. /** Whether the gadget is active or not (currently uses/produces the energy in energyPerElement) */
  38. @Expose
  39. private boolean active;
  40. /** Gives us whether this element is flexible and can flexibly use any part of the energy in flexibleEnergyAvailable */
  41. @Expose
  42. private boolean flexible;
  43. /** Flexibility (meaning the actual */
  44. @Expose
  45. private float flexibleEnergyAvailable;
  46. /** Place where the Object is Stored */
  47. @Expose
  48. private Pair<String, String> saving;
  49. /** ID */
  50. @Expose
  51. private int id;
  52. //private static final int DEFAULT_GRAPH_LENGTH=100;
  53. //private int graphLength=DEFAULT_GRAPH_LENGTH; Unimplementable due to former developer's dark magic.
  54. /*
  55. * Energy at each point of the graph with 100 predefined points. At the
  56. * beginning, it starts with all values at energyPerElement.
  57. * If switched to flexible, this represents the maximum of usable energy
  58. */
  59. private float[] curveSample;
  60. @Expose
  61. private int localPeriod;
  62. @Expose
  63. private boolean isUsingLocalPeriod;
  64. /**
  65. * Create a new HolonElement with a user-defined name, amount of the same
  66. * element, energyPerElement and corresponding model.
  67. *
  68. * @param eleName String
  69. * @param amount int
  70. * @param energy float
  71. * @param model Model
  72. */
  73. public HolonElement(String eleName, int amount, float energy, Model model) {
  74. this(eleName, amount, energy, IdCounterElem.nextId(),model);
  75. }
  76. /**
  77. * same as standard constructor, but with already given id (so the counter is not increased twice)
  78. */
  79. public HolonElement(String eleName, int amount, float energy, int id, Model model){
  80. setUseLocalPeriod(false);
  81. setEleName(eleName);
  82. setAmount(amount);
  83. setEnergyPerElement(energy);
  84. setActive(true);
  85. setGraphPoints(new LinkedList<>());
  86. initGraphPoints();
  87. sampleGraph();
  88. setId(id);
  89. setFlexibleEnergyAvailable(0);
  90. setFlexible(false);
  91. }
  92. /**
  93. * Create a copy of the HolonElement given each one a new ID.
  94. *
  95. * @param element element to copy
  96. */
  97. public HolonElement(HolonElement element) {
  98. setLocalPeriod(element.getLocalPeriod());
  99. setUseLocalPeriod(element.isUsingLocalPeriod());
  100. setEleName(element.getEleName());
  101. setLocalPeriod(element.getLocalPeriod());
  102. setAmount(element.getAmount());
  103. setEnergyPerElement(element.getEnergyPerElement());
  104. setActive(element.isActive());
  105. setGraphPoints(new LinkedList<>());
  106. for (Point2D.Double p : element.getGraphPoints()) {
  107. this.graphPoints.add(new Point2D.Double(p.getX(), p.getY()));
  108. }
  109. sampleGraph();
  110. setSaving(null);
  111. setId(IdCounterElem.nextId());
  112. setFlexibleEnergyAvailable(0);
  113. setFlexible(false);
  114. }
  115. public String getObjName() {
  116. return objName;
  117. }
  118. public void setObjName(String objName) {
  119. this.objName = objName;
  120. }
  121. /**
  122. * Get the user-defined Name.
  123. *
  124. * @return the name String
  125. */
  126. public String getEleName() {
  127. return eleName;
  128. }
  129. /**
  130. * Set the name to any new name.
  131. *
  132. * @param name the name to set
  133. */
  134. public void setEleName(String name) {
  135. this.eleName = name;
  136. }
  137. /**
  138. * Get the actual amount of Elements in the selected Object.
  139. *
  140. * @return the amount int
  141. */
  142. public int getAmount() {
  143. return amount;
  144. }
  145. /**
  146. * Set the amount of the Element in the selected Object.
  147. *
  148. * @param amount the amount to set
  149. */
  150. public void setAmount(int amount) {
  151. this.amount = amount;
  152. }
  153. /**
  154. * Get the energyPerElement value of the selected Element.
  155. *
  156. * @return the energyPerElement
  157. */
  158. public float getEnergyPerElement() {
  159. return energyPerElement;
  160. }
  161. /**
  162. * Check the HolonElemnet is a Producer
  163. * @return true when the energy used be each element is higher then 0
  164. */
  165. public boolean isProducer()
  166. {
  167. return (energyPerElement > 0);
  168. }
  169. /**
  170. * Check the HolonElemnet is a Consumer
  171. * @return true when the energy used be each element is lower then 0
  172. */
  173. public boolean isConsumer()
  174. {
  175. return (energyPerElement < 0);
  176. }
  177. /**
  178. * Set the energyPerElement value of the selected Element.
  179. *
  180. * @param energyPerElement the energyPerElement to set
  181. */
  182. public void setEnergyPerElement(float energyPerElement) {
  183. this.energyPerElement = energyPerElement;
  184. }
  185. /**
  186. * Get the Status of the Element (see description of variables).
  187. *
  188. * @return the active
  189. */
  190. public boolean isActive() {
  191. return active;
  192. }
  193. /**
  194. * Set the Status of the Element (see description of variables).
  195. *
  196. * @param active the active to set
  197. */
  198. public void setActive(boolean active) {
  199. this.active = active;
  200. }
  201. /**
  202. * Multiply the amount of gadgets, given by the user, and the
  203. * consumption/production. If the switch isWorking is turned off for on
  204. * gadget, the energyPerElement of this gadget have to be subtracted.
  205. *
  206. * @return totalEnergy (actual)
  207. */
  208. public float getOverallEnergy() {
  209. float totalEnergy = amount * energyPerElement;
  210. return totalEnergy;
  211. }
  212. /**
  213. * Get the energyPerElement value at a selected time x.
  214. *
  215. * @param timestep int
  216. * @return energyPerElement value
  217. */
  218. public float getOverallEnergyAtTimeStep(int timestep) {
  219. if (flexible) {
  220. return ((float) amount) * energyPerElement;
  221. } else {
  222. return ((float) amount) * energyPerElement * curveSample[IndexTranslator.getEffectiveIndex(this, timestep)];
  223. }
  224. }
  225. /**
  226. * Get the energyPerElement currently(at given time step) available
  227. */
  228. public float getAvailableEnergyAt(int timestep) {
  229. System.out.println("getAvailableEnergyAt");
  230. return amount * energyPerElement * this.curveSample[IndexTranslator.getEffectiveIndex(this, timestep)];
  231. }
  232. /**
  233. * Get the flexibleEnergyAvailable of an element
  234. */
  235. public float getFlexibleEnergyAvailablePerElement() {
  236. return this.flexibleEnergyAvailable;
  237. }
  238. /**
  239. * Set the flexibleEnergyAvailable of an element
  240. */
  241. public void setFlexibleEnergyAvailable(float energy) {
  242. this.flexibleEnergyAvailable = energy;
  243. }
  244. /**
  245. * Get the flexibleEnergyAvailable of an element
  246. */
  247. public boolean isFlexible() {
  248. return this.flexible;
  249. }
  250. /**
  251. * Set the flexibleEnergyAvailable of an element, ~switches energyPerElement and flexible energyPerElement
  252. */
  253. public void setFlexible(boolean b) {
  254. // if flexibleEnergyAvailable was set to true
  255. if (b && !this.flexible) {
  256. this.flexible = b;
  257. // move energyPerElement to flexibleEnergyAvailable (becomes the possible-to-use energyPerElement)
  258. if (getEnergyPerElement() != 0) {
  259. setFlexibleEnergyAvailable(getEnergyPerElement());
  260. setEnergyPerElement(0);
  261. }
  262. } else if (!b && this.flexible) {
  263. this.flexible = b;
  264. // move the energyPerElement to actually used energyPerElement and set flexible amount to 0
  265. if (getFlexibleEnergyAvailablePerElement() != 0) {
  266. setEnergyPerElement(getFlexibleEnergyAvailablePerElement());
  267. }
  268. setFlexibleEnergyAvailable(0);
  269. }
  270. }
  271. /**
  272. * Get the Id of the selected HolonElement.
  273. *
  274. * @return id the id
  275. */
  276. public int getId() {
  277. return id;
  278. }
  279. /**
  280. * Set the ID of the HolonElement (one time only).
  281. *
  282. * @param id the id
  283. */
  284. public void setId(int id) {
  285. this.id = id;
  286. }
  287. /**
  288. * @return the saving
  289. */
  290. public Pair<String, String> getSaving() {
  291. return saving;
  292. }
  293. /**
  294. * @param saving the saving to set
  295. */
  296. public void setSaving(Pair<String, String> saving) {
  297. this.saving = saving;
  298. }
  299. public String toString() {
  300. StringBuilder sb = new StringBuilder();
  301. sb.append("[HolonElement: ");
  302. sb.append("id=").append(id)
  303. .append(", eleName=").append(eleName)
  304. .append(", amount=").append(amount)
  305. .append(", active=").append(active)
  306. .append(", flexible=").append(flexible)
  307. .append(", energyPerElement used=").append(energyPerElement)
  308. .append(", flexible energyPerElement available=").append(flexibleEnergyAvailable);
  309. sb.append("]");
  310. return sb.toString();
  311. }
  312. /**
  313. * Initialize the {@link HolonElement#graphPoints} List with the normal 2 Points at 100%.
  314. */
  315. private void initGraphPoints()
  316. {
  317. graphPoints.clear();
  318. graphPoints.add(new Point2D.Double(0,1.0));
  319. graphPoints.add(new Point2D.Double(1,1.0));
  320. }
  321. /**
  322. * Getter for the graphPoint List.
  323. * @return {@link HolonElement#graphPoints}
  324. */
  325. public LinkedList<Point2D.Double> getGraphPoints() {
  326. return graphPoints;
  327. }
  328. /**
  329. * Setter for the graphPoint List.
  330. * @param testGraphPoints is new {@link HolonElement#graphPoints}
  331. */
  332. public void setGraphPoints(LinkedList<Point2D.Double> testGraphPoints) {
  333. this.graphPoints = testGraphPoints;
  334. }
  335. //interfaces.GraphEditable
  336. @Override
  337. public Graphtype getGraphType() {
  338. return Graphtype.doubleGraph;
  339. }
  340. @Override
  341. public LinkedList<Double> getStateGraph() {
  342. return getGraphPoints();
  343. }
  344. @Override
  345. public void sampleGraph() {
  346. curveSample = sampleGraph(100);
  347. }
  348. @Override
  349. public void reset() {
  350. initGraphPoints();
  351. sampleGraph();
  352. }
  353. /**
  354. * Generate out of the Graph Points a array of floats that represent the Curve at each sample position. The Values are in the Range [0,1].
  355. * e.g. 0.0 represent: "0%" , 0.34 represent: 34% 1.0 represent: "100%"
  356. * @param sampleLength amount of samplePositions. The positions are equidistant on the Range[0,1].
  357. * @return the float array of samplepoints.
  358. */
  359. private float[] sampleGraph(int sampleLength)
  360. {
  361. ListIterator<Point2D.Double> iter = this.graphPoints.listIterator();
  362. Point.Double before = iter.next();
  363. Point.Double after = iter.next();
  364. float [] sampleCurve = new float[sampleLength];
  365. for(int i = 0; i<sampleLength ; i++)
  366. {
  367. double graphX = (double)i / (double) (sampleLength - 1); //from 0.0 to 1.0
  368. if(graphX > after.x)
  369. {
  370. before = after;
  371. after = iter.next();
  372. }
  373. //t to determine how many percentage the graphX is to the next Point needed to calc Bezier
  374. //inverseLerp(valueBetween, min, max) (valueBetween - min) / (max - min)
  375. // e.g. old.x = 0.4, actual.x = 0.8 and graphX = 0.6 then t is 0.5
  376. double t = (after.x -before.x > 0)? (graphX - before.x) / (after.x -before.x) : 0.0;
  377. sampleCurve[i] = (float) getYBetweenTwoPoints(t, before, after);
  378. }
  379. return sampleCurve;
  380. }
  381. /**
  382. * Helper method for {@link HolonElement#sampleGraph(int)}.
  383. * <p>
  384. * Its get the start and Endposition and calculate the Points in between for the Beziér Curve.
  385. * Then its get the Y Value a.k.a. the percentage from the curve at the X value t.
  386. * @param t is in Range [0,1] and represent how much the X value is traverse along the Curve between the two Points.
  387. * @param start is the start Point of the Curve.
  388. * @param end is the end Point of the Curve.
  389. * @return the percentage from the Curve at the X Value based on t.
  390. */
  391. private double getYBetweenTwoPoints(double t, Point.Double start, Point.Double end) {
  392. double mitte = (start.x + end.x)* 0.5;
  393. Point.Double bezier = getBezierPoint(t, start, new Point.Double(mitte, start.y), new Point.Double(mitte, end.y), end);
  394. return bezier.y;
  395. }
  396. /**
  397. * Helper method for {@link HolonElement#getYBetweenTwoPoints(double, Point.Double, Point.Double)}.
  398. * <p>
  399. * A Method for a normal Cubic Bezier Curve. A Cubic Bezier curve has four control points.
  400. * @param t is in Range [0,1] how much it traverse along the curve.
  401. * @param p0 StartPoint
  402. * @param p1 ControlPoint
  403. * @param p2 ControlPoint
  404. * @param p3 EndPoint
  405. * @return the BezierPosition at t.
  406. */
  407. private Point.Double getBezierPoint(double t, Point.Double p0, Point.Double p1,Point.Double p2,Point.Double p3) {
  408. /*
  409. * Calculate Beziér:
  410. * B(t) = (1-t)^3 * P0 + 3*(1-t)^2 * t * P1 + 3*(1-t)*t^2 * P2 + t^3 * P3 , 0 < t < 1
  411. *
  412. * Source: //http://www.theappguruz.com/blog/bezier-curve-in-games
  413. */
  414. Point.Double bezier = new Point.Double();
  415. double OneSubT = 1-t;
  416. double OneSubT2 = Math.pow(OneSubT, 2);
  417. double OneSubT3 = Math.pow(OneSubT, 3);
  418. double t2 = Math.pow(t , 2);
  419. double t3 = Math.pow(t , 3);
  420. bezier.x = OneSubT3 * p0.x + 3 * OneSubT2 * t * p1.x + 3 * OneSubT * t2 * p2.x + t3 * p3.x;
  421. bezier.y = OneSubT3 * p0.y + 3 * OneSubT2 * t * p1.y + 3 * OneSubT * t2 * p2.y + t3 * p3.y;
  422. return bezier;
  423. }
  424. //interfaces.LocalMode
  425. @Override
  426. public void setLocalPeriod(int period) {
  427. localPeriod=period;
  428. }
  429. @Override
  430. public int getLocalPeriod() {
  431. return localPeriod;
  432. }
  433. @Override
  434. public boolean isUsingLocalPeriod() {
  435. return isUsingLocalPeriod;
  436. }
  437. @Override
  438. public void setUseLocalPeriod(boolean state) {
  439. this.isUsingLocalPeriod=state;
  440. }
  441. }