Pré-visualização parcial do texto
Baixe STATECHARTS original e outras Notas de estudo em PDF para Engenharia Elétrica, somente na Docsity!
Science of Computer Programming 8 (1987) 23t-274 Br North-Holiand STATECHARTS: A VISUAL FORMALISM FOR COMPLEX SYSTEMS* David HAREL Department of Applied Mathematics, The Weizmann Institute of Science, Rehonoi, Israel Communicated by A. Prueli Received December 1984 Revised Juty 1986 Abstract. We present a broad extension of the conventional formatism of state machines and state diagrams, that is relevant to the specification and design of complex discrete-event systems, such as multi-computer real-time systems, communication protocols and digital controt units. Our dingrams, which we call stasecharis, extend conventional state-transition diagrams with essentially three elemenis, dealing, respectively, with the notions of hierarchy, concurrency and communica- tion. These transform the language of state diagrams into a highly structured and economical description language. Statecharts are thus compact and expressive-—small diagrams can express complex behavior-—as well as compositional and modular. When coupled with the capabilities of computerized graphics, statecharts enable viewing the description at different leveis of detail, and make even very large specifications manageabie and comprehensible. In fact, we intend to demonstrate here that statecharts counter many of the objecttons raised against conventional state diagrams, and thus appear to render specification by diagrams an attractive and plausible approach. Statecharts can be used either as a stand-alone behavioral description or as part of a more general design methodology that deals also with the system's other aspects, such as functional decomposi- tion and data-flow specification. We also discuss some practical experience that was gained over the last three years in applying the statechari formalism to the specification of a particularly complex system. 1. Introduction The literature on software and systems engineering is almost unanimous in recognizing the existence of a major problem in the specification and design of large and complex reactive systems. A reactive system (see [14]), in contrast with a transformational system, is characterized by being, to a large extent, event-driven, continuousty having to react to external and internal stimuli. Examples include telephones, automobiles, communication networks, computer operating systems, missile and avionics systems, and the man-machine interface of many kinds of ordinary software. The problem is rooted in the difficulty of describing reactive behavior in ways that are clear and realistic, and at the same time formal and * The initial part of this research was carried out while the author was consulting for the Research and Development Division of the Israel Aircraft Industries (IAN), Lod, Israel. Later stages were supported in part by grants from IAI and AD CAD, Ltd. 0167-6423/87/$3.50 O 1987, Elsevier Science Publishers B.V. (North-Holland) 232 D. Harel rigorous, sufficiently so to be amenable to detailed computerized simulation. The behavior of a reactive system is really the set of allowed sequences of input and output events, conditions, and actions, perhaps with some additional information such as timing constraints. What makes the problem especially acute is the fact that a set of sequences (usually a very large and complex one) does not seem to lend itself naturally to “friendly” gradual, level-by-level descriptions, that would fit nicely into a human being's frame of mind. For transformational systems (e.g., many kinds of data-processing systems) one really has to specify a transformation, oi function, so that an input/output relation is usually sufficient. While transformational systems can also be highly complex, there are several excellent methods that allow one to decompose the system's transformational behavior into ever-smaller parts in ways that are both coherent and rigorous. Many of these approaches are supported by languages and imple- mented tools that perform very well in practice. We are of the opinion that for reactive systems, which present the more difficult cases, this problem has not yet been satisfactorily solved. Several important and promising approaches have been proposed, and Section 8 of this paper discusses a number of them. However, the general feeling is that many more improvements and developments are necessary. This paper represents a certain attempt to progress along these lines. Much of the literature also seems to be in agreement that states and events are a priori a rather natural medium for describing the dynamic behavior of a complex system. See, for example, [7-9, 19,23]. À basic fragment of such a description is a state transition, which takes the general form “when event a occurs in state A, if condition C is true at the time, the system transfers to state B”, Indeed, many of the informal exchanges concerning the dynamics of systems are of this nature, e.g., “when the plane is in cruise mode and switch x is thrown it enters navigate mode”, or “when displaying the time, if button y is pressed the watch starts displaying the date”, Finite state machines and their corresponding state-transition diagrams (or state diagrams for short) are the formal mechanism for collecting such fragments into a whole. State diagrams are simply directed graphs, with nodes denoting states, and arrows (labelled with the triggering events and guarding conditions) denoting transitions. Figure 1 shows a simple self-explanatory state diagram. However, it is also generally agreed that a complex system cannot be beneficially described in this naive fashion, because of the unmanageable, exponentially growing multitude of states, all of which have to be arranged in a “flat” unstratified fashion, 234 D. Harel The running example used throughout these sections concerns the author's Citizen Quartz Multi-Alarm III wristwatch?, the example being sufficientiy simple to be contained here (almost) in its entirety, but sufficiently complex to serve as an illustration of the method. Section 6 discusses a number of advanced features that are under investigation as possible additions to the basic formalism, including the integration of statecharts with temporal logic and the introduction of probabilism. Section 7 contains a brief account of the formal semantics of statecharts; a more complete treatment, however, is deferred to a separate paper. Section 8, as mentioned, discusses related work and compares the statechart formalism with some alternative notations suggested for the specification of reactive systems. Section 9 reports briefly on the experience accumulated with the language and on an implementation that is in the workings. 2. State-levels: Clustering and refinement In deciding upon a graphical representation for capturing depth and hierarchy, there is a real disadvantage in drawing trees or other line-graphs. These media make no use whatsoever of the area of the diagram: lines and points are of no width, and no advantage is taken of location. We shall use rounded rectangles (boxes in the sequel) to denote states at any level, using encapsulation to express the hierarchy relation. Arrows will be allowed to originate and terminate at any level. The graphics is actually based on a more general concept, the higraph, which combines notions from Euler circles, Venn diagrams and hypergraphs, and which seems to have a wide variety of applications. See [13]. An arrow will be labelled with an event (or an abbreviation of one) and optionally also with a parenthesized condition. (In Section 5 it will be allowed to be labelted also with Mealy-like outputs, or actions.) Thus, in Fig. 1 there are three states A, B, and C and, for example, event y occurring in state A transfers the system to state C, but only if condition P holds at the instant of occurrence. Now, since event 2 takes the system to B from either 4 or € we can cluster the latter into a new super-state D and replace the two £ arrows by one, as in Fig. 2. Fig 2. 2 This is a product of the Citizen Watch Company of America, Inc. Siatecharts: A visual formalism for complex systems 235 The semantics of D is then the exclusive-or (XOR) of A and C; ie., to be in state D one must be either in A orin C, and not in both. Thus, D is really an abstraction of A and €. The state D and its outgoing 8 arrows thus'capture a common property of A and C, namely, that 8 leads from them to B. The decision to let transitions that leave a super-state, such as the 8 in Fig. 2, stand for transitions leaving all substates turns out to be highly important, and is the main way statecharts economize in the number of arrows. Figure 2 might also be approached from a different angle: first we might have decided upon the simple situation of Fig. 3, and then state D could have been refined to consist of A and C, yielding Fig. 4. Having made this refinement, however, the incoming « and £ arrows become underspecified, as they do not say which of A or € is to be entered. Extending them to point directly to Aand C, respectively, does the job, and if the y transition within D is added, one indeed obtains Fig. 2. Thus, clustering, or abstraction, is a bottom-up concept and refinement is a top-down one; both give rise to the or-relationship between à state's substates. D (A E D a 8 dB ytP) 8 JF (a E é º Fig 3. Fig. 4. Fig. 5. Both zooming-in and zooming-out can be illustrated using this simple example. The first is achieved by looking 'inside' D (disregarding external interface for the time being) and finding simply Fig. 5, and the latter is done by eliminating the inside of D and abstracting Fig. 2 to Fig. 3. These notions will acquire more significance later on. Suppose now that as far as the 'outside' world is concerned A is the default state among A, B and €, in the sense that if asked to enter the 4, B, € group of states the system is to enter A unless otherwise specified. In the description given by Fig. 1 this can be captured by a small arrow as in Fig. 6(i). For Fig. 2 one can use the direct notation of Fig. 6(ii), or alternatively, the two-step one of Fig. 6(iii), which [ D — DP A A A to) ti) ti) Fig.6. Statecharts: A visual formalism for complex systems 237 olarms-beep atorm beeps (p2) Tits 72 Fig.8. enabled A alarm2 enabled A T1=T2”. These conditions will take on a more precise form when more of the description of the watch is available. Notice the clustering, which replaces six arrows by two. Actually, the displays do not change while the alarm is beeping, but this information should be specified under the topic of activities discussed in Section 5. A refinement of the displays state yields Fig. 9 in which it has been decided that there will be a cycle of displays linked by repeated depressings of a; that the time and date displays are linked by d's but that the time display will resume after 2 minutes in date. Also, the time display is the default, meaning, among other things, that the entrances from alarms-beep in Fig. 8 will actually be entrances to time, This will later be changed, but for the time being it is good enough. (displays N Fig. 9. 238 D. Harel One of the most interesting and frequent ways of entering a group of states is by the system's history in that group. The simplest kind of this “enter-by-history” is entering the state most recently visited. In our watch example, we can refine each of the three states alarm1, alarm2 and chime of Fig. 9 with off and on substates, intended to model the display of the disabled or enabled status of the corresponding feature. The new alarm1 state with its a-entrance can be described as in Fig. 10(a), meaning “enter the most recently visited of the two, and enter off if you are here for the first time”. Equivalentiy, one can use the continuation-arrow notation of Fig. 10(b). In either case, d is used to switch between the two substates. It should be noted that an H generally means that history is applied only on the level in which it appears, so that in Fig. 1i(a) history chooses only between Gand F; that is, the system enters B ifit was in A or B when it most recently left K, and Cifitwasin C, Dor E on that last visit. The choice of B and € here is by the default arrows. History can be made to apply ali the way down to the lowest level of states by attaching an asterisk to the H-entry. Thus, in Fig. 11(b) the system will enter the most recently visited state from among A-E, overriding both defaults. One can achieve effects in between these one-level/all-levels extremes by addi- tional H-entrances. In Fig. 12 the system will enter B if its last visit to K was actually to G, or that one of C, D or E last visited, otherwise. In this way only the default arrow to C is overridden by the history. For specifying more complex notions of history one could use temporal logic, as discussed in Section 6.3. The displays state is now enriched to include updating capabilities for both alarms as well as for the general time/date combination. Entering update modes is achieved via c, with a 2-second continuous depressing required in the time/ date update mode. In all cases, depressing b brings control back to the previous display. The situation of displays so far (including the addition of an H*-entrance, modifying the earlier decision to return to time after an alarm beep) is given in Fig. 13. The next step is the refinement of these updating modes. The statecharts for these are given in Figs. 14 and 15, In both cases, the additional exit via c could not have been given on the previous level in the naive way of Fig. 16(a), since c does not apply to the whole of update. However, in such a case one uses the notation of Fig. 16(b) to illustrate the fact that c applies to certain parts of update, to be specified later. Indeed, zooming out of, say, Fig. 14, would result in the likes of Fig. 16(b), not 16(a). We should say something about the H-arrows in Figs. 14 and 15. Depressing d in any of the updating substates causes exit (but not exit from the encapsulating superstate) and immediae entrance to the most. recently visited substate, i.e., that just left! In the case of Fig. 14 this d-arrow replaces nine d-arrows, one for each substate. For the time being, these d-arrows are merely no-op's, but, as discussed later, they actually are responsible for the updating itself, and would require one additional level of states to specify how each pressing of d boosts the appropriate internal setting by one unit. 240 D. Harel Pi displays Fig. 14, stopwatch a (crime alarm 2 o felorms bf le fupdote 2 fupdate + Fig. 13. fait update lmonfic Ihr sec e Fc c te o Jc fi min min DM (e e ime day | c fyor] c E (a) e] Statecharts: A visual formalism for complex systems 21 alarm1 b q ç update 1 min. min d Fig. 15. update b update a um c q Ro (a) (b) Fig. 16. We allow for an economica! representation of arrows with common sources, targets or events, as in Fig. 17. Note that the variant of Fig. 17(c), in which the arrows are reversed, is a contradiction to the desired determinism of the system. Clearly, more subtle contradictions can occur as a result of the “deep” character of statecharts, and should be carefully avoided. For example, Fig. 18 shows an a contradiction from A, resulting from the fact that the possible transitions leading out of a state are those emanating from its periphery, as well as those emanating from any of its ancestors' peripheries. Figure 18 also contains a default contradiction upon entering B via £. Had the default arrow entering D been entirely contained within the area of C, there would have been no problem; it would only have influenced the entrance to C viay. This fact is a consequences of our area-dominated graphical representation. (As it is, y is underspecified, since € contains no default.) Lj LJ Us LE dA Ed ta tb) tc) Fig. 17. Statecharis: A visual formalism for complex systems 243 event a then occurs, it transfers Bto C and F to G simultaneous!y, resulting in the new combined state (C, G). This illustrates a certain kind of synchronization: a single event causing two simultaneous happenings. If, on the other hand, 4 occurs at(B, F) it affects only the D component, resulting in (B, E). This, in turn, illustrates a certain kind of independence, since the transition is the same whether the system isin Borin C in its A component. Both behaviors are part of the orthogonality of A and D, which is the term we use to describe the AND decomposition. Figure 20 is the conventional AND-free equivalent of Fig. 19. The reader will no doubt realize that Fig. 20 contains six states because the components of Fig. 19 contained two and three. Clearly, two components with one thousand states each would result in one miltion states in the product. This, of course, is root of the exponential blow-up in the number of states, which occurs when classical finite-state áutomata or state diagrams are used, and orthogonality is our way of avoiding it. Note that the B-transition from C to B has the condition “in G” attached to it, with the obvious consequences, shown explicitly in Fig. 20. Thus, while Y has indeed been split into two orthogonal components, there will in general be some dependence. The “in G” condition causes A to depend somewhat on D, and indeed to know" something about the inner states of D. Formally, orthogonal product is a peneralization of the usual product of automata, the difference being that the latter is usually required to be a disjoint product, whereas here some dependence between components can be introduced, by common events or “in G"-like condi- tions. Fig. 20. One slightly bothersome notational problem is the lack of an appropriate location for the name “ Y”, The product state Y will, in general, lie within some superstate Z, to which the area outside the borderline of the (A, D) box “belongs”. Of course, it is possible to use an additional box as in Fig. 21(a). We prefer to try managing without the name Y or simply to attach it to the outside as in Fig. 21(b). 244 D. Hare! Fr Fa D “fa ta) tby Fig.21. An obvious application of orthogonality is in splitting a state in accordance with its physical subsystems. This typically occurs on a very high level of the specification. 1n an avionics system, for example, one might have a general-mode component, and orthogonal components for subsystems, such as the radar. An overly simplified first attempt might look like Fig. 22. AVIONICS SYSTEM general - made ] rador [ abe-system ; I 1 | | I | ) eruias ] 1 . switch ] | | ! I ! I I novigate) | | | o touchdown t ] l | take oft I o | Lo. 1 ! 1 I I l q l 1 l .. A ... — subsystems Fig.22. Before showing where orthogonality occurs in the watch example, let us complicate matters slightly by discussing exits and entrances to orthogonal states. Observe Fig. 23, which is a possible interface description of the state Y of Fig. 19 (internal transitions have been omitted for simplicity). The split ô exit from J illustrates a simple explicit indication that upon occurrence of 8 the combination (B, E) is entered. An a-event in K causes the system to enter (C, F); € by the arrow and F by defauit, and a v-event from J causes entrance to the default (B, F). A B-event at L causes entrance to the combination of C with the most recently visited state in D, and an w-event in combination (B, G) causes transfer to K. An alternative to 246 D. Harel Figure 25 contains a refinement of the stopwatch display state of Figs. 9 and 13 using orthogonality, and should be self-explanatory. In it, regular and lap are two kinds of displays and zero is the special state in which the stopwatch is off but in its initial position. This description could have been the outcome of a separate person or group dedicated to specifying the behavior of the stopwatch. Ps fstopwatch 1 * pes fime Zero o! d tinoff) b displa: run reg. 11 fon ] d I tinonk /É ! dp 1 off ' U ) Fig.25. Orthogonality appears in the Citizen watch on the high levels too. One might start a top-down behavioral specification of the watch, accounting for battery insertion and removal, as in Fig. 26, and then decide (see Fig. 27) that the alive state is to consist of six orthogonal components: à main component containing displays and alarm-beep modes, one component for the enabled/ disabled status of each of the alarms and the chime (the latter containing the chime-beeping state too), one for the power status, and one for the light. The resulting leveis of the statechart are given in Fig. 28, where the main component of the alive state has been described in detail earlier. Citizen quartz muiti-alarm HI bott. inserted alive dead bott. remove: A weck bott. dies Fig. 26. Statecharts: À visual formalism for complex systems 247 alive main Tam lerime- qugm | Istatus status | ] ] I pasa ] 1 I | | | — Ee) Lo Latarmz - | power ] status | I om ] 1 ] |] I ] 1 |) ] 1 l L a Fig.27 Although the two-state fight component looks rather innocent, it is actually quite subtle because of its scope. Orthogonality of the component on this level prescribes that depressing b, say, in the update state of Figs. 13 and 14 will simultaneously turn on the light and cause the system to exit the updating state. We shall see even more interesting combinations later. Note that the disabled / enabled status of the alarms and the chime are directly linked to the corresponding 0f/ on substates of displays; this is one way of modeling a display change and its “hidden” consequences. Of course, there are other ways, and constructing statecharts, like writing programs, should encourage many possibilities, depending, among other things, also on personal style. At this point, the situation permits the conditions P1 and P2 for the beeping alarms to be made more precise. For example “alarml enabled” is to be replaced by “in alarml-status.enabled”. Note that our previous H-entrances (e.g. in Figs. 13 and 25) can no longer be interpreted without reservation, as “enter most recently visited”. Now that we have catered for battery removal and death within the specification, these attain a more sophisticated meaning whereby history is to be “forgotten' if dead has been entered in the meantime, To deal with this more complex historical criterion we use the special actions clear-history(state) and clear-history(state*), which cause the forget- ting of recently visited states on the first level, or all levels, respectively, of state. (The combination clear-history is abbreviated as clh.) Once forgotten, H-entrances do not apply, and defaults are employed. We have chosen to attach the action to the transitions entering dead, to the right of a “/”, as discussed in Section 5 below. Here are two final features of the Citizen watch that seem to nicely illustrate the painless way certain kinds of changes can be made to statecharts. We can think of these features as being handed down to the team of specifiers/ designers from above at a late stage in the process of specification. The first is a beep-testing feature, and the second involves a 2-minute automatic return to time from alt displays other than the stopwatch, on condition that no button has been depressed in the interim. As to the beeper test, depressing both b and d causes a healthy becper to beep. Clearly, this test, modeled in detached form in Fig. 29 (assuming, for simplicity, Sratecharts: A visual formalism for complex systems 249 beep-test Fig. 29. that b and d cannot be depressed simultancously), is applicable in some states of the watch and not in others. For example, it is obvious that it should not be relevant in the dead state and probably not in the other beeping states either. The claim now is that a behavioral designer of the watch can attach this piece of the description as an orthogonal component to whichever portion desired. One reasonable choice might be the time display mode, as in Fig. 30. The significance of this kind of Fig. 30. decision should be clear: the test will work only when the system is in time, in this case. As it happens, the beep-test of the author's actual watch is applicable precisely in both the time and date states and, curiously enough, also throughout the general update sequence. It is not applicable (though it took the author quite a while, and required some strenuous finger-twisting, to discover the fact) in the 2-second wait period. A new box was therefore drawn around the relevant portions, their common property being the applicability of the beep-test, and the test itself was attached properly to it. See Fig. 31. We might add here that Citizen's documentation of the watch lists both the light feature and the beep-test in the same way: if you press so-and-so this-and-that will happen; no indication of the scope is provided, despite the major difference between the two. As to the second feature, here life is even easier: the addition involves merely drawing another box around the relevant displays, with the appropriate event and condition on the outgoing arrow. Figure 31 (see fold-out) contains the full statechart of the watch so far. 250 D. Harel K is instructive to work through certain linear (or branching) sequences of events, called scenarios, and observe their effects as prescribed by the statechart. For example, assume the system is in the mon updating state (see Fig. 31), and that, for some reason, the user has an urge to try out his beeper. Suppose he depresses d and b in that order without releasing either of them. The regular component says we shall end up in time (with the month updated one step, although this fact does not show up in Fig. 31), the beep-test component says we shall end up in the beep state, and, finally, the light component (orthogonal on a higher level to the other two) says we shall end up with the light on. This is in fact quite the case; we shall end up in time, one month ahead, with the beeper beeping and the light on! As à small example of a subtle anomaly of the Citizen watch, it so happens that the continuous beeping during a beep test stops upon depressing a and resumes upon letting go. Figure 32 shows a refinement of the beep state capturing this fact, but it is not included in Fig. 31. Fig.32. It should be noted that neither the time elapsing activity itseM nor the internal values of the time, date, and alarm settings are included in Fig. 31. These parts can be modelled appropriateiy, the first as an additional component orthogonal to the alive state and the latter in the form of one extra level of states within the three update states (cf. Figs. 38 and 39 and the accompanying text in Section 6.1). Alternatively, one can regard these as involving variables that change values, and postpone their specification to the activity part of the system; ses Section 5. Also, no mention is made of the contents of displays, though some of the state names are suggestive; this also is taken up in Section 5. 4. Additional statechart features Here are a number of features that are part of the basic statechart formalism, but did not show up in the watch example.