HolonElement.java 13 KB

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