HolonElement.java 12 KB

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