HolonElement.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. package classes;
  2. import com.google.gson.annotations.Expose;
  3. import interfaces.GraphEditable;
  4. import interfaces.IGraphedElement;
  5. import ui.model.Model;
  6. import ui.view.IndexTranslator;
  7. import ui.view.UnitGraph;
  8. import java.awt.*;
  9. import java.awt.geom.Point2D;
  10. import java.awt.geom.Point2D.Double;
  11. import java.util.LinkedList;
  12. import java.util.ListIterator;
  13. /**
  14. * The class "HolonElement" represents any possible element that can be added to
  15. * a CpsObject (such as TV (consumer) or any energyPerElement source/producer).
  16. *
  17. * @author Gruppe14
  18. */
  19. public class HolonElement implements IGraphedElement, GraphEditable{
  20. /** Points of new TestGraph
  21. * Represent the Graph
  22. * the X component from a Point is period from 0..1
  23. * the Y component from a Point is the percentage from 0..1
  24. * currently saved in int -> TODO serelized float point class
  25. * */
  26. private LinkedList<Point2D.Double> testGraphPoints;
  27. /** Name of the object, e.g. House, 1 */
  28. @Expose
  29. private String objName;
  30. /** Name of the gadget, e.g. TV */
  31. @Expose
  32. private String eleName;
  33. /** Amount of same elements */
  34. @Expose
  35. private int amount;
  36. /** Currently used energy per element (- indicates consumation of energy, + indicates production of energy)*/
  37. @Expose
  38. private float energyPerElement;
  39. /** Whether the gadget is active or not (currently uses/produces the energy in energyPerElement) */
  40. @Expose
  41. private boolean active;
  42. /** Gives us whether this element is flexible and can flexibly use any part of the energy in flexibleEnergyAvailable */
  43. @Expose
  44. private boolean flexible;
  45. /** Flexibility (meaning the actual */
  46. @Expose
  47. private float flexibleEnergyAvailable;
  48. /** Place where the Object is Stored */
  49. @Expose
  50. private Pair<String, String> saving;
  51. /** ID */
  52. @Expose
  53. private int id;
  54. //private static final int DEFAULT_GRAPH_LENGTH=100;
  55. //private int graphLength=DEFAULT_GRAPH_LENGTH; Unimplementable due to former developer's dark magic.
  56. /*
  57. * Energy at each point of the graph with 100 predefined points. At the
  58. * beginning, it starts with all values at energyPerElement.
  59. * If switched to flexible, this represents the maximum of usable energy
  60. */
  61. private float[] curveSample;
  62. @Expose
  63. private int localPeriod;
  64. @Expose
  65. private boolean stretch;
  66. /**
  67. * Create a new HolonElement with a user-defined name, amount of the same
  68. * element, energyPerElement and corresponding model.
  69. *
  70. * @param eleName String
  71. * @param amount int
  72. * @param energy float
  73. * @param model Model
  74. */
  75. public HolonElement(String eleName, int amount, float energy, Model model) {
  76. this(eleName, amount, energy, IdCounterElem.nextId(),model);
  77. }
  78. /**
  79. * same as standard constructor, but with already given id (so the counter is not increased twice)
  80. */
  81. public HolonElement(String eleName, int amount, float energy, int id, Model model){
  82. setLocalPeriod(model==null? IGraphedElement.STANDARD_GRAPH_ACCURACY : model.getGraphIterations());
  83. setStretching(IGraphedElement.STRETCH_BY_DEFAULT);
  84. setEleName(eleName);
  85. setAmount(amount);
  86. setEnergyPerElement(energy);
  87. setActive(true);
  88. System.out.println("heiNEW");
  89. setTestGraphPoints(new LinkedList<>());
  90. initTestGraphPoints(1);
  91. sampleGraph();
  92. setId(id);
  93. setFlexibleEnergyAvailable(0);
  94. setFlexible(false);
  95. }
  96. /**
  97. * Create a copy of the HolonElement given each one a new ID.
  98. *
  99. * @param element element to copy
  100. */
  101. public HolonElement(HolonElement element) {
  102. setLocalPeriod(element.getLocalPeriod());
  103. setStretching(element.isStretching());
  104. setEleName(element.getEleName());
  105. setLocalPeriod(element.getLocalPeriod());
  106. setAmount(element.getAmount());
  107. setEnergyPerElement(element.getEnergyPerElement());
  108. setActive(element.isActive());
  109. setTestGraphPoints(new LinkedList<>());
  110. System.out.println("hei");
  111. for (Point2D.Double p : element.getTestGraphPoints()) {
  112. this.testGraphPoints.add(new Point2D.Double(p.getX(), p.getY()));
  113. }
  114. for (Point2D.Double p : getTestGraphPoints()) {
  115. System.out.println(testGraphPoints);
  116. }
  117. sampleGraph();
  118. setSaving(null);
  119. setId(IdCounterElem.nextId());
  120. setFlexibleEnergyAvailable(0);
  121. setFlexible(false);
  122. }
  123. public String getObjName() {
  124. return objName;
  125. }
  126. public void setObjName(String objName) {
  127. this.objName = objName;
  128. }
  129. /**
  130. * Set energyPerElement to any value to the whole array.
  131. *
  132. * @param energy the value
  133. */
  134. public void setAvailableEnergyPerElementAt(float energy) {
  135. this.curveSample = new float[100];
  136. for (int i = 0; i < curveSample.length; i++) {
  137. this.curveSample[i] = energy;
  138. }
  139. }
  140. /**
  141. * Get the energyPerElement currently(at given time step) available
  142. */
  143. public float getAvailableEnergyAt(int timestep) {
  144. System.out.println("getAvailableEnergyAt");
  145. return amount * energyPerElement * this.curveSample[IndexTranslator.getEffectiveIndex(this, timestep)];
  146. }
  147. /**
  148. * Set energyPerElement to any value at a given position.
  149. *
  150. * @param pos int
  151. * @param energyPerElement float
  152. */
  153. public void setAvailableEnergyPerElementAt(int pos, float energyPerElement) {
  154. this.curveSample[pos] = energyPerElement;
  155. }
  156. /**
  157. * Get the user-defined Name.
  158. *
  159. * @return the name String
  160. */
  161. public String getEleName() {
  162. return eleName;
  163. }
  164. /**
  165. * Set the name to any new name.
  166. *
  167. * @param name the name to set
  168. */
  169. public void setEleName(String name) {
  170. this.eleName = name;
  171. }
  172. /**
  173. * Get the actual amount of Elements in the selected Object.
  174. *
  175. * @return the amount int
  176. */
  177. public int getAmount() {
  178. return amount;
  179. }
  180. /**
  181. * Set the amount of the Element in the selected Object.
  182. *
  183. * @param amount the amount to set
  184. */
  185. public void setAmount(int amount) {
  186. this.amount = amount;
  187. }
  188. /**
  189. * Get the energyPerElement value of the selected Element.
  190. *
  191. * @return the energyPerElement
  192. */
  193. public float getEnergyPerElement() {
  194. return energyPerElement;
  195. }
  196. /**
  197. * Check the HolonElemnet is a Producer
  198. * @return true when the energy used be each element is higher then 0
  199. */
  200. public boolean isProducer()
  201. {
  202. return (energyPerElement > 0);
  203. }
  204. /**
  205. * Check the HolonElemnet is a Consumer
  206. * @return true when the energy used be each element is lower then 0
  207. */
  208. public boolean isConsumer()
  209. {
  210. return (energyPerElement < 0);
  211. }
  212. /**
  213. * Set the energyPerElement value of the selected Element.
  214. *
  215. * @param energyPerElement the energyPerElement to set
  216. */
  217. public void setEnergyPerElement(float energyPerElement) {
  218. this.energyPerElement = energyPerElement;
  219. }
  220. /**
  221. * Get the Status of the Element (see description of variables).
  222. *
  223. * @return the active
  224. */
  225. public boolean isActive() {
  226. return active;
  227. }
  228. /**
  229. * Set the Status of the Element (see description of variables).
  230. *
  231. * @param active the active to set
  232. */
  233. public void setActive(boolean active) {
  234. this.active = active;
  235. }
  236. /**
  237. * Multiply the amount of gadgets, given by the user, and the
  238. * consumption/production. If the switch isWorking is turned off for on
  239. * gadget, the energyPerElement of this gadget have to be subtracted.
  240. *
  241. * @return totalEnergy (actual)
  242. */
  243. public float getOverallEnergy() {
  244. float totalEnergy = amount * energyPerElement;
  245. return totalEnergy;
  246. }
  247. /**
  248. * Get the energyPerElement value at a selected time x.
  249. *
  250. * @param timestep int
  251. * @return energyPerElement value
  252. */
  253. public float getOverallEnergyAtTimeStep(int timestep) {
  254. if (flexible) {
  255. return ((float) amount) * energyPerElement;
  256. } else {
  257. return ((float) amount) * energyPerElement * curveSample[IndexTranslator.getEffectiveIndex(this, timestep)];
  258. }
  259. }
  260. /**
  261. * Get the points (values) in the graph.
  262. *
  263. * @return the Graph Points
  264. */
  265. public LinkedList<Point> getGraphPoints() {
  266. return null;
  267. //TODO DELETE
  268. }
  269. /**
  270. * Set the points (values) in the graph.
  271. *
  272. * @param points the Graph points
  273. */
  274. public void setGraphPoints(LinkedList<Point> points) {
  275. //TODO DELETE
  276. }
  277. /**
  278. * Get the flexibleEnergyAvailable of an element
  279. */
  280. public float getFlexibleEnergyAvailablePerElement() {
  281. return this.flexibleEnergyAvailable;
  282. }
  283. /**
  284. * Set the flexibleEnergyAvailable of an element
  285. */
  286. public void setFlexibleEnergyAvailable(float f) {
  287. this.flexibleEnergyAvailable = f;
  288. }
  289. /**
  290. * Get the flexibleEnergyAvailable of an element
  291. */
  292. public boolean isFlexible() {
  293. return this.flexible;
  294. }
  295. /**
  296. * Set the flexibleEnergyAvailable of an element, ~switches energyPerElement and flexible energyPerElement
  297. */
  298. public void setFlexible(boolean b) {
  299. // if flexibleEnergyAvailable was set to true
  300. if (b && !this.flexible) {
  301. this.flexible = b;
  302. // move energyPerElement to flexibleEnergyAvailable (becomes the possible-to-use energyPerElement)
  303. if (getEnergyPerElement() != 0) {
  304. setFlexibleEnergyAvailable(getEnergyPerElement());
  305. setEnergyPerElement(0);
  306. }
  307. } else if (!b && this.flexible) {
  308. this.flexible = b;
  309. // move the energyPerElement to actually used energyPerElement and set flexible amount to 0
  310. if (getFlexibleEnergyAvailablePerElement() != 0) {
  311. setEnergyPerElement(getFlexibleEnergyAvailablePerElement());
  312. }
  313. setFlexibleEnergyAvailable(0);
  314. }
  315. }
  316. /**
  317. * Get the Id of the selected HolonElement.
  318. *
  319. * @return id the id
  320. */
  321. public int getId() {
  322. return id;
  323. }
  324. /**
  325. * Set the ID of the HolonElement (one time only).
  326. *
  327. * @param id the id
  328. */
  329. public void setId(int id) {
  330. this.id = id;
  331. }
  332. /**
  333. * @return the saving
  334. */
  335. public Pair<String, String> getSaving() {
  336. return saving;
  337. }
  338. /**
  339. * @param saving the saving to set
  340. */
  341. public void setSaving(Pair<String, String> saving) {
  342. this.saving = saving;
  343. }
  344. public String toString() {
  345. StringBuilder sb = new StringBuilder();
  346. sb.append("[HolonElement: ");
  347. sb.append("id=").append(id)
  348. .append(", eleName=").append(eleName)
  349. .append(", amount=").append(amount)
  350. .append(", active=").append(active)
  351. .append(", flexible=").append(flexible)
  352. .append(", energyPerElement used=").append(energyPerElement)
  353. .append(", flexible energyPerElement available=").append(flexibleEnergyAvailable);
  354. sb.append("]");
  355. return sb.toString();
  356. }
  357. @Override
  358. public void setLocalPeriod(int period) {
  359. localPeriod=period;
  360. }
  361. @Override
  362. public int getLocalPeriod() {
  363. return localPeriod;
  364. }
  365. @Override
  366. public boolean isStretching() {
  367. return stretch;
  368. }
  369. @Override
  370. public void setStretching(boolean stretch) {
  371. this.stretch=stretch;
  372. }
  373. //--> Test Area --> TODO Neue float Point Klasse
  374. public void initTestGraphPoints(int percentage)
  375. {
  376. testGraphPoints.clear();
  377. testGraphPoints.add(new Point2D.Double(0,percentage));
  378. testGraphPoints.add(new Point2D.Double(1,percentage));
  379. }
  380. public LinkedList<Point2D.Double> getTestGraphPoints() {
  381. return testGraphPoints;
  382. }
  383. public void setTestGraphPoints(LinkedList<Point2D.Double> testGraphPoints) {
  384. this.testGraphPoints = testGraphPoints;
  385. }
  386. @Override
  387. public Graphtype getGraphType() {
  388. return Graphtype.doubleGraph;
  389. }
  390. @Override
  391. public LinkedList<Double> getStateGraph() {
  392. return getTestGraphPoints();
  393. }
  394. private Point.Double getBezierPoint(double t, Point.Double p0, Point.Double p1,Point.Double p2,Point.Double p3) {
  395. /*
  396. * Calculate Beziér:
  397. * B(t) = (1-t)^3 * P0 + 3*(1-t)^2 * t * P1 + 3*(1-t)*t^2 * P2 + t^3 * P3 , 0 < t < 1
  398. *
  399. * Source: //http://www.theappguruz.com/blog/bezier-curve-in-games
  400. */
  401. Point.Double bezier = new Point.Double();
  402. double OneSubT = 1-t;
  403. double OneSubT2 = Math.pow(OneSubT, 2);
  404. double OneSubT3 = Math.pow(OneSubT, 3);
  405. double t2 = Math.pow(t , 2);
  406. double t3 = Math.pow(t , 3);
  407. bezier.x = OneSubT3 * p0.x + 3 * OneSubT2 * t * p1.x + 3 * OneSubT * t2 * p2.x + t3 * p3.x;
  408. bezier.y = OneSubT3 * p0.y + 3 * OneSubT2 * t * p1.y + 3 * OneSubT * t2 * p2.y + t3 * p3.y;
  409. return bezier;
  410. }
  411. private double getYBetweenTwoPoints(double t, Point.Double start, Point.Double end) {
  412. double mitte = (start.x + end.x)* 0.5;
  413. Point.Double bezier = getBezierPoint(t, start, new Point.Double(mitte, start.y), new Point.Double(mitte, end.y), end);
  414. return bezier.y;
  415. }
  416. private float[] sampleGraph(int sampleLength)
  417. {
  418. ListIterator<Point2D.Double> iter = this.testGraphPoints.listIterator();
  419. Point.Double before = iter.next();
  420. Point.Double after = iter.next();
  421. float [] sampleCurve = new float[sampleLength];
  422. for(int i = 0; i<sampleLength ; i++)
  423. {
  424. double graphX = (double)i / (double) (sampleLength - 1); //from 0.0 to 1.0
  425. if(graphX > after.x)
  426. {
  427. before = after;
  428. after = iter.next();
  429. }
  430. //inverseLerp(valueBetween, min, max) (valueBetween - min) / (max - min)
  431. // e.g. old.x = 0.4, actual.x = 0.8 and graphX = 0.6 then t is 0.5
  432. double t = (after.x -before.x > 0)? (graphX - before.x) / (after.x -before.x) : 0.0;
  433. sampleCurve[i] = (float) getYBetweenTwoPoints(t, before, after);
  434. }
  435. return sampleCurve;
  436. }
  437. @Override
  438. public void sampleGraph() {
  439. curveSample = sampleGraph(100);
  440. }
  441. }