HolonElement.java 13 KB

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