Class RobotWorld

java.lang.Object
   |
   +----RobotWorld

public class RobotWorld
extends Object
implements Runnable
This is the "overlord" of the engine. It contains the main symbol tables and a two-dimensional array of Cells. It has routines for all of the top-level statements as well as the main tree-traversal/ execution routine. It also evaluates all expressions, inside and outside of a robot. It has a pointer to an applet which handles all the display.

Author:
Peter Sagerson, petersag@wpi.edu
See Also:
Cell, Robot

Variable Index

 o applet
The applet that this object will report to.
 o delay
The time (in milliseconds) to delay between robot actions.
 o EAST
This value must NOT change (see Robot.turnLeft())
 o execute
When the user just wants to parse the program, this is set to true before executing.
 o fDebug
When true, the node being executed will be displayed during normal running conditions.
 o firstExecution
A cheesy boolean so that we only set the world size the first time through.
 o interrupted
When true, the execution thread will pause between three nodes until it is notified.
 o kennyFrame
 o lastCode
The last string to be parsed.
 o lastRoot
The AST associated with lastCode.
 o mapSize
The length of one side of the map in grid squares.
 o NORTH
This value must NOT change (see Robot.turnLeft())
 o parser
A reference to the parser created by RobotlApplet.
 o robots
The list of existing Robots.
 o robotTypes
The list of existing RobotTypes.
 o SOUTH
This value must NOT change (see Robot.turnLeft())
 o variables
The list of existing global variables.
 o WEST
This value must NOT change (see Robot.turnLeft())
 o world
Keeps track of the contents of each individual grid square.
 o worldEntities
The list of existing WorldEntities.

Constructor Index

 o RobotWorld(RobotlApplet)
The one and only constructor.

Method Index

 o addRobot(Robot, int, int)
Adds an existing robot to the world.
 o beepersAt(int, int)
Returns the number of beepers at the given location.
 o debug(SimpleNode)
This is called before the execution of every significant node.
 o error(String, int)
Posts a message to the user (with postMessage) and then halts execution.
 o eval(Node, Robot)
Evaluates all expressions, inside and outside of a Tell.
 o execAssignment(Node, Robot)
Handles an assignment, inside or outside of a Tell.
 o execDoWhile(Node, Robot)
Handles a Do-While loop, inside or outside of a Tell.
 o execIf(Node, Robot)
Handles an If conditional, inside or outside of a Tell.
 o execIterate(Node, Robot)
Handles an Iterate loop, inside or outside of a Tell.
 o execute(Node)
Executes a branch of the AST.
 o execWhile(Node, Robot)
Handles a While loop, inside or outside of a Tell.
 o getCell(int, int)
Returns the cell at the given coordinates.
 o getGlobals()
Returns an enumeration containing all of the global variables.
 o getMapSize()
Returns length of one side of the world, in grid squares.
 o getRobot(String)
Returns a Robot reference for the given name.
 o getWorldArray()
Returns the array of Cells.
 o interrupt()
Pauses or resumes execution, depending on the current state.
 o moveRobot(Robot, int, int)
Moves the given robot the given number of spaces in the given direction.
 o paintCell(int, int)
Asks the applet to paint a single cell, specified in grid coordinates.
 o parse()
Initializes the parser and parses the code.
 o pause()
Pauses the execution thread for the amount of time specified in the delay field.
 o pickBeeper(int, int)
Tells the world that a robot is trying to pick up a beeper at the given location.
 o placeWorldEntity(WorldEntity, int, int)
Called for the RoBOTL "place" command.
 o postMessage(String, int)
An interface to the applet's postMessage method.
 o putBeeper(int, int)
Tells the world that a robot is putting down a beeper at the given location.
 o reInit(boolean, boolean)
Sets up to run a new program.
 o removeRobot(String)
Removes a robot from the world.
 o run()
The required run method that parses and executes and handles the exceptions.
 o setDebug(boolean)
Sets the fDebug field.
 o setDelay(int)
Sets the delay field.
 o spaceIsClear(int, int)
Returns 1 if the given space does not have a robot or a wall.
 o step()
Pauses execution (if it's already paused, there's no change) and signals the execution thread to proceed.
 o updateRobotFeedback(Robot)
A wrapper for RobotlApplet.updateRobotFeedback().
 o warning(String, int)
Posts a message to the user (with postMessage) and then continues with execution.

Variables

 o world
 Cell world[][]
