InformationPanel.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package exampleAlgorithms;
  2. import java.awt.BorderLayout;
  3. import java.awt.Dimension;
  4. import java.awt.GridBagConstraints;
  5. import java.awt.GridBagLayout;
  6. import java.awt.GridLayout;
  7. import java.awt.Insets;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Locale;
  11. import java.util.function.Supplier;
  12. import java.util.stream.Collectors;
  13. import javax.swing.JButton;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JPanel;
  17. import javax.swing.JSeparator;
  18. import javax.swing.JTextField;
  19. import javax.swing.SpringLayout;
  20. import api.AddOn;
  21. import classes.Flexibility;
  22. import classes.HolonElement.Priority;
  23. import ui.controller.Control;
  24. import ui.controller.FlexManager.FlexState;
  25. import ui.controller.FlexManager.FlexWrapper;
  26. import ui.model.DecoratedHolonObject.HolonObjectState;
  27. import ui.model.DecoratedState;
  28. public class InformationPanel implements AddOn {
  29. Control control;
  30. private JPanel content = new JPanel();
  31. //Fields
  32. private int amountHolonObjects;
  33. private int amountSwitch;
  34. private int amountConsumer;
  35. private int amountUnderSupplied;
  36. private int amountPatiallySupplied;
  37. private int amountFullySupplied;
  38. private int amountOversupllied;
  39. private int amountSupplier;
  40. private int amountHolonElements;
  41. private int amountActiveHolonElements;
  42. private int amountInactiveHolonElements;
  43. private int amountFlexibilities;
  44. private int amountEssential;
  45. private int amountHigh;
  46. private int amountMedium;
  47. private int amountLow;
  48. private int amountConsumingFlexibilities;
  49. private int amountProducingFlexibilities;
  50. private int amountPassiv;
  51. List<Entry> entryList = new ArrayList<Entry>();
  52. private int currentEntryVerticalPosition = 0;
  53. public InformationPanel(){
  54. content.setLayout(new BorderLayout());
  55. content.add(createMiddlePanel(), BorderLayout.CENTER);
  56. JButton button = new JButton("calculate");
  57. button.addActionListener(action -> updateEntrys());
  58. content.add(button, BorderLayout.SOUTH);
  59. }
  60. public static void main(String[] args)
  61. {
  62. JFrame newFrame = new JFrame("exampleWindow");
  63. InformationPanel instance = new InformationPanel();
  64. newFrame.setContentPane(instance.getPanel());
  65. newFrame.pack();
  66. newFrame.setVisible(true);
  67. newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  68. }
  69. private JPanel createMiddlePanel() {
  70. JPanel middle = new JPanel();
  71. middle.setLayout(new GridBagLayout());
  72. int size = 60;
  73. middle.setPreferredSize(new Dimension(16*size, 9*size));
  74. entryList.add(new Entry(() -> Integer.toString(amountHolonObjects), "amountHolonObjects", middle));
  75. entryList.add(new Entry(() -> Integer.toString(amountConsumer) + addPercentage(amountConsumer, amountHolonObjects), "\tamountConsumer", middle));
  76. entryList.add(new Entry(() -> Integer.toString(amountUnderSupplied) + addPercentage(amountUnderSupplied, amountConsumer), "\t\tamountUnderSupplied", middle));
  77. entryList.add(new Entry(() -> Integer.toString(amountPatiallySupplied) + addPercentage(amountPatiallySupplied, amountConsumer), "\t\tamountPatiallySupplied", middle));
  78. entryList.add(new Entry(() -> Integer.toString(amountFullySupplied) + addPercentage(amountFullySupplied, amountConsumer), "\t\tamountFullySupplied", middle));
  79. entryList.add(new Entry(() -> Integer.toString(amountOversupllied) + addPercentage(amountOversupllied, amountConsumer), "\t\tamountOversupllied", middle));
  80. entryList.add(new Entry(() -> Integer.toString(amountSupplier) + addPercentage(amountSupplier, amountHolonObjects), "\tamountSupplier", middle));
  81. entryList.add(new Entry(() -> Integer.toString(amountPassiv) + addPercentage(amountPassiv, amountHolonObjects), "\tamountPassiv", middle));
  82. addSeperator(middle);
  83. entryList.add(new Entry(() -> Integer.toString(amountSwitch), "amountSwitch (not a HolonObject)", middle));
  84. addSeperator(middle);
  85. entryList.add(new Entry(() -> Integer.toString(amountHolonElements), "amountHolonElements", middle));
  86. entryList.add(new Entry(() -> Integer.toString(amountActiveHolonElements) + addPercentage(amountActiveHolonElements, amountHolonElements), "\tamountActiveHolonElements", middle));
  87. entryList.add(new Entry(() -> Integer.toString(amountInactiveHolonElements) + addPercentage(amountInactiveHolonElements, amountHolonElements), "\tamountInactiveHolonElements", middle));
  88. addSeperator(middle);
  89. entryList.add(new Entry(() -> Integer.toString(amountFlexibilities), "amountFlexibilities", middle));
  90. entryList.add(new Entry(() -> Integer.toString(amountConsumingFlexibilities) + addPercentage(amountConsumingFlexibilities, amountFlexibilities), "\tamountConsumingFlexibilities", middle));
  91. entryList.add(new Entry(() -> Integer.toString(amountProducingFlexibilities) + addPercentage(amountProducingFlexibilities, amountFlexibilities), "\tamountProducingFlexibilities", middle));
  92. addSeperator(middle);
  93. entryList.add(new Entry(() -> Integer.toString(amountFlexibilities), "amountFlexibilities", middle));
  94. entryList.add(new Entry(() -> Integer.toString(amountLow) + addPercentage(amountLow, amountFlexibilities), "\tamountLow", middle));
  95. entryList.add(new Entry(() -> Integer.toString(amountMedium) + addPercentage(amountMedium, amountFlexibilities), "\tamountMedium", middle));
  96. entryList.add(new Entry(() -> Integer.toString(amountHigh) + addPercentage(amountHigh, amountFlexibilities), "\tamountHigh", middle));
  97. entryList.add(new Entry(() -> Integer.toString(amountEssential) + addPercentage(amountEssential, amountFlexibilities), "\tamountEssential", middle));
  98. return middle;
  99. }
  100. String addPercentage(int amountActual, int amountMaximum) {
  101. return (amountMaximum > 0) ? " " + String.format (Locale.US, "%.2f", ((float) amountActual) / ((float) amountMaximum) * 100) + "%" : "";
  102. }
  103. private void addSeperator(JPanel middle) {
  104. GridBagConstraints c = new GridBagConstraints();
  105. c.fill = GridBagConstraints.HORIZONTAL;
  106. c.gridwidth = 2;
  107. c.weighty = 0.5;
  108. c.weightx = 0.5;
  109. c.gridx = 0;
  110. c.gridy = this.currentEntryVerticalPosition++;
  111. middle.add(new JSeparator(JSeparator.HORIZONTAL), c);
  112. }
  113. private GridBagConstraints createGbConstrain(int x, int y) {
  114. GridBagConstraints c = new GridBagConstraints();
  115. c.fill = GridBagConstraints.HORIZONTAL;
  116. c.insets = new Insets(3,6, 3, 6);
  117. c.weighty = 0.5;
  118. c.weightx = 0.5;
  119. c.gridx = x;
  120. c.gridy = y;
  121. return c;
  122. }
  123. void updateEntrys() {
  124. calculateValues();
  125. entryList.forEach(entry -> entry.update());
  126. }
  127. void calculateValues(){
  128. DecoratedState dState = control.getSimManager().getActualDecorState();
  129. amountConsumer = dState.getNetworkList().stream().map(net -> net.getAmountOfConsumer()).reduce(0, Integer::sum);
  130. this.amountSwitch = dState.getDecoratedSwitches().size();
  131. this.amountUnderSupplied = dState.getNetworkList().stream().map(net -> net.getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED)).reduce(0, Integer::sum);
  132. this.amountPatiallySupplied = dState.getNetworkList().stream().map(net -> net.getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED)).reduce(0, Integer::sum);
  133. this.amountFullySupplied = dState.getNetworkList().stream().map(net -> net.getAmountOfConsumerWithState(HolonObjectState.SUPPLIED)).reduce(0, Integer::sum);
  134. this.amountOversupllied = dState.getNetworkList().stream().map(net -> net.getAmountOfConsumerWithState(HolonObjectState.OVER_SUPPLIED)).reduce(0, Integer::sum);
  135. amountSupplier = dState.getNetworkList().stream().map(net -> net.getAmountOfSupplier()).reduce(0, Integer::sum);
  136. amountPassiv = dState.getNetworkList().stream().map(net -> net.getAmountOfPassiv()).reduce(0, Integer::sum);
  137. this.amountHolonObjects = amountConsumer + amountSupplier;
  138. int elements = 0;
  139. elements += dState.getNetworkList().stream().map(net -> net.getConsumerList().stream().map(con->con.getModel().getNumberOfElements()).reduce(0, Integer::sum) ).reduce(0, Integer::sum);
  140. elements += dState.getNetworkList().stream().map(net -> net.getSupplierList().stream().map(con->con.getModel().getNumberOfElements()).reduce(0, Integer::sum) ).reduce(0, Integer::sum);
  141. elements += dState.getNetworkList().stream().map(net -> net.getConsumerSelfSuppliedList().stream().map(con->con.getModel().getNumberOfElements()).reduce(0, Integer::sum) ).reduce(0, Integer::sum);
  142. elements += dState.getNetworkList().stream().map(net -> net.getPassivNoEnergyList().stream().map(con->con.getModel().getNumberOfElements()).reduce(0, Integer::sum) ).reduce(0, Integer::sum);
  143. this.amountHolonElements = elements;
  144. int activeElements = 0;
  145. activeElements += dState.getNetworkList().stream().map(net -> net.getConsumerList().stream().map(con->con.getModel().getNumberOfActiveElements()).reduce(0, Integer::sum) ).reduce(0, Integer::sum);
  146. activeElements += dState.getNetworkList().stream().map(net -> net.getSupplierList().stream().map(con->con.getModel().getNumberOfActiveElements()).reduce(0, Integer::sum) ).reduce(0, Integer::sum);
  147. activeElements += dState.getNetworkList().stream().map(net -> net.getConsumerSelfSuppliedList().stream().map(con->con.getModel().getNumberOfActiveElements()).reduce(0, Integer::sum) ).reduce(0, Integer::sum);
  148. activeElements += dState.getNetworkList().stream().map(net -> net.getPassivNoEnergyList().stream().map(con->con.getModel().getNumberOfActiveElements()).reduce(0, Integer::sum) ).reduce(0, Integer::sum);
  149. this.amountActiveHolonElements = activeElements;
  150. this.amountInactiveHolonElements = amountHolonElements - amountActiveHolonElements;
  151. List<Flexibility> flexList = control.getSimManager().getActualFlexManager().getAllFlexWrapper().stream().filter(flexwrapper -> flexwrapper.getFlex().offered).map(flex -> flex.getFlex()).collect(Collectors.toList());
  152. amountEssential = (int)flexList.stream().filter(flex -> flex.getElement().getPriority() == Priority.Essential).count();
  153. amountHigh = (int)flexList.stream().filter(flex -> flex.getElement().getPriority() == Priority.High).count();
  154. amountMedium = (int)flexList.stream().filter(flex -> flex.getElement().getPriority() == Priority.Medium).count();
  155. amountLow = (int)flexList.stream().filter(flex -> flex.getElement().getPriority() == Priority.Low).count();
  156. this.amountFlexibilities = amountEssential + amountHigh + amountMedium + amountLow;
  157. int cost = 0;
  158. int consumingFlex = 0;
  159. float consumingFlexEnergy = 0.0f;
  160. int producingFlex = 0;
  161. float producingFlexEnergy = 0.0f;
  162. int maxCooldown = 0;
  163. for(Flexibility flex :flexList) {
  164. cost += flex.cost;
  165. float energy = flex.bringtmir();
  166. if(energy < 0) {
  167. consumingFlex++;
  168. consumingFlexEnergy += -energy;
  169. }else {
  170. producingFlex++;
  171. producingFlexEnergy += energy;
  172. }
  173. }
  174. this.amountConsumingFlexibilities = consumingFlex;
  175. this.amountProducingFlexibilities = producingFlex;
  176. }
  177. @Override
  178. public JPanel getPanel() {
  179. return content;
  180. }
  181. @Override
  182. public void setController(Control control) {
  183. this.control = control;
  184. if(control != null) updateEntrys();
  185. }
  186. private class Entry{
  187. public Entry( Supplier<String> getter, String label, JPanel panel) {
  188. this.getter = getter;
  189. //RegisterToPanel
  190. label = label.replaceAll("\t", " ");
  191. JLabel jlabel = new JLabel(label);
  192. panel.add(jlabel, createGbConstrain(0, currentEntryVerticalPosition));
  193. tf = new JTextField(getter.get());
  194. tf.setEditable(false);
  195. panel.add(tf, createGbConstrain(1, currentEntryVerticalPosition));
  196. currentEntryVerticalPosition++;
  197. }
  198. private JTextField tf; //the Textfield to update
  199. private Supplier<String> getter; //Getter for the field
  200. public void update() {
  201. tf.setText(getter.get());
  202. }
  203. }
  204. }