HolonInformationPanel.java 15 KB

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