Keeps track of the contents of each individual grid square.

 o applet
 RobotlApplet applet
The applet that this object will report to.

 o robots
 SymbolTable robots
The list of existing Robots.

 o robotTypes
 SymbolTable robotTypes
The list of existing RobotTypes.

 o worldEntities
 SymbolTable worldEntities
The list of existing WorldEntities.

 o variables
 SymbolTable variables
The list of existing global variables.

 o parser
 RoBOTL parser
A reference to the parser created by RobotlApplet.

 o mapSize
 int mapSize
The length of one side of the map in grid squares.

 o delay
 int delay
The time (in milliseconds) to delay between robot actions.

 o lastCode
 String lastCode
The last string to be parsed.

 o lastRoot
 ASTStart lastRoot
The AST associated with lastCode.

 o execute
 boolean execute
When the user just wants to parse the program, this is set to true before executing. It is set in reInit().

 o interrupted
 boolean interrupted
When true, the execution thread will pause between three nodes until it is notified.

 o fDebug
 public boolean fDebug
When true, the node being executed will be displayed during normal running conditions. The applicable line will also be highlighted.

 o firstExecution
 private boolean firstExecution
A cheesy boolean so that we only set the world size the first time through.

 o NORTH
 public static final int NORTH
This value must NOT change (see Robot.turnLeft())

 o EAST
 public static final int EAST
This value must NOT change (see Robot.turnLeft())

 o SOUTH
 public static final int SOUTH
This value must NOT change (see Robot.turnLeft())

 o WEST
 public static final int WEST
This value must NOT change (see Robot.turnLeft())

 o kennyFrame
 ImageFrame kennyFrame

Constructors

 o RobotWorld
 public RobotWorld(RobotlApplet inApplet)
The one and only constructor. Calls reInit().

Parameters:
inApplet - the applet that this RobotWorld reports to

Methods

 o reInit
 public void reInit(boolean inExecute,
                    boolean inStepping)
Sets up to run a new program. Creates blank symbol tables, defines basic_bot and a few other miscellaneous housekeeping tasks. This must be called between calls to execute().

Parameters:
inStepping - true if execution should be immediately paused.
 o getWorldArray
 public Cell[][] getWorldArray()
Returns the array of Cells. RobotlCanvas calls this in order to update the screen.

 o getCell
 public Cell getCell(int x,
                     int y) throws ArrayIndexOutOfBoundsException
Returns the cell at the given coordinates.

Returns:
null if the array of cells hasn't been created yet.
Throws: ArrayIndexOutOfBoundsException
if the coordinates are out of range.
 o getMapSize
 public int getMapSize()
Returns length of one side of the world, in grid squares. The applet uses this to calculate the size of a grid square in pixels.

 o getGlobals
 public Enumeration getGlobals()
Returns an enumeration containing all of the global variables.

 o beepersAt
 public int beepersAt(int x,
                      int y)
Returns the number of beepers at the given location.

 o spaceIsClear
 public int spaceIsClear(int x,
                         int y)
Returns 1 if the given space does not have a robot or a wall. Ultimately called from eval().

 o setDelay
 public void setDelay(int d)
Sets the delay field. The applet calls this at each execution and sets it based on the value in the "delay" TextField.

Parameters:
d - the delay in milliseconds
 o setDebug
 public void setDebug(boolean b)
Sets the fDebug field.

 o paintCell
 public void paintCell(int x,
                       int y)
Asks the applet to paint a single cell, specified in grid coordinates.

 o step
 public synchronized void step()
Pauses execution (if it's already paused, there's no change) and signals the execution thread to proceed. It will execute one node and then pause again in debug().

 o interrupt
 public synchronized boolean interrupt()
Pauses or resumes execution, depending on the current state. Returns true if execution is paused after the function exits.

 o debug
 public synchronized void debug(SimpleNode node)
This is called before the execution of every significant node. If debug output is enabled or execution is paused, it prints out the next node to be executed. If interrupt is true, this function pauses the current thread indefinitely (i.e. until notified).

Parameters:
node - the node that is about to be executed.
 o pause
 public synchronized void pause()
Pauses the execution thread for the amount of time specified in the delay field.

See Also:
delay
 o getRobot
 public Robot getRobot(String name) throws NameNotFoundException
Returns a Robot reference for the given name.

