📄 algorithmmenu.java
字号:
m_manager.getVisualTreePanel().setEnabled( false );
}
else if( state == PAUSE_STATE ) {
m_run.setEnabled( true );
m_traceRun.setEnabled( true );
m_pause.setEnabled( false );
m_step.setEnabled( true );
m_cancel.setEnabled( true );
m_backup.setEnabled( false );
m_initialize.setEnabled( false );
m_splitMenu.setEnabled( false );
m_pruneMenu.setEnabled( false );
m_prefs.setEnabled( true );
if( m_manager.getDatasetMenu() != null )
m_manager.getDatasetMenu().setEnabled( false );
if( m_manager.getVisualTreePanel() != null )
m_manager.getVisualTreePanel().setEnabled( false );
}
else if( state == COMPLETE_STATE ) {
m_run.setEnabled( false );
m_traceRun.setEnabled( false );
m_pause.setEnabled( false );
m_step.setEnabled( false );
m_cancel.setEnabled( false );
m_initialize.setEnabled( true );
m_splitMenu.setEnabled( true );
m_pruneMenu.setEnabled( true );
m_prefs.setEnabled( true );
if( m_manager.getAlgorithm() != null )
if( !m_manager.getAlgorithm().getTree().isEmpty() )
m_backup.setEnabled( true );
else
m_backup.setEnabled( false );
else
m_backup.setEnabled( false );
if( m_manager.getDatasetMenu() != null )
m_manager.getDatasetMenu().setEnabled( true );
if( m_manager.getVisualTreePanel() != null )
m_manager.getVisualTreePanel().setEnabled( true );
}
}
// Private methods
/**
* Handles and coordinates operation when the user
* selects the 'Run' menu item.
*/
private void handleRun()
{
// Check - are we trying to perform reduced-error
// pruning without a testing dataset?
if( !ensureTestDatasetAvailIfNeeded() ) return;
// Disable menu items.
setMenuState( RUN_STATE );
// Turn on run mode and disable verbose messaging.
m_manager.getAlgorithm().setRunMode( DecisionTreeAlgorithm.NORMAL_MODE );
m_manager.getAlgorithm().setVerbose( false );
continueRunning();
}
/**
* Handles and coordinates operation when the user
* selects the 'Trace Run' menu item.
*/
private void handleTraceRun()
{
// Check - are we trying to perform reduced-error
// pruning without a testing dataset?
if( !ensureTestDatasetAvailIfNeeded() ) return;
// Set all menu items to their 'trace' states.
setMenuState( RUN_STATE );
// Turn on trace mode and enable verbose messaging.
m_manager.getAlgorithm().setRunMode( DecisionTreeAlgorithm.TRACE_MODE );
m_manager.getAlgorithm().setVerbose( true );
continueRunning();
}
/**
* Handles and coordinates operation when the user
* selects the 'Pause' menu item.
*/
private void handlePause()
{
// Set all menu items to their 'pause' states.
setMenuState( PAUSE_STATE );
// Turn on break mode and enable verbose messaging.
m_manager.getAlgorithm().setRunMode( DecisionTreeAlgorithm.BREAK_MODE );
m_manager.getAlgorithm().setVerbose( true );
}
/**
* Handles and coordinates operation when the user
* selects the 'Step' menu item.
*/
private void handleStep()
{
// Check - are we trying to perform reduced-error
// pruning without a testing dataset?
if( !ensureTestDatasetAvailIfNeeded() ) return;
// Set all menu items to their 'pause' states.
setMenuState( PAUSE_STATE );
// Turn on break mode and enable verbose messaging.
m_manager.getAlgorithm().setRunMode( DecisionTreeAlgorithm.BREAK_MODE );
m_manager.getAlgorithm().setVerbose( true );
continueRunning();
}
/**
* Handles and coordinates operation when the user
* selects the 'Cancel' menu item.
*/
private void handleCancel()
{
// Reset all menu items to their initial states.
setMenuState( INITIAL_STATE );
// Stop the algorithm.
m_manager.getAlgorithm().stopRunning();
m_manager.setAlgorithmThread( null );
}
/**
* Handles and coordinates operation when the user
* selects the 'Backup' menu item.
*/
private void handleBackup()
{
// If there's a thread currently running,
// we have to kill it - otherwise, we might
// end up backing up while we're in the middle
// of adding a node to the tree.
m_manager.setAlgorithmThread( null );
m_manager.getAlgorithm().getTree().backup();
// Once we've backed up, the algorithm can
// no longer be finished, so reset the menu.
setMenuState( INITIAL_STATE );
}
/**
* Handles and coordinates operation when the user
* selects the 'Initialize' menu item.
*/
private void handleInitialize()
{
// Kill current thread, if one exists.
m_manager.setAlgorithmThread( null );
m_manager.getAlgorithm().reset();
// Inform the tree view panel that the old
// tree no longer exists.
if( m_manager.getVisualTreePanel() != null )
m_manager.getVisualTreePanel().notifyNewTree();
// Reset all menu items to their start states.
setMenuState( INITIAL_STATE );
}
private void continueRunning()
{
// Continue running the algorithm, or start fresh.
if( m_manager.getAlgorithmThread() != null )
m_manager.getAlgorithm().continueRunning();
else {
m_manager.setAlgorithmThread(
new Thread( m_manager.getAlgorithm() ) );
m_manager.getAlgorithmThread().start();
}
}
/**
* Checks to ensure that a testing dataset is available
* if the algorithm is being run with reduced-error
* pruning turned on. If the algorithm <i>is</i> being run with
* reduced-error pruning turned on and no testing dataset
* is available, the method displays an error dialog
* prompting the user to create a testing dataset before
* continuing.
*
* @return <code>true</code> if a testing dataset is defined
* or the pruning algorithm is set to 'none' or
* 'pessimistic', <code>false</code> otherwise.
*/
private boolean ensureTestDatasetAvailIfNeeded()
{
// Check the current pruning function.
String pruningAlg = m_manager.getAlgorithm().getPruningAlgorithm();
if( !pruningAlg.equals( DecisionTreeAlgorithm.PRUNING_REDUCED_ERROR ) )
return true;
// Do we have at least one test example?
Dataset dataset = m_manager.getAlgorithm().getDataset();
if( dataset.getNumTestingExamples() > 0 )
return true;
// Display an error dialog
JOptionPane.showMessageDialog( null,
"You must define a testing dataset before you can use " +
"reduced-error pruning", "Hey!", JOptionPane.ERROR_MESSAGE );
return false;
}
/**
* Build the menu.
*/
private void buildMenu()
{
m_run = new JMenuItem( RUN_CMND );
m_traceRun = new JMenuItem( TRACE_RUN_CMND );
m_pause = new JMenuItem( PAUSE_CMND );
m_step = new JMenuItem( STEP_CMND );
m_cancel = new JMenuItem( CANCEL_CMND );
m_backup = new JMenuItem( BACKUP_CMND );
m_initialize = new JMenuItem( INITIALIZE_CMND );
// Register keystrokes for the menu items.
m_run.setAccelerator( RUN_KEYSTROKE );
m_traceRun.setAccelerator( TRACE_RUN_KEYSTROKE );
m_pause.setAccelerator( PAUSE_KEYSTROKE );
m_step.setAccelerator( STEP_KEYSTROKE );
m_cancel.setAccelerator( CANCEL_KEYSTROKE );
m_backup.setAccelerator( BACKUP_KEYSTROKE );
m_initialize.setAccelerator( INITIALIZE_KEYSTROKE );
// Register ActionListeners for the menu items.
m_run.addActionListener( this );
m_traceRun.addActionListener( this );
m_pause.addActionListener( this );
m_step.addActionListener( this );
m_cancel.addActionListener( this );
m_backup.addActionListener( this );
m_initialize.addActionListener( this );
// Populate the menu.
add( m_run );
add( m_traceRun );
add( m_pause );
add( m_step );
add( m_cancel );
add( m_backup );
add( m_initialize );
addSeparator();
m_splitMenu = new JMenu( "Set Splitting Function" );
// Add list of the available splitting functions.
String[] splitFunList = DecisionTreeAlgorithm.SPLIT_FUNCTIONS;
ButtonGroup splitFunGroup = new ButtonGroup();
for( int i = 0; i < splitFunList.length; i++) {
JRadioButtonMenuItem splitFunItem =
new JRadioButtonMenuItem( splitFunList[i] );
splitFunItem.addActionListener( this );
m_splitMenu.add( splitFunItem );
splitFunGroup.add( splitFunItem );
}
// Set the first splitting function as the default.
if( m_splitMenu.getItemCount() != 0 )
m_splitMenu.getItem( 0 ).setSelected( true );
add( m_splitMenu );
m_pruneMenu = new JMenu( "Set Pruning Algorithm" );
// Add list of the available pruning algorithms.
String[] pruneAlgList = DecisionTreeAlgorithm.PRUNING_ALGORITHMS;
ButtonGroup pruneAlgGroup = new ButtonGroup();
for( int i = 0; i < pruneAlgList.length; i++ ) {
JRadioButtonMenuItem pruneAlgItem =
new JRadioButtonMenuItem( pruneAlgList[i] );
pruneAlgItem.addActionListener( this );
m_pruneMenu.add( pruneAlgItem );
pruneAlgGroup.add( pruneAlgItem );
}
// Set the first pruning algorithm as the default.
if( m_pruneMenu.getItemCount() != 0 )
m_pruneMenu.getItem( 0 ).setSelected( true );
add( m_pruneMenu );
addSeparator();
// Add preferences item.
m_prefs = new JMenuItem( "Preferences..." );
m_prefs.addActionListener( this );
add( m_prefs );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -