8.2 From Models to Implementations


8.2 From Models to Implementations

As mentioned, a design architecture describes the gross organization and global structure of the system in terms of its constituent components. In order to support behavior modeling and analysis, the architecture supports an elaborated view of its structure and the behavior of its components. We have used structure diagrams and FSP for this purpose. However, there are other forms of model and analysis that might be of interest (Figure 8.11). These too can be considered as views on the underlying structure, the elaboration adding those particular details of concern. For instance, another example of a view is a performance model, such as a queuing network.

image from book
Figure 8.11: Design architecture, behavior model and other models.

Our particular concern after modeling is implementation. Note that even the implementation of the system should be considered as an elaboration of the underlying design architecture. Here the detail is the implementation of the component processes as threads and monitors. Maintaining consistency between these views is facilitated by the fact that the architectural structure is common to the different model views and the implementation.

The program described in this section provides a simulation for the environment in which the cruise control implementation executes. Our simulation is a Java applet that provides buttons to simulate the actions of accelerating, braking and turning on and off both the engine and the cruise control system. Car speed is displayed on a simulated speedometer, which includes an odometer that registers the distance the car has progressed. A screen shot of the cruise control applet is depicted in Figure 8.12. The display shows the situation in which cruise control has been enabled to maintain the car at a steady speed of 65 miles per hour. The car has moved 1.18 miles since it was started.

image from book
Figure 8.12: Cruise control applet display.

The class diagram for the cruise control program is shown in Figure 8.13. The classes Controller and SpeedControl implement the model processes CRUISECONTROLLER and SPEEDCONTROL. SpeedControl interacts with the car simulation provided by the class CarSimulator via the interface CarSpeed. This interface provides methods to set the throttle and to get the current speed at which the car is traveling. We have introduced this interface so that the classes implementing control are insulated from all the details of the simulation. The interface is listed in Program 8.1.

image from book
Figure 8.13: Cruise control class diagram.

Program 8.1: CarSpeed interface.

image from book
 public interface CarSpeed {     public int getSpeed();     public void setThrottle(double val); }
image from book

When a button is pressed, this event is passed by the Applet class to both the car simulator and the cruise controller using a method call. Thus, the Controller class provides a method corresponding to each button.

The implementation of Controller follows directly from the model process CRUISECONTROLLER. Each method modifies the control state and invokes speed control actions. The implementation is listed in Program 8.2. Controller is a passive entity; it always reacts to events and does not instigate them. It is implemented as a monitor with each model action becoming a synchronized method.

Program 8.2: Controller class.

image from book
 class Controller {   final static int INACTIVE  = 0;  // cruise controller states   final static int ACTIVE   = 1;   final static int CRUISING  = 2;   final static int STANDBY   = 3;   private int controlState  = INACTIVE;  // initial state   private SpeedControl sc;   Controller(CarSpeed cs, CruiseDisplay disp)     {sc=new SpeedControl(cs,disp);}   synchronized void brake(){     if (controlState==CRUISING )       {sc.disableControl(); controlState=STANDBY; }   }   synchronized void accelerator(){     if (controlState==CRUISING )       {sc.disableControl(); controlState=STANDBY; }   }   synchronized void engineOff(){     if(controlState!=INACTIVE) {       if (controlState==CRUISING) sc.disableControl();       controlState=INACTIVE;     }   }   synchronized void engineOn(){     if(controlState==INACTIVE)       {sc.clearSpeed(); controlState=ACTIVE;}   }   synchronized void on(){     if(controlState!=INACTIVE){       sc.recordSpeed(); sc.enableControl();       controlState=CRUISING;     }   }   synchronized void off(){     if(controlState==CRUISING )       {sc.disableControl(); controlState=STANDBY;}     }   synchronized void resume(){     if(controlState==STANDBY)      {sc.enableControl(); controlState=CRUISING;}   } }
image from book

In contrast, SpeedControl, listed in Program 8.3, is an active entity. When it is enabled, a thread is created which periodically obtains the current car speed from the car simulator and sets the throttle so that the target cruising speed is maintained. The thread terminates when speed control is disabled. The run() method is also synchronized to prevent interference with the other methods of the class, and uses a wait with timeout to provide periodic execution. The wait(long) method of Java can be used to put the current thread to sleep until it is notified or the specified timeout period (in milliseconds) elapses.

Program 8.3: SpeedControl class.

image from book
 class SpeedControl implements Runnable {   final static int DISABLED = 0; //speed control states   final static int ENABLED  = 1;   private int state = DISABLED; //initial state   private int setSpeed = 0;     //target cruise control speed   private Thread speedController;   private CarSpeed cs;          //interface to control speed of engine   private CruiseDisplay disp;   SpeedControl(CarSpeed cs, CruiseDisplay disp){     this.cs=cs; this.disp=disp;     disp.disable(); disp.record(0);   }   synchronized void recordSpeed(){     setSpeed=cs.getSpeed(); disp.record(setSpeed);   }   synchronized void clearSpeed(){     if (state==DISABLED) {setSpeed=0;disp.record(setSpeed);}   }   synchronized void enableControl(){     if (state==DISABLED) {       disp.enable(); speedController= new Thread(this);       speedController.start(); state=ENABLED;     }   }   synchronized void disableControl(){     if (state==ENABLED)  {disp.disable(); state=DISABLED;}   }   synchronized public void run() {  //the speed controller thread     try {       while (state==ENABLED) {         double error = (float)(setSpeed-cs.getSpeed())/6.0;         double steady = (double)setSpeed/12.0;         cs.setThrottle(steady+error); //simplified feed back control         wait(500);        }     } catch (InterruptedException e) {}     speedController=null; }
image from book

SpeedControl is the first example we have met of a class that combines both synchronized access methods and a thread. We could have implemented it as two classes, a purely passive entity which encapsulated the state and setSpeed variables and an active entity with only a run() method. However, this would have been unnecessarily complex and would lead to additional methods to set and get the variables. As implemented, the class satisfactorily encapsulates the thread and the methods that control its execution.




Concurrency(c) State Models & Java Programs
Concurrency: State Models and Java Programs
ISBN: 0470093552
EAN: 2147483647
Year: 2004
Pages: 162

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net