DemoAlgo.java 15 KB

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