HolonElement.java 13 KB

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