21.08.2013 Views

A State-Based Programming Model for Wireless Sensor Networks

A State-Based Programming Model for Wireless Sensor Networks

A State-Based Programming Model for Wireless Sensor Networks

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.

104 Chapter 6. Implementation<br />

Program 6.2: A hierarchical state machine and its representation in the OSM textual<br />

language.<br />

PROG_2<br />

A<br />

int a<br />

outB()<br />

B<br />

int b<br />

prB()<br />

f<br />

C<br />

int c<br />

inB(a,b)<br />

f<br />

outC(a,c)<br />

e<br />

e<br />

D<br />

1 state PROG_2 {<br />

2 initial state A {<br />

3 var int a;<br />

4<br />

5 initial state B {<br />

6 var int b;<br />

7 onEntry: f / inB(a, b);<br />

8 f / outB() -> C;<br />

9 onPreemption: prB();<br />

10 } // end state B<br />

11 state C {<br />

12 extern var int a;<br />

13 var int c;<br />

14 f / outC(a, c) -> B;<br />

15 } // end state C<br />

16<br />

17 e / -> D;<br />

18 } // end state A<br />

19<br />

20 state D { e / -> A; }<br />

21 }<br />

state. If desired, the external variable interface may be declared explicitly, but if<br />

it is declared, external variables must be marked with the extern keyword. In<br />

the example, the integer variable a, defined in state A (line 3), is visible in A’s<br />

substates B and C. There<strong>for</strong>e a can be used in their actions, such as inB() and<br />

outC() in lines 7 and 14 of the example Prog. 6.2, respectively. In state C the<br />

external variable a is declared explicitly.<br />

Preemption Actions<br />

Substates may declare preemption actions that are executed at runtime when<br />

the substate is left because its superstate is left through a transition. Preemption<br />

actions are declared in the context of states—they are not explicitly associated<br />

with particular transitions. Definitions of preemption actions have the following<br />

<strong>for</strong>m (where apreemption() is the preemption action):<br />

onPreemption: / apreemption();<br />

In the example Prog. 6.2, state B declares a preemption action prB() in line 9. It<br />

could be used to release resources previously allocated in the entry action inB()<br />

when B is left implicitly by preemption, that is, when leaving B’s superstate A<br />

through a transition to D.

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

Saved successfully!

Ooh no, something went wrong!