HolonElement.java 14 KB

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