You can model C++ global variables and methods in UML by
applying the «cpp_globals» stereotype to a class in a UML model. You
can also generate UML representations of global variables and methods
from C++ header files.
Before you begin
You must be in the Modeling perspective. You must have
the following items in the workspace:
- An open UML model that has the C++ transformation profile applied
- An empty C++ project
About this task
This procedure demonstrates how to model C++ global methods
and variables in UML and how to transform C++ global variables into
UML. The example in this topic assumes a reconciled modeling environment
where you begin with a UML model, run the UML-to-C++ transformation,
create a global variable in the generated C++ code, and reconcile
the changes between the model and code by running the C++-to-UML transformation.
Procedure
- Add a class to the UML model that
has the C++ transformation profile applied.
- Apply the «cpp_globals» stereotype to the class that you
created in step 1.
- For each C++ global variable or method
to create, add a corresponding UML type to the class that you created
in step 1. For
this example, add a UML operation called Operation1 and an attribute
called MAX_LENGTH of type int.
- Create a transformation configuration for the UML-to-C++
transformation:
- Click ,
and complete the pages in the wizard by using the values listed in
the following steps.
- On the Specify a Configuration Name and Transformation
page, from the Transformation list, select
the UML-to-C++ transformation, and in the Protocol section, select Reconciled,
and click Next.
- On the Source and Target page, in the Selected source
pane, select the UML model that contains the class that you added
in step 1. In
the Selected target pane, select an empty C++ project, and click Next,
and click Next again.
- On the Mapping page, select the Enable the
mapping functionality for this transformation check box.
- In the Mapping model file name field,
specify a name for the mapping model.
- Click Edit Mapping, specify a
location in the workspace and a name for the mapping model, and click OK.
- Click Edit Mapping.
- In the Edit the Mapped Names window,
select the UML model that contains the class that you added in step 1.
- In the Mapped name field, specify
a name that identifies this mapped element. For example, specify mapped_model;
then click Apply, and click OK.
- Click Next, Next,
and Next to accept the default values on the
next three pages of the wizard.
- On the Common page, select the Create trace
relationships check box, and click Finish.
The transformation configuration editor opens and shows
the new configuration file.
- Run the UML-to-C++ transformation: On the Main page of
the transformation configuration editor, in the "Forward transformation"
section, click Run.
- In the Project Explorer view, browse to the target C++
project and open the header file that has the same name as the class
that you created in 1.
Notice that for each method or variable that you created in step 3, the transformation
generates a corresponding C++ global method or variable in the header
file.
- Add a global variable or operation
to the header file, as in the following example: int NEW_MAXLENGTH;.
- Reconcile the differences between the model and code: From
the Main page of the transformation configuration editor, run the
C++-to-UML transformation. In the Merge window,
observe the proposed changes to the UML model, which include the global
variable that you added in step 7.
- Click OK to accept the proposed
changes to the UML model. After the transformation
runs, notice that the updated UML class now contains the global variable
that you added in step 7.