InformationPanel.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. package addOns;
  2. import java.awt.BorderLayout;
  3. import java.awt.Dimension;
  4. import java.awt.GridBagConstraints;
  5. import java.awt.GridBagLayout;
  6. import java.awt.Insets;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.Locale;
  10. import java.util.Random;
  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 api.AddOn;
  21. import classes.Flexibility;
  22. import classes.HolonElement;
  23. import classes.HolonElement.Priority;
  24. import ui.controller.Control;
  25. import ui.controller.FlexManager.FlexState;
  26. import ui.model.DecoratedHolonObject.HolonObjectState;
  27. import ui.model.DecoratedState;
  28. import ui.model.DecoratedSwitch.SwitchState;
  29. import ui.model.VisualRepresentationalState;
  30. public class InformationPanel implements AddOn {
  31. Control control;
  32. private JPanel content = new JPanel();
  33. private JScrollPane scrollPane;
  34. //Fields
  35. private int amountSwitch;
  36. private int amountActiveSwitch;
  37. private int amountInactiveSwitch;
  38. private int amountHolonObjects;
  39. private int amountConsumer;
  40. private int amountUnderSupplied;
  41. private int amountPatiallySupplied;
  42. private int amountFullySupplied;
  43. private int amountOversupllied;
  44. private int amountSupplier;
  45. private int amountHolonElements;
  46. private int amountElementEssential;
  47. private int amountElementHigh;
  48. private int amountElementMedium;
  49. private int amountElementLow;
  50. private int amountElementActiveEssential;
  51. private int amountElementActiveHigh;
  52. private int amountElementActiveMedium;
  53. private int amountElementActiveLow;
  54. private int amountElementInactiveEssential;
  55. private int amountElementInactiveHigh;
  56. private int amountElementInactiveMedium;
  57. private int amountElementInactiveLow;
  58. private int amountActiveHolonElements;
  59. private int amountInactiveHolonElements;
  60. private int amountFlexibilities;
  61. private int amountActiveFlexibilities;
  62. private int amountEssential;
  63. private int amountActiveEssential;
  64. private int amountHigh;
  65. private int amountActiveHigh;
  66. private int amountMedium;
  67. private int amountActiveMedium;
  68. private int amountLow;
  69. private int amountActiveLow;
  70. private int amountConsumingFlexibilities;
  71. private int amountProducingFlexibilities;
  72. private int amountPassiv;
  73. private int amountGroupNodes;
  74. private int amountHolons;
  75. List<Entry> entryList = new ArrayList<Entry>();
  76. private int currentEntryVerticalPosition = 0;
  77. Random r = new Random();
  78. public InformationPanel(){
  79. content.setLayout(new BorderLayout());
  80. scrollPane = new JScrollPane(createMiddlePanel(), JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  81. JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  82. int size = 75;
  83. scrollPane.setPreferredSize(new Dimension(16*size, 9*size));
  84. content.add(scrollPane, BorderLayout.CENTER);
  85. JButton button = new JButton("calculate");
  86. button.addActionListener(action -> updateEntrys());
  87. content.add(button, BorderLayout.SOUTH);
  88. }
  89. public static void main(String[] args)
  90. {
  91. JFrame newFrame = new JFrame("exampleWindow");
  92. InformationPanel instance = new InformationPanel();
  93. newFrame.setContentPane(instance.getPanel());
  94. newFrame.pack();
  95. newFrame.setVisible(true);
  96. newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  97. }
  98. private JPanel createMiddlePanel() {
  99. JPanel middle = new JPanel();
  100. middle.setLayout(new GridBagLayout());
  101. entryList.add(new Entry(() -> Integer.toString(amountHolonObjects), "HolonObjects", middle));
  102. entryList.add(new Entry(() -> Integer.toString(amountConsumer) + addPercentage(amountConsumer, amountHolonObjects), "\tConsumer", middle));
  103. entryList.add(new Entry(() -> Integer.toString(amountUnderSupplied) + addPercentage(amountUnderSupplied, amountConsumer), "\t\tUnderSupplied", middle));
  104. entryList.add(new Entry(() -> Integer.toString(amountPatiallySupplied) + addPercentage(amountPatiallySupplied, amountConsumer), "\t\tPatiallySupplied", middle));
  105. entryList.add(new Entry(() -> Integer.toString(amountFullySupplied) + addPercentage(amountFullySupplied, amountConsumer), "\t\tFullySupplied", middle));
  106. entryList.add(new Entry(() -> Integer.toString(amountOversupllied) + addPercentage(amountOversupllied, amountConsumer), "\t\tOversupllied", middle));
  107. entryList.add(new Entry(() -> Integer.toString(amountSupplier) + addPercentage(amountSupplier, amountHolonObjects), "\tSupplier", middle));
  108. entryList.add(new Entry(() -> Integer.toString(amountPassiv) + addPercentage(amountPassiv, amountHolonObjects), "\tPassiv", middle));
  109. addSeperator(middle);
  110. entryList.add(new Entry(() -> Integer.toString(amountSwitch), "Switch (not a HolonObject)", middle));
  111. entryList.add(new Entry(() -> Integer.toString(amountActiveSwitch) + addPercentage(amountActiveSwitch, amountSwitch), "\t[Active] Switch", middle));
  112. entryList.add(new Entry(() -> Integer.toString(amountInactiveSwitch) + addPercentage(amountInactiveSwitch, amountSwitch), "\t[Inactive] Switch", middle));
  113. addSeperator(middle);
  114. entryList.add(new Entry(() -> Integer.toString(amountHolonElements), "HolonElements", middle));
  115. entryList.add(new Entry(() -> Integer.toString(this.amountElementLow) + addPercentage(amountElementLow, amountHolonElements), "\tLow", middle));
  116. entryList.add(new Entry(() -> Integer.toString(this.amountElementMedium) + addPercentage(amountElementMedium, amountHolonElements), "\tMedium", middle));
  117. entryList.add(new Entry(() -> Integer.toString(this.amountElementHigh) + addPercentage(amountElementHigh, amountHolonElements), "\tHigh", middle));
  118. entryList.add(new Entry(() -> Integer.toString(this.amountElementEssential) + addPercentage(amountElementEssential, amountHolonElements), "\tEssential", middle));
  119. addSeperator(middle);
  120. entryList.add(new Entry(() -> Integer.toString(amountActiveHolonElements) + addPercentage(amountActiveHolonElements, amountHolonElements), "[Active] HolonElements", middle));
  121. entryList.add(new Entry(() -> Integer.toString(this.amountElementActiveLow) + addPercentage(amountElementActiveLow, amountActiveHolonElements), "\tLow", middle));
  122. entryList.add(new Entry(() -> Integer.toString(this.amountElementActiveMedium) + addPercentage(amountElementActiveMedium, amountActiveHolonElements), "\tMedium", middle));
  123. entryList.add(new Entry(() -> Integer.toString(this.amountElementActiveHigh) + addPercentage(amountElementActiveHigh, amountActiveHolonElements), "\tHigh", middle));
  124. entryList.add(new Entry(() -> Integer.toString(this.amountElementActiveEssential) + addPercentage(amountElementActiveEssential, amountActiveHolonElements), "\tEssential", middle));
  125. addSeperator(middle);
  126. entryList.add(new Entry(() -> Integer.toString(amountInactiveHolonElements) + addPercentage(amountInactiveHolonElements, amountHolonElements), "[Inactive] HolonElements", middle));
  127. entryList.add(new Entry(() -> Integer.toString(this.amountElementInactiveLow) + addPercentage(amountElementInactiveLow, amountInactiveHolonElements), "\tLow", middle));
  128. entryList.add(new Entry(() -> Integer.toString(this.amountElementInactiveMedium) + addPercentage(amountElementInactiveMedium, amountInactiveHolonElements), "\tMedium", middle));
  129. entryList.add(new Entry(() -> Integer.toString(this.amountElementInactiveHigh) + addPercentage(amountElementInactiveHigh, amountInactiveHolonElements), "\tHigh", middle));
  130. entryList.add(new Entry(() -> Integer.toString(this.amountElementInactiveEssential) + addPercentage(amountElementInactiveEssential, amountInactiveHolonElements), "\tEssential", middle));
  131. addSeperator(middle);
  132. entryList.add(new Entry(() -> Integer.toString(amountFlexibilities), "Flexibilities", middle));
  133. entryList.add(new Entry(() -> Integer.toString(amountConsumingFlexibilities) + addPercentage(amountConsumingFlexibilities, amountFlexibilities), "\tConsumingFlexibilities", middle));
  134. entryList.add(new Entry(() -> Integer.toString(amountProducingFlexibilities) + addPercentage(amountProducingFlexibilities, amountFlexibilities), "\tProducingFlexibilities", middle));
  135. addSeperator(middle);
  136. entryList.add(new Entry(() -> Integer.toString(amountFlexibilities), "Flexibilities", middle));
  137. entryList.add(new Entry(() -> Integer.toString(amountLow) + addPercentage(amountLow, amountFlexibilities), "\tLow", middle));
  138. entryList.add(new Entry(() -> Integer.toString(amountMedium) + addPercentage(amountMedium, amountFlexibilities), "\tMedium", middle));
  139. entryList.add(new Entry(() -> Integer.toString(amountHigh) + addPercentage(amountHigh, amountFlexibilities), "\tHigh", middle));
  140. entryList.add(new Entry(() -> Integer.toString(amountEssential) + addPercentage(amountEssential, amountFlexibilities), "\tEssential", middle));
  141. addSeperator(middle);
  142. entryList.add(new Entry(() -> Integer.toString(amountActiveFlexibilities), "ActiveFlexibilities", middle));
  143. entryList.add(new Entry(() -> Integer.toString(amountActiveLow) + addPercentage(amountActiveLow, amountActiveFlexibilities), "\tLow", middle));
  144. entryList.add(new Entry(() -> Integer.toString(amountActiveMedium) + addPercentage(amountActiveMedium, amountActiveFlexibilities), "\tMedium", middle));
  145. entryList.add(new Entry(() -> Integer.toString(amountActiveHigh) + addPercentage(amountActiveHigh, amountActiveFlexibilities), "\tHigh", middle));
  146. entryList.add(new Entry(() -> Integer.toString(amountActiveEssential) + addPercentage(amountActiveEssential, amountActiveFlexibilities), "\tEssential", middle));
  147. addSeperator(middle);
  148. entryList.add(new Entry(() -> Integer.toString(amountGroupNodes), "GroupNodes", middle));
  149. entryList.add(new Entry(() -> Integer.toString(amountHolons), "Holons", middle));
  150. return middle;
  151. }
  152. String addPercentage(int amountActual, int amountMaximum) {
  153. return (amountMaximum > 0) ? " " + String.format (Locale.US, "%.2f", ((float) amountActual) / ((float) amountMaximum) * 100) + "%" : "";
  154. }
  155. private void addSeperator(JPanel middle) {
  156. GridBagConstraints c = new GridBagConstraints();
  157. c.fill = GridBagConstraints.HORIZONTAL;
  158. c.gridwidth = 2;
  159. c.weighty = 0.5;
  160. c.weightx = 0.5;
  161. c.gridx = 0;
  162. c.gridy = this.currentEntryVerticalPosition++;
  163. middle.add(new JSeparator(JSeparator.HORIZONTAL), c);
  164. }
  165. private GridBagConstraints createGbConstrain(int x, int y) {
  166. GridBagConstraints c = new GridBagConstraints();
  167. c.fill = GridBagConstraints.HORIZONTAL;
  168. c.insets = new Insets(3,6, 3, 6);
  169. c.weighty = 0.5;
  170. c.weightx = 0.5;
  171. c.gridx = x;
  172. c.gridy = y;
  173. return c;
  174. }
  175. void updateEntrys() {
  176. calculateValues();
  177. entryList.forEach(entry -> entry.update());
  178. //printInfos();
  179. }
  180. void calculateValues(){
  181. DecoratedState dState = control.getSimManager().getActualDecorState();
  182. amountConsumer = dState.getNetworkList().stream().map(net -> net.getAmountOfConsumer()).reduce(0, Integer::sum);
  183. this.amountSwitch = dState.getDecoratedSwitches().size();
  184. amountActiveSwitch = (int)dState.getDecoratedSwitches().stream().filter(dswitch -> (dswitch.getState() == SwitchState.Closed)).count();
  185. amountInactiveSwitch = amountSwitch - amountActiveSwitch;
  186. this.amountUnderSupplied = dState.getNetworkList().stream().map(net -> net.getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED)).reduce(0, Integer::sum);
  187. this.amountPatiallySupplied = dState.getNetworkList().stream().map(net -> net.getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED)).reduce(0, Integer::sum);
  188. this.amountFullySupplied = dState.getNetworkList().stream().map(net -> net.getAmountOfConsumerWithState(HolonObjectState.SUPPLIED)).reduce(0, Integer::sum);
  189. this.amountOversupllied = dState.getNetworkList().stream().map(net -> net.getAmountOfConsumerWithState(HolonObjectState.OVER_SUPPLIED)).reduce(0, Integer::sum);
  190. amountSupplier = dState.getNetworkList().stream().map(net -> net.getAmountOfSupplier()).reduce(0, Integer::sum);
  191. amountPassiv = dState.getNetworkList().stream().map(net -> net.getAmountOfPassiv()).reduce(0, Integer::sum);
  192. this.amountHolonObjects = amountConsumer + amountSupplier + amountPassiv;
  193. List<Flexibility> flexList = control.getSimManager().getActualFlexManager().getAllFlexWrapper().stream().filter(flexwrapper -> flexwrapper.getFlex().offered).map(flex -> flex.getFlex()).collect(Collectors.toList());
  194. amountEssential = (int)flexList.stream().filter(flex -> flex.getElement().getPriority() == Priority.Essential).count();
  195. amountHigh = (int)flexList.stream().filter(flex -> flex.getElement().getPriority() == Priority.High).count();
  196. amountMedium = (int)flexList.stream().filter(flex -> flex.getElement().getPriority() == Priority.Medium).count();
  197. amountLow = (int)flexList.stream().filter(flex -> flex.getElement().getPriority() == Priority.Low).count();
  198. this.amountFlexibilities = amountEssential + amountHigh + amountMedium + amountLow;
  199. List<Flexibility> flexActiveList = control.getSimManager().getActualFlexManager().getAllFlexWrapperWithState(FlexState.IN_USE).stream().map(flex -> flex.getFlex()).collect(Collectors.toList());
  200. amountActiveEssential = (int)flexActiveList.stream().filter(flex -> flex.getElement().getPriority() == Priority.Essential).count();
  201. amountActiveHigh = (int)flexActiveList.stream().filter(flex -> flex.getElement().getPriority() == Priority.High).count();
  202. amountActiveMedium = (int)flexActiveList.stream().filter(flex -> flex.getElement().getPriority() == Priority.Medium).count();
  203. amountActiveLow = (int)flexActiveList.stream().filter(flex -> flex.getElement().getPriority() == Priority.Low).count();
  204. this.amountActiveFlexibilities = amountActiveEssential + amountActiveHigh + amountActiveMedium + amountActiveLow;
  205. VisualRepresentationalState visualState =control.getSimManager().getActualVisualRepresentationalState();
  206. amountGroupNodes = visualState.getAmountfOfGroupNodes();
  207. amountHolons = dState.getNetworkList().size();
  208. List<HolonElement> listHolonElements = control.getModel().getAllHolonElemnts();
  209. this.amountHolonElements = listHolonElements.size();
  210. List<HolonElement> listAcitveHolonElemnts = listHolonElements.stream().filter(ele -> ele.isActive()).collect(Collectors.toList());
  211. this.amountActiveHolonElements = listAcitveHolonElemnts.size();
  212. this.amountInactiveHolonElements = amountHolonElements - amountActiveHolonElements;
  213. this.amountElementLow = (int) listHolonElements.stream().filter(ele -> ele.getPriority() == Priority.Low).count();
  214. this.amountElementMedium= (int) listHolonElements.stream().filter(ele -> ele.getPriority() == Priority.Medium).count();
  215. this.amountElementHigh = (int) listHolonElements.stream().filter(ele -> ele.getPriority() == Priority.High).count();
  216. this.amountElementEssential = (int) listHolonElements.stream().filter(ele -> ele.getPriority() == Priority.Essential).count();
  217. this.amountElementActiveLow = (int) listAcitveHolonElemnts.stream().filter(ele -> ele.getPriority() == Priority.Low).count();
  218. this.amountElementActiveMedium= (int) listAcitveHolonElemnts.stream().filter(ele -> ele.getPriority() == Priority.Medium).count();
  219. this.amountElementActiveHigh = (int) listAcitveHolonElemnts.stream().filter(ele -> ele.getPriority() == Priority.High).count();
  220. this.amountElementActiveEssential = (int) listAcitveHolonElemnts.stream().filter(ele -> ele.getPriority() == Priority.Essential).count();
  221. this.amountElementInactiveLow = amountElementLow - amountElementActiveLow;
  222. this.amountElementInactiveMedium= amountElementMedium - amountElementActiveMedium;
  223. this.amountElementInactiveHigh = amountElementHigh - amountElementActiveHigh;
  224. this.amountElementInactiveEssential = amountElementEssential - amountElementActiveEssential;
  225. int cost = 0;
  226. int consumingFlex = 0;
  227. float consumingFlexEnergy = 0.0f;
  228. int producingFlex = 0;
  229. float producingFlexEnergy = 0.0f;
  230. int maxCooldown = 0;
  231. for(Flexibility flex :flexList) {
  232. cost += flex.cost;
  233. float energy = flex.bringtmir();
  234. if(energy < 0) {
  235. consumingFlex++;
  236. consumingFlexEnergy += -energy;
  237. }else {
  238. producingFlex++;
  239. producingFlexEnergy += energy;
  240. }
  241. }
  242. this.amountConsumingFlexibilities = consumingFlex;
  243. this.amountProducingFlexibilities = producingFlex;
  244. }
  245. @Override
  246. public JPanel getPanel() {
  247. return content;
  248. }
  249. @Override
  250. public void setController(Control control) {
  251. this.control = control;
  252. if(control != null) updateEntrys();
  253. }
  254. private class Entry{
  255. public Entry( Supplier<String> getter, String label, JPanel panel) {
  256. this.getter = getter;
  257. //RegisterToPanel
  258. label = label.replaceAll("\t", " ");
  259. JLabel jlabel = new JLabel(label);
  260. panel.add(jlabel, createGbConstrain(0, currentEntryVerticalPosition));
  261. tf = new JTextField(getter.get());
  262. tf.setEditable(false);
  263. panel.add(tf, createGbConstrain(1, currentEntryVerticalPosition));
  264. currentEntryVerticalPosition++;
  265. }
  266. private JTextField tf; //the Textfield to update
  267. private Supplier<String> getter; //Getter for the field
  268. public void update() {
  269. tf.setText(getter.get());
  270. }
  271. }
  272. }