Troubleshooting
Problem
How to create a nested pattern
Resolving The Problem
QUESTION:
How to create a nested pattern?
ANSWER:
A simple example below will demonstrate steps to create and apply a nested pattern using Rational XDE for VS.NET. The process is the same for Rational XDE for Java Platform.
Here is a summary of the steps described in this example:
Two patterns will be created in a model, and the second pattern will be bound (nested) with the first one. Both patterns will require input parameters of type class.
The first pattern that requires two input parameters will show inheritance relationships between two classes. The nested pattern requires one input parameter. The binding (nesting) will be done with the following goal in mind: the nested pattern will automatically be applied to any class that plays the role of the derived class in the first pattern.
1. Create a blank solution and add a C# Windows Application Project to it.
2. Highlight the solution in the Solution Explorer and click on Synchronize icon. An XDE model is created in Model Explorer after synchronization is complete.
3. Using the model's context menu in Model Explorer, select Add UML > Pattern Asset. Add Pattern Asset dialog comes up. For the Pattern Name enter OuterPattern. In the Asset Properties section of the same dialog rename Asset Name to OuterPatternPackage. Other settings may remain unchanged. Click OK.
4. Using the model's context menu in Model Explorer, select Add UML > Pattern Asset. Add Pattern Asset dialog comes up. For the Pattern Name enter InnerPattern. In the Asset Properties section of the same dialog rename Asset Name to InnerPatternPackage. Other settings may remain unchanged. Click OK.
5. Now there are two pattern assets in the model. Expand OuterPatternPackage. Right click on the OuterPattern collaboration. Select Add UML > Template Parameter. Rename default name TemplateParameter1 to OClassTemplateParameter1.
6. The template parameter type has to be specified. It defines the type of input the pattern will expect. Right click on OClassTemplateParameter1 and select Add UML > Type > Class. Rename it to OClass1.
7. Repeat steps 5 and 6 to create another template parameter, OClassTemplateParameter2, and its template parameter type, OClass2.
8. In this step inheritance relationship is established between OClass1 and OClass2. Right click on OuterPattern collaboration and select Add Diagram > Class. Drag and drop these two classes onto the new class diagram. Select Generalization arrow from UML Class section of the Toolbox and indicate on the diagram that OClass2 inherits from OClass1.
9. Expand InnerPatternPackage. Right click on the InnerPattern collaboration. Select Add UML > Template Parameter. Rename default name TemplateParameter1 to IClassTemplateParameter1.
10. Right click on IClassTemplateParameter1 and select Add UML > Type > Class. Rename it to IClass. For the purposes of this example, add an attribute to this class. Its name and type is not important in the context of this example. The attribute will help to recognize the effect of the pattern application in the end of this process.
11. Save the model.
12. Right click on OuterPatternPackage and click Select in Pattern Explorer. Switch back to Model Explorer. Right click on InnerPatternPackage and click Select in Pattern Explorer. Now both assets are in Pattern Explorer.
13. In this step InnerPattern will be bound to OuterPattern. In Pattern Explorer, right click on InnerPattern and select Bind. In Binding Location dialog, expand the model package and select <<Asset>> OuterPatternPackage and close the dialog. InnerPattern_Binding will appear nested inside OuterPattern in Pattern Explorer.
14. Now template parameters of these two patterns have to be mapped. Expand InnerPattern_Binding and locate the template parameter for this pattern - IClassTemplateParameter1. In the next step IClassTemplateParameter1 of the InnerPattern will be mapped to OClassTemplateParameter2 of OuterPattern. The mapping is done so that InnerPattern is automatically applied to any class that will play the role of the derived class in OuterPattern.
15. Right click on IClassTemplateParameter1 and select Edit Argument Values > User Values. In User-Provided Values dialog expand the model, and then expand <<Asset>> OuterPatternPackage. Expand OuterPattern collaboration. Select OClassTemplateParameter2. Click OK to close the dialog. Parameter mapping is completed with this step.
16. Switch back to Model Explorer. Notice InnerPattern collaboration in <<Asset>> OuterPatternPackage. It is time to test the patterns. Right click on the model and select Add UML > Class. Create two classes Test1 and Test2.
17. Right click on <<Asset>> OuterPatternPackage and select Apply This Pattern. Click Next on Apply Pattern wizard's first screen. Next screen prompts to choose a class for OClassTemplateParameter1. Expand the model package in the left window and select Test1. Click Next.
18. Next screen prompts to choose a class for OclassTemplateParameter2. Expand the model package in the left window and select Test2. Click Next.
19. Next screen prompts to choose expansion location. Select the current model package and click Finish. Click OK on "Pattern expansion succeeded" message.
20. Drag and drop both Test1 and Test2 onto the model's Main diagram. Notice that Test2 now inherits from Test1. Also notice that Test2 has the same attribute, which was specified in IClass of InnerPattern (see step 10). It demonstrates that though InnerPattern was not explicitly applied to the second participant of OuterPattern collaboration (class Test2), it was still applied to this class automatically.
Historical Number
136852419
Was this topic helpful?
Document Information
Modified date:
16 June 2018
UID
swg21130348