Parameters:
name - the name of the robot.
Throws: s
NameNotFoundException if the robot does not exist.
 o placeWorldEntity
 public void placeWorldEntity(WorldEntity we,
                              int x,
                              int y) throws BadCoordException
Called for the RoBOTL "place" command. Adds the contents of the WorldEntity to the world at the given coordinates, with the following first-come, first-serve rules: 1. Nothing will be placed in an occupied cell. 2. A block will not replace a pile of beepers. An occupied cell is one that contains a wall or a robot. Beepers placed on existing beepers will simply increase the number of beepers in that cell. Anything that is off the map will be ignored.

Parameters:
we - the WorldEntity to place
Throws: BadCoordException
if x or y are out of bounds.
 o addRobot
 public void addRobot(Robot bot,
                      int x,
                      int y) throws ObstacleException, DuplicateNameException, BadCoordException
Adds an existing robot to the world.

Parameters:
bot - the robot to be placed
Throws: ObstacleException
if the robot collides with another object.
 o removeRobot
 public Robot removeRobot(String name) throws NameNotFoundException
Removes a robot from the world.

Parameters:
name - the name of the robot to be removed.
 o moveRobot
 public Point moveRobot(Robot r,
                        int direction,
                        int n) throws BadCountException
Moves the given robot the given number of spaces in the given direction.

Parameters:
r - the robot to be moved.
direction - the direction to move it
n - the number of spaces to move it
Throws: ObstacleException
if it runs into a wall or another robot.
 o pickBeeper
 void pickBeeper(int x,
                 int y) throws BadCountException
Tells the world that a robot is trying to pick up a beeper at the given location.

Throws: BadCountException
Thrown if there is no beeper to pick up at (x, y).
 o putBeeper
 void putBeeper(int x,
                int y)
Tells the world that a robot is putting down a beeper at the given location. Can't fail.

 o updateRobotFeedback
 void updateRobotFeedback(Robot r)
A wrapper for RobotlApplet.updateRobotFeedback(). Called from Robot.tell().

 o execAssignment
 void execAssignment(Node node,
                     Robot robot) throws NameNotFoundException
Handles an assignment, inside or outside of a Tell.

Parameters:
node - the root of the assignment branch
robot - null if this assignment is outside of a Tell
 o execIf
 void execIf(Node node,
             Robot robot)
Handles an If conditional, inside or outside of a Tell.

Parameters:
node - the root of the If branch
robot - null if this If is outside of a Tell.
 o execIterate
 void execIterate(Node node,
                  Robot robot)
Handles an Iterate loop, inside or outside of a Tell.

Parameters:
node - the root of the Iterate branch
robot - null if this Iterate is outside of a Tell.
 o execWhile
 void execWhile(Node node,
                Robot robot)
Handles a While loop, inside or outside of a Tell.

Parameters:
node - the root of the While branch
robot - null if this While is outside of a Tell.
 o execDoWhile
 void execDoWhile(Node node,
                  Robot robot)
Handles a Do-While loop, inside or outside of a Tell.

Parameters:
node - the root of the Do-While branch
robot - null if this Do-While is outside of a Tell.
 o execute
 public void execute(Node root)
Executes a branch of the AST. This is the main execution loop, but it is called recursively.

Parameters:
root - the root of the branch to execute
 o eval
 public int eval(Node root,
                 Robot robot)
Evaluates all expressions, inside and outside of a Tell.

Parameters:
root - the root of the expression branch
robot - null if the expression is outside of a Tell.
 o parse
 private ASTStart parse() throws ParseException, TokenMgrError
Initializes the parser and parses the code. Returns the root of the resulting abstract syntax tree.

 o run
 public void run()
The required run method that parses and executes and handles the exceptions.

 o postMessage
 public void postMessage(String msg,
                         int line)
An interface to the applet's postMessage method. Adds the line number and a newline to the end and tells the applet to select the given line in the code.

Parameters:
msg - The basic message
line - The line of code associated with the message
 o warning
 public void warning(String msg,
                     int line)
Posts a message to the user (with postMessage) and then continues with execution. Used to display non-fatal errors.

Parameters:
msg - the basic message to display
line - the line number associated with the message. -1 indicates that there is no associated line.
 o error
 public void error(String msg,
                   int line)
Posts a message to the user (with postMessage) and then halts execution. Used to display fatal errors.

Parameters:
msg - the basic message to display
line - the line number associated with the message. -1 indicates that there is no associated line.