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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Advanced Topics 253<br />

15.3 Object IDs and composition<br />

Whether a model program is written in an object-oriented style is not visible during<br />

composition. All kinds of model programs can be composed. In this section we<br />

explain how this works.<br />

When a class T inherits the LabeledInstance base class, it gets a built-in<br />

term label for instances of T. By default, the term label uses the name of class T<br />

as the function symbol of the term and an integer value as the term’s argument.<br />

For example, in the model program Payroll2, instances of the Employee class have<br />

labels like Employee(1), Employee(2), etc. 1<br />

When objects of type T appear as arguments to actions, the term label of the object<br />

is used. Using terms in action arguments provides an insulating layer of syntax. This<br />

allows us to compose arbitrary model programs, regardless of whether they are<br />

written in an object-oriented style, or with compound value types or as finite state<br />

machines. 2<br />

For example, suppose we have the following scenario described as a finite state<br />

machine:<br />

FSM(0, AcceptingStates(2), Transitions(<br />

t(0, CreateEmployee(Employee(1)), 1),<br />

t(1, CreateEmployee(Employee(2)), 2),<br />

t(2, SetSalary(_, 0), 2),<br />

t(2, SetSalary(_, 200), 2)),<br />

Vocabulary("CreateEmployee", "SetSalary", "DeleteEmployee"))<br />

This scenario FSM allows two employees to be created. It then permits any number<br />

of SetSalary actions, using either 0 or 200 as the salary argument. It explicitly<br />

disallows any DeleteEmployee actions. Figure 15.1 shows this graphically.<br />

This FSM may be composed with model program Payroll2. Figure 15.2 shows<br />

the result of the composition.<br />

Notice that the FSM did not define objects or .NET types. It was only necesary<br />

that the FSM and the Payroll2 model program agree on the term vocabulary<br />

(Employee(1), Employee(2), etc.). When the model program Payroll2 sees a term<br />

like Employee(1) it knows to interpret this term as an instance of the Employee class<br />

within its context.<br />

1 The N<strong>Model</strong> framework allows you to choose a symbol other than the type name by means of the<br />

[Sort] attribute. For example, we could have annotated class Employee with [Sort("Emp")]<br />

if we wanted labels in the form Emp(1), Emp(2), etc.<br />

2 It is even possible to program your own type of <strong>Model</strong>Program using a derived class. As long<br />

as you choose the same vocabulary for the actions and data elements, composition with any of<br />

the other <strong>Model</strong>Program types is possible.<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!