HolonElement.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. package holeg.model;
  2. import holeg.interfaces.TimelineDependent;
  3. import holeg.model.Flexibility.FlexState;
  4. import holeg.serialize.PostDeserialize;
  5. import holeg.ui.controller.IndexTranslator;
  6. import holeg.utility.math.vector.Vec2f;
  7. import java.util.ArrayList;
  8. import java.util.LinkedList;
  9. import java.util.List;
  10. import java.util.ListIterator;
  11. import java.util.logging.Logger;
  12. /**
  13. * The class "HolonElement" represents any possible element that can be added to a HolonObject.
  14. *
  15. * @author Gruppe14
  16. */
  17. public class HolonElement implements TimelineDependent, PostDeserialize {
  18. private static final Logger log = Logger.getLogger(HolonElement.class.getName());
  19. /**
  20. * Owner of the Element
  21. */
  22. public transient HolonObject parentObject;
  23. /**
  24. * Whether the gadget is active or not (currently uses/produces the energy in energyPerElement)
  25. */
  26. public boolean active = true;
  27. public Priority priority = Priority.Low;
  28. public List<Flexibility> flexList = new ArrayList<>();
  29. /**
  30. * Points of new TestGraph Represent the Graph the X component from a Point is period from 0..1
  31. * the Y component from a Point is the percentage from 0..1
  32. */
  33. private LinkedList<Vec2f> graphPoints = new LinkedList<>();
  34. /**
  35. * Name of the gadget, e.g. TV
  36. */
  37. private String name;
  38. /**
  39. * Amount of same elements
  40. */
  41. private float energy;
  42. private Period period = new Period();
  43. /*
  44. * Energy at each point of the graph with 100 predefined points. At the
  45. * beginning, it starts with all values at energyPerElement.
  46. * If switched to flexible, this represents the maximum of usable energy
  47. */
  48. private transient float[] curveSample;
  49. private transient float actualEnergy = 0;
  50. /**
  51. * Create a new HolonElement with a user-defined name, amount of the same
  52. * element, energyPerElement and corresponding model.
  53. *
  54. * @param eleName String
  55. * @param energy float
  56. */
  57. /**
  58. * same as standard constructor, but with already given id (so the counter is not increased
  59. * twice)
  60. */
  61. public HolonElement(HolonObject parentObject, String name, float energy) {
  62. this.parentObject = parentObject;
  63. setName(name);
  64. setEnergy(energy);
  65. initGraphPoints();
  66. sampleGraph();
  67. }
  68. /**
  69. * Create a copy of the HolonElement given each one a new ID.
  70. *
  71. * @param other element to copy
  72. */
  73. public HolonElement(HolonElement other) {
  74. this.parentObject = other.parentObject;
  75. this.priority = other.getPriority();
  76. this.period = other.period;
  77. this.flexList = new ArrayList<>(other.flexList);
  78. setName(other.getName());
  79. setEnergy(other.getEnergy());
  80. this.active = other.active;
  81. setGraphPoints(new LinkedList<>());
  82. for (Vec2f p : other.getGraphPoints()) {
  83. this.graphPoints.add(new Vec2f(p));
  84. }
  85. this.actualEnergy = other.actualEnergy;
  86. sampleGraph();
  87. }
  88. @Override
  89. public Period getPeriod() {
  90. return period;
  91. }
  92. @Override
  93. public void setPeriod(Period period) {
  94. this.period = period;
  95. }
  96. /**
  97. * Get the user-defined Name.
  98. *
  99. * @return the name String
  100. */
  101. public String getName() {
  102. return name;
  103. }
  104. /**
  105. * Set the name to any new name.
  106. *
  107. * @param name the name to set
  108. */
  109. public void setName(String name) {
  110. this.name = name;
  111. }
  112. /**
  113. * Get the energyPerElement value of the selected Element.
  114. *
  115. * @return the energyPerElement
  116. */
  117. public float getEnergy() {
  118. return energy;
  119. }
  120. /**
  121. * Set the energyPerElement value of the selected Element.
  122. *
  123. * @param energyPerElement the energyPerElement to set
  124. */
  125. public void setEnergy(float energyPerElement) {
  126. log.finest(this.energy + " -> " + energyPerElement);
  127. this.energy = energyPerElement;
  128. }
  129. /**
  130. * Check the HolonElemnet is a Producer
  131. *
  132. * @return true when the energy used be each element is higher then 0
  133. */
  134. public boolean isProducer() {
  135. return (energy > 0);
  136. }
  137. /**
  138. * Check the HolonElemnet is a Consumer
  139. *
  140. * @return true when the energy used be each element is lower then 0
  141. */
  142. public boolean isConsumer() {
  143. return (energy < 0);
  144. }
  145. public Priority getPriority() {
  146. return priority;
  147. }
  148. public void setPriority(Priority priority) {
  149. this.priority = priority;
  150. }
  151. public String toString() {
  152. return "[HolonElement: " +
  153. ", eleName=" + name +
  154. ", parentName=" + parentObject.getName() +
  155. ", active=" + active +
  156. ", energyPerElement used=" + energy +
  157. "]";
  158. }
  159. /**
  160. * Initialize the {@link HolonElement#graphPoints} List with the normal 2 Points at 100%.
  161. */
  162. private void initGraphPoints() {
  163. graphPoints.clear();
  164. graphPoints.add(new Vec2f(0f, 1.0f));
  165. graphPoints.add(new Vec2f(1f, 1.0f));
  166. }
  167. /**
  168. * Getter for the graphPoint List.
  169. *
  170. * @return {@link HolonElement#graphPoints}
  171. */
  172. public LinkedList<Vec2f> getGraphPoints() {
  173. return graphPoints;
  174. }
  175. /**
  176. * Setter for the graphPoint List.
  177. */
  178. public void setGraphPoints(LinkedList<Vec2f> graphPoints) {
  179. this.graphPoints = graphPoints;
  180. }
  181. //interfaces.GraphEditable
  182. @Override
  183. public GraphType getGraphType() {
  184. return GraphType.doubleGraph;
  185. }
  186. @Override
  187. public LinkedList<Vec2f> getStateGraph() {
  188. return getGraphPoints();
  189. }
  190. @Override
  191. public void sampleGraph() {
  192. curveSample = sampleGraph(100);
  193. }
  194. @Override
  195. public void reset() {
  196. initGraphPoints();
  197. sampleGraph();
  198. }
  199. /**
  200. * Generate out of the Graph Points a array of floats that represent the Curve at each sample
  201. * position. The Values are in the Range [0,1]. e.g. 0.0 represent: "0%" , 0.34 represent: 34%
  202. * 1.0 represent: "100%"
  203. *
  204. * @param sampleLength amount of samplePositions. The positions are equidistant on the
  205. * Range[0,1].
  206. * @return the float array of samplepoints.
  207. */
  208. private float[] sampleGraph(int sampleLength) {
  209. ListIterator<Vec2f> iter = this.graphPoints.listIterator();
  210. Vec2f before = iter.next();
  211. Vec2f after = iter.next();
  212. float[] sampleCurve = new float[sampleLength];
  213. for (int i = 0; i < sampleLength; i++) {
  214. double graphX = (double) i / (double) (sampleLength - 1); //from 0.0 to 1.0
  215. if (graphX > after.x) {
  216. before = after;
  217. after = iter.next();
  218. }
  219. //t to determine how many percentage the graphX is to the next Point needed to calc Bezier
  220. //inverseLerp(valueBetween, min, max) (valueBetween - min) / (max - min)
  221. // e.g. old.x = 0.4, actual.x = 0.8 and graphX = 0.6 then t is 0.5
  222. double t = (after.x - before.x > 0) ? (graphX - before.x) / (after.x - before.x) : 0.0;
  223. sampleCurve[i] = (float) getYBetweenTwoPoints(t, before, after);
  224. }
  225. return sampleCurve;
  226. }
  227. /**
  228. * Helper method for {@link HolonElement#sampleGraph(int)}.
  229. * <p>
  230. * Its get the start and Endposition and calculate the Points in between for the Bezi�r Curve.
  231. * Then its get the Y Value a.k.a. the percentage from the curve at the X value t.
  232. *
  233. * @param t is in Range [0,1] and represent how much the X value is traverse along the Curve
  234. * between the two Points.
  235. * @param start is the start Point of the Curve.
  236. * @param end is the end Point of the Curve.
  237. * @return the percentage from the Curve at the X Value based on t.
  238. */
  239. private double getYBetweenTwoPoints(double t, Vec2f start, Vec2f end) {
  240. float mitte = (start.x + end.x) * 0.5f;
  241. Vec2f bezier = getBezierPoint(t, start, new Vec2f(mitte, start.y), new Vec2f(mitte, end.y),
  242. end);
  243. return bezier.y;
  244. }
  245. /**
  246. * Helper method for {@link HolonElement#getYBetweenTwoPoints(double, Vec2f, Vec2f)}.
  247. * <p>
  248. * A Method for a normal Cubic Bezier Curve. A Cubic Bezier curve has four control points.
  249. *
  250. * @param t is in Range [0,1] how much it traverse along the curve.
  251. * @param p0 StartPoint
  252. * @param p1 ControlPoint
  253. * @param p2 ControlPoint
  254. * @param p3 EndPoint
  255. * @return the BezierPosition at t.
  256. */
  257. private Vec2f getBezierPoint(double t, Vec2f p0, Vec2f p1, Vec2f p2, Vec2f p3) {
  258. /*
  259. * Calculate Bezi�r:
  260. * B(t) = (1-t)^3 * P0 + 3*(1-t)^2 * t * P1 + 3*(1-t)*t^2 * P2 + t^3 * P3 , 0 < t < 1
  261. *
  262. * Source: //http://www.theappguruz.com/blog/bezier-curve-in-games
  263. */
  264. Vec2f bezier = new Vec2f();
  265. double OneSubT = 1 - t;
  266. double OneSubT2 = Math.pow(OneSubT, 2);
  267. double OneSubT3 = Math.pow(OneSubT, 3);
  268. double t2 = Math.pow(t, 2);
  269. double t3 = Math.pow(t, 3);
  270. bezier.x = (float) (OneSubT3 * p0.x + 3 * OneSubT2 * t * p1.x + 3 * OneSubT * t2 * p2.x
  271. + t3 * p3.x);
  272. bezier.y = (float) (OneSubT3 * p0.y + 3 * OneSubT2 * t * p1.y + 3 * OneSubT * t2 * p2.y
  273. + t3 * p3.y);
  274. return bezier;
  275. }
  276. /*
  277. * STATE
  278. */
  279. public void calculateState(int iteration) {
  280. flexList.forEach(flex -> flex.calculateState(iteration));
  281. float energyWhenActive =
  282. energy * this.curveSample[IndexTranslator.getEffectiveIndex(this, iteration)];
  283. actualEnergy = isOn() ? energyWhenActive : 0;
  284. }
  285. /**
  286. * Get the energyPerElement currently(at given time step) available
  287. */
  288. public float calculateExpectedEnergyAtTimeStep(int iteration) {
  289. float energyWhenActive =
  290. energy * this.curveSample[IndexTranslator.getEffectiveIndex(this, iteration)];
  291. return active ? energyWhenActive : 0;
  292. }
  293. public float getActualEnergy() {
  294. return actualEnergy;
  295. }
  296. public boolean isOn() {
  297. //return isFlexActive()?!active:active;
  298. //Bool logic XOR
  299. return isFlexActive() ^ active;
  300. }
  301. public boolean isFlexActive() {
  302. return flexList.stream().anyMatch(
  303. flex -> flex.getState() == FlexState.IN_USE || flex.getState() == FlexState.ON_COOLDOWN);
  304. }
  305. @Override
  306. public void postDeserialize() {
  307. flexList.forEach(flex -> flex.setElement(this));
  308. sampleGraph();
  309. }
  310. public enum Priority {
  311. Low, Medium, High, Essential
  312. }
  313. }