An implementation of the ID3 Decision Tree algorithm in Java from scratch.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

438 lines
11 KiB

4 years ago
  1. package com.AI;
  2. import com.DBpackage.MyDatabase;
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.WindowAdapter;
  8. import java.awt.event.WindowEvent;
  9. import java.sql.SQLException;
  10. import java.util.ArrayList;
  11. import java.util.Set;
  12. class MySwing extends JFrame
  13. {
  14. private MyDatabase myDatabase;
  15. private ID3 id3;
  16. private NaryTree decisionTree;
  17. MySwing(MyDatabase myDatabase)
  18. {
  19. this.myDatabase = myDatabase;
  20. this.myDatabase.connectDatabase();
  21. id3 = new ID3();
  22. decisionTree = new NaryTree();
  23. }
  24. private class InsertPanel extends JPanel //means panel for insert pane, not inserting a panel
  25. {
  26. private JLabel label_roll;
  27. private JLabel label_fname;
  28. private JLabel label_lname;
  29. private JLabel label_marks;
  30. private JLabel c;
  31. InsertPanel()
  32. {
  33. setLayout(new FlowLayout());
  34. c = new JLabel();
  35. label_roll =new JLabel();
  36. label_fname =new JLabel();
  37. label_lname =new JLabel();
  38. label_marks =new JLabel();
  39. JTextField roll = new JTextField(10);
  40. JTextField fname = new JTextField(10);
  41. JTextField lname = new JTextField(10);
  42. JTextField marks = new JTextField(10);
  43. c.setText("Hello");
  44. label_roll.setText("Roll Number: ");
  45. label_fname.setText("First Name: ");
  46. label_lname.setText("Last Name: ");
  47. label_marks.setText("Marks: ");
  48. JButton submit = new JButton();
  49. JButton reset = new JButton();
  50. submit.setText("Submit");
  51. reset.setText("Reset");
  52. add(label_roll);
  53. add(roll);
  54. add(label_fname);
  55. add(fname);
  56. add(label_lname);
  57. add(lname);
  58. add(label_marks);
  59. add(marks);
  60. //add(ps);
  61. add(submit);
  62. add(reset);
  63. add(c);
  64. submit.addActionListener(new ActionListener()
  65. {
  66. public void actionPerformed(ActionEvent e)
  67. {
  68. int r,m;
  69. String fn,ln;
  70. c.setText("Button clicked!");
  71. /*preparedStatement.setString(1, "Gary");
  72. preparedStatement.setString(2, "Larson");
  73. preparedStatement.setLong (3, 123);*/
  74. //myDatabase.insertValues(sql);
  75. r = Integer.parseInt(roll.getText());
  76. m = Integer.parseInt(marks.getText());
  77. fn = fname.getText();
  78. ln = lname.getText();
  79. myDatabase.insertValues(r,fn,ln,m);
  80. //myDatabase.deleteValues(107);
  81. //myDatabase.updateValues("Henry",106);
  82. }
  83. });
  84. reset.addActionListener(new ActionListener()
  85. {
  86. public void actionPerformed(ActionEvent e)
  87. {
  88. roll.setText("");
  89. fname.setText("");
  90. lname.setText("");
  91. marks.setText("");
  92. c.setText("Cleared!");
  93. }
  94. });
  95. }
  96. }
  97. private class UpdatePanel extends JPanel
  98. {
  99. UpdatePanel()
  100. {
  101. JButton upd = new JButton();
  102. JLabel new_fname_label = new JLabel();
  103. JLabel id_label = new JLabel();
  104. JTextField new_fname = new JTextField(10);
  105. JTextField id = new JTextField(10);
  106. upd.setText("Update");
  107. new_fname_label.setText("New first name: ");
  108. id_label.setText("Enter the id of the record to be updated: ");
  109. add(new_fname_label);
  110. add(new_fname);
  111. add(id_label);
  112. add(id);
  113. add(upd);
  114. upd.addActionListener(new ActionListener()
  115. {
  116. @Override
  117. public void actionPerformed(ActionEvent e)
  118. {
  119. String name = new_fname.getText();
  120. int i = Integer.parseInt(id.getText());
  121. myDatabase.updateValues(name, i);
  122. }
  123. });
  124. }
  125. }
  126. private class DeletePanel extends JPanel // panel for delete not deleting a panel
  127. {
  128. DeletePanel()
  129. {
  130. JLabel id_label = new JLabel();
  131. JTextField id = new JTextField(10);
  132. JButton delete_but = new JButton();
  133. id_label.setText("Enter id of the record to be deleted");
  134. delete_but.setText("Delete");
  135. add(id_label);
  136. add(id);
  137. add(delete_but);
  138. delete_but.addActionListener(new ActionListener() {
  139. @Override
  140. public void actionPerformed(ActionEvent e) {
  141. int i = Integer.parseInt(id.getText());
  142. myDatabase.deleteValues(i);
  143. }
  144. });
  145. }
  146. }
  147. private class DisplayPanel extends JPanel
  148. {
  149. private JButton show;
  150. private JLabel show_label;
  151. DisplayPanel()
  152. {
  153. setLayout(new FlowLayout());
  154. show = new JButton();
  155. show_label = new JLabel();
  156. show.setText("Display");
  157. show_label.setText("Click to display table");
  158. add(show_label);
  159. add(show);
  160. show.addActionListener(new ActionListener()
  161. {
  162. public void actionPerformed(ActionEvent e)
  163. {
  164. myDatabase.displayValues();
  165. }
  166. });
  167. }
  168. }
  169. private class StorePanel extends JPanel
  170. {
  171. private JButton store;
  172. private JLabel store_label;
  173. StorePanel()
  174. {
  175. store = new JButton();
  176. store_label = new JLabel();
  177. store.setText("Store");
  178. store_label.setText("Click to store and display table");
  179. add(store_label);
  180. add(store);
  181. store.addActionListener(new ActionListener()
  182. {
  183. public void actionPerformed(ActionEvent e)
  184. {
  185. try
  186. {
  187. JTable table = new JTable();
  188. Row climate;
  189. ArrayList<Row> climateArrayList = myDatabase.storeValues();
  190. id3 = new ID3();
  191. // id3.setTableData(climateArrayList);
  192. //id3.createNode();
  193. // for (Row c : climateArrayList) {
  194. // System.out.println(c.getDay() + " " +
  195. // c.getWeather() + " " + c.getTemperature() + " " +
  196. // c.getHumidity() + " " + c.getWind());
  197. // }
  198. }
  199. catch(SQLException se)
  200. {
  201. se.printStackTrace();
  202. }
  203. }
  204. });
  205. }
  206. }
  207. private class DecisionTreePanel extends JPanel
  208. {
  209. private static final int DISTANCE_X = 4;
  210. private static final int DISTANCE_Y = 65;
  211. private static final int radius = 20;
  212. private static final int diameter = radius * 2;
  213. private int treeArity = 3;
  214. private int treeDepth = 3;
  215. private int childDistanceX;
  216. private JPanel treeDisplayPanel;
  217. char status;
  218. private NaryTree createDecisionTree()
  219. {
  220. NaryTree tree = new NaryTree();
  221. try {
  222. ArrayList<Row> examples = myDatabase.storeValues();
  223. for (Row row : examples) {
  224. for (Cell cell : row.getCellsList()) {
  225. System.out.print(cell.getAttributeName() + " " + cell.getValue() + ", ");
  226. }
  227. System.out.println();
  228. }
  229. Set<String> attributesSet = id3.getUniqueAttributes("Play", "Day", examples);
  230. id3.setAttributesNew(examples);
  231. id3.setModeAndPN(examples);
  232. id3.createDecisionTree(tree, attributesSet, examples);
  233. tree.displayTree();
  234. myDatabase.deleteTree();
  235. tree.dbTest(myDatabase);
  236. } catch (SQLException se) {
  237. se.printStackTrace();
  238. }
  239. return tree;
  240. }
  241. DecisionTreePanel()
  242. {
  243. // setLayout(new FlowLayout());
  244. // setSize(800,600);
  245. status = 'n';
  246. JButton displayButton = new JButton();
  247. JButton clearButton = new JButton();
  248. JButton createTreeButton = new JButton();
  249. displayButton.setText("Display Tree");
  250. clearButton.setText("Clear");
  251. createTreeButton.setText("Create Tree");
  252. displayButton.addActionListener(new ActionListener()
  253. {
  254. public void actionPerformed(ActionEvent e)
  255. {
  256. status = 'y';
  257. repaint();
  258. }
  259. });
  260. clearButton.addActionListener(new ActionListener() {
  261. @Override
  262. public void actionPerformed(ActionEvent e) {
  263. status = 'n';
  264. repaint();
  265. }
  266. });
  267. createTreeButton.addActionListener(new ActionListener() {
  268. @Override
  269. public void actionPerformed(ActionEvent e) {
  270. decisionTree = createDecisionTree();
  271. }
  272. });
  273. add(displayButton);
  274. add(clearButton);
  275. add(createTreeButton);
  276. childDistanceX = 100;
  277. }
  278. public void paintComponent(Graphics g) {
  279. super.paintComponent(g);
  280. // System.out.println("Inside paintcomponent");
  281. Dimension size = getSize();
  282. int x = size.width / 2;
  283. int y = ( treeDepth * DISTANCE_Y) / 2;
  284. // drawTree(g, x, y, treeArity, treeDepth);
  285. if(status == 'y')
  286. {
  287. // g.drawOval(x,y,diameter,diameter);
  288. System.out.println("Inside if");
  289. // drawMyTree(g, x, y);
  290. displayTree(g,x,y);
  291. }
  292. }
  293. // public void drawTree(Graphics g, int x, int y, int arity, int depth) {
  294. // if (depth > 0) {
  295. // int childDistanceX = (int) (Math.pow(arity, depth) * DISTANCE_X);
  296. // int childX = x - (arity - 1) * childDistanceX / 2;
  297. // int childY = y - DISTANCE_Y;
  298. //
  299. // for (int child = 0; child < arity; child++) {
  300. // g.drawLine(x, y, childX, childY);
  301. // drawTree(g, childX, childY, arity, depth - 1);
  302. // childX += childDistanceX;
  303. // }
  304. // }
  305. // }
  306. public void drawMyTree(Graphics g, int x, int y)
  307. {
  308. System.out.println("Inside drawmytree");
  309. g.drawOval(x,y,diameter,diameter);
  310. int childX = x - childDistanceX / 2;
  311. int childY = y + DISTANCE_Y;
  312. g.drawLine(x + radius ,y + diameter,childX + childDistanceX + radius,childY);
  313. g.drawLine(x + radius ,y + diameter,childX + radius,childY);
  314. g.drawOval(childX,childY,diameter,diameter);
  315. g.drawOval(childX+childDistanceX,childY,diameter,diameter);
  316. }
  317. //////////////////////////////////////////////////////////////////////////////////
  318. private void traverse(Node node, Graphics g, int x, int y, int i)
  319. {
  320. if(node == null)
  321. {
  322. System.out.println();
  323. return;
  324. }
  325. int childX = x - childDistanceX;
  326. int childY = y + DISTANCE_Y;
  327. i++;
  328. g.drawOval(x,y,diameter,diameter);
  329. if(node.getAttributeName().equalsIgnoreCase("Yes") ||
  330. node.getAttributeName().equalsIgnoreCase("No"))
  331. if(node.getValuesList() == null) {
  332. return;
  333. }
  334. for(Node.Value v: node.getValuesList())
  335. {
  336. // g.drawOval(childX,y,diameter,diameter);
  337. g.drawLine(x+radius, y + diameter,childX + radius, childY);
  338. traverse(v.getNext(), g, childX, childY, i);
  339. childX = childX + childDistanceX;
  340. }
  341. }
  342. void displayTree(Graphics g, int x, int y)
  343. {
  344. System.out.println("Displaying tree");
  345. if(decisionTree.getHead()!=null) {
  346. int i = 0;
  347. traverse(decisionTree.getHead(), g, x, y, i);
  348. }
  349. else
  350. {
  351. System.out.println("Head node not created/initialized");
  352. }
  353. }
  354. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  355. }
  356. public void start()
  357. {
  358. JTabbedPane jtp = new JTabbedPane();
  359. //This creates the template on the windowed application that we will be using
  360. jtp.setBounds(10,10,1030,640);
  361. InsertPanel insertPanel = new InsertPanel();
  362. com.AI.MySwing.UpdatePanel updatePanel = new com.AI.MySwing.UpdatePanel();
  363. com.AI.MySwing.DeletePanel deletePanel = new com.AI.MySwing.DeletePanel();
  364. com.AI.MySwing.DisplayPanel displayPanel = new com.AI.MySwing.DisplayPanel();
  365. com.AI.MySwing.StorePanel storePanel = new com.AI.MySwing.StorePanel();
  366. DecisionTreePanel decisionTreePanel = new DecisionTreePanel();
  367. //This adds the first and second tab to our tabbed pane object and names it
  368. jtp.addTab("Insert", insertPanel);
  369. jtp.addTab("Update", updatePanel);
  370. jtp.addTab("Delete", deletePanel);
  371. jtp.addTab("Display", displayPanel);
  372. jtp.addTab("Store", storePanel);
  373. jtp.addTab("Decision-Tree", decisionTreePanel);
  374. getContentPane().add(jtp);
  375. setLayout(null);
  376. setVisible(true);
  377. setSize(1050,700);
  378. addWindowListener(new WindowAdapter()
  379. {
  380. @Override
  381. public void windowClosing(WindowEvent e)
  382. {
  383. System.out.println("Closing");
  384. myDatabase.closeConnection();
  385. dispose();
  386. }
  387. });
  388. setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
  389. }
  390. }