gui.java

来自「Weka」· Java 代码 · 共 2,050 行 · 第 1/5 页

JAVA
2,050
字号
			}
		} // c'tor

		/* Place holder. Should be implemented by derived classes.
		 *  (non-Javadoc)
		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
		 */
		public void actionPerformed(ActionEvent ae) {}
	} // class MyAction

	class ActionGenerateNetwork extends MyAction {
		/** for serialization */
		private static final long serialVersionUID = -2038911085935517L;

		public ActionGenerateNetwork() {
			super("Generate Network", "Generate Random Bayesian Network", "generate.network", "ctrl N");
		} // c'tor

		int m_nNrOfNodes = 10;

		int m_nNrOfArcs = 15;

		int m_nCardinality = 2;

		int m_nSeed = 123;

		JDialog dlg = null;

		public void actionPerformed(ActionEvent ae) {
			if (dlg == null) {
				dlg = new JDialog();
				dlg.setTitle("Generate Random Bayesian Network Options");

				final JLabel jLbNrOfNodes = new JLabel("Nr of nodes");
				final JTextField jTfNrOfNodes = new JTextField(3);
				jTfNrOfNodes.setHorizontalAlignment(JTextField.CENTER);
				jTfNrOfNodes.setText("" + m_nNrOfNodes);
				final JLabel jLbNrOfArcs = new JLabel("Nr of arcs");
				final JTextField jTfNrOfArcs = new JTextField(3);
				jTfNrOfArcs.setHorizontalAlignment(JTextField.CENTER);
				jTfNrOfArcs.setText("" + m_nNrOfArcs);
				final JLabel jLbCardinality = new JLabel("Cardinality");
				final JTextField jTfCardinality = new JTextField(3);
				jTfCardinality.setHorizontalAlignment(JTextField.CENTER);
				jTfCardinality.setText("" + m_nCardinality);
				final JLabel jLbSeed = new JLabel("Random seed");
				final JTextField jTfSeed = new JTextField(3);
				jTfSeed.setHorizontalAlignment(JTextField.CENTER);
				jTfSeed.setText("" + m_nSeed);

				JButton jBtGo;
				jBtGo = new JButton("Generate Network");

				jBtGo.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent ae) {
						try {
							BayesNetGenerator generator = new BayesNetGenerator();
							m_BayesNet = generator;
							m_BayesNet.clearUndoStack();
							
							String[] options = new String[8];
							options[0] = "-N";
							options[1] = "" + jTfNrOfNodes.getText();
							options[2] = "-A";
							options[3] = "" + jTfNrOfArcs.getText();
							options[4] = "-C";
							options[5] = "" + jTfCardinality.getText();
							options[6] = "-S";
							options[7] = "" + jTfSeed.getText();
							generator.setOptions(options);
							generator.generateRandomNetwork();
							// Convert to EditableBayesNet
							// This ensures the getOptions() called by GenericObjectEditor to get the correct result.
							BIFReader bifReader = new BIFReader();
							bifReader.processString(m_BayesNet.toXMLBIF03());
							m_BayesNet = new EditableBayesNet(bifReader);

							updateStatus();
							layoutGraph();
							a_datagenerator.setEnabled(true);
							m_Instances = null;;
							a_learn.setEnabled(false);
							a_learnCPT.setEnabled(false);

							dlg.setVisible(false);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});

				JButton jBtCancel;
				jBtCancel = new JButton("Cancel");
				jBtCancel.setMnemonic('C');
				jBtCancel.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent ae) {
						dlg.setVisible(false);
					}
				});
				GridBagConstraints gbc = new GridBagConstraints();
				dlg.setLayout(new GridBagLayout());

				Container c = new Container();
				c.setLayout(new GridBagLayout());
				gbc.gridwidth = 2;
				gbc.insets = new Insets(8, 0, 0, 0);
				gbc.anchor = GridBagConstraints.NORTHWEST;
				gbc.gridwidth = GridBagConstraints.RELATIVE;
				gbc.fill = GridBagConstraints.HORIZONTAL;
				c.add(jLbNrOfNodes, gbc);
				gbc.gridwidth = GridBagConstraints.REMAINDER;
				c.add(jTfNrOfNodes, gbc);
				gbc.gridwidth = GridBagConstraints.RELATIVE;
				c.add(jLbNrOfArcs, gbc);
				gbc.gridwidth = GridBagConstraints.REMAINDER;
				c.add(jTfNrOfArcs, gbc);
				gbc.gridwidth = GridBagConstraints.RELATIVE;
				c.add(jLbCardinality, gbc);
				gbc.gridwidth = GridBagConstraints.REMAINDER;
				c.add(jTfCardinality, gbc);
				gbc.gridwidth = GridBagConstraints.RELATIVE;
				c.add(jLbSeed, gbc);
				gbc.gridwidth = GridBagConstraints.REMAINDER;
				c.add(jTfSeed, gbc);

				gbc.fill = GridBagConstraints.HORIZONTAL;
				dlg.add(c, gbc);
				dlg.add(jBtGo);
				gbc.gridwidth = GridBagConstraints.REMAINDER;
				dlg.add(jBtCancel);
			}
			dlg.setLocation(100, 100);
			dlg.setVisible(true);
			dlg.setSize(dlg.getPreferredSize());
			dlg.setVisible(false);
			dlg.setVisible(true);
			dlg.repaint();
		} // actionPerformed
	} // class ActionGenerate

	class ActionGenerateData extends MyAction {
		/** for serialization */
		private static final long serialVersionUID = -2038911085935516L;

		public ActionGenerateData() {
			super("Generate Data", "Generate Random Instances from Network", "generate.data", "ctrl D");
		} // c'tor

		int m_nNrOfInstances = 100;

		int m_nSeed = 1234;

		String m_sFile = "";

		JDialog dlg = null;

		public void actionPerformed(ActionEvent ae) {
			if (dlg == null) {
				dlg = new JDialog();
				dlg.setTitle("Generate Random Data Options");

				final JLabel jLbNrOfInstances = new JLabel("Nr of instances");
				final JTextField jTfNrOfInstances = new JTextField(3);
				jTfNrOfInstances.setHorizontalAlignment(JTextField.CENTER);
				jTfNrOfInstances.setText("" + m_nNrOfInstances);
				final JLabel jLbSeed = new JLabel("Random seed");
				final JTextField jTfSeed = new JTextField(3);
				jTfSeed.setHorizontalAlignment(JTextField.CENTER);
				jTfSeed.setText("" + m_nSeed);
				final JLabel jLbFile = new JLabel("Output file (optional)");
				final JTextField jTfFile = new JTextField(12);
				jTfFile.setHorizontalAlignment(JTextField.CENTER);
				jTfFile.setText(m_sFile);

				JButton jBtGo;
				jBtGo = new JButton("Generate Data");

				jBtGo.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent ae) {
						try {
							String tmpfilename = "tmp.bif.file.xml";
							BayesNetGenerator generator = new BayesNetGenerator();
							String[] options = new String[4];
							options[0] = "-M";
							options[1] = "" + jTfNrOfInstances.getText();
							options[2] = "-F";
							options[3] = tmpfilename;
							FileWriter outfile = new FileWriter(tmpfilename);
							StringBuffer text = new StringBuffer();
							if (m_marginCalculator == null) {
								m_marginCalculator = new MarginCalculator();
								m_marginCalculator.calcMargins(m_BayesNet);
							}
							text.append(m_marginCalculator.toXMLBIF03());
							outfile.write(text.toString());
							outfile.close();

							generator.setOptions(options);
							generator.generateRandomNetwork();
							generator.generateInstances();
							m_Instances = generator.m_Instances;
							a_learn.setEnabled(true);
							a_learnCPT.setEnabled(true);

							m_sFile = jTfFile.getText();
							if (m_sFile != null && !m_sFile.equals("")) {
								FileWriter outfile2 = new FileWriter(m_sFile);
								StringBuffer text2 = new StringBuffer();
								text2.append(m_Instances.toString());
								outfile2.write(text2.toString());
								outfile2.close();
							}

						} catch (Exception e) {
							e.printStackTrace();
						}
						dlg.setVisible(false);
					}
				});

				JButton jBtFile = new JButton("Browse");
				;
				jBtFile.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent ae) {
						JFileChooser fc = new JFileChooser(System.getProperty("user.dir"));
						ExtensionFileFilter ef1 = new ExtensionFileFilter(".arff", "Arff files");
						fc.addChoosableFileFilter(ef1);
						fc.setDialogTitle("Save Instances As");
						int rval = fc.showSaveDialog(GUI.this);

						if (rval == JFileChooser.APPROVE_OPTION) {
							String filename = fc.getSelectedFile().toString();
							jTfFile.setText(filename);
						}
						dlg.setVisible(true);
					}
				});
				JButton jBtCancel;
				jBtCancel = new JButton("Cancel");
				jBtCancel.setMnemonic('C');
				jBtCancel.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent ae) {
						dlg.setVisible(false);
					}
				});
				GridBagConstraints gbc = new GridBagConstraints();
				dlg.setLayout(new GridBagLayout());

				Container c = new Container();
				c.setLayout(new GridBagLayout());
				gbc.gridwidth = 2;
				gbc.insets = new Insets(8, 0, 0, 0);
				gbc.anchor = GridBagConstraints.NORTHWEST;
				gbc.gridwidth = GridBagConstraints.RELATIVE;
				gbc.fill = GridBagConstraints.HORIZONTAL;
				c.add(jLbNrOfInstances, gbc);
				gbc.gridwidth = GridBagConstraints.REMAINDER;
				c.add(jTfNrOfInstances, gbc);
				gbc.gridwidth = GridBagConstraints.RELATIVE;
				c.add(jLbSeed, gbc);
				gbc.gridwidth = GridBagConstraints.REMAINDER;
				c.add(jTfSeed, gbc);
				gbc.gridwidth = GridBagConstraints.RELATIVE;
				c.add(jLbFile, gbc);
				gbc.gridwidth = GridBagConstraints.REMAINDER;
				c.add(jTfFile, gbc);
				gbc.gridwidth = GridBagConstraints.REMAINDER;
				c.add(jBtFile, gbc);

				gbc.fill = GridBagConstraints.HORIZONTAL;
				dlg.add(c, gbc);
				dlg.add(jBtGo);
				gbc.gridwidth = GridBagConstraints.REMAINDER;
				dlg.add(jBtCancel);
			}
			dlg.setLocation(100, 100);
			dlg.setVisible(true);
			dlg.setSize(dlg.getPreferredSize());
			dlg.setVisible(false);
			dlg.setVisible(true);
			dlg.repaint();

		} // actionPerformed
	} // class ActionGenerateData

	class ActionLearn extends MyAction {
		/** for serialization */
		private static final long serialVersionUID = -2038911085935516L;

		public ActionLearn() {
			super("Learn Network", "Learn Bayesian Network", "learn", "ctrl L");
			setEnabled(false);
		} // c'tor

		JDialog dlg = null;

		public void actionPerformed(ActionEvent ae) {
			if (dlg == null) {
				dlg = new JDialog();
				dlg.setTitle("Learn Bayesian Network");

				final JButton jBtOptions = new JButton("Options");
				jBtOptions.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent ae) {
						//m_BayesNet = new EditableBayesNet();
						try {
							GenericObjectEditor.registerEditors();
							GenericObjectEditor ce = new GenericObjectEditor(true);
							ce.setClassType(weka.classifiers.Classifier.class);
							ce.setValue(m_BayesNet);

							PropertyDialog pd = new PropertyDialog(ce, 100, 100);
							pd.addWindowListener(new WindowAdapter() {
								public void windowClosing(WindowEvent e) {
									PropertyEditor pe = ((PropertyDialog) e.getSource()).getEditor();
									Object c = (Object) pe.getValue();
									String options = "";
									if (c instanceof OptionHandler) {
										options = Utils.joinOptions(((OptionHandler) c).getOptions());
										try {
											m_BayesNet.setOptions(((OptionHandler) c).getOptions());
										} catch (Exception e2) {
											e2.printStackTrace();
										}
									}
									System.out.println(c.getClass().getName() + " " + options);
									System.exit(0);
								}
							});
						} catch (Exception ex) {
							ex.printStackTrace();
							System.err.println(ex.getMessage());
						}
						m_BayesNet.clearUndoStack();
						a_undo.setEnabled(false);
						a_redo.setEnabled(false);
					}
				});

				final JTextField jTfOptions = new JTextField(40);
				jTfOptions.setHorizontalAlignment(JTextField.CENTER);
				jTfOptions.setText("" + Utils.joinOptions(m_BayesNet.getOptions()));

				JButton jBtGo;
				jBtGo = new JButton("Learn");

				jBtGo.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent ae) {
						try {
							m_BayesNet.buildClassifier(m_Instances);
							layoutGraph();
							updateStatus();
							m_BayesNet.clearUndoStack();

							dlg.setVisible(false);
						} catch (Exception e) {
							e.printStackTrace();
						}
						dlg.setVisible(false);
					}
				});

				JButton jBtCancel;
				jBtCancel = new JButton("Cancel");
				jBtCancel.setMnemonic('C');
				jBtCancel.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent ae) {
						dlg.setVisible(false);
					}
				});
				GridBagConstraints gbc = new GridBagConstraints();
				dlg.setLayout(new GridBagLayout());

				Container c = new Container();
				c.setLayout(new GridBagLayout());
				gbc.gridwidth = 2;
				gbc.insets = new Insets(8, 0, 0, 0);
				gbc.anchor = GridBagConstraints.NORTHWEST;
				gbc.gridwidth = GridBagConstraints.RELATIVE;
				gbc.fill = GridBagConstraints.HORIZONTAL;
				c.add(jBtOptions, gbc);
				gbc.gridwidth = GridBagConstraints.REMAINDER;
				c.add(jTfOptions, gbc);

				gbc.fill = GridBagConstraints.HORIZONTAL;
				dlg.add(c, gbc);
				dlg.add(jBtGo);
				gbc.gridwidth = GridBagConstraints.REMAINDER;
				dlg.add(jBtCancel);
			}
			dlg.setLocation(100, 100);
			dlg.setVisible(true);
			dlg.setSize(dlg.getPreferredSize());
			dlg.setVisible(false);
			dlg.setVisible(true);
			dlg.repaint();
		} // actionPerformed
	} // class ActionLearn

	class ActionLearnCPT extends MyAction {
		/** for serialization */
		private static final long serialVersionUID = -2022211085935516L;

		public ActionLearnCPT() {
			super("Learn CPT", "Learn conditional probability tables", "learncpt", "");
			setEnabled(false);
		} // c'tor

		public void actionPerformed(ActionEvent ae) {
			if (m_Instances == null) {

⌨️ 快捷键说明

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