DemoAlgo.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. package holeg.algorithm.example;
  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.List;
  11. import java.util.Optional;
  12. import java.util.concurrent.TimeUnit;
  13. import java.util.stream.Stream;
  14. import javax.swing.BorderFactory;
  15. import javax.swing.ImageIcon;
  16. import javax.swing.JButton;
  17. import javax.swing.JCheckBox;
  18. import javax.swing.JFormattedTextField;
  19. import javax.swing.JFrame;
  20. import javax.swing.JLabel;
  21. import javax.swing.JOptionPane;
  22. import javax.swing.JPanel;
  23. import javax.swing.JScrollPane;
  24. import javax.swing.JSplitPane;
  25. import javax.swing.JTextArea;
  26. import javax.swing.text.NumberFormatter;
  27. import holeg.api.AddOn;
  28. import holeg.model.AbstractCanvasObject;
  29. import holeg.model.GroupNode;
  30. import holeg.model.HolonElement;
  31. import holeg.model.HolonObject;
  32. import holeg.model.HolonSwitch;
  33. import holeg.model.HolonSwitch.SwitchMode;
  34. import holeg.model.HolonSwitch.SwitchState;
  35. import holeg.ui.controller.Control;
  36. import holeg.ui.model.Model;
  37. public class DemoAlgo implements AddOn {
  38. //Parameter for Algo with default Values:
  39. //Settings For GroupNode using and cancel
  40. private boolean useGroupNode = false;
  41. private Optional<GroupNode> groupNode = Optional.empty();
  42. private boolean cancel = false;
  43. //Parameter defined by Algo
  44. private HashMap<Integer, AccessWrapper> access;
  45. private List<Boolean> initialState;
  46. private List<HolonSwitch> switchList;
  47. private List<HolonObject> objectList;
  48. //Gui Part:
  49. private Control control;
  50. private JTextArea textArea;
  51. private JPanel content = new JPanel();
  52. private long startTime;
  53. private Thread runThread;
  54. //Windrad
  55. private HolonObject windrad;
  56. private int waitDurationWindradStep = 400;
  57. private int waitDurationEnd = 1000;
  58. int counter;
  59. public static void main(String[] args)
  60. {
  61. JFrame newFrame = new JFrame("exampleWindow");
  62. DemoAlgo instance = new DemoAlgo();
  63. newFrame.setContentPane(instance.getPanel());
  64. newFrame.pack();
  65. newFrame.setVisible(true);
  66. newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  67. }
  68. public DemoAlgo() {
  69. content.setLayout(new BorderLayout());
  70. textArea = new JTextArea();
  71. textArea.setEditable(false);
  72. JScrollPane scrollPane = new JScrollPane(textArea);
  73. JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
  74. createOptionPanel() , scrollPane);
  75. splitPane.setResizeWeight(0.0);
  76. content.add(splitPane, BorderLayout.CENTER);
  77. content.setPreferredSize(new Dimension(800,800));
  78. }
  79. public JPanel createOptionPanel() {
  80. JPanel optionPanel = new JPanel(new BorderLayout());
  81. JScrollPane scrollPane = new JScrollPane(createParameterPanel());
  82. scrollPane.setBorder(BorderFactory.createTitledBorder("Parameter"));
  83. optionPanel.add(scrollPane, BorderLayout.CENTER);
  84. optionPanel.add(createButtonPanel(), BorderLayout.PAGE_END);
  85. return optionPanel;
  86. }
  87. private Component createParameterPanel() {
  88. JPanel parameterPanel = new JPanel(null);
  89. parameterPanel.setPreferredSize(new Dimension(510,300));
  90. // JLabel showDiagnosticsLabel = new JLabel("Set all switches closed:");
  91. // showDiagnosticsLabel.setBounds(200, 60, 170, 20);
  92. // parameterPanel.add(showDiagnosticsLabel);
  93. JPanel borderPanel = new JPanel(null);
  94. borderPanel.setBounds(200, 85, 185, 50);
  95. borderPanel.setBorder(BorderFactory.createTitledBorder(""));
  96. parameterPanel.add(borderPanel);
  97. JLabel showGroupNodeLabel = new JLabel("Use Group Node:");
  98. showGroupNodeLabel.setBounds(10, 1, 170, 20);
  99. borderPanel.add(showGroupNodeLabel);
  100. JButton selectGroupNodeButton = new JButton("Select GroupNode");
  101. selectGroupNodeButton.setEnabled(false);
  102. selectGroupNodeButton.setBounds(10, 25, 165, 20);
  103. selectGroupNodeButton.addActionListener(actionEvent -> selectGroupNode());
  104. borderPanel.add(selectGroupNodeButton);
  105. JCheckBox useGroupNodeCheckBox = new JCheckBox();
  106. useGroupNodeCheckBox.setSelected(false);
  107. useGroupNodeCheckBox.setBounds(155, 1, 25, 20);
  108. useGroupNodeCheckBox.addActionListener(actionEvent -> {
  109. useGroupNode = useGroupNodeCheckBox.isSelected();
  110. selectGroupNodeButton.setEnabled(useGroupNode);
  111. });
  112. borderPanel.add(useGroupNodeCheckBox);
  113. // JCheckBox switchesCheckBox = new JCheckBox();
  114. // switchesCheckBox.setSelected(closeSwitches);
  115. // switchesCheckBox.setBounds(370, 60, 25, 20);
  116. // switchesCheckBox.addActionListener(actionEvent -> closeSwitches = switchesCheckBox.isSelected());
  117. // parameterPanel.add(switchesCheckBox);
  118. JButton selectRoom1Button = new JButton("Select");
  119. selectRoom1Button.setBounds(10,300, 90, 20);
  120. selectRoom1Button.addActionListener(actionEvent -> this.selectHolonObject());
  121. parameterPanel.add(selectRoom1Button);
  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. JFormattedTextField betweenTF = new JFormattedTextField(integerFormatter);
  132. betweenTF.setText(""+waitDurationWindradStep);
  133. betweenTF.setBounds(80 ,330, 80, 30);
  134. betweenTF.addPropertyChangeListener(propertyChange ->{
  135. String text = betweenTF.getValue().toString();
  136. text = text.replaceAll("\\s", "");
  137. waitDurationWindradStep = Integer.parseInt((text));
  138. });
  139. parameterPanel.add(betweenTF);
  140. JLabel afterLabel = new JLabel("after:");
  141. afterLabel.setBounds(10, 360, 70, 30);
  142. parameterPanel.add(afterLabel);
  143. JFormattedTextField afterTF = new JFormattedTextField(integerFormatter);
  144. afterTF.setText(""+waitDurationEnd);
  145. afterTF.setBounds(80 ,360, 80, 30);
  146. afterTF.addPropertyChangeListener(propertyChange ->{
  147. String text = afterTF.getValue().toString();
  148. text = text.replaceAll("\\s", "");
  149. waitDurationEnd = Integer.parseInt((text));
  150. });
  151. parameterPanel.add(afterTF);
  152. return parameterPanel;
  153. }
  154. public JPanel createButtonPanel() {
  155. JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
  156. JButton cancelButton = new JButton("Cancel Run");
  157. cancelButton.addActionListener(actionEvent -> cancel());
  158. buttonPanel.add(cancelButton);
  159. JButton clearButton = new JButton("Clear Console");
  160. clearButton.addActionListener(actionEvent -> clear());
  161. buttonPanel.add(clearButton);
  162. JButton resetButton = new JButton("Reset");
  163. resetButton.setToolTipText("Resets the State to before the Algorithm has runed.");
  164. resetButton.addActionListener(actionEvent -> reset());
  165. buttonPanel.add(resetButton);
  166. JButton runButton = new JButton("Run");
  167. runButton.addActionListener(actionEvent -> {
  168. Runnable task = () -> run();
  169. runThread = new Thread(task);
  170. runThread.start();
  171. });
  172. buttonPanel.add(runButton);
  173. return buttonPanel;
  174. }
  175. private void cancel() {
  176. if(runThread.isAlive()) {
  177. println("");
  178. println("Cancel run.");
  179. cancel = true;
  180. } else {
  181. println("Nothing to cancel.");
  182. }
  183. }
  184. private void run() {
  185. cancel = false;
  186. disableGuiInput(true);
  187. startTimer();
  188. executeDemoAlgo();
  189. if(cancel) {
  190. reset();
  191. disableGuiInput(false);
  192. return;
  193. }
  194. printElapsedTime();
  195. disableGuiInput(false);
  196. }
  197. private void reset() {
  198. if(initialState != null) {
  199. println("Resetting..");
  200. resetState();
  201. updateVisual();
  202. }else {
  203. println("No run inistialized.");
  204. }
  205. }
  206. private void disableGuiInput(boolean bool) {
  207. control.guiSetEnabled(bool);
  208. }
  209. @Override
  210. public JPanel getPanel() {
  211. return content;
  212. }
  213. @Override
  214. public void setController(Control control) {
  215. this.control = control;
  216. }
  217. private void clear() {
  218. textArea.setText("");
  219. }
  220. private void println(String message) {
  221. textArea.append(message + "\n");
  222. }
  223. private void selectGroupNode() {
  224. Object[] possibilities = control.getModel().getCanvas().getAllGroupNodeObjectsRecursive().toArray();
  225. GroupNode selected = (GroupNode) JOptionPane.showInputDialog(content, "Select GroupNode:", "GroupNode?", JOptionPane.OK_OPTION,new ImageIcon(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)) , possibilities, "");
  226. if(selected != null) {
  227. println("Selected: " + selected);
  228. groupNode = Optional.of(selected);
  229. }
  230. }
  231. private void startTimer(){
  232. startTime = System.currentTimeMillis();
  233. }
  234. private void printElapsedTime(){
  235. long elapsedMilliSeconds = System.currentTimeMillis() - startTime;
  236. println("Execution Time of Algo in Milliseconds:" + elapsedMilliSeconds);
  237. }
  238. //Algo Part:
  239. private void executeDemoAlgo() {
  240. extractPositionAndAccess();
  241. counter = 0;
  242. int actualIteration = control.getModel().getCurrentIteration();
  243. deactivateWindrad();
  244. setAllSwitchesClosed();
  245. updateVisual();
  246. try {
  247. //Schalte Slow das Windrad Ein
  248. if(windrad == null)return;
  249. windrad.getElements().forEach(hE -> {
  250. hE.active = true;
  251. try {
  252. TimeUnit.MILLISECONDS.sleep(waitDurationWindradStep);
  253. } catch (InterruptedException e) {
  254. }
  255. updateVisual();
  256. });
  257. TimeUnit.MILLISECONDS.sleep(waitDurationEnd);
  258. } catch (InterruptedException e) {
  259. }
  260. setHolonElemntsAktiv(actualIteration);
  261. println("Changed Elements: " + counter);
  262. updateVisual();
  263. }
  264. private void deactivateWindrad() {
  265. if(windrad == null)return;
  266. windrad.getElements().forEach(ele -> ele.active = false);
  267. }
  268. private void setHolonElemntsAktiv(int actualIteration) {
  269. for(int i = 0;i<access.size();i++) {
  270. AccessWrapper aw = access.get(i);
  271. if(aw.getState(actualIteration) ==false) counter++;
  272. if(aw.getType() == AccessWrapper.HOLONELEMENT) aw.setState(true);
  273. }
  274. }
  275. private void setAllSwitchesClosed() {
  276. for(HolonSwitch hSwitch : switchList) {
  277. if(hSwitch.getManualState().isOpen()) counter++;
  278. hSwitch.setMode(SwitchMode.Manual);
  279. hSwitch.setManualState(SwitchState.Closed);
  280. }
  281. }
  282. /**
  283. * Method to get the current Position alias a ListOf Booleans for aktive settings on the Objects on the Canvas.
  284. * Also initialize the Access Hashmap to swap faster positions.
  285. * @param ModelTest
  286. * @return
  287. */
  288. private List<Boolean> extractPositionAndAccess() {
  289. Model model = control.getModel();
  290. switchList = new ArrayList<HolonSwitch>();
  291. objectList = new ArrayList<HolonObject>();
  292. initialState = new ArrayList<Boolean>();
  293. access= new HashMap<Integer, AccessWrapper>();
  294. rollOutNodes((useGroupNode && groupNode.isPresent())? groupNode.get().getObjectsInThisLayer() :model.getCanvas().getObjectsInThisLayer(), initialState, model.getCurrentIteration());
  295. return initialState;
  296. }
  297. /**
  298. * Method to extract the Informations recursively out of the Model.
  299. * @param nodes
  300. * @param positionToInit
  301. * @param timeStep
  302. */
  303. private void rollOutNodes(Stream<AbstractCanvasObject> nodes, List<Boolean> positionToInit, int timeStep) {
  304. nodes.forEach(aCps -> {
  305. if (aCps instanceof HolonObject hO) {
  306. hO.getElements().forEach(hE -> {
  307. positionToInit.add(hE.active);
  308. access.put(positionToInit.size() - 1 , new AccessWrapper(hE));
  309. });
  310. objectList.add(hO);
  311. }
  312. else if (aCps instanceof HolonSwitch sw) {
  313. positionToInit.add(sw.getState().isClosed());
  314. switchList.add(sw);
  315. access.put(positionToInit.size() - 1 , new AccessWrapper(sw));
  316. }
  317. else if(aCps instanceof GroupNode groupnode) {
  318. rollOutGroupNode(groupnode, positionToInit ,timeStep);
  319. }
  320. });
  321. }
  322. private void rollOutGroupNode (GroupNode groupNode, List<Boolean> positionToInit, int timeStep) {
  323. groupNode.getAllHolonObjectsRecursive().forEach(hObject -> {
  324. hObject.getElements().forEach(hE -> {
  325. positionToInit.add(hE.active);
  326. access.put(positionToInit.size() - 1 , new AccessWrapper(hE));
  327. });
  328. objectList.add(hObject);
  329. });
  330. groupNode.getAllSwitchObjectsRecursive().forEach(sw -> {
  331. positionToInit.add(sw.getState().isClosed());
  332. switchList.add(sw);
  333. access.put(positionToInit.size() - 1 , new AccessWrapper(sw));
  334. });
  335. }
  336. /**
  337. * To let the User See the current state without touching the Canvas.
  338. */
  339. private void updateVisual() {
  340. control.calculateStateAndVisualForCurrentTimeStep();
  341. control.updateCanvas();
  342. }
  343. /**
  344. * Sets the Model back to its original State before the LAST run.
  345. */
  346. private void resetState() {
  347. setState(initialState);
  348. }
  349. /**
  350. * Sets the State out of the given position for calculation or to show the user.
  351. * @param position
  352. */
  353. private void setState(List<Boolean> position) {
  354. for(int i = 0;i<position.size();i++) {
  355. access.get(i).setState(position.get(i));
  356. }
  357. }
  358. private void selectHolonObject() {
  359. List<HolonObject> holonObjectList = new ArrayList<HolonObject>();
  360. addObjectToList(control.getModel().getCanvas().getObjectsInThisLayer().toList(),holonObjectList);
  361. Object[] possibilities = holonObjectList.stream().map(aCps -> new Handle<HolonObject>(aCps)).toArray();
  362. @SuppressWarnings("unchecked")
  363. Handle<HolonObject> selected = (Handle<HolonObject>) JOptionPane.showInputDialog(content, "Select HolonObject:", "HolonObject?", JOptionPane.OK_OPTION,new ImageIcon(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)) , possibilities, "");
  364. if(selected != null) {
  365. //println("Selected: " + selected);
  366. windrad = selected.object;
  367. }
  368. }
  369. private void addObjectToList(List<AbstractCanvasObject> listToSearch, List<HolonObject> listToAdd){
  370. for (AbstractCanvasObject aCps : listToSearch) {
  371. if (aCps instanceof HolonObject hO) listToAdd.add(hO);
  372. else if(aCps instanceof GroupNode groupNode) {
  373. listToAdd.addAll(groupNode.getAllHolonObjectsRecursive().toList());
  374. }
  375. }
  376. }
  377. /**
  378. * A Wrapper Class for Access HolonElement and HolonSwitch in one Element and not have to split the List.
  379. */
  380. private class AccessWrapper {
  381. public static final int HOLONELEMENT = 0;
  382. public static final int SWITCH = 1;
  383. private int type;
  384. private HolonSwitch hSwitch;
  385. private HolonElement hElement;
  386. public AccessWrapper(HolonSwitch hSwitch){
  387. type = SWITCH;
  388. this.hSwitch = hSwitch;
  389. }
  390. public AccessWrapper(HolonElement hElement){
  391. type = HOLONELEMENT;
  392. this.hElement = hElement;
  393. }
  394. public void setState(boolean state) {
  395. if(type == HOLONELEMENT) {
  396. hElement.active = state;
  397. }else{//is switch
  398. hSwitch.setMode(SwitchMode.Manual);
  399. hSwitch.setManualState(state? SwitchState.Closed: SwitchState.Open);
  400. }
  401. }
  402. public boolean getState(int timeStep) {
  403. return (type == HOLONELEMENT)?hElement.active:hSwitch.getState().isClosed();
  404. }
  405. public int getType() {
  406. return type;
  407. }
  408. }
  409. private class Handle<T>{
  410. public T object;
  411. Handle(T object){
  412. this.object = object;
  413. }
  414. public String toString() {
  415. return object.toString();
  416. }
  417. }
  418. }