HolonElement.java 12 KB

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