InformationPanel.java 14 KB

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