/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* GUI.java
* Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
*
*/
package weka.classifiers.bayes.net;
import weka.core.FastVector;
import weka.core.Instances;
import weka.core.OptionHandler;
import weka.core.SerializedObject;
import weka.core.Utils;
import weka.gui.ExtensionFileFilter;
import weka.gui.GenericObjectEditor;
import weka.gui.PropertyDialog;
import weka.gui.graphvisualizer.BIFFormatException;
import weka.gui.graphvisualizer.BIFParser;
import weka.gui.graphvisualizer.GraphNode;
import weka.gui.graphvisualizer.HierarchicalBCEngine;
import weka.gui.graphvisualizer.LayoutCompleteEvent;
import weka.gui.graphvisualizer.LayoutCompleteEventListener;
import weka.gui.graphvisualizer.LayoutEngine;
import weka.gui.visualize.PrintablePanel;
import weka.classifiers.bayes.net.BIFReader;
import weka.classifiers.bayes.net.BayesNetGenerator;
import weka.classifiers.bayes.net.EditableBayesNet;
import weka.classifiers.bayes.net.MarginCalculator;
import weka.classifiers.bayes.net.MarginCalculator.JunctionTreeNode;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.beans.PropertyEditor;
import java.io.*;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.table.AbstractTableModel;
/**
* GUI interface to Bayesian Networks. Allows editing Bayesian networks
* on screen and provides GUI interface to various Bayesian network facilities
* in Weka, including random network generation, data set generation and
* Bayesion network inference.
*
* @author Remco Bouckaert (remco@cs.waikato.ac.nz)
* @version $Revision: 1.1 $
*/
public class GUI extends JPanel implements LayoutCompleteEventListener {
/** for serialization */
private static final long serialVersionUID = -2038911085935515624L;
/** The current LayoutEngine */
protected LayoutEngine m_layoutEngine;
/** Panel actually displaying the graph */
protected GraphPanel m_GraphPanel;
/** Container of Bayesian network */
EditableBayesNet m_BayesNet = new EditableBayesNet(true);
/** String containing file name storing current network */
protected String m_sFileName = "";
/** used for calculating marginals in Bayesian netwowrks */
MarginCalculator m_marginCalculator = null;
/**
* used for calculating marginals in Bayesian netwowrks when evidence is
* present
*/
MarginCalculator m_marginCalculatorWithEvidence = null;
/** flag indicating whether marginal distributions of each of the nodes
* should be shown in display.
*/
boolean m_bViewMargins = false;
boolean m_bViewCliques = false;
/** data selected from file. Used to train a Bayesian network on */
Instances m_Instances = null;
/** Text field for specifying zoom */
final JTextField m_jTfZoom;
/** toolbar containing buttons at top of window */
final JToolBar m_jTbTools;
/** status bar at bottom of window */
final JLabel m_jStatusBar;
/** TextField for node's width */
private final JTextField m_jTfNodeWidth = new JTextField(3);
/** TextField for nodes height */
private final JTextField m_jTfNodeHeight = new JTextField(3);
/** this contains the m_GraphPanel GraphPanel */
JScrollPane m_jScrollPane;
/** path for icons */
private final String ICONPATH = "weka/classifiers/bayes/net/icons/";
/** current zoom value */
private double m_fScale = 1;
/** standard width of node */
private int m_nNodeHeight = 2 * getFontMetrics(getFont()).getHeight();
/** standard height of node */
final static int DEFAULT_NODE_WIDTH = 50;
private int m_nNodeWidth = DEFAULT_NODE_WIDTH;
/** width of node, allowing for some padding */
final static int PADDING = 10;
private int m_nPaddedNodeWidth = DEFAULT_NODE_WIDTH + PADDING;
/** used when using zoomIn and zoomOut buttons */
private int [] m_nZoomPercents = { 10, 25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 350, 400, 450, 500,
550, 600, 650, 700, 800, 900, 999 };
/** actions triggered by GUI events */
Action a_new = new ActionNew();
Action a_quit = new ActionQuit();
Action a_save = new ActionSave();
ActionExport a_export = new ActionExport();
ActionPrint a_print = new ActionPrint();
Action a_load = new ActionLoad();
Action a_zoomin = new ActionZoomIn();
Action a_zoomout = new ActionZoomOut();
Action a_layout = new ActionLayout();
Action a_saveas = new ActionSaveAs();
Action a_viewtoolbar = new ActionViewToolbar();
Action a_viewstatusbar = new ActionViewStatusbar();
Action a_networkgenerator = new ActionGenerateNetwork();
Action a_datagenerator = new ActionGenerateData();
Action a_datasetter = new ActionSetData();
Action a_learn = new ActionLearn();
Action a_learnCPT = new ActionLearnCPT();
Action a_help = new ActionHelp();
Action a_about = new ActionAbout();
ActionAddNode a_addnode = new ActionAddNode();
Action a_delnode = new ActionDeleteNode();
Action a_cutnode = new ActionCutNode();
Action a_copynode = new ActionCopyNode();
Action a_pastenode = new ActionPasteNode();
Action a_selectall = new ActionSelectAll();
Action a_addarc = new ActionAddArc();
Action a_delarc = new ActionDeleteArc();
Action a_undo = new ActionUndo();
Action a_redo= new ActionRedo();
Action a_alignleft = new ActionAlignLeft();
Action a_alignright = new ActionAlignRight();
Action a_aligntop = new ActionAlignTop();
Action a_alignbottom = new ActionAlignBottom();
Action a_centerhorizontal = new ActionCenterHorizontal();
Action a_centervertical = new ActionCenterVertical();
Action a_spacehorizontal = new ActionSpaceHorizontal();
Action a_spacevertical = new ActionSpaceVertical();
/** node currently selected through rig
评论6