arview.java

来自「数据挖掘中」· Java 代码 · 共 481 行 · 第 1/2 页

JAVA
481
字号
    rest.setBackground(Color.lightGray);    rest.add(logo, BorderLayout.NORTH);    rest.add(btn,  BorderLayout.SOUTH);    pane.setLayout(new FlowLayout());    pane.setBackground(Color.lightGray);    pane.add(text);    pane.add(rest);    dlg.setTitle("About ARView...");    dlg.setModal(true);    dlg.setLocationRelativeTo(owner);    dlg.pack();    dlg.setResizable(false);    return dlg;  }  /* createAbout() */  /*------------------------------------------------------------------*/  public void loadRules (File file)  {                             /* --- load an association rule set */    JScrollPane scroll;         /* scroll pane viewport */    if (file != null)           /* if a file name is given, */      this.curr = file;         /* set it as the current file, */    else {                      /* otherwise show file chooser */      if (chooser == null) chooser = createChooser();      chooser.setDialogType(JFileChooser.OPEN_DIALOG);      int r = chooser.showDialog(this, null);      if (r != JFileChooser.APPROVE_OPTION) return;      this.curr = chooser.getSelectedFile();    }                           /* get the selected file */    try {                       /* load the rule set */      ARSet set = new ARSet(new FileInputStream(curr));      if (table != null)        /* if a table exists, store the rules */        this.table.setARSet(set);      else {                    /* otherwise create a table */        this.table = new ARTable(set);        scroll     = new JScrollPane(table);        this.getContentPane().remove(1);        this.getContentPane().add(scroll, BorderLayout.CENTER);        this.pack();            /* create a scroll pane and */      }                         /* re-layout the window */      stat.setText(this.curr.toString()); }    catch (java.io.IOException ioe) {      String msg = "file " +curr.getName() +":\n" +ioe.getMessage();      stat.setText(msg); System.err.println(msg);      JOptionPane.showMessageDialog(this, msg, "alert",        JOptionPane.ERROR_MESSAGE);    }                           /* set the status text */  }  /* loadRules() */  /*------------------------------------------------------------------*/  public void saveRules (File file)  {                             /* --- save an association rule set */    int                i, n;    /* loop variables */    ARSet              arset;   /* set of association rules */    String             desc;    /* description of a rule */    FileOutputStream   stream;  /* stream to write to */    OutputStreamWriter writer;  /* writer for the output file */    if (file != null)           /* if a file name is given, */      this.curr = file;         /* set it as the current file, */    else {                      /* otherwise show file chooser */      if (chooser == null) chooser = createChooser();      chooser.setDialogType(JFileChooser.SAVE_DIALOG);      int r = chooser.showDialog(this, null);      if (r != JFileChooser.APPROVE_OPTION) return;      this.curr = chooser.getSelectedFile();    }                           /* get the setected file */    try {                       /* save the current decision tree */      arset  = this.table.getARSet();      stream = new FileOutputStream(curr);      writer = new OutputStreamWriter(stream);      for (n = arset.getCount(), i = 0; i < n; i++) {        desc = arset.getRule(i).toString();        writer.write(desc, 0, desc.length());        writer.write("\n", 0, 1);      }                         /* traverse and print the rules */      writer.close(); stream.close(); }    catch (java.io.IOException ioe) {      String msg = "file " +curr.getName() +":\n" +ioe.getMessage();      stat.setText(msg); System.err.println(msg);      JOptionPane.showMessageDialog(this, msg, "alert",        JOptionPane.ERROR_MESSAGE);    }                           /* set the status text */  }  /* saveRules() */  /*------------------------------------------------------------------*/  public ARView (boolean isProg)  {                             /* --- create a ass. rule viewer */    int       i;                /* position in file name */    JMenuBar  mbar;             /* menu bar */    JMenu     menu;             /* to create menu titles */    JMenuItem item;             /* to create menu items */    JPanel    panel;            /* panel for initial window */    this.getContentPane().setLayout(new BorderLayout());    /* --- create and set the menu bar --- */    mbar = new JMenuBar();    this.getContentPane().add(mbar, BorderLayout.NORTH);    menu = mbar.add(new JMenu("File"));    menu.setMnemonic('f');    item = menu.add(new JMenuItem("Load Rule Set..."));    item.setMnemonic('l');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        ARView.this.loadRules(null); } } );    item = menu.add(new JMenuItem("Reload Rule Set"));    item.setMnemonic('r');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        ARView.this.loadRules(ARView.this.curr); } } );    item = menu.add(new JMenuItem("Save Rule Set"));    item.setMnemonic('s');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        ARView.this.saveRules(ARView.this.curr); } } );    item = menu.add(new JMenuItem("Save Rule Set as..."));    item.setMnemonic('a');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        ARView.this.saveRules(null); } } );    if (isProg) {               /* if stand-alone program */      menu.addSeparator();      item = menu.add(new JMenuItem("Open Toolbox..."));      item.setMnemonic('t');      item.addActionListener(new AbstractAction() {        public void actionPerformed (ActionEvent e) {          if (ARView.this.toolbox == null)            ARView.this.toolbox = new ARToolbox(ARView.this);          ARView.this.toolbox.setVisible(true);          ARView.this.toolbox.toFront();        } } );                  /* add the dialog frame */    }                           /* with the tree toolbox */    menu.addSeparator();    item = menu.add(new JMenuItem("Quit"));    item.setMnemonic('q');    if (isProg) {               /* if stand-alone program */      item.addActionListener(new AbstractAction() {        public void actionPerformed (ActionEvent e) {          System.exit(0); } } ); }     /* terminate the program */    else {                      /* if only visualization module */      item.addActionListener(new AbstractAction() {        public void actionPerformed (ActionEvent e) {          ARView.this.setVisible(false); } } );    }                           /* only close the window */    menu = mbar.add(new JMenu("Sort"));    menu.setMnemonic('s');    item = menu.add(new JMenuItem("by Consequent"));    item.setMnemonic('q');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        if (ARView.this.table == null) return;        ARView.this.table.sort(ARule.HEAD); } } );    item = menu.add(new JMenuItem("by Size"));    item.setMnemonic('z');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        if (ARView.this.table == null) return;        ARView.this.table.sort(ARule.SIZE); } } );    item = menu.add(new JMenuItem("by Support"));    item.setMnemonic('s');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        if (ARView.this.table == null) return;        ARView.this.table.sort(ARule.SUPP); } } );    item = menu.add(new JMenuItem("by Confidence"));    item.setMnemonic('c');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        if (ARView.this.table == null) return;        ARView.this.table.sort(ARule.CONF); } } );    item = menu.add(new JMenuItem("by Lift Value"));    item.setMnemonic('l');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        if (ARView.this.table == null) return;        ARView.this.table.sort(ARule.LIFT); } } );    item = menu.add(new JMenuItem("by add. Evaluation"));    item.setMnemonic('e');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        if (ARView.this.table == null) return;        ARView.this.table.sort(ARule.AREM); } } );    menu = mbar.add(new JMenu("Help"));    menu.setMnemonic('h');    item = menu.add(new JMenuItem("About..."));    item.setMnemonic('a');    item.addActionListener(new AbstractAction() {      public void actionPerformed (ActionEvent e) {        if (about == null) about = createAbout(ARView.this);        about.setVisible(true);      } } );    panel = new JPanel();    panel.setPreferredSize(new Dimension(400, 16));    panel.setBackground(Color.white);    this.getContentPane().add(panel, BorderLayout.CENTER);    /* --- create and set a status bar --- */    stat = new JTextField("no rule set loaded");    stat.setEditable(false);    this.getContentPane().add(stat, BorderLayout.SOUTH);    /* --- show the frame window --- */    this.setTitle("ARView");    this.setDefaultCloseOperation(isProg      ? JFrame.EXIT_ON_CLOSE : JFrame.HIDE_ON_CLOSE);    this.setLocation(50, 50);    this.pack();    if (isProg) this.setVisible(true);  }  /* ARView() */  /*------------------------------------------------------------------*/  public ARView () { this(false); }  public ARView (String title) { this(false); this.setTitle(title); }  public ARView (File file)  { this(false); this.loadRules(file); }  public ARView (String title, File file)  { this(title); this.loadRules(file); }  /*------------------------------------------------------------------*/  public static void main (String args[])  {                             /* --- main function */    ARView v = new ARView(true);/* create a association rule viewer */    if (args.length > 0) v.loadRules(new File(args[0]));  }  /* main() */               /* load decision tree */}  /* class ARView */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?