InformationPanel.java 18 KB

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