Home > Articles > Software Development & Management > UML

  • Print
  • + Share This
This chapter is from the book

7.3 Interactions

A state diagram provides a static view of the entire state space of an object at some level of abstraction, from the complete system down to simple primitive objects. Because the complete behavior of a state-driven object can be represented by a sufficiently detailed state model, state models are said to be constructive. This means that they can be used to fully generate executable code for the object.6

What state diagrams do not show are typical paths through the state space as the system is used, nor are they effective at showing how collaborations of objects interact. Such interactions are captured as scenarios. Scenarios may not visit all states in all the participant objects nor activate all transitions, but they provide an order-dependent view of how the set of objects is expected to behave under some set of conditions with a specific ordering of external and internal stimuli. Because a scenario does not have enough information to fully define the complete behavioral model of an object, scenarios are said to be partially constructive. They can add operations and event handling to a state model, but they do not fully define the model.

There are two methods for showing scenarios that are particularly useful in real-time systems. The primary notational form for scenarios is the sequence diagram, which shows order but not strict timing. The second is the timing diagram, which is best used when strict timing must be shown. There is a third, the diagram formerly known as a collaboration diagram but in UML 2.0 called a communication diagram. A communication diagram is basically an object diagram showing the message sequences as being attached to the links between the objects and indicating ordering with numbers on the messages. This last form is used rather infrequently and so will not be discussed here.

7.3.1 Sequence Diagrams

Sequence diagrams are (by far) the more common way to show scenarios, as discussed earlier in this book. Sequence diagrams use vertical lines to represent the objects participating in the scenario and horizontal directed lines to represent the messages sent from one object to another. Time flows from top to bottom; that is, messages shown lower on the page take place later than ones above it. The basic syntax and semantics of sequence diagrams have been discussed in Chapter 3. The focus in this section is to discuss how sequence diagrams may be usefully employed in the constructions of analysis object models.

Sequence diagrams are representations of specific interactions among the elements of a collaboration. They only tell a part of the story (what happened in this particular case) and not what always must happen (a specification). As such, sequence diagrams are a way to detail a use case. The sequence diagram helps to capture the operational perspective of a set of elements, such as a system, and the actors with which it interacts, or a set of elements inside a system. As we construct our object analysis model, we can use sequence diagrams to capture how the elements in the object analysis model interact to achieve the higher-level interaction described in the use case scenario.

The other use of sequence diagrams has to do with validation, the use of sequence diagrams as either a debugging tool or as a test vector. CardioNada Scenarios Example

It would be useful to look at the interaction of the instances in the pacemaker model. In a real development project we would create many sequence diagrams. Some of these sequence diagrams would be for the communications with sending and receiving messages, with and without errors. Other sequence diagrams would show the interactions necessary to achieve the pacing behavior, most likely several in each of the pacing modes. Still other sequence diagrams would depict the interaction of the communications and the pacing system to show how the programmer sets and queries the pacing modes and parameters. In this chapter, we are going to focus on pacing in AAI.

In this case, I am going to use the executability characteristics of the Rhapsody tool to both drive and monitor the execution of the model. First, to help drive the model, I will create a statechart for the Heart Chamber actor. We call this "instrumenting the actor" because we are creating executable behavior for the actor to help us explore how the objects inside the system interact to make sure that we got it right. This is almost exactly the same process as creating the «test buddy» class we saw previously in the calculator example. It differs in a couple of important ways. First, the actor is outside the scope of the pacing subsystem so we can't rely on the pacing subsystem to connect the actor with the parts of the subsystem automatically—we'll have to do that ourselves. Secondly, instrumenting the actor is less obtrusive in the model of the system, since the actor must use the specified interfaces and services. The statechart for the actor is shown in Figure 7-22.

07fig22.jpgFigure 7-22. HeartChamber Actor Statechart

We can see that the HeartChamber actor primarily acts like a heart. It beats at the rate held in the attribute timeToNextBeat. It can have randomness if we set the local attribute isRandom to TRUE. When it beats, it invokes the beat() method on the VoltageSensor to which it connects (which in turn sends an evSense event to the AtrialModel in this case). It accepts evPaceStart and evPaceStop events sent to it from the OutputCapacitor to which it connects. Code Listing 7-6 shows the code used to create the pacing subsystem and heart chamber instances. All but the sections between //#[and //#] are generated automatically by Rhapsody. The two lines of code between those marks were added to allow the heart chamber to send events to the voltage sensor, and to allow it to receive events from the output capacitor. The Object Execution Framework (OXF ) code starts up the framework so it can begin processing events and executing the state machines.

