StatisticGraph.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package ui.view;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.RenderingHints;
  7. import java.awt.geom.GeneralPath;
  8. import java.util.ArrayList;
  9. import javax.swing.JPanel;
  10. import classes.HolonElement;
  11. import classes.HolonObject;
  12. import classes.HolonSwitch;
  13. import classes.TrackedDataSet;
  14. import ui.controller.Control;
  15. import ui.model.Model;
  16. public class StatisticGraph extends JPanel {
  17. /**
  18. *
  19. */
  20. private static final long serialVersionUID = 1L;
  21. // Max Objects
  22. final int MAX_OBJECTS = 8;
  23. // Maximum y Value
  24. double maximum = 0;
  25. // is the Simulation running?
  26. private boolean isSimRunning;
  27. // model and controller
  28. private Model model;
  29. private Control controller;
  30. // Graphics2D
  31. private Graphics2D g2;
  32. GeneralPath path = new GeneralPath();
  33. // Data
  34. private ArrayList<TrackedDataSet> objects = new ArrayList<>();
  35. /**
  36. * Constructor.
  37. *
  38. * @param model
  39. * the Model
  40. * @param control
  41. * the Controller
  42. */
  43. public StatisticGraph(final Model model, Control control) {
  44. this.controller = control;
  45. this.setBackground(Color.WHITE);
  46. }
  47. /**
  48. * Paints all Components on the Canvas.
  49. *
  50. * @param g
  51. * Graphics
  52. *
  53. */
  54. public void paintComponent(Graphics g) {
  55. super.paintComponent(g);
  56. // Graphics2D init
  57. g2 = (Graphics2D) g;
  58. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  59. g2.setRenderingHints(rh);
  60. // Paint the Grid
  61. g2.setStroke(new BasicStroke(0));
  62. g2.setColor(Color.BLACK);
  63. for (int i = 0; i <= this.getWidth(); i += 10) {
  64. g2.drawLine(i, 0, i, this.getHeight());
  65. }
  66. for (int i = 0; i <= this.getHeight(); i += 5) {
  67. g2.drawLine(0, i, this.getWidth(), i);
  68. }
  69. isSimRunning = model.getIsSimulation();
  70. // if sim is on
  71. if (isSimRunning) {
  72. g2.setStroke(new BasicStroke(3));
  73. // Calculate the Maximum
  74. calcMaximum();
  75. // Calculate values for each set and add them
  76. addValues();
  77. //Create Paths and draw them
  78. for (TrackedDataSet set : objects) {
  79. path.reset();
  80. switch (set.getProperty()) {
  81. case TrackedDataSet.CONSUMPTION:
  82. case TrackedDataSet.PRODUCTION:
  83. case TrackedDataSet.ACTIVATED_ELEMENTS:
  84. createPathFloats(set);
  85. break;
  86. case TrackedDataSet.ON_OFF:
  87. createPathBooleans(set);
  88. break;
  89. default:
  90. break;
  91. }
  92. g2.setColor(set.getColor());
  93. g2.draw(path);
  94. }
  95. }
  96. }
  97. /**
  98. * Add an Object to the Graph if the maximum has not reached yet.
  99. *
  100. * @param obj
  101. * the Object to add
  102. */
  103. public void addObject(TrackedDataSet set) {
  104. if (objects.size() < MAX_OBJECTS && !objects.contains(set)) {
  105. objects.add(set);
  106. }
  107. }
  108. /**
  109. * Removes an Object from the Graph.
  110. *
  111. * @param obj
  112. * the Object to remove
  113. */
  114. public void removeObject() {
  115. for (TrackedDataSet set : objects) {
  116. controller.addTextToConsole("Function not available");
  117. }
  118. }
  119. /**
  120. * converts the number to fit the canvas.
  121. *
  122. * @param d
  123. * the number to convert
  124. * @return the converted number
  125. */
  126. public double convertToCanvasY(float d) {
  127. return Math.abs((this.getHeight() - (d * (this.getHeight() / maximum))));
  128. }
  129. /**
  130. * Does take into account which timestep is watched, calculates the max
  131. * values.
  132. *
  133. * @return the currentEnergy
  134. */
  135. public float getEnergyAtCurrentTimeStep(HolonObject obj) {
  136. float temp = 0;
  137. for (HolonElement e : obj.getElements()) {
  138. if (e.getActive()) {
  139. temp = temp + e.getEnergyAt()[model.getCurIteration()];
  140. }
  141. }
  142. return temp;
  143. }
  144. /**
  145. * Calculate the Max Value of the Graph
  146. */
  147. private void calcMaximum() {
  148. maximum = 0;
  149. for (TrackedDataSet set : objects) {
  150. int val = 0;
  151. switch (set.getProperty()) {
  152. case TrackedDataSet.CONSUMPTION:
  153. for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
  154. if (h.getEnergy() < 0) {
  155. val += h.getEnergy();
  156. }
  157. }
  158. val *= -1;
  159. break;
  160. case TrackedDataSet.PRODUCTION:
  161. for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
  162. if (h.getEnergy() > 0) {
  163. val += h.getEnergy();
  164. }
  165. }
  166. break;
  167. case TrackedDataSet.ACTIVATED_ELEMENTS:
  168. for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
  169. if (h.getEnergy() < 0) {
  170. val += h.getAmount();
  171. }
  172. }
  173. break;
  174. default:
  175. break;
  176. }
  177. if (val > maximum) {
  178. maximum = val;
  179. }
  180. }
  181. }
  182. /**
  183. * Add the Current Values to each set
  184. */
  185. private void addValues() {
  186. for (TrackedDataSet set : objects) {
  187. switch (set.getProperty()) {
  188. case TrackedDataSet.CONSUMPTION:
  189. for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
  190. if (h.getEnergy() < 0) {
  191. set.setValAt(-h.getEnergyAt()[model.getCurIteration()], model.getCurIteration());
  192. }
  193. }
  194. break;
  195. case TrackedDataSet.PRODUCTION:
  196. for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
  197. if (h.getEnergy() > 0) {
  198. set.setValAt(h.getEnergyAt()[model.getCurIteration()], model.getCurIteration());
  199. }
  200. }
  201. break;
  202. case TrackedDataSet.ACTIVATED_ELEMENTS:
  203. for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
  204. if (h.getEnergy() < 0) {
  205. }
  206. }
  207. break;
  208. case TrackedDataSet.ON_OFF:
  209. if (((HolonSwitch) set.getCpsObject()).getState()) {
  210. set.setValAt(1, model.getCurIteration());
  211. } else {
  212. set.setValAt(0, model.getCurIteration());
  213. }
  214. break;
  215. default:
  216. break;
  217. }
  218. }
  219. }
  220. /**
  221. * create Path with floats
  222. *
  223. * @param set
  224. */
  225. private void createPathFloats(TrackedDataSet set) {
  226. boolean init = true;
  227. for (int i = 0; i < set.getValues().length - 1; i++) {
  228. if (init && set.getValues()[i] != -1) {
  229. path.moveTo(model.getCurIteration() * this.getWidth() / model.getIterations() - 1,
  230. convertToCanvasY(Math.abs(set.getValues()[i])));
  231. }
  232. path.lineTo(model.getCurIteration() * this.getWidth() / model.getIterations(),
  233. convertToCanvasY(Math.abs(set.getValues()[i + 1])));
  234. }
  235. }
  236. /**
  237. * create Path with booleans(0 and 1)
  238. *
  239. * @param set
  240. */
  241. private void createPathBooleans(TrackedDataSet set) {
  242. boolean init = true;
  243. for (int i = 0; i < set.getValues().length - 1; i++) {
  244. if (init && set.getValues()[i] != -1) {
  245. path.moveTo(model.getCurIteration() * this.getWidth() / model.getIterations() - 1,
  246. convertToCanvasY((float) (set.getValues()[i] * maximum)));
  247. }
  248. path.lineTo(model.getCurIteration() * this.getWidth() / model.getIterations(),
  249. convertToCanvasY((float) (set.getValues()[i + 1] * maximum)));
  250. }
  251. }
  252. }