HolonInformationPanel.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. updateToolTips(panelHolonObject);
  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. updateToolTips(panelPriority);
  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. updateToolTips(panelEnergy);
  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. updateToolTips(panelFlexibility);
  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. updateToolTips(panelActive);
  179. this.revalidate();
  180. this.repaint();
  181. }
  182. private void updateToolTips(XChartPanel<PieChart> chart) {
  183. log.info("UpdatetoolTips");
  184. }
  185. private Optional<FilterableGroupNode> multiSelectionToFilterableGroupNode() {
  186. FilterableGroupNode temp = new FilterableGroupNode(new GroupNode("Temp"),
  187. control.getModel().getCurrentIteration());
  188. Optional<VisualRepresentationalState> visState = control.getSimManager().getActualVisualRepresentationalState();
  189. if(visState.isEmpty()) {
  190. return Optional.empty();
  191. }
  192. // GroupNodes
  193. HashMap<GroupNode, DecoratedGroupNode> accessMapGroupNode = visState.get().getCreatedGroupNodes();
  194. List<FilterableGroupNode> groupNodeList = GuiSettings.getSelectedObjects().stream()
  195. .filter(object -> object instanceof GroupNode).map(object -> accessMapGroupNode.get(object))
  196. .map(node -> new FilterableGroupNode(node, control.getModel().getCurrentIteration()))
  197. .collect(Collectors.toList());
  198. temp.getGroupNodeList().addAll(groupNodeList);
  199. // HolonObjects
  200. HashMap<HolonObject, DecoratedHolonObject> accessMapHolonObject = visState.get().createdHolonObjects;
  201. List<DecoratedHolonObject> holonObjectList = GuiSettings.getSelectedObjects().stream()
  202. .filter(object -> object instanceof HolonObject).map(object -> accessMapHolonObject.get(object))
  203. .collect(Collectors.toList());
  204. for (DecoratedHolonObject object : holonObjectList) {
  205. switch (object.getState()) {
  206. case NO_ENERGY:
  207. temp.getPassivList().add((Passiv) object);
  208. break;
  209. case PRODUCER:
  210. temp.getSupplierList().add((Supplier) object);
  211. break;
  212. default:
  213. temp.getConsumerList().add((Consumer) object);
  214. break;
  215. }
  216. }
  217. return Optional.of(temp);
  218. }
  219. public void initGraphPanel() {
  220. graphPanel.setBackground(ColorPreference.Panel.Background);
  221. panelHolonObject = new XChartPanel<PieChart>(supplyChart);
  222. panelHolonObject.setBackground(ColorPreference.Panel.Background);
  223. graphPanel.add(panelHolonObject);
  224. panelPriority = new XChartPanel<PieChart>(priorityChart);
  225. panelPriority.setBackground(ColorPreference.Panel.Background);
  226. graphPanel.add(panelPriority);
  227. panelFlexibility = new XChartPanel<PieChart>(flexibilityChart);
  228. panelFlexibility.setBackground(ColorPreference.Panel.Background);
  229. graphPanel.add(panelFlexibility);
  230. Component panel = initEnergyChart();
  231. graphPanel.add(panel);
  232. panelActive = new XChartPanel<PieChart>(activeChart);
  233. panelActive.setBackground(ColorPreference.Panel.Background);
  234. panelActive.setLayout(new GridBagLayout());
  235. graphPanel.add(panelActive);
  236. graphPanel.setBorder(BorderFactory.createLineBorder(Color.lightGray));
  237. }
  238. private JLayeredPane initEnergyChart() {
  239. JLayeredPane panel = new JLayeredPane();
  240. JPanel panelMiddle = new JPanel(new GridBagLayout());
  241. panelEnergy = new XChartPanel<PieChart>(energyChart);
  242. panelEnergy.setBackground(ColorPreference.Panel.Background);
  243. GridBagConstraints c = new GridBagConstraints();
  244. c.gridx = 0;
  245. c.gridy = 0;
  246. c.insets = new Insets(15, 0, 0, 0); // top padding
  247. JLabel difference = new JLabel("Difference:");
  248. difference.setHorizontalAlignment(JLabel.CENTER);
  249. difference.setAlignmentX(Component.CENTER_ALIGNMENT);
  250. difference.setForeground(ColorPreference.Panel.Title);
  251. difference.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD, 10));
  252. panelMiddle.add(difference, c);
  253. differenceEnergyLabelAmount.setHorizontalAlignment(JLabel.CENTER);
  254. differenceEnergyLabelAmount.setForeground(Color.red);
  255. differenceEnergyLabelAmount.setAlignmentX(Component.CENTER_ALIGNMENT);
  256. differenceEnergyLabelAmount.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD, 12));
  257. c.insets = new Insets(0, 0, 0, 0);
  258. c.gridy = 1;
  259. panelMiddle.add(differenceEnergyLabelAmount, c);
  260. panel.setLayout(new OverlayLayout(panel));
  261. panelEnergy.setOpaque(false);
  262. panelMiddle.setOpaque(false);
  263. panel.add(panelMiddle, Integer.valueOf(0));
  264. panel.add(panelEnergy, Integer.valueOf(1));
  265. return panel;
  266. }
  267. public void initTitlePanel() {
  268. titlePanel.setBackground(ColorPreference.Panel.Background);
  269. titlePanel.setBorder(new EmptyBorder(5, 5, 2, 0));
  270. }
  271. public void setDefaultPieChartSettings(PieChart chart) {
  272. PieStyler styler = chart.getStyler();
  273. styler.setChartTitleVisible(true);
  274. styler.setDefaultSeriesRenderStyle(PieSeriesRenderStyle.Donut);
  275. styler.setLabelsFont(new java.awt.Font("Arial", java.awt.Font.PLAIN, 14));
  276. styler.setLabelType(LabelType.Percentage);
  277. styler.setLabelsFontColor(Color.black);
  278. styler.setLabelsFontColorAutomaticEnabled(false);
  279. styler.setLabelsDistance(0.8);
  280. styler.setChartFontColor(ColorPreference.Panel.Title);
  281. styler.setToolTipsEnabled(true);
  282. styler.setPlotContentSize(0.9);
  283. styler.setPlotBackgroundColor(ColorPreference.Panel.Transparent);
  284. styler.setPlotBorderColor(ColorPreference.Panel.Transparent);
  285. styler.setLegendVisible(false);
  286. styler.setChartBackgroundColor(ColorPreference.Panel.Transparent);
  287. }
  288. public PieChart createSupplyStateChart() {
  289. PieChart chart = new PieChartBuilder().width(defaultWidth).height(defaultHeight).title("Holon Objects").build();
  290. setDefaultPieChartSettings(chart);
  291. chart.getStyler().setSeriesColors(supplyStateColors);
  292. // Series
  293. chart.addSeries("Producer", 0);
  294. chart.addSeries("Over supplied", 0);
  295. chart.addSeries("Supplied", 0);
  296. chart.addSeries("Partial supplied", 0);
  297. chart.addSeries("Not supplied", 0);
  298. chart.addSeries("No energy", 0);
  299. return chart;
  300. }
  301. public PieChart createPriorityChart() {
  302. PieChart chart = new PieChartBuilder().width(defaultWidth).height(defaultHeight).title("Priotities").build();
  303. setDefaultPieChartSettings(chart);
  304. // Customize Chart
  305. chart.getStyler().setSeriesColors(priorityColors);
  306. // Series
  307. chart.addSeries("Essential", 0);
  308. chart.addSeries("High", 0);
  309. chart.addSeries("Medium", 0);
  310. chart.addSeries("Low", 0);
  311. chart.addSeries("No Data", 0);
  312. return chart;
  313. }
  314. public PieChart createFlexibilityChart() {
  315. PieChart chart = new PieChartBuilder().width(defaultWidth).height(defaultHeight).title("Flexibilities").build();
  316. setDefaultPieChartSettings(chart);
  317. // Customize Chart
  318. chart.getStyler().setSeriesColors(flexibilityColors);
  319. // Series
  320. chart.addSeries("Offered", 0);
  321. chart.addSeries("In use", 0);
  322. chart.addSeries("On cooldown", 0);
  323. chart.addSeries("Not offered", 0);
  324. chart.addSeries("Unavailable", 0);
  325. chart.addSeries("No Data", 0);
  326. return chart;
  327. }
  328. public PieChart createProductionChart() {
  329. PieChart chart = new PieChartBuilder().width(defaultWidth).height(defaultHeight)
  330. .title("Production vs. Consumption").build();
  331. setDefaultPieChartSettings(chart);
  332. // Customize Chart
  333. // Customize Chart
  334. chart.getStyler().setSeriesColors(productionColors);
  335. // Series
  336. chart.addSeries("Production", 0);
  337. chart.addSeries("Consumption", 0);
  338. return chart;
  339. }
  340. public PieChart createActiveChart() {
  341. PieChart chart = new PieChartBuilder().width(defaultWidth).height(defaultHeight).title("Active").build();
  342. setDefaultPieChartSettings(chart);
  343. // Customize Chart
  344. // Customize Chart
  345. chart.getStyler().setSeriesColors(activeColors);
  346. // Series
  347. chart.addSeries("Active", 0);
  348. chart.addSeries("Inactive", 0);
  349. return chart;
  350. }
  351. }