HolonElement.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. package holeg.model;
  2. import holeg.interfaces.TimelineDependent;
  3. import holeg.model.Flexibility.FlexState;
  4. import holeg.ui.controller.IndexTranslator;
  5. import holeg.ui.model.IdCounter;
  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 {
  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;
  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 eleName, float energy) {
  64. this.parentObject = parentObject;
  65. setName(eleName);
  66. setEnergy(energy);
  67. this.active = true;
  68. initGraphPoints();
  69. sampleGraph();
  70. }
  71. /**
  72. * Create a copy of the HolonElement given each one a new ID.
  73. *
  74. * @param other element to copy
  75. */
  76. public HolonElement(HolonElement other) {
  77. this.parentObject = other.parentObject;
  78. this.priority = other.getPriority();
  79. this.period = other.period;
  80. this.flexList = new ArrayList<>(other.flexList);
  81. setName(other.getName());
  82. setEnergy(other.getEnergy());
  83. this.active = other.active;
  84. setGraphPoints(new LinkedList<>());
  85. for (Vec2f p : other.getGraphPoints()) {
  86. this.graphPoints.add(new Vec2f(p));
  87. }
  88. this.actualEnergy = other.actualEnergy;
  89. sampleGraph();
  90. }
  91. @Override
  92. public Period getPeriod() {
  93. return period;
  94. }
  95. @Override
  96. public void setPeriod(Period period) {
  97. this.period = period;
  98. }
  99. /**
  100. * Get the user-defined Name.
  101. *
  102. * @return the name String
  103. */
  104. public String getName() {
  105. return name;
  106. }
  107. /**
  108. * Set the name to any new name.
  109. *
  110. * @param name the name to set
  111. */
  112. public void setName(String name) {
  113. this.name = name;
  114. }
  115. /**
  116. * Get the energyPerElement value of the selected Element.
  117. *
  118. * @return the energyPerElement
  119. */
  120. public float getEnergy() {
  121. return energy;
  122. }
  123. /**
  124. * Set the energyPerElement value of the selected Element.
  125. *
  126. * @param energyPerElement the energyPerElement to set
  127. */
  128. public void setEnergy(float energyPerElement) {
  129. log.finest(this.energy + " -> " + energyPerElement);
  130. this.energy = energyPerElement;
  131. }
  132. /**
  133. * Check the HolonElemnet is a Producer
  134. *
  135. * @return true when the energy used be each element is higher then 0
  136. */
  137. public boolean isProducer() {
  138. return (energy > 0);
  139. }
  140. /**
  141. * Check the HolonElemnet is a Consumer
  142. *
  143. * @return true when the energy used be each element is lower then 0
  144. */
  145. public boolean isConsumer() {
  146. return (energy < 0);
  147. }
  148. public Priority getPriority() {
  149. return priority;
  150. }
  151. public void setPriority(Priority priority) {
  152. this.priority = priority;
  153. }
  154. public String toString() {
  155. return "[HolonElement: " +
  156. ", eleName=" + name +
  157. ", parentName=" + parentObject.getName() +
  158. ", active=" + active +
  159. ", energyPerElement used=" + energy +
  160. "]";
  161. }
  162. /**
  163. * Initialize the {@link HolonElement#graphPoints} List with the normal 2 Points at 100%.
  164. */
  165. private void initGraphPoints() {
  166. graphPoints.clear();
  167. graphPoints.add(new Vec2f(0f, 1.0f));
  168. graphPoints.add(new Vec2f(1f, 1.0f));
  169. }
  170. /**
  171. * Getter for the graphPoint List.
  172. *
  173. * @return {@link HolonElement#graphPoints}
  174. */
  175. public LinkedList<Vec2f> getGraphPoints() {
  176. return graphPoints;
  177. }
  178. /**
  179. * Setter for the graphPoint List.
  180. */
  181. public void setGraphPoints(LinkedList<Vec2f> graphPoints) {
  182. this.graphPoints = graphPoints;
  183. }
  184. //interfaces.GraphEditable
  185. @Override
  186. public GraphType getGraphType() {
  187. return GraphType.doubleGraph;
  188. }
  189. @Override
  190. public LinkedList<Vec2f> getStateGraph() {
  191. return getGraphPoints();
  192. }
  193. @Override
  194. public void sampleGraph() {
  195. curveSample = sampleGraph(100);
  196. }
  197. @Override
  198. public void reset() {
  199. initGraphPoints();
  200. sampleGraph();
  201. }
  202. /**
  203. * 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].
  204. * e.g. 0.0 represent: "0%" , 0.34 represent: 34% 1.0 represent: "100%"
  205. *
  206. * @param sampleLength amount of samplePositions. The positions are equidistant on the Range[0,1].
  207. * @return the float array of samplepoints.
  208. */
  209. private float[] sampleGraph(int sampleLength) {
  210. ListIterator<Vec2f> iter = this.graphPoints.listIterator();
  211. Vec2f before = iter.next();
  212. Vec2f after = iter.next();
  213. float[] sampleCurve = new float[sampleLength];
  214. for (int i = 0; i < sampleLength; i++) {
  215. double graphX = (double) i / (double) (sampleLength - 1); //from 0.0 to 1.0
  216. if (graphX > after.x) {
  217. before = after;
  218. after = iter.next();
  219. }
  220. //t to determine how many percentage the graphX is to the next Point needed to calc Bezier
  221. //inverseLerp(valueBetween, min, max) (valueBetween - min) / (max - min)
  222. // e.g. old.x = 0.4, actual.x = 0.8 and graphX = 0.6 then t is 0.5
  223. double t = (after.x - before.x > 0) ? (graphX - before.x) / (after.x - before.x) : 0.0;
  224. sampleCurve[i] = (float) getYBetweenTwoPoints(t, before, after);
  225. }
  226. return sampleCurve;
  227. }
  228. /**
  229. * Helper method for {@link HolonElement#sampleGraph(int)}.
  230. * <p>
  231. * Its get the start and Endposition and calculate the Points in between for the Bezi�r Curve.
  232. * Then its get the Y Value a.k.a. the percentage from the curve at the X value t.
  233. *
  234. * @param t is in Range [0,1] and represent how much the X value is traverse along the Curve 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), end);
  242. return bezier.y;
  243. }
  244. /**
  245. * Helper method for {@link HolonElement#getYBetweenTwoPoints(double, Vec2f, Vec2f)}.
  246. * <p>
  247. * A Method for a normal Cubic Bezier Curve. A Cubic Bezier curve has four control points.
  248. *
  249. * @param t is in Range [0,1] how much it traverse along the curve.
  250. * @param p0 StartPoint
  251. * @param p1 ControlPoint
  252. * @param p2 ControlPoint
  253. * @param p3 EndPoint
  254. * @return the BezierPosition at t.
  255. */
  256. private Vec2f getBezierPoint(double t, Vec2f p0, Vec2f p1, Vec2f p2, Vec2f p3) {
  257. /*
  258. * Calculate Bezi�r:
  259. * B(t) = (1-t)^3 * P0 + 3*(1-t)^2 * t * P1 + 3*(1-t)*t^2 * P2 + t^3 * P3 , 0 < t < 1
  260. *
  261. * Source: //http://www.theappguruz.com/blog/bezier-curve-in-games
  262. */
  263. Vec2f bezier = new Vec2f();
  264. double OneSubT = 1 - t;
  265. double OneSubT2 = Math.pow(OneSubT, 2);
  266. double OneSubT3 = Math.pow(OneSubT, 3);
  267. double t2 = Math.pow(t, 2);
  268. double t3 = Math.pow(t, 3);
  269. bezier.x = (float) (OneSubT3 * p0.x + 3 * OneSubT2 * t * p1.x + 3 * OneSubT * t2 * p2.x + t3 * p3.x);
  270. bezier.y = (float) (OneSubT3 * p0.y + 3 * OneSubT2 * t * p1.y + 3 * OneSubT * t2 * p2.y + t3 * p3.y);
  271. return bezier;
  272. }
  273. /*
  274. * STATE
  275. */
  276. //TODO(Tom2021-12-1): public -> package
  277. public void calculateState(int timestep) {
  278. flexList.forEach(flex -> flex.calculateState(timestep));
  279. float energyWhenActive = energy * this.curveSample[IndexTranslator.getEffectiveIndex(this, timestep)];
  280. actualEnergy = isOn() ? energyWhenActive : 0;
  281. }
  282. /**
  283. * Get the energyPerElement currently(at given time step) available
  284. */
  285. public float calculateExpectedEnergyAtTimeStep(int timestep) {
  286. float energyWhenActive = energy * this.curveSample[IndexTranslator.getEffectiveIndex(this, timestep)];
  287. return active ? energyWhenActive : 0;
  288. }
  289. public float getActualEnergy() {
  290. return actualEnergy;
  291. }
  292. public boolean isOn() {
  293. //return isFlexActive()?!active:active;
  294. //Bool logic XOR
  295. return isFlexActive() ^ active;
  296. }
  297. public boolean isFlexActive() {
  298. return flexList.stream().anyMatch(flex -> flex.getState() == FlexState.IN_USE || flex.getState() == FlexState.ON_COOLDOWN);
  299. }
  300. public enum Priority {
  301. Low, Medium, High, Essential
  302. }
  303. public void updateReference(){
  304. flexList.forEach(flex -> flex.setElement(this));
  305. }
  306. }