AlgorithmMenu.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package ui.view;
  2. import algorithms.geneticAlgorithm.holegGA.GenAlgoInit;
  3. import algorithms.geneticAlgorithm.holegGA.GenAlgoWindow;
  4. import api.CpsAlgorithm;
  5. import ui.controller.Control;
  6. import ui.model.Model;
  7. import javax.swing.*;
  8. import javax.tools.JavaCompiler;
  9. import javax.tools.ToolProvider;
  10. import java.awt.*;
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13. import java.io.BufferedReader;
  14. import java.io.File;
  15. import java.io.FileReader;
  16. import java.net.URL;
  17. import java.net.URLClassLoader;
  18. import java.util.HashMap;
  19. /**
  20. * This Class represents the Menu, where you can select the Algorithms.
  21. *
  22. * @author Gruppe14
  23. */
  24. public class AlgorithmMenu extends JMenu {
  25. private static final long serialVersionUID = 1L;
  26. GenAlgoWindow genAlgoWindow;
  27. JMenuItem geneticAlgoBtn = new JMenuItem("Build Network"); //opens the window for gen algo
  28. JMenuItem evaluateAlgoBtn = new JMenuItem("Evaluate Algorithm"); //evaluates the gen algo
  29. JMenuItem algoFolderButton = new JMenuItem("Select Algorithm Folder");
  30. // root Directory
  31. File root = null;
  32. Model model;
  33. Control controller;
  34. private JMenu mnSelectAlgorithm = new JMenu("Select Algorithm");
  35. private HashMap<String, File> algosHash = new HashMap<>();
  36. private JMenuItem noneItem = new JMenuItem("none");
  37. /**
  38. * Constructor.
  39. *
  40. * @param mod
  41. * the Model
  42. * @param cont
  43. * the Controller
  44. */
  45. public AlgorithmMenu(Model mod, Control cont, GUI parentGui) {
  46. super();
  47. // Init Stuff
  48. this.model = mod;
  49. this.controller = cont;
  50. this.setText("Algorithm");
  51. // algoFolderButton Action
  52. algoFolderButton.addActionListener(new ActionListener() {
  53. @Override
  54. public void actionPerformed(java.awt.event.ActionEvent evt) {
  55. menuSetFolderActionPerformed(evt);
  56. }
  57. private void menuSetFolderActionPerformed(java.awt.event.ActionEvent evt) {
  58. JFileChooser fileChooser = new JFileChooser();
  59. fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  60. fileChooser.setAcceptAllFileFilterUsed(false);
  61. if (fileChooser.showOpenDialog(parentGui.getFrmCyberPhysical()) == JFileChooser.APPROVE_OPTION) {
  62. // empty everything and reset the selected algorithm
  63. controller.setAlgorithm(null);
  64. mnSelectAlgorithm.removeAll();
  65. mnSelectAlgorithm.add(noneItem);
  66. noneItem.doClick();
  67. File[] files = fileChooser.getSelectedFile().listFiles();
  68. // Set Root Folder Path
  69. root = new File(fileChooser.getCurrentDirectory().getPath());
  70. for (int i = 0; i < files.length; i++) {
  71. if (files[i].toString()
  72. .endsWith(".java") /*
  73. * || files[i].toString().
  74. * endsWith(".class")
  75. */) {
  76. String name = files[i].getName();
  77. int tmpB = name.lastIndexOf('.');
  78. name = name.substring(0, tmpB);
  79. algosHash.put(name, files[i]);
  80. JMenuItem tempItem = new JMenuItem(name);
  81. tempItem.addActionListener(new ActionListener() {
  82. @Override
  83. public void actionPerformed(ActionEvent e) {
  84. for (int i = 0; i < mnSelectAlgorithm.getItemCount(); i++) {
  85. mnSelectAlgorithm.getItem(i).setForeground(null);
  86. }
  87. tempItem.setForeground(Color.BLUE);
  88. setAlgorithm(algosHash.get(tempItem.getText()), tempItem.getText());
  89. }
  90. });
  91. mnSelectAlgorithm.add(tempItem);
  92. }
  93. }
  94. }
  95. }
  96. });
  97. geneticAlgoBtn.addActionListener(new ActionListener(){
  98. @Override
  99. public void actionPerformed(ActionEvent arg0) {
  100. GenAlgoInit algoInit = new GenAlgoInit(controller, model);
  101. algoInit.start();
  102. }
  103. });
  104. evaluateAlgoBtn.addActionListener(new ActionListener(){
  105. @Override
  106. public void actionPerformed(ActionEvent arg0) {
  107. GenAlgoInit algoInit = new GenAlgoInit(controller, model);
  108. algoInit.evaluate();
  109. }
  110. });
  111. // Add to Panel
  112. this.add(geneticAlgoBtn);
  113. this.add(evaluateAlgoBtn);
  114. this.add(algoFolderButton);
  115. this.add(mnSelectAlgorithm);
  116. mnSelectAlgorithm.add(noneItem);
  117. noneItem.setForeground(Color.BLUE);
  118. noneItem.addActionListener(new ActionListener() {
  119. @Override
  120. public void actionPerformed(ActionEvent e) {
  121. for (int i = 0; i < mnSelectAlgorithm.getItemCount(); i++) {
  122. mnSelectAlgorithm.getItem(i).setForeground(null);
  123. }
  124. noneItem.setForeground(Color.BLUE);
  125. controller.setAlgorithm(null);
  126. }
  127. });
  128. }
  129. public void setAlgorithm(File file, String name) {
  130. boolean missingCompiler = false;
  131. boolean instantiationError = false;
  132. try {
  133. BufferedReader br = new BufferedReader(new FileReader(file.getPath()));
  134. String line = br.readLine();
  135. // Package Name
  136. String packageName = "";
  137. while (line != null) {
  138. line = line.trim();
  139. if (!line.isEmpty()) {
  140. if (line.length() >= 7 && line.substring(0, 7).equals("package")) {
  141. packageName = line.substring(8, line.length() - 1);
  142. }
  143. }
  144. if (packageName.isEmpty()) {
  145. line = br.readLine();
  146. } else {
  147. line = null;
  148. }
  149. }
  150. // Compile source file.
  151. JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  152. if (ToolProvider.getSystemJavaCompiler() == null) {
  153. missingCompiler = true;
  154. }
  155. compiler.run(null, null, null, file.getPath());
  156. // Load and instantiate compiled class.
  157. URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
  158. instantiationError = true;
  159. Class<?> cls;
  160. if (packageName.isEmpty()) {
  161. cls = Class.forName(name, true, classLoader);
  162. } else {
  163. cls = Class.forName(packageName + "." + name, true, classLoader);
  164. }
  165. Object t = cls.newInstance();
  166. if (t instanceof CpsAlgorithm) {
  167. controller.setAlgorithm(t);
  168. } else {
  169. JOptionPane.showMessageDialog(null, "Class does not implement CpsAlgorithm!", "Error!",
  170. JOptionPane.ERROR_MESSAGE);
  171. noneItem.doClick();
  172. }
  173. } catch (Exception e) {
  174. if (missingCompiler) {
  175. JOptionPane.showMessageDialog(null, "Missing Compiiler! Please install the JDK!", "Error!",
  176. JOptionPane.ERROR_MESSAGE);
  177. } else if (instantiationError) {
  178. JOptionPane.showMessageDialog(null, "Class does not implement CpsAlgorithm!", "Error!",
  179. JOptionPane.ERROR_MESSAGE);
  180. noneItem.doClick();
  181. } else {
  182. JOptionPane.showMessageDialog(null, e.toString(), "Error!", JOptionPane.ERROR_MESSAGE);
  183. }
  184. }
  185. }
  186. }