⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jshop2gui.java

📁 SHOP2 一个人工智能里面关于任务分解和任务规划的系统。JSHOP2是其java版本。
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                int numSteps = ( Integer.valueOf(multiStepField.getText()) ).intValue();                
                for ( int i = 0; i < numSteps; i++ ) {
                    if ( runOneStep() == false )
                        break;
                }
                /*
                for (int i = 0; i < commandList.size(); i++)
                    ((Command)commandList.elementAt(i)).print();
                */
            }
        });
        
        JButton runButton = new JButton( "Run" );
        runButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                while ( runOneStep() ) {}
            }
        });        
 
        
        JButton restartButton = new JButton( "Restart" );
        restartButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                iterator = 0;
                planNumber = 0;
                treeNodeReferences.clear();
                
                // clearing the tree model and refreshing the display
                rootNode.removeAllChildren();
                treeModel.reload();
                
                // clearing the text in the top status label
                messageLabel.setText("");
                
                // clearing text in the bottom message boxes
                stateTextArea.replaceRange( "", 0, stateTextArea.getText().length() );
                stepInfoTextArea.replaceRange( "", 0, stepInfoTextArea.getText().length() );
                
                // clearing the list of leaf nodes
                leafNodes.clear();
                
                // resetting progress bar and label
                String msg = "Progress:  0 / ";
                msg += String.valueOf( planStepList.size() );
                progressLabel.setText( msg );
                progressBar.setValue( 0 );
                
                // resetting stateLabel
                stateLabel.setText( "Current State          ( Total: -- )" );
                
                // updating leaf node tracker
                leafTracker.updateNodeCount();
                
            }
        });
        
        // Creating the multi-step size input field
        multiStepField = new JTextField("1");
        
        // Adding components        
        JPanel innerPanel_1 = new JPanel();
        innerPanel_1.setLayout( new GridLayout(0, 1, 5, 5) );
        innerPanel_1.add( multiStepField );
        innerPanel_1.add( multiStepButton );
        innerPanel_1.add( singleStepButton );
        innerPanel_1.add( runButton );
        innerPanel_1.add( restartButton );
        
        JPanel innerPanel_2 = new JPanel();
        innerPanel_2.setLayout( new GridLayout(0, 1) );
        innerPanel_2.add( new Label( "Multi-step size:", Label.CENTER ) );
        innerPanel_2.add( multiStepField );
        
        
        JPanel outerPanel = new JPanel();
        outerPanel.setLayout( new BorderLayout() );        
        outerPanel.add( innerPanel_1, BorderLayout.CENTER );
        outerPanel.add( innerPanel_2, BorderLayout.NORTH );
        
        getContentPane().add( outerPanel, BorderLayout.EAST );
        
        
        
        // ******************************************************* //
        // --------------- Creating the South Side --------------- //
        // ******************************************************* //        
        
        // Creating message area  
        outerPanel = new JPanel();
        getContentPane().add( outerPanel, BorderLayout.SOUTH ); 
        outerPanel.setLayout( new BorderLayout() );
        
        JPanel messagePanel = new JPanel();
        outerPanel.add( messagePanel, BorderLayout.CENTER );
        messagePanel.setLayout( new GridLayout(1, 0) );
        
        JPanel leftMessagePanel = new JPanel();
        messagePanel.add( leftMessagePanel );
        leftMessagePanel.setLayout( new BorderLayout() );
        stateTextArea = new TextArea();
        stateTextArea.setEditable( false );       
        leftMessagePanel.add( stateTextArea, BorderLayout.CENTER );
        stateLabel = new Label( "Current State          ( Total: -- )" );
        leftMessagePanel.add( stateLabel, BorderLayout.NORTH );        
        
        JPanel rightMessagePanel = new JPanel();
        messagePanel.add( rightMessagePanel );
        rightMessagePanel.setLayout( new BorderLayout() );
        stepInfoTextArea = new TextArea();
        stepInfoTextArea.setEditable( false );       
        rightMessagePanel.add( stepInfoTextArea, BorderLayout.CENTER );  
        rightMessagePanel.add( new Label("Step Info"), BorderLayout.NORTH );
        
        
        // Creating progress bar
        progressBar = new JProgressBar();
        progressLabel = new Label( "Progress:  0 / 0" );
        
        innerPanel_1 = new JPanel();
        outerPanel.add( innerPanel_1, BorderLayout.EAST );        
        innerPanel_1.setLayout( new BorderLayout() );        
        
        innerPanel_2 = new JPanel();
        innerPanel_1.add( innerPanel_2, BorderLayout.NORTH );
        innerPanel_2.setLayout( new GridLayout(0,1) );
        innerPanel_2.add( progressLabel );
        innerPanel_2.add( progressBar );
        
        
        
        // ******************************************************* //
        // --------------- Creating the North Side --------------- //
        // ******************************************************* //        
        
        // Creating message Label        
        messageLabel = new Label(""); 
        getContentPane().add( messageLabel, BorderLayout.NORTH );
        
        
        
        // ********************************************************* //
        // --------------- Making the window visible --------------- //
        // ********************************************************* //        
        
        // Create main window
        setSize( new Dimension(800, 700) );
        setTitle( "Graphical Interface for JSHOP2" );
       
        
        // Register listeners
        addWindowListener( new SHOP2GUIWindowAdapter() );
        tree.addKeyListener( new SHOP2GUIKeyAdapter() );
        
        
    }    
    
//-----------------------------------------------------------------------------    
    
    /**
     * Executes a single step in the plan step list
     */    
    private boolean runOneStep() {
        boolean retval = true;
        ArrayList<Node> toAdd = new ArrayList<Node>();
        DefaultMutableTreeNode parent = null;
        
        if ( iterator < planStepList.size() ) {
            PlanStepInfo step = planStepList.get(iterator++);
            
            // setting progress bar
            String msg = "Progress:  ";
            msg += String.valueOf( iterator );
            msg += " / ";
            msg += String.valueOf( planStepList.size() );
            progressLabel.setText( msg );
            progressBar.setValue( iterator );
            
            
            // if a plan has been found            
            if ( step.planFound == true ) {
                processPlanFound();
                
                // retval is set to false here so that the "run" feature will stop every time
                // a plan is found.  
                retval = false;
            }
            
            // trying a task
            else if ( step.action.equals("TRYING") ) {
                // If the last step was a "plan found" step, then the leafNodes
                // vector has to be cleared now that it is working on a new plan.  Here,
                // iterator is subtracted by 2 due to the fact that it's been post-incremented
                // above.  This step assumes that a "plan found" step will always be followed
                // by a "trying" step, and it's implemented to enable dynamic leaf node tracking.
                if ( iterator - 2 >= 0 )
                    if ( planStepList.get(iterator - 2).planFound == true )
                        leafNodes.clear();
                parent = processTrying( step, toAdd );
            }
                
            // reducing a task
            else if ( step.action.equals("REDUCED") )
                parent = processReduced( step, toAdd );
            
            else if ( step.action.equals("STATECHANGED") )
            	parent = processStateChanged( step );
            
            else if ( step.action.equals("SETGOALTASKS")) 
            	parent = processSetGoalTasks(step, toAdd);            
                
            // backtracking   
            else if ( step.action.equals("BACKTRACKING") )
                processBacktracking( step );
                
            // updating leaf node tracker
            leafTracker.updateNodeCount();
            
            // adding nodes to treeModel and treeNodeReferences    
            for (int i = 0; i < toAdd.size(); i++) {
                Node add = toAdd.get(i);               
                DefaultMutableTreeNode child = new DefaultMutableTreeNode( add );
                treeNodeReferences.put( add.ID, child );
                treeModel.insertNodeInto( child, parent, parent.getChildCount() );
                tree.scrollPathToVisible(new TreePath(child.getPath())); // makes the node visible
                if (iterator == 1) // special case when displaying the goal task
                    tree.setSelectionPath(new TreePath(child.getPath())); 
            }
            
            // Ensures that after a REDUCED and STATECHANGED step, a 'valueChanged' event will be generated
            // for the parent so the updated info will be displayed in the GUI.
            if (step.action.equals("REDUCED") || step.action.equals("STATECHANGED")) {
            	TreePath parentPath = new TreePath(parent.getPath());
            	TreeSelectionEvent e = new TreeSelectionEvent(parent, parentPath, false,
            			parentPath, parentPath);            	
            	TreeSelectionListener listener = tree.getTreeSelectionListeners()[0];
            	listener.valueChanged(e);
            }
            
        } else
            retval = false;
        
        return retval;
    }    
    
//-----------------------------------------------------------------------------  
    
    /**
     * Helper function to runOneStep().
     * This function executes the steps required to display the plan on-screen
     */
    private void processPlanFound() {
        planNumber++;
        ArrayList<String> plan = new ArrayList<String>(); // vector of strings containing the leaves' numbered names
        
        // setting messageLabel
        messageLabel.setText( "Plan found" );
        
        // Labeling leaf nodes with appropriate numbers.
        // Although renumberLeaves() does the same thing, it isn't called here
        // so that planList can be created in the process of renumbering the leaves.
        for (int i = 0; i < leafNodes.size(); i++) {                    
            DefaultMutableTreeNode leaf = leafNodes.get(i);
            Node node = (Node)leaf.getUserObject();
            node.tag = i+1;            
            plan.add( node.toString() );
            
            treeModel.nodeChanged( leaf );
        }        
        
        // Creating found plan dialog box
        String title = "Plan ";
        title += String.valueOf( planNumber );
        title += " of ";
        title += String.valueOf( numPlans );
        new PlanDialog( title, plan );
    }
    
//-----------------------------------------------------------------------------
    
    /**
     * Helper function to runOneStep().
     * This function determines the current state of the world for every node
     * and inserts the goal task into the tree.
     */    
    private DefaultMutableTreeNode processTrying( PlanStepInfo step, ArrayList<Node> toAdd ) {        
        DefaultMutableTreeNode parent = null;
        
        // set messageLabel
        String msg = "Trying ";
        msg += step.taskAtom;
        messageLabel.setText( msg );                
                    
        // if adding the root node
        if ( iterator == 1 ) {
            parent = rootNode;
            Node temp = new Node( step.taskAtom.toString(), step.taskAtom.getHead().getID() );
            temp.state = step.state;
            toAdd.add( temp );
            

⌨️ 快捷键说明

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