Listing 7-6. CardioNada main()

int main(int  argc, char*  argv[]) {
    if(OXF::init(argc, argv, 6423))
            HeartChamber * p_HeartChamber;
            Pacing_Subsystem * p_Pacing_Subsystem;
            p_HeartChamber = new HeartChamber;
            p_Pacing_Subsystem = new Pacing_Subsystem;
            //#[ configuration PacingOnly::DefaultConfig
            // set the heart chamber to send events
// to first voltage sensor
            // set first output capacitor to send
// its pace events to the heart chamber

            delete p_HeartChamber;
            delete p_Pacing_Subsystem;
            return 0;
            return 1;

A word about debugging and testing: When I build systems using the UML, I want to debug and test at the same level of abstraction at which I design—that is, I want to test and debug in UML, not in the underlying source language or the even-more underlying assembly language. While I may, at times, find it necessary to drill down to those levels of detail, the vast majority of my testing and debugging ought be at the model level, not the code level. For this reason, Rhapsody provides a rich set of debugging and testing tools that we'll use here. I bring this up now because once we begin to look at the sequence diagrams it is important to know how they were created mechanically (via the insertion of events, for example) as well as to understand that these sequence diagrams were generated automatically by Rhapsody as the system was executed.

When testing or debugging state-based objects, one of the basic things you need to be able to do is to insert events. Rhapsody provides a number of means to do this. Figure 7-23 shows one way—a dialog box that can be opened that allows the user to select the object and event to be inserted into the running model. It should be noted that Rhapsody also provides a means to call operations as well (not shown here). Figure 7-24 shows a different way. Rhapsody allows you to "webify" a model; that is, mark which elements you wish to monitor and/or control via a standard Web browser and then automatically generate such a Web page and to insert a Web server in your application. This is an extremely useful feature because it allows you to monitor and control a system via the Internet even while it is running on the actual target hardware (provided, of course, that it supports TCP/IP and can connect to the Internet). In the figure, we see two panes in the browser—the left identifies the elements that I've specified that I want to monitor or control, and the right presents the elements that I can use. We see that I can view (and set) the value for timeToNextBeat, set the heartMode, and isRandom Boolean attributes, and even send events such as evPace and evPaceStart (which takes a single int parameter). I can also construct custom views, picking among the elements I want to view together.

07fig23.gifFigure 7-23. Inserting Events

07fig24.gifFigure 7-24. Debugging with a Web Browser

Lastly, I also want to be able to see various features of the objects in the running system. Figure 7-25 shows the debugging view for the HeartMonitor. We see all the current attributes and even the links connecting the HeartMonitor to the VoltageSensor. The instance is identified by its name rather than by its memory address, making it much more useful. With these basic tools, I can control and monitor the execution of my system, whether it runs on my desktop, on the actual target hardware connected to the desktop, or on a machine thousands of miles away. Pretty cool, huh?

07fig25.gifFigure 7-25. Debugging View

The ability to debug systems is a crucial one, so even if you're not going to generate the system from the UML model,7 you must still be able to answer the question "Is this right???" about the model you've constructed. These debugging tools allow you to run your system and validate that it is doing the right thing.

Now, back to our main subject—the sequences of interaction of the pacing subsystem parts. The first sequence, shown in Figure 7-26, captures the creation of the two primary objects, the HeartMonitor and the Pacing_Subsystem, and how, in turn, the Pacing_Subsystem creates its internal parts. The ones shown are the instances of the AtrialModel, VoltageSensor, and OutputCapacitor. Note that there are other instances created as well, such as the VentricularModel instance and another instance each of the VoltageSensor and OutputCapacitor. Since we're not using them in this scenario, I didn't draw those instance lines and the creation messages to those instances are therefore not shown.

07fig26.jpgFigure 7-26. CardioNada Sequence 1—Creation and Initialization

The calls we entered into the main() shown in Code Listing 7-6 appear in the sequence diagram right after the construction of the system objects; we see getSensorInput() and setPaceChamberOutput() sent from the main() (as represented by the collaboration boundary) and the Pacing_Subsystem instance.

Since the statechart of the HeartChamber begins immediately we see the timeout events on the HeartChamber lifeline and the invocation of the VoltageSensor::beat() operation. Then from the debugger, we enter the enablePacing and toInhibited events.

Figure 7-27 shows an example of pacing. Notes were manually added to the automatically generated sequence to aid understanding. This figure is, in fact, a continuation of the scenario shown in Figure 7-26. In Figure 7-27 we can look at the interaction among the instances. At the top of the figure we see the timeout that causes the transition of the AtrialModel from its Refractory to its Waiting state. The transition action, a call to VoltageSensor::enable() lets the VoltageSensor turn on the hardware to listen (in the real system).

07fig27.jpgFigure 7-27. CardioNada Sequence 2—Pacing

Next we see something interesting. The HeartChamber times out and sends a beat to the VoltageSensor, which in turn sends an evSense event to the AtrialModel. Even as that happens, though, the Atrial Model state machine times out before it receives the evSense event. Therefore it proceeds to pace the heart even though the heart just completed a pace.8 Such race conditions occur frequently in real-time systems and it is good to see that the evSense event is appropriate discarded when that occurred.

The pacing of the heart is accomplished by first disabling the VoltageSensor and then invoking the OutputCapacitor::enable() method (which passes the amplitude of voltage pulse to be delivered). The pace is completed with the OutputCapacitor::disable() call. Then the Atrial model waits for a period of time (its refractory period) to allow the electrical charge in the heart to dissipate before reenabling the VoltageSensor.

The next sequence diagram, Figure 7-28, is a continuation of the previous one. In this case, the HeartChamber "beats" and invokes the VoltageSensor::beat() method. This method then sends an evSense event to the AtrialModel. It is received, causing the Atrial model to exit and then reenter its Waiting state. This causes the timeout that will invoke the pacing behavior to retrigger and start over. This is exactly what was desired in the SelfInihibited mode of operation.

07fig28.gifFigure 7-28. CardioNada Sequence 3—Inhibiting Calculator Scenario Example

In the previous section, we saw how the scenarios allowed us to examine and ensure that the pacemaker was pacing properly. In this section, we'll use scenarios to make sure that the Calculator is doing the same.

At the black-box level of testing, we can just pump mathematical expressions in one end of the Calculator and look at standard output to see if the value is correct. However, unit testing is all about the white-box view: making sure that the internal pieces of the system are doing what they are supposed to be doing. Scenario-based testing can allow us to insert events, change values, and invoke behaviors to actually demonstrate that objects are doing so and the interactions are what we expect.

In this case we can take a few simple expressions and run them through the various parts of the Calculator. In this way, we can examine them and see if we are convinced that the parts are all doing the right thing.

In this effort, we'll take advantage of the multiple configuration features of the Rhapsody tool, just as we've taken advantage of the model-level execution, debugging, and code-generation features previously. We'll create another configuration of the system, called Debug, that generates instrumentation in the code, allowing the generated application to talk back to Rhapsody. This feedback lets Rhapsody animate, via dynamic color-coding, the various states of the objects and dynamically construct the sequence diagrams during the execution of the system. We used these features in the previous section to create the sequence diagrams we looked at, but now we'll create a debug configuration of the Calculator as a separate item. This allows us to, at the click of a button, generate either a production version of the application, which we could ship off to a customer, or a debug version. The underlying model is the same either way, which greatly facilitates the testing of our model, since changes to the model we make as a result of testing and debugging automatically appear in the production version.

Figure 7-29 shows the Setting pane of the features of the Debug configuration of our calculator. The difference between this and the default (production) configuration is that instrumentation mode is set to Animation. This tells Rhapsody to insert macros in the generated code that send information back to the Rhapsody tool when events are processed, method calls are invoked, and attributes are changed. This allows Rhapsody to construct the dynamic visualizations of the application execution. Note that both configurations have Web Enabling checked. The Advanced button allows us to set which things are webified in the generated system, and so control whether this configuration has monitoring and control of internal portions of the system or just to enter expressions and see results.

07fig29.jpgFigure 7-29. Debug Configuration

It is common to have multiple such configurations. Some will have debugging capabilities enabled while others won't; some will be targeted at desktop systems such as Windows or Solaris, while others will be targeted at embedded RTOSes, such as VxWorks or OSE.

For the purpose of this debugging effort, let's examine what happens when we process the expression 2*(3+4). This simple expression allows us to ensure that we process both the addition and multiplication operators as well as parentheses properly. To create the figures for the scenario, I used the webify interface to enter in the expression and start things going. The Debug configuration sets the webify interface and the model animation features active.

The first figure (Figure 7-30) starts after the objects are created. The CharParser instance parses the characters in the expression string, identifies the kind of character it is (digit, operator, or white space), and sends them off to the Tokenizer via events such as evDigit or evOp, along with the actual character. The Tokenizer has the more interesting job—to construct the tokens from the incoming events. We see that it begins constructing the number token when it receives the first evDigit event through its invocation of the beginToken() method. The next event received is an evOp, passing the * character. Now the Tokenizer knows that the number is complete (after all, the number could have been 2345.78, in which case it would have had to construct the number token from seven characters instead of just one). It can now pass the complete number token off to the Evaluator via an evNumber event. The Evaluator simply pushes the number onto the stack. It calls reduceUnary() to reduce a unary –, if one was present.

07fig30.gifFigure 7-30. Calculator Scenario 2*(3+4) page 1

The Tokenizer also begins the operator processing. When it receives the next character (the open paragraph operator), it sends the * operator off to the Evaluator, which pushes it onto the operator stack. Note that since all operators are single characters, we could have sent off the * operator immediately. However, a design decision was made to allow for multiple character operators in the future, such as ++ and +='. This would allow subsequent versions of the Calculator to have variables and more elaborate behavior.

Figure 7-31 picks up where the previous figure leaves off. The Evaluator pushes the * operator. The CharParser meanwhile sends the digit 3 to the Tokenizer. The latter now sends the ( operator off to the Evaluator, which pushes it onto the operator stack. The processing continues with the + operator inside the parenthesis.

07fig31.gifFigure 7-31. Calculator Scenario 2*(3+4) page 2

Figure 7-32 continues the scenario with the digit 4. Now the Tokenizer can send the operator + to the Evaluator. The Evaluator now wants to see if it can apply the operator, so it calls reduce(). It cannot, as it happens, so it pushes the operator onto the operator stack. At this point, the number stack has the value 3 at the top, followed by 2 (the number 4 hasn't been pushed onto the stack yet). The operator stack has + at the top, followed by ( and then *.

07fig32.gifFigure 7-32. Calculator Scenario 2*(3+4) page 3

The next character is the ) operator. The processing continues in Figure 7-33. We see that the last number, 4, is pushed on the stack. The last token is a special one, called end of string (EOS). That forces the evaluation of the last operator, ). This calls the operation reduceSubExpr(), which does a reduction until a left parenthesis operator is found. We see that the Evaluator checks to ensure that the next operator is not a left parenthesis (after all (5) is a perfectly legal subexpression). Since it is not, the Evaluator pops the operator (a +) and two operands off the number stack, does the math (actually performed in the body of the reduceFactor() method), and pushes the result (7) onto the number stack. Then the ( is popped from the operator stack and discarded.

07fig33.gifFigure 7-33. Calculator Scenario 2*(3+4) page 4

The final reduction is done by processing the EOS event in the Evaluator. This is shown in the last of these figures, Figure 7-34. The reduceFactor() method is called (since we're processing a multiplicative operator) and we pop the operator from the stack (*), the two operands (7 and 2), do the math, and push the result on the stack (14). Since we're at the end of the string, we call displayResult(), which pops the value off the number stack and sends it to standard output.

07fig34.gifFigure 7-34. Calculator Scenario 2*(3+4) page 5

In a book, this sequence seems long and involved, as it is spread across five figures. In a tool, however, the entire scenario is a single diagram, and may be scrolled and zoomed as necessary.

  • + Share This
  • 🔖 Save To Your Account