27.07.2013 Views

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

34 <strong>Why</strong> <strong>We</strong> <strong>Need</strong> <strong>Model</strong>-<strong>Based</strong> Analysis<br />

by more than a particular tolerance, then the sensor is considered to be faulty and<br />

no more samples are requested. Instead, the control program commands the sensor<br />

to reset. If the sensor responds by sending back an acceptable temperature that is<br />

close to the last good sample, the sensor is considered to be working correctly again<br />

and the control program resumes polling.<br />

The control program must not compute a calibration factor with a temperature<br />

sample that is considered erroneous, because that would result in controlling the<br />

process with an incorrect calibration factor, which could have serious consequences.<br />

<strong>We</strong> have a safety requirement which stipulates that the controller must not do<br />

anything bad: the program must not compute a calibration factor with an erroneous<br />

temperature sample. <strong>We</strong> also have a liveness requirement which stipulates that the<br />

controller must do something good: the controller must respond to a command<br />

by calculating the calibration factor (except where this would violate the safety<br />

requirement).<br />

3.2 Implementation<br />

Our control program uses a typical strategy for implementing reactive programs<br />

(Figures 3.2–3.6). The observable actions are implemented by events, and the controllable<br />

actions are implemented by methods called handlers. The program waits<br />

for an observable action. When one occurs, the program executes a dispatcher, code<br />

that selects a handler, and then executes the handler selected by the dispatcher. When<br />

the handler returns, the program waits again for the next observable action.<br />

<strong>We</strong> divide our control program into two classes. The Controller class is a library<br />

that contains the dispatcher and the handlers (Figures 3.2–3.4). It is largely<br />

independent of any operating system or framework. 1 <strong>We</strong> also provide a simulator<br />

application, so we can experiment with the control program design on any computer<br />

that provides the .NET framework. The simulator substitutes for the much more<br />

elaborate support (including device drivers, etc.) needed by the actual control program.<br />

The Simulator class is an application that generates the events that implement<br />

our observable actions (Figures 3.5 and 3.6). It also provides a rudimentary user<br />

interface so we can raise events by clicking buttons on a form.<br />

Our simulator uses C# events and the .NET framework, in particular the event<br />

machinery provided by the System.Windows.Forms namespace. For example, in our<br />

Simulator class, c.timer is the .NET timer and c.timer.Tick is its Tick event<br />

(Figure 3.5). The statement<br />

c.timer.Tick += new EventHandler(timer_Tick);<br />

1 The Controller class does use the .NET Timer class from System.Windows.Forms.<br />

more free ebooks download links at:<br />

http://www.ebook-x.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!