HolonInformationPanel.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. package ui.view.information;
  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.GridBagConstraints;
  6. import java.awt.GridBagLayout;
  7. import java.awt.GridLayout;
  8. import java.awt.Insets;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.function.Predicate;
  12. import java.util.stream.Collectors;
  13. import java.util.stream.Stream;
  14. import javax.swing.BorderFactory;
  15. import javax.swing.JLabel;
  16. import javax.swing.JLayeredPane;
  17. import javax.swing.JPanel;
  18. import javax.swing.OverlayLayout;
  19. import javax.swing.border.EmptyBorder;
  20. import org.knowm.xchart.PieChart;
  21. import org.knowm.xchart.PieChartBuilder;
  22. import org.knowm.xchart.PieSeries.PieSeriesRenderStyle;
  23. import org.knowm.xchart.XChartPanel;
  24. import org.knowm.xchart.style.PieStyler;
  25. import org.knowm.xchart.style.PieStyler.LabelType;
  26. import model.Flexibility;
  27. import model.GroupNode;
  28. import model.HolonElement;
  29. import model.HolonObject;
  30. import model.HolonElement.Priority;
  31. import preferences.ColorPreference;
  32. import ui.controller.Control;
  33. import ui.model.Consumer;
  34. import ui.model.DecoratedGroupNode;
  35. import ui.model.DecoratedHolonObject;
  36. import ui.model.DecoratedHolonObject.HolonObjectState;
  37. import ui.model.Passiv;
  38. import ui.model.Supplier;
  39. import utility.FormatFloat;
  40. public class HolonInformationPanel extends JPanel {
  41. private Predicate<DecoratedHolonObject> stateFilter = (object) -> true;// = (object) -> object.getState() ==
  42. // HolonObjectState.PRODUCER;
  43. private Predicate<HolonElement> priorityFilter = (ele) -> ele.getPriority() == Priority.Low;
  44. private final int defaultWidth = 50;
  45. private final int defaultHeight = 200;
  46. private static final Color[] supplyStateColors = new Color[] { ColorPreference.HolonObject.Producer,
  47. ColorPreference.HolonObject.OverSupplied, ColorPreference.HolonObject.Supplied,
  48. ColorPreference.HolonObject.PartiallySupplied, ColorPreference.HolonObject.NotSupplied,
  49. ColorPreference.HolonObject.NoEnergy };
  50. private static final Color[] productionColors = new Color[] { ColorPreference.Energy.Production,
  51. ColorPreference.Energy.Consumption };
  52. private static final Color[] activeColors = new Color[] { ColorPreference.Element.Active,
  53. ColorPreference.Element.Inactive };
  54. private static final Color[] flexibilityColors = new Color[] { ColorPreference.Flexibility.Offered,
  55. ColorPreference.Flexibility.InUse, ColorPreference.Flexibility.OnCooldown,
  56. ColorPreference.Flexibility.NotOffered, ColorPreference.Flexibility.Unavailable,
  57. ColorPreference.Element.Priority.NoData };
  58. private static final Color[] priorityColors = new Color[] { ColorPreference.Element.Priority.Essential,
  59. ColorPreference.Element.Priority.High, ColorPreference.Element.Priority.Medium,
  60. ColorPreference.Element.Priority.Low, ColorPreference.Element.Priority.NoData };
  61. private PieChart supplyChart = createSupplyStateChart();
  62. private PieChart priorityChart = createPriorityChart();
  63. private PieChart flexibilityChart = createFlexibilityChart();
  64. private PieChart energyChart = createProductionChart();
  65. private PieChart activeChart = createActiveChart();
  66. private XChartPanel<PieChart> panelHolonObject;
  67. private XChartPanel<PieChart> panelPriority;
  68. private XChartPanel<PieChart> panelFlexibility;
  69. private XChartPanel<PieChart> panelEnergy;
  70. private XChartPanel<PieChart> panelActive;
  71. private JPanel graphPanel = new JPanel(new GridLayout(0, 2));
  72. private JPanel titlePanel = new JPanel(new BorderLayout(0, 0));
  73. private Control control;
  74. private JLabel differenceEnergyLabelAmount = new JLabel("");
  75. public void updateCharts() {
  76. if (control.getModel().getSelectedObjects().isEmpty()) {
  77. return;
  78. }
  79. FilterableGroupNode decoratedGroupNode = multiSelectionToFilterableGroupNode();
  80. // UPDATE SUPPLY STATE
  81. int producerAmount = decoratedGroupNode.getAmountOfSupplier(stateFilter);
  82. int overSuppliedAmount = decoratedGroupNode.getAmountOfConsumerWithState(stateFilter,
  83. HolonObjectState.OVER_SUPPLIED);
  84. int suppliedAmount = decoratedGroupNode.getAmountOfConsumerWithState(stateFilter, HolonObjectState.SUPPLIED);
  85. int partiallySuppliedAmount = decoratedGroupNode.getAmountOfConsumerWithState(stateFilter,
  86. HolonObjectState.PARTIALLY_SUPPLIED);
  87. int notSuppliedAmount = decoratedGroupNode.getAmountOfConsumerWithState(stateFilter,
  88. HolonObjectState.NOT_SUPPLIED);
  89. int noEnergyAmount = decoratedGroupNode.getAmountOfPassiv(stateFilter);
  90. supplyChart.updatePieSeries("Producer", producerAmount);
  91. supplyChart.updatePieSeries("Over supplied", overSuppliedAmount);
  92. supplyChart.updatePieSeries("Supplied", suppliedAmount);
  93. supplyChart.updatePieSeries("Partial supplied", partiallySuppliedAmount);
  94. supplyChart.updatePieSeries("Not supplied", notSuppliedAmount);
  95. supplyChart.updatePieSeries("No energy", noEnergyAmount);
  96. panelHolonObject.updateToolTips();
  97. // UPDATE PRIORITYS
  98. ui.view.information.FilterableGroupNode.PriorityCounts priorityCounts = decoratedGroupNode
  99. .getPriorityCounts(stateFilter);
  100. priorityChart.updatePieSeries("Essential", priorityCounts.essential);
  101. priorityChart.updatePieSeries("High", priorityCounts.high);
  102. priorityChart.updatePieSeries("Medium", priorityCounts.medium);
  103. priorityChart.updatePieSeries("Low", priorityCounts.low);
  104. boolean hasPriority = priorityCounts.essential + priorityCounts.high + priorityCounts.medium
  105. + priorityCounts.low > 0;
  106. priorityChart.updatePieSeries("No Data", hasPriority ? 0 : 1);
  107. panelPriority.updateToolTips();
  108. // UPDATE PRODUCTION
  109. float production = decoratedGroupNode.getProduction(stateFilter, priorityFilter);
  110. float consumption = decoratedGroupNode.getConsumption(stateFilter, priorityFilter);
  111. System.out.println("production" + production);
  112. System.out.println("consumption" + consumption);
  113. float difference = Math.abs(production - consumption);
  114. energyChart.updatePieSeries("Production", production);
  115. energyChart.updatePieSeries("Consumption", consumption);
  116. differenceEnergyLabelAmount.setText(FormatFloat.doubleFixedPlaces(1, difference));
  117. panelEnergy.updateToolTips();
  118. // UPDATE FLEXIBILITIES
  119. int inUse = 0;
  120. int offered = 0;
  121. int onCooldown = 0;
  122. int notOffered = 0;
  123. int unavailable = 0;
  124. Stream<Flexibility> flexWrapperStream = decoratedGroupNode.getFlexibilitiesStream(stateFilter, priorityFilter);
  125. List<Flexibility> flexList = flexWrapperStream.collect(Collectors.toList());
  126. for (Flexibility flex : flexList) {
  127. switch (flex.getState()) {
  128. case IN_USE:
  129. inUse++;
  130. break;
  131. case NOT_OFFERED:
  132. notOffered++;
  133. break;
  134. case OFFERED:
  135. offered++;
  136. break;
  137. case ON_COOLDOWN:
  138. onCooldown++;
  139. break;
  140. case UNAVAILABLE:
  141. unavailable++;
  142. break;
  143. default:
  144. break;
  145. }
  146. }
  147. flexibilityChart.updatePieSeries("Offered", offered);
  148. flexibilityChart.updatePieSeries("In use", inUse);
  149. flexibilityChart.updatePieSeries("On cooldown", onCooldown);
  150. flexibilityChart.updatePieSeries("Not offered", notOffered);
  151. flexibilityChart.updatePieSeries("Unavailable", unavailable);
  152. boolean hasFlex = offered + inUse + onCooldown + notOffered + unavailable > 0;
  153. flexibilityChart.updatePieSeries("No Data", hasFlex ? 0 : 1);
  154. panelFlexibility.updateToolTips();
  155. // UPDATE ActiveInActive
  156. int activeAmount = decoratedGroupNode.getAmountOfAktiveElementsFromHolonObjects(stateFilter, priorityFilter);
  157. int inactiveAmounts = decoratedGroupNode.getAmountOfElementsFromHolonObjects(stateFilter, priorityFilter)
  158. - activeAmount;
  159. activeChart.updatePieSeries("Active", activeAmount);
  160. activeChart.updatePieSeries("Inactive", inactiveAmounts);
  161. panelActive.updateToolTips();
  162. this.revalidate();
  163. this.repaint();
  164. }
  165. private FilterableGroupNode multiSelectionToFilterableGroupNode() {
  166. FilterableGroupNode temp = new FilterableGroupNode(new GroupNode("Temp"),
  167. control.getModel().getActualTimeStep());
  168. // GroupNodes
  169. HashMap<GroupNode, DecoratedGroupNode> accessMapGroupNode = control.getSimManager()
  170. .getActualVisualRepresentationalState().getCreatedGroupNodes();
  171. List<FilterableGroupNode> groupNodeList = control.getModel().getSelectedObjects().stream()
  172. .filter(object -> object instanceof GroupNode).map(object -> accessMapGroupNode.get(object))
  173. .map(node -> new FilterableGroupNode(node, control.getModel().getActualTimeStep()))
  174. .collect(Collectors.toList());
  175. temp.getGroupNodeList().addAll(groupNodeList);
  176. // HolonObjects
  177. HashMap<HolonObject, DecoratedHolonObject> accessMapHolonObject = control.getSimManager()
  178. .getActualVisualRepresentationalState().createdHolonObjects;
  179. List<DecoratedHolonObject> holonObjectList = control.getModel().getSelectedObjects().stream()
  180. .filter(object -> object instanceof HolonObject).map(object -> accessMapHolonObject.get(object))
  181. .collect(Collectors.toList());
  182. for (DecoratedHolonObject object : holonObjectList) {
  183. switch (object.getState()) {
  184. case NO_ENERGY:
  185. temp.getPassivList().add((Passiv) object);
  186. break;
  187. case PRODUCER:
  188. temp.getSupplierList().add((Supplier) object);
  189. break;
  190. default:
  191. temp.getConsumerList().add((Consumer) object);
  192. break;
  193. }
  194. }
  195. return temp;
  196. }
  197. public HolonInformationPanel(Control control) {
  198. control.OnSelectionChanged.addListener(() -> updateCharts());
  199. this.control = control;
  200. this.setLayout(new BorderLayout());
  201. initGraphPanel();
  202. initTitlePanel();
  203. this.setBackground(ColorPreference.Panel.Background);
  204. this.add(titlePanel, BorderLayout.PAGE_START);
  205. this.add(graphPanel, BorderLayout.CENTER);
  206. }
  207. public void initGraphPanel() {
  208. graphPanel.setBackground(ColorPreference.Panel.Background);
  209. panelHolonObject = new XChartPanel<PieChart>(supplyChart);
  210. panelHolonObject.setBackground(ColorPreference.Panel.Background);
  211. graphPanel.add(panelHolonObject);
  212. panelPriority = new XChartPanel<PieChart>(priorityChart);
  213. panelPriority.setBackground(ColorPreference.Panel.Background);
  214. graphPanel.add(panelPriority);
  215. panelFlexibility = new XChartPanel<PieChart>(flexibilityChart);
  216. panelFlexibility.setBackground(ColorPreference.Panel.Background);
  217. graphPanel.add(panelFlexibility);
  218. Component panel = initEnergyChart();
  219. graphPanel.add(panel);
  220. panelActive = new XChartPanel<PieChart>(activeChart);
  221. panelActive.setBackground(ColorPreference.Panel.Background);
  222. panelActive.setLayout(new GridBagLayout());
  223. graphPanel.add(panelActive);
  224. graphPanel.setBorder(BorderFactory.createLineBorder(Color.lightGray));
  225. }
  226. private JLayeredPane initEnergyChart() {
  227. JLayeredPane panel = new JLayeredPane();
  228. JPanel panelMiddle = new JPanel(new GridBagLayout());
  229. panelEnergy = new XChartPanel<PieChart>(energyChart);
  230. panelEnergy.setBackground(ColorPreference.Panel.Background);
  231. GridBagConstraints c = new GridBagConstraints();
  232. c.gridx = 0;
  233. c.gridy = 0;
  234. c.insets = new Insets(15, 0, 0, 0); // top padding
  235. JLabel difference = new JLabel("Difference:");
  236. difference.setHorizontalAlignment(JLabel.CENTER);
  237. difference.setAlignmentX(Component.CENTER_ALIGNMENT);
  238. difference.setForeground(ColorPreference.Panel.Title);
  239. difference.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD, 10));
  240. panelMiddle.add(difference, c);
  241. differenceEnergyLabelAmount.setHorizontalAlignment(JLabel.CENTER);
  242. differenceEnergyLabelAmount.setForeground(Color.red);
  243. differenceEnergyLabelAmount.setAlignmentX(Component.CENTER_ALIGNMENT);
  244. differenceEnergyLabelAmount.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD, 12));
  245. c.insets = new Insets(0, 0, 0, 0);
  246. c.gridy = 1;
  247. panelMiddle.add(differenceEnergyLabelAmount, c);
  248. panel.setLayout(new OverlayLayout(panel));
  249. panelEnergy.setOpaque(false);
  250. panelMiddle.setOpaque(false);
  251. panel.add(panelMiddle, Integer.valueOf(0));
  252. panel.add(panelEnergy, Integer.valueOf(1));
  253. return panel;
  254. }
  255. public void initTitlePanel() {
  256. titlePanel.setBackground(ColorPreference.Panel.Background);
  257. titlePanel.setBorder(new EmptyBorder(5, 5, 2, 0));
  258. }
  259. public void setDefaultPieChartSettings(PieChart chart) {
  260. PieStyler styler = chart.getStyler();
  261. styler.setChartTitleVisible(true);
  262. styler.setDefaultSeriesRenderStyle(PieSeriesRenderStyle.Donut);
  263. styler.setLabelsFont(new java.awt.Font("Arial", java.awt.Font.PLAIN, 14));
  264. styler.setLabelType(LabelType.Percentage);
  265. styler.setLabelsFontColor(Color.black);
  266. styler.setLabelsFontColorAutomaticEnabled(false);
  267. styler.setLabelsDistance(0.8);
  268. styler.setChartFontColor(ColorPreference.Panel.Title);
  269. styler.setToolTipsEnabled(true);
  270. styler.setPlotContentSize(0.9);
  271. styler.setPlotBackgroundColor(ColorPreference.Panel.Transparent);
  272. styler.setPlotBorderColor(ColorPreference.Panel.Transparent);
  273. styler.setLegendVisible(false);
  274. styler.setChartBackgroundColor(ColorPreference.Panel.Transparent);
  275. }
  276. public PieChart createSupplyStateChart() {
  277. PieChart chart = new PieChartBuilder().width(defaultWidth).height(defaultHeight).title("Holon Objects").build();
  278. setDefaultPieChartSettings(chart);
  279. chart.getStyler().setSeriesColors(supplyStateColors);
  280. // Series
  281. chart.addSeries("Producer", 0);
  282. chart.addSeries("Over supplied", 0);
  283. chart.addSeries("Supplied", 0);
  284. chart.addSeries("Partial supplied", 0);
  285. chart.addSeries("Not supplied", 0);
  286. chart.addSeries("No energy", 0);
  287. return chart;
  288. }
  289. public PieChart createPriorityChart() {
  290. PieChart chart = new PieChartBuilder().width(defaultWidth).height(defaultHeight).title("Priotities").build();
  291. setDefaultPieChartSettings(chart);
  292. // Customize Chart
  293. chart.getStyler().setSeriesColors(priorityColors);
  294. // Series
  295. chart.addSeries("Essential", 0);
  296. chart.addSeries("High", 0);
  297. chart.addSeries("Medium", 0);
  298. chart.addSeries("Low", 0);
  299. chart.addSeries("No Data", 0);
  300. return chart;
  301. }
  302. public PieChart createFlexibilityChart() {
  303. PieChart chart = new PieChartBuilder().width(defaultWidth).height(defaultHeight).title("Flexibilities").build();
  304. setDefaultPieChartSettings(chart);
  305. // Customize Chart
  306. chart.getStyler().setSeriesColors(flexibilityColors);
  307. // Series
  308. chart.addSeries("Offered", 0);
  309. chart.addSeries("In use", 0);
  310. chart.addSeries("On cooldown", 0);
  311. chart.addSeries("Not offered", 0);
  312. chart.addSeries("Unavailable", 0);
  313. chart.addSeries("No Data", 0);
  314. return chart;
  315. }
  316. public PieChart createProductionChart() {
  317. PieChart chart = new PieChartBuilder().width(defaultWidth).height(defaultHeight)
  318. .title("Production vs. Consumption").build();
  319. setDefaultPieChartSettings(chart);
  320. // Customize Chart
  321. // Customize Chart
  322. chart.getStyler().setSeriesColors(productionColors);
  323. // Series
  324. chart.addSeries("Production", 0);
  325. chart.addSeries("Consumption", 0);
  326. return chart;
  327. }
  328. public PieChart createActiveChart() {
  329. PieChart chart = new PieChartBuilder().width(defaultWidth).height(defaultHeight).title("Active").build();
  330. setDefaultPieChartSettings(chart);
  331. // Customize Chart
  332. // Customize Chart
  333. chart.getStyler().setSeriesColors(activeColors);
  334. // Series
  335. chart.addSeries("Active", 0);
  336. chart.addSeries("Inactive", 0);
  337. return chart;
  338. }
  339. }