HolonElement.java 13 KB

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