HolonElement.java 13 KB

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