HolonElement.java 11 KB

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