Monday 4 May 2020

IBM Rhapsody Tip #77 - How to animate user-defined types in sequence diagrams (serialization)

With thousands of properties to explore IBM Rational Rhapsody is a powerful and configurable tool for both systems and software engineering. This short silent tips and tricks video comes at subscriber request!

In the video I use IBM Engineering Systems Design Rhapsody 9.0 but the same works for (most) earlier versions.

Here's the transcript:

This silent video covers how to get Rhapsody to show the value of user-defined types in an animated sequence diagram.Let's first have a look at the model. It's a C++ one I created that sends a couple of argument-carrying events to control a traffic light sequence. These events have arguments typed by an enum type and a struct type.

By default, when we animate these in a sequence diagram then raw, rather than type-aware values are shown. Fortunately, this is can be fixed. Let's look at the easy way first. On the properties for the Type we can set the property CPP_CG::Type::GenerateSerializationFunctions.

With this property set, Rhapsody will automatically generate the magic code needed to show meaningful text values when animating. Serialize is just a fancy way of saying 'convert this type to text'. Unserialize is the opposite, i.e. 'convert text (a char *) to a type value'. An unserialize function is used when you type text into the Generate Event dialog.

With the GenerateSerializationFunctions property set to SerializationAndUnserialization, Rhapsody automatically generates global serialize and unserialize functions into the .h/.cpp files for the application. Let's have a peek! Here is the auto-generated serialize function.

This is an auto-generated unserialize function.

Note the function's signatures. We need to match these when creating our own. Any user-defined functions would need to take the same argument and return types.

Here are some user-defined serialize/unserialize functions. They are global functions, i.e. contained by a package.

It's important that animation is turned off.

In this user-defined serialization function, I'm converting the enum type to a char * of Y(es), N(o), F(lash), or E(rror). My user-defined unserialization function does the opposite, i.e. converts a char * to the enum type.

We can tell Rhapsody to use these with the properties CPP_CG::Type::AnimSerializeOperation and CPP_CG::Type::AnimUnserializeOperation. We also need to turn off the auto-generation of serialization functions.

When we rebuild and run we can now see the user-defined serialization in play on the sequence diagram.

The user-defined unserialization is also in play!

In summary, you can easily get Rhapsody to auto-generate serialize and unserialize functions for types by setting the GenerateSerializationFunctions property, or (less easily) you can also create your own serialize/unserialize functions for animation, and tell Rhapsody to use them via properties. Just make sure the arguments and return values match and be careful about releasing memory ;-)

1 comment:

Note: only a member of this blog may post a comment.