HolonElement.java 14 KB

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