Question

See the following code for example. How can I model different return values of a function in a UML Activity-Diagram?

typedef enum {CLOSED, OPEN, UNKNOWN} sw_state_t;

sw_state_t read_input(int index)
{
    uint8_t sw_state;

    if (spi_read(&sw_state) == STATUS_OK) {
       if (sw_state & (1 << index))
           return CLOSED;
       else
           return OPEN;
    }

    return UNKNOWN;
}

What I want to show is the value the function returns. What I have is an Activity block with one input ActivityParameter and one output ActivityParameter.

Update

Does the following diagram make sense?

enter image description here

Was it helpful?

Solution

Use a data object, as they are output of activities. Then link the object with the activity

Looking at your diagram, just before each relevant ActivityFinal (return OPEN, CLOSED, UNKNOWN) add an action to return the corresponding object the following way. Then connect each object with the OutputParameter.

enter image description here

OTHER TIPS

I think your diagram is too fine grained. Why do you need the internals?

I'd do it simply like that:

Simple

  1. your example with one function is too small, wider context is missing. What will be the outcomes of the read_input function used for, the follow ups, how does it integrate into the surrounding system. As @PsiX suggests it is probably too fine grained. Without the broader view modeling single small pieces of code does not make much sense.

  2. I'm not an expert myself, still learning → some links to explanations of the activity diagrams that are useful in my opinion, especially Conrad Bock's articles

    In your case the way to model the result will depend on the way how you want to model the rest, the wider context.

  3. If you want to concentrate on control flow then you can use e.g. "send signal action" approach. The "send event"/"receive event" modeling is used by ARIS Event-Driven Process Chain (EPC) visual language where it plays important role. I'm not sure how well does it fit into the UML activity diagram concept but it seems to be "legal"

    focus on control flow

  4. If you want to concentrate on data flow then you can use the way electric circuits work (I believe it is the source of the "pin" concept anyway). The activity will send data token through one of the output pins. It will set one of the output wires high. Set one of the bits, return one enum value.

    focus on data flow

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top