FlexExample.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. package exampleAlgorithms;
  2. import java.awt.BorderLayout;
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.FlowLayout;
  6. import java.awt.image.BufferedImage;
  7. import java.text.NumberFormat;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.HashSet;
  11. import java.util.LinkedList;
  12. import java.util.List;
  13. import java.util.Set;
  14. import java.util.concurrent.TimeUnit;
  15. import java.util.stream.Collectors;
  16. import java.util.stream.Stream;
  17. import javax.swing.BorderFactory;
  18. import javax.swing.ImageIcon;
  19. import javax.swing.JButton;
  20. import javax.swing.JCheckBox;
  21. import javax.swing.JFormattedTextField;
  22. import javax.swing.JFrame;
  23. import javax.swing.JLabel;
  24. import javax.swing.JOptionPane;
  25. import javax.swing.JPanel;
  26. import javax.swing.JProgressBar;
  27. import javax.swing.JScrollPane;
  28. import javax.swing.JSplitPane;
  29. import javax.swing.JTextArea;
  30. import javax.swing.text.NumberFormatter;
  31. import api.Algorithm;
  32. import classes.AbstractCpsObject;
  33. import classes.CpsUpperNode;
  34. import classes.HolonElement;
  35. import classes.HolonElement.Priority;
  36. import classes.HolonObject;
  37. import classes.HolonSwitch;
  38. import ui.controller.Control;
  39. import ui.controller.FlexManager;
  40. import ui.controller.FlexManager.FlexState;
  41. import ui.controller.FlexManager.FlexWrapper;
  42. import ui.model.DecoratedGroupNode;
  43. import ui.model.DecoratedNetwork;
  44. import ui.model.DecoratedState;
  45. import ui.model.Model;
  46. public class FlexExample implements Algorithm {
  47. //Parameter for Algo with default Values:
  48. private boolean closeSwitches = true;
  49. //Settings For GroupNode using and cancel
  50. private boolean useGroupNode = false;
  51. private DecoratedGroupNode dGroupNode = null;
  52. private boolean cancel = false;
  53. //Parameter defined by Algo
  54. private HashMap<Integer, AccessWrapper> access;
  55. LinkedList<List<Boolean>> resetChain = new LinkedList<List<Boolean>>();
  56. private List<Boolean> initialState;
  57. private List<HolonSwitch> switchList;
  58. private List<HolonObject> objectList;
  59. //Gui Part:
  60. private Control control;
  61. private JTextArea textArea;
  62. private JPanel content = new JPanel();
  63. //ProgressBar
  64. private JProgressBar progressBar = new JProgressBar();
  65. private int progressBarCount = 0;
  66. private long startTime;
  67. private Thread runThread;
  68. public static void main(String[] args)
  69. {
  70. JFrame newFrame = new JFrame("exampleWindow");
  71. DemoAlgo instance = new DemoAlgo();
  72. newFrame.setContentPane(instance.getAlgorithmPanel());
  73. newFrame.pack();
  74. newFrame.setVisible(true);
  75. newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  76. }
  77. public FlexExample() {
  78. content.setLayout(new BorderLayout());
  79. textArea = new JTextArea();
  80. textArea.setEditable(false);
  81. JScrollPane scrollPane = new JScrollPane(textArea);
  82. JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
  83. createOptionPanel() , scrollPane);
  84. splitPane.setResizeWeight(0.0);
  85. content.add(splitPane, BorderLayout.CENTER);
  86. content.setPreferredSize(new Dimension(800,800));
  87. }
  88. public JPanel createOptionPanel() {
  89. JPanel optionPanel = new JPanel(new BorderLayout());
  90. JScrollPane scrollPane = new JScrollPane(createParameterPanel());
  91. scrollPane.setBorder(BorderFactory.createTitledBorder("Parameter"));
  92. optionPanel.add(scrollPane, BorderLayout.CENTER);
  93. optionPanel.add(createButtonPanel(), BorderLayout.PAGE_END);
  94. return optionPanel;
  95. }
  96. private Component createParameterPanel() {
  97. JPanel parameterPanel = new JPanel(null);
  98. parameterPanel.setPreferredSize(new Dimension(510,300));
  99. // JLabel showDiagnosticsLabel = new JLabel("Set all switches closed:");
  100. // showDiagnosticsLabel.setBounds(200, 60, 170, 20);
  101. // parameterPanel.add(showDiagnosticsLabel);
  102. JPanel borderPanel = new JPanel(null);
  103. borderPanel.setBounds(200, 85, 185, 50);
  104. borderPanel.setBorder(BorderFactory.createTitledBorder(""));
  105. parameterPanel.add(borderPanel);
  106. JLabel showGroupNodeLabel = new JLabel("Use Group Node:");
  107. showGroupNodeLabel.setBounds(10, 1, 170, 20);
  108. borderPanel.add(showGroupNodeLabel);
  109. JButton selectGroupNodeButton = new JButton("Select GroupNode");
  110. selectGroupNodeButton.setEnabled(false);
  111. selectGroupNodeButton.setBounds(10, 25, 165, 20);
  112. selectGroupNodeButton.addActionListener(actionEvent -> selectGroupNode());
  113. borderPanel.add(selectGroupNodeButton);
  114. JCheckBox useGroupNodeCheckBox = new JCheckBox();
  115. useGroupNodeCheckBox.setSelected(false);
  116. useGroupNodeCheckBox.setBounds(155, 1, 25, 20);
  117. useGroupNodeCheckBox.addActionListener(actionEvent -> {
  118. useGroupNode = useGroupNodeCheckBox.isSelected();
  119. selectGroupNodeButton.setEnabled(useGroupNode);
  120. });
  121. borderPanel.add(useGroupNodeCheckBox);
  122. NumberFormat format = NumberFormat.getIntegerInstance();
  123. format.setGroupingUsed(false);
  124. format.setParseIntegerOnly(true);
  125. NumberFormatter integerFormatter = new NumberFormatter(format);
  126. integerFormatter.setMinimum(0);
  127. integerFormatter.setCommitsOnValidEdit(true);
  128. JLabel portLabel = new JLabel("between:");
  129. portLabel.setBounds(10, 330, 70, 30);
  130. parameterPanel.add(portLabel);
  131. JLabel afterLabel = new JLabel("after:");
  132. afterLabel.setBounds(10, 360, 70, 30);
  133. parameterPanel.add(afterLabel);
  134. return parameterPanel;
  135. }
  136. public JPanel createButtonPanel() {
  137. JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
  138. JButton resetButton = new JButton("ResetAll");
  139. resetButton.setToolTipText("Resets the State to before the Algorithm has runed.");
  140. resetButton.addActionListener(actionEvent -> resetAll());
  141. buttonPanel.add(resetButton);
  142. JButton cancelButton = new JButton("Cancel Run");
  143. cancelButton.addActionListener(actionEvent -> cancel());
  144. buttonPanel.add(cancelButton);
  145. JButton clearButton = new JButton("Clear Console");
  146. clearButton.addActionListener(actionEvent -> clear());
  147. buttonPanel.add(clearButton);
  148. JButton undoButton = new JButton("Undo");
  149. undoButton.setToolTipText("One Algo Step Back.");
  150. undoButton.addActionListener(actionEvent -> resetLast());
  151. buttonPanel.add(undoButton);
  152. JButton runButton = new JButton("Run");
  153. runButton.addActionListener(actionEvent -> {
  154. Runnable task = () -> run();
  155. runThread = new Thread(task);
  156. runThread.start();
  157. });
  158. buttonPanel.add(runButton);
  159. return buttonPanel;
  160. }
  161. private void cancel() {
  162. if(runThread.isAlive()) {
  163. println("");
  164. println("Cancel run.");
  165. cancel = true;
  166. progressBar.setValue(0);
  167. } else {
  168. println("Nothing to cancel.");
  169. }
  170. }
  171. private void run() {
  172. cancel = false;
  173. disableGuiInput(true);
  174. startTimer();
  175. executeDemoAlgo();
  176. if(cancel) {
  177. resetLast();
  178. disableGuiInput(false);
  179. return;
  180. }
  181. printElapsedTime();
  182. disableGuiInput(false);
  183. }
  184. private void resetLast() {
  185. if(!resetChain.isEmpty()) {
  186. println("Resetting..");
  187. resetState();
  188. resetChain.removeLast();
  189. control.resetSimulation();
  190. updateVisual();
  191. }else {
  192. println("No run inistialized.");
  193. }
  194. }
  195. private void resetAll() {
  196. if(!resetChain.isEmpty()) {
  197. println("Resetting..");
  198. setState(resetChain.getFirst());
  199. resetChain.clear();
  200. control.resetSimulation();
  201. control.setCurIteration(0);
  202. updateVisual();
  203. }else {
  204. println("No run inistialized.");
  205. }
  206. }
  207. private void disableGuiInput(boolean bool) {
  208. control.guiDiable(bool);
  209. }
  210. @Override
  211. public JPanel getAlgorithmPanel() {
  212. return content;
  213. }
  214. @Override
  215. public void setController(Control control) {
  216. this.control = control;
  217. }
  218. private void clear() {
  219. textArea.setText("");
  220. }
  221. private void print(String message) {
  222. textArea.append(message);
  223. }
  224. private void println(String message) {
  225. textArea.append(message + "\n");
  226. }
  227. private void selectGroupNode() {
  228. Object[] possibilities = control.getSimManager().getActualVisualRepresentationalState().getCreatedGroupNodes().values().stream().map(aCps -> new Handle<DecoratedGroupNode>(aCps)).toArray();
  229. @SuppressWarnings("unchecked")
  230. Handle<DecoratedGroupNode> selected = (Handle<DecoratedGroupNode>) JOptionPane.showInputDialog(content, "Select GroupNode:", "GroupNode?", JOptionPane.OK_OPTION,new ImageIcon(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)) , possibilities, "");
  231. if(selected != null) {
  232. println("Selected: " + selected);
  233. dGroupNode = selected.object;
  234. }
  235. }
  236. private void progressBarStep(){
  237. progressBar.setValue(++progressBarCount);
  238. }
  239. private void calculateProgressBarParameter() {
  240. int max = 100;
  241. progressBarCount = 0;
  242. progressBar.setValue(0);
  243. progressBar.setMaximum(max);
  244. }
  245. private void startTimer(){
  246. startTime = System.currentTimeMillis();
  247. }
  248. private void printElapsedTime(){
  249. long elapsedMilliSeconds = System.currentTimeMillis() - startTime;
  250. println("Execution Time of Algo in Milliseconds:" + elapsedMilliSeconds);
  251. }
  252. //Algo Part:
  253. /**
  254. * The Execution of the FlexAlgo.
  255. *
  256. *
  257. * Begin
  258. * for(All Networks) do
  259. *
  260. * if(not (production < consumption)) continue;
  261. *
  262. *
  263. * for(Priority emergencyShutDownPriority: priorityListASC) do
  264. * difference = Math.abs(production - consumption);
  265. * amountOfAllEnergyOffered = sumEnergyAvailable(flexList);
  266. * if(amountOfAllEnergyOffered > difference) break;
  267. * shutDownAllConsumerWithPriority(emergencyShutDownPriority)
  268. * end for
  269. *
  270. * takeAKombinationOfOffers(); (nach welchem Kriterium)
  271. *
  272. *
  273. * end for
  274. * End
  275. *
  276. */
  277. private void executeDemoAlgo() {
  278. extractPositionAndAccess();
  279. int actualIteration = control.getModel().getCurIteration();
  280. println("AlgoStart....");
  281. control.calculateStateAndVisualForCurrentTimeStep();
  282. List<Priority> priorityListASC = createPriorityListASC();
  283. DecoratedState actualstate = control.getSimManager().getActualDecorState();
  284. for(DecoratedNetwork net : actualstate.getNetworkList()) {
  285. float production = net.getSupplierList().stream().map(supplier -> supplier.getEnergyToSupplyNetwork()).reduce(0.0f, (a, b) -> a + b);
  286. float consumption = net.getConsumerList().stream().map(con -> con.getEnergyNeededFromNetwork()).reduce(0.0f, (a, b) -> a + b);
  287. float difference = Math.abs(production - consumption);
  288. println("production: " + production);
  289. println("consumption: " + consumption);
  290. println("difference: " + difference);
  291. if(difference == 0)continue;
  292. Set<HolonElement> allHolonElemntsInThisNetwork = createListOfAllHolonElemnts(net);
  293. FlexManager flexManager = control.getSimManager().getActualFlexManager();
  294. List<FlexWrapper> allOfferedFlex = flexManager.getAllFlexWrapperWithState(FlexState.OFFERED).stream().filter(flexWrapper -> allHolonElemntsInThisNetwork.contains(flexWrapper.getFlex().getElement())).collect(Collectors.toList());
  295. List<FlexWrapper> allFlexThatGetMeEnergy = allOfferedFlex.stream().filter(flexWrapper -> (flexWrapper.getFlex().bringtmir() > 0)).collect(Collectors.toList());
  296. float amountOfAllEnergyOffered = sumEnergyAvailable(allFlexThatGetMeEnergy);
  297. println("amountOfAllFlexEnergyOffered:" + amountOfAllEnergyOffered);
  298. if(production > consumption) continue;
  299. //ShuddownPriorities
  300. for(Priority emergencyShutDownPriority: priorityListASC) {
  301. if(amountOfAllEnergyOffered >= difference) break;
  302. println("ShutDown: " + emergencyShutDownPriority);
  303. difference -= shutDownAllConsumerElementsWithPriority(flexManager, allHolonElemntsInThisNetwork, emergencyShutDownPriority);
  304. }
  305. //SortFlexes
  306. allFlexThatGetMeEnergy.sort((flex1, flex2) -> Float.compare(flex1.getFlex().cost / flex1.getFlex().bringtmir(), flex2.getFlex().cost / flex2.getFlex().bringtmir()));
  307. //OrderFlexes
  308. float costForThisTimeStep = 0f;
  309. for(FlexWrapper flexWrapper : allFlexThatGetMeEnergy) {
  310. if(!flexWrapper.canOrder()) continue;
  311. float energy = flexWrapper.getFlex().bringtmir();
  312. if(energy <= difference) {
  313. println("energyGained:" + energy);
  314. difference -= energy;
  315. costForThisTimeStep += flexWrapper.getFlex().cost;
  316. flexWrapper.order();
  317. continue;
  318. }
  319. }
  320. println("CostForThisTimeStep:" + costForThisTimeStep);
  321. println("Ende");
  322. }
  323. println("AlgoEnde....");
  324. updateVisual();
  325. }
  326. private float shutDownAllConsumerElementsWithPriority(FlexManager flexManager, Set<HolonElement> allHolonElemntsInThisNetwork,
  327. Priority emergencyShutDownPriority) {
  328. List<HolonElement> elementsOfPriorityToShutdown = allHolonElemntsInThisNetwork.stream().filter(hElement -> hElement.isConsumer() && hElement.getPriority() == emergencyShutDownPriority && !hElement.isFlexActive(flexManager) && hElement.isActive()).collect(Collectors.toList());
  329. //.forEach(hElement -> hElement.setActive(false));
  330. float energyGained = elementsOfPriorityToShutdown.stream().map(hElement -> -hElement.getEnergyPerElement() * hElement.getAmount()).reduce(0.0f, (a, b) -> a + b);
  331. elementsOfPriorityToShutdown.forEach(hElement -> hElement.setActive(false));
  332. int shutdownCount = elementsOfPriorityToShutdown.size();
  333. println("Gained " + energyGained + "Energy from Shutdown with Priority:" + emergencyShutDownPriority + " AmountOfShutDowned HolonElements: " + shutdownCount);
  334. return energyGained;
  335. }
  336. private Set<HolonElement> createListOfAllHolonElemnts(DecoratedNetwork net) {
  337. Set<HolonElement> allHolonElemntsInThisNetwork = new HashSet<HolonElement>();
  338. allHolonElemntsInThisNetwork.addAll(net.getConsumerList().stream().flatMap(con -> con.getModel().getElements().stream()).collect(Collectors.toList()));
  339. allHolonElemntsInThisNetwork.addAll(net.getConsumerSelfSuppliedList().stream().flatMap(con -> con.getModel().getElements().stream()).collect(Collectors.toList()));
  340. allHolonElemntsInThisNetwork.addAll(net.getSupplierList().stream().flatMap(con -> con.getModel().getElements().stream()).collect(Collectors.toList()));
  341. return allHolonElemntsInThisNetwork;
  342. }
  343. private float sumEnergyAvailable(List<FlexWrapper> flexList) {
  344. HashMap<HolonElement, FlexWrapper> dublicateFilter = new HashMap<HolonElement, FlexWrapper>();
  345. flexList.stream().forEach(flexWrapper -> dublicateFilter.put(flexWrapper.getFlex().getElement(), flexWrapper));
  346. return dublicateFilter.values().stream().map(flexWrapper -> flexWrapper.getFlex().bringtmir()).reduce(0.0f,(a, b) -> a + b);
  347. }
  348. private List<Priority> createPriorityListASC() {
  349. List<Priority> priorityASC = new ArrayList<Priority>();
  350. priorityASC.add(Priority.Low);
  351. priorityASC.add(Priority.Medium);
  352. priorityASC.add(Priority.High);
  353. priorityASC.add(Priority.Essential);
  354. return priorityASC;
  355. }
  356. /**
  357. * Method to get the current Position alias a ListOf Booleans for aktive settings on the Objects on the Canvas.
  358. * Also initialize the Access Hashmap to swap faster positions.
  359. * @param model
  360. * @return
  361. */
  362. private List<Boolean> extractPositionAndAccess() {
  363. Model model = control.getModel();
  364. switchList = new ArrayList<HolonSwitch>();
  365. objectList = new ArrayList<HolonObject>();
  366. initialState = new ArrayList<Boolean>();
  367. access= new HashMap<Integer, AccessWrapper>();
  368. rollOutNodes((useGroupNode && (dGroupNode != null))? dGroupNode.getModel().getNodes() :model.getObjectsOnCanvas(), initialState, model.getCurIteration());
  369. resetChain.add(initialState);
  370. return initialState;
  371. }
  372. /**
  373. * Method to extract the Informations recursively out of the Model.
  374. * @param nodes
  375. * @param positionToInit
  376. * @param timeStep
  377. */
  378. private void rollOutNodes(List<AbstractCpsObject> nodes, List<Boolean> positionToInit, int timeStep) {
  379. for(AbstractCpsObject aCps : nodes) {
  380. if (aCps instanceof HolonObject) {
  381. for (HolonElement hE : ((HolonObject) aCps).getElements()) {
  382. positionToInit.add(hE.isActive());
  383. access.put(positionToInit.size() - 1 , new AccessWrapper(hE));
  384. }
  385. objectList.add((HolonObject) aCps);
  386. }
  387. else if (aCps instanceof HolonSwitch) {
  388. HolonSwitch sw = (HolonSwitch) aCps;
  389. positionToInit.add(sw.getState(timeStep));
  390. switchList.add(sw);
  391. access.put(positionToInit.size() - 1 , new AccessWrapper(sw));
  392. }
  393. else if(aCps instanceof CpsUpperNode) {
  394. rollOutNodes(((CpsUpperNode)aCps).getNodes(), positionToInit ,timeStep );
  395. }
  396. }
  397. }
  398. /**
  399. * To let the User See the current state without touching the Canvas.
  400. */
  401. private void updateVisual() {
  402. control.calculateStateAndVisualForCurrentTimeStep();
  403. control.updateCanvas();
  404. control.getGui().triggerUpdateController(null);
  405. }
  406. /**
  407. * Sets the Model back to its original State before the LAST run.
  408. */
  409. private void resetState() {
  410. setState(resetChain.getLast());
  411. }
  412. /**
  413. * Sets the State out of the given position for calculation or to show the user.
  414. * @param position
  415. */
  416. private void setState(List<Boolean> position) {
  417. for(int i = 0;i<position.size();i++) {
  418. access.get(i).setState(position.get(i));
  419. }
  420. }
  421. private void addObjectToList(List<AbstractCpsObject> listToSearch, List<HolonObject> listToAdd){
  422. for (AbstractCpsObject aCps : listToSearch) {
  423. if (aCps instanceof HolonObject) listToAdd.add((HolonObject) aCps);
  424. else if(aCps instanceof CpsUpperNode) {
  425. addObjectToList(((CpsUpperNode)aCps).getNodes(),listToAdd);
  426. }
  427. }
  428. }
  429. /**
  430. * A Wrapper Class for Access HolonElement and HolonSwitch in one Element and not have to split the List.
  431. */
  432. private class AccessWrapper {
  433. public static final int HOLONELEMENT = 0;
  434. public static final int SWITCH = 1;
  435. private int type;
  436. private HolonSwitch hSwitch;
  437. private HolonElement hElement;
  438. public AccessWrapper(HolonSwitch hSwitch){
  439. type = SWITCH;
  440. this.hSwitch = hSwitch;
  441. }
  442. public AccessWrapper(HolonElement hElement){
  443. type = HOLONELEMENT;
  444. this.hElement = hElement;
  445. }
  446. public void setState(boolean state) {
  447. if(type == HOLONELEMENT) {
  448. hElement.setActive(state);
  449. }else{//is switch
  450. hSwitch.setManualMode(true);
  451. hSwitch.setManualState(state);
  452. }
  453. }
  454. public boolean getState(int timeStep) {
  455. return (type == HOLONELEMENT)?hElement.isActive():hSwitch.getState(timeStep);
  456. }
  457. public int getType() {
  458. return type;
  459. }
  460. }
  461. private class Handle<T>{
  462. public T object;
  463. Handle(T object){
  464. this.object = object;
  465. }
  466. public String toString() {
  467. return object.toString();
  468. }
  469. }
  470. }