How to Add Stereotypes from Reference Project to Model Element Using API

Stereotype is one of the extensibility mechanism used in Unified Modeling Language (UML). It allows developers to extend the meaning of a model element. By using stereotype, developer can define domain specific properties to model element and make it suitable for a particular problem domain. Visual Paradigm’s product support defining stereotypes for model, not just via the application interface, but also using Open API. In the old API, you can only specify stereotype to model element from your current working project. Starting from VP Suite 5.2 (build 20110518r), we introduced new interface to let you specify stereotypes from your dependent projects. This article will teach you how to specify a stereotype which is from a dependent to a model element.

Locate the stereotype model element

In order to insert stereotype to your target model element, you first have to retrieve it from the dependent project. You can call the method IProject. getLinkedProjects(); to list out all its dependent projects. Once you listed the dependent project, you can then walk through them one by one to find out their stereotype with the method IProject.toAllLevelModelElementArray(IModelElementFactory.MODEL_TYPE_STEREOTYPE);

Insert stereotype to model element

Now you can walk through your stereotypes to find out the one you need, and insert it to your target model element. You can do this by calling

ModelElement.addStereotype(IStereotype sterotype);

And if you want to remove a stereotype from model element, you can call

ModelElement.removeStereotype(IStereotype sterotype);

Note that this method works only if the stereotype you specified is valid to the model type you working on, or the target model element support adding stereotype. Otherwise it will through an IllegalArgumentException.

Sample plugins

This sample plugin demonstrate how to retrieve stereotypes from dependent project and add it a class model. Once you have deployed the plugin into VP Application, you can then download the sample project file at the bottom of the page, and specify it as a dependent project.

Add dependent project

Add dependent project

After that you can create a class diagram and a class. Right click on the class and click on the Add Stereotype from Project(s).

Adding stereotype from dependent projects

Adding stereotype from dependent projects

This will bring up a dialog and showing stereotypes from dependent projects. By selecting the stereotype and press OK button, it will then add the selected stereotype to class model.

Select stereotype from dependent project

Select stereotype from dependent project

Download Sample Plugin

Download Sample Base Project

API Requirement

This method supported since VP Suite 5.2 build 20110518r. The API documentation can be found at here.

** Only those model element support assigning stereotype will have these operation interface.

Related Articles

Related Links

48 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply