📄 jshop2gui.java
字号:
// I should add temp to the leafNodes list here, but it's problematic
// because I don't have access to its DefaultMutableTreeNode wrapper object
// due to it not being created yet. This is only a problem in the trivial case
// where the plan consists of only the root node, so I deemed it unnecessary to
// deal with...for now.
} else { // setting node's state to current state
DefaultMutableTreeNode treeNode = treeNodeReferences.get( step.taskAtom.getHead().getID() );
Node temp = (Node)treeNode.getUserObject();
temp.state = step.state;
treeModel.nodeChanged( treeNode );
// adding the node as a leaf, as long as it's not (!!INOP)
if ( !step.taskAtom.equals("(!!INOP)") ) {
leafNodes.add( treeNode );
renumberLeaves();
}
// selecting the node onscreen
tree.setSelectionPath(new TreePath(treeNode.getPath()));
tree.scrollPathToVisible(new TreePath(treeNode.getPath())); // makes the node visible
}
return parent;
}
//-----------------------------------------------------------------------------
/**
* Helper function to runOneStep().
* This function adds children to existing nodes and marks them if they are ordered.
*/
private DefaultMutableTreeNode processReduced( PlanStepInfo step, ArrayList<Node> toAdd ) {
DefaultMutableTreeNode parent = null;
// set messageLabel
String msg = "Reduced ";
msg += step.taskAtom;
msg += " into the following: ";
// The term "parent" here is referring to the current node (parent as in parent of
// the children that are about to be added).
parent = treeNodeReferences.get( step.taskAtom.getHead().getID() );
// Setting the method name for this node
Node node = (Node)parent.getUserObject();
node.method = step.method;
// Removing this node from the leaf nodes list, since it now has children.
// This step assumes that a REDUCED statement will always follow a TRYING
// statement pertaining to the same task atom, resulting in the deletion of the
// last element in leafNodes.
if ( !leafNodes.isEmpty() ) {
// removing numbering from this node
node.tag = null;
treeModel.nodeChanged( parent );
// removing this node from leafNodes list
leafNodes.remove( leafNodes.size()-1 );
}
// backtrack if this node has already been reduced and is being reduced again
if ( !parent.isLeaf() )
backtrack( parent );
// selecting the node onscreen
tree.setSelectionPath(new TreePath(parent.getPath()));
tree.scrollPathToVisible(new TreePath(parent.getPath())); // makes the node visible
// creating the children to be added
for ( int i = 0; i < step.children.length; i++ ) {
String childName = step.children[i].getTask().toString();
Node newNode = new Node( childName, step.children[i].getTask().getHead().getID() );
msg += childName;
msg += " ";
if ( !step.ordered )
newNode.ordered = false;
toAdd.add(newNode);
}
messageLabel.setText( msg );
return parent;
}
//-----------------------------------------------------------------------------
private DefaultMutableTreeNode processStateChanged(PlanStepInfo step) {
DefaultMutableTreeNode retval = null;
// set messageLabel
String msg = "State changed by ";
msg += step.operatorInstance;
messageLabel.setText( msg );
retval = treeNodeReferences.get( step.taskAtom.getHead().getID() );
// Setting delAdd for this node
Node node = (Node)retval.getUserObject();
node.delAdd = step.delAdd;
// Changing name to intantiated version
node.name = step.operatorInstance;
// selecting the node onscreen
tree.setSelectionPath(new TreePath(retval.getPath()));
tree.scrollPathToVisible(new TreePath(retval.getPath())); // makes the node visible
return retval;
}
//-----------------------------------------------------------------------------
/**
* Sets the goal tasks for the problem. The goal tasks are stored in the 'children' field
* of the 'step' parameter. Each element of 'children' represents a TaskList that encapsulates
* a goal task. Calling getTask() on each TaskList gives access to the TaskAtom that represents
* the goal task. If getTask() returns null, then that means the goal task is encapsulated
* one or more levels down and getSubtasks() must be called. This is done recursively by
* 'setGoalTasksHelper'.
*/
private DefaultMutableTreeNode processSetGoalTasks(PlanStepInfo step, ArrayList<Node> toAdd) {
DefaultMutableTreeNode retval = null;
retval = rootNode;
setGoalTasksHelper(step.children, step, toAdd);
messageLabel.setText("Goal tasks added");
return retval;
}
private void setGoalTasksHelper(TaskList[] children, PlanStepInfo step, ArrayList<Node> toAdd) {
for (int i = 0; i < children.length; i++) {
TaskList child = children[i];
if (child.getTask() != null) {
Node temp = new Node( child.getTask().toString(), child.getTask().getHead().getID());
temp.state = step.state;
temp.ordered = step.ordered;
toAdd.add( temp );
} else if (child.getSubtasks() != null)
setGoalTasksHelper(child.getSubtasks(), step, toAdd);
}
}
//-----------------------------------------------------------------------------
/**
* Helper function to runOneStep().
* This function takes care of backtracking procedures.
*/
private void processBacktracking( PlanStepInfo step ) {
String msg = "Backtracking from ";
msg += step.taskAtom;
messageLabel.setText( msg );
DefaultMutableTreeNode treeNode = treeNodeReferences.get( step.taskAtom.getHead().getID() );
if ( !treeNode.isLeaf() )
backtrack( treeNode );
// If backtracking from a leaf node, check to see if this leaf node was the most recent
// leaf node to be added to 'leafNodes'. If true, then delete this leaf node's tag and
// remove it from 'leafNodes'.
// <Note:> I'm assuming that whenever you backtrack from a leaf node, that leaf node will
// always have the highest tag value in 'leafNodes'. I'm not certain if this is always the case.
else {
Node treeNodeUserObj = (Node)treeNode.getUserObject();
Node leafNodeUserObj = (Node)leafNodes.get(leafNodes.size()-1).getUserObject();
if (treeNodeUserObj.ID == leafNodeUserObj.ID) {
// removing numbering from this node
treeNodeUserObj.tag = null;
treeModel.nodeChanged( treeNode );
// removing this node from leafNodes list
leafNodes.remove( leafNodes.size()-1 );
}
}
// selecting the node onscreen
tree.setSelectionPath(new TreePath(treeNode.getPath())); // selects the node
tree.scrollPathToVisible(new TreePath(treeNode.getPath())); // makes the node visible
}
//-----------------------------------------------------------------------------
/**
* This function handles the procedures that take place when backtracking.
* First, it modifies the leafNodes list and then deletes any children under
* treeNode.
*/
private void backtrack( DefaultMutableTreeNode treeNode ) {
// removing any leaves in leafNodes that are descendants of treeNode
for (int i = 0; i < leafNodes.size(); i++) {
if ( treeNode.isNodeDescendant(leafNodes.get(i)) ) {
leafNodes.remove(i);
i--;
}
}
// reunumbering leaves on-screen
renumberLeaves();
// deleting children from this node
treeNode.removeAllChildren();
treeModel.reload( treeNode );
}
//-----------------------------------------------------------------------------
/**
* This function renumbers all current leaf nodes in the order they were visited
* relative to one another.
*/
private void renumberLeaves() {
for (int i = 0; i < leafNodes.size(); i++) {
DefaultMutableTreeNode leaf = leafNodes.get(i);
Node node = (Node)leaf.getUserObject();
node.tag = i+1;
treeModel.nodeChanged( leaf );
}
}
//-----------------------------------------------------------------------------
/**
* Listener Classes
*/
private class SHOP2GUIWindowAdapter extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
//-----------------------------------------------------------------------------
private class SHOP2GUIKeyAdapter extends KeyAdapter {
public void keyTyped(KeyEvent ke) {
if ( ke.getKeyChar() == ' ' ) {
runOneStep();
}
}
}
//-----------------------------------------------------------------------------
private class SHOP2GUIMenuHandler implements ActionListener {
public void actionPerformed( ActionEvent ae ) {
String arg = (String)ae.getActionCommand();
if ( arg.equals("Exit") )
System.exit(0);
else if ( arg.equals("Leaf Node Tracker...") )
leafTracker.visible( true );
else if ( arg.equals("Show State...")) {
String title = "State for ";
title += selectedNodeName;
new StateWindowDialog( title );
}
}
}
//-----------------------------------------------------------------------------
/**
* User object utilized by the tree model
*/
private class Node {
public String name;
public String method;
public boolean ordered;
public ArrayList<String> state;
public Integer tag;
public int ID;
public Vector[] delAdd;
public Node( String nameIn, int IDin ) {
name = nameIn;
method = "";
ordered = true;
state = null;
tag = null;
ID = IDin;
delAdd = null;
}
public String toString() {
if ( tag == null )
return name;
else {
String newName = "[ ";
newName += String.valueOf( tag.intValue() );
newName += " ] ";
newName += name;
return newName;
}
}
public boolean hasState() {
return state != null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -