Java Reference
In-Depth Information
public synchronized void
start() {
if
(! alive) {
alive
#
true
;
thread.start();
}
}
// The run() method implements the finite state machine
// that manages the commands' execution
public void
run() {
do
{
try
{
if
(running) // pause before the next step
synchronized
(
this
) { wait(delay); }
else if
(commands.size() > 0) {
// extracts the the next command and executes it
String command
#
(String) commands.remove(0);
executeCommand(command);
}
else
// waits for a new command
synchronized
(lock) { lock.wait(); }
// processes the next step of the current command
nextStep();
}
catch
(InterruptedException ie) {
continue
;}
}
while
(alive);
}
// These abstract methods must be implemented in every
// subclass of Device
public abstract void
executeCommand(String command);
public abstract void
nextStep();
}
Class
Platform
extends class
Device
to implement the dynamics of the
mobile platform device.
public class
Platform
extends
Device {
int
orientation
#
1; // 1: clockwise -1: otherwise
double
rotStep
#
1.0; // one degree
double
moveStep
#
1.0; // one centimetre
double
numRotSteps
#
0;
// number of steps to complete the rotation
double
numMoveSteps
#
0;
// number of steps to complete the movement
public
Platform(String name, Position global,
Position local, Environment env){ ... }
// This method parses the command string and initializes
// the state variables
public void
executeCommand(String command) {