HolonInformationPanel.java 15 KB

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