RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Using a Software Factory Approach for Customized Modeling : Page 2

To overcome the challenges of a traditional approach to software solution development, consider the software factory path.

So we have three Top Node References: Bank, Branch & Service and two Link Mapping: bankBranchRelation & branchServiceRelation in gmfmap file.

Adding validations

Validations are added to avoid user from making mistakes while creating diagram and to provide some constraints. GMF supports OCL for validations. OCL is a rule based language and expressions are used to add constraints. For e.g. name.size()>0 (this implies that name should not be null).

Figure 8: Validations.

Eclipse GMF supports OCL validations for adding constraints. We will add some basic validations to prevent user from committing mistakes. Follow these steps to achieve the same:
    i. Add a child 'Audit Container' under Mapping node. Give its name, Id and description. All rules will be added under this.

    ii. For each constraint, an Audit Rule is added. To put a constraint for uniqueness of the name of a node, name->isUnique(property) constraint is added. So we will add uniqueness check for name of the Bank because two banks cannot have same names. Create an Audit Rule under Audit Container and give it a name - bankNameUniqueViolation. Message property value will be shown as error message when the rule is violated. Add a Constraint under Audit Rule and put following constraint under body banks->isUnique(name)

    Add Domain Element Target under Audit Rule and point it to Application. This means that in Application, bank name should be unique.

    iii. We will add one more type of validation which is the check for null names. It is mandatory for nodes to have name. To achieve this, create an Audit Rule 'bankNameNullValidation' and add Constraint with body value as name.size() > 0 and Domain Element Target as Bank. This means that property name in bank should have size greater than zero means it should have some value.

    iv. Follow the same steps for checking null for name of Branch and Service.

Step 5: Generator Model

After mapping file is created, create generator model by right clicking the gmfmap file and selecting option 'Create Generator Model'. We will customize this file little bit for enabling validations and for managing the diagram file.
    i. Select the Gen Editor Generator node, set the following properties:

      a. Same File for Diagram and Model : true

      b. Diagram File Extension : model

      c. Domain File Extension : model

    These settings will create a single file for diagram editor and with model extension. If you want two files, set it to false and you can also change the extension of the diagram file by setting a different value.

    ii. Select Gen Diagram node under Gen Editor Generator node and set the following properties :

      a. Validation Decorators : true

      b. Validation Enabled : true
This enables the validation. Without setting it to true, rules added under Audit Container will not be enabled.

Step 6: Alter Diagram Code

Right click the generator model and generate the diagram code. There will be some errors in the generated code as we have referred ScalableImageFigure class for custom figure which is not available to the project. We need to add plugin for that class to remove the error.
    i. Open plugin.xml of project with diagram extension (one which is generated above) and add org.eclipse.gmf.runtime.draw2d.ui.render to required plug-ins under dependencies tab.

    ii. We have to modify a method to show custom images on our diagram editor. Open BankEditPart.java and copy the following code in createNodeShape() method.
URL url = FileLocator.find(ModelDiagramEditorPlugin.getInstance()
.getBundle(), new Path("icons/bank.jpg"), null);
return new ScalableImageFigure(RenderedImageFactory.getInstance(url)
	,true, true, true);

Change the annotation of the method from @generated to @NOT generated. This does not overwrite the method if diagram code is generated again.

Do the same for BranchEditPart.java and ServiceEditPart.java.

Copy the images you have selected for Bank, Branch and Service under icons directory in diagram project.

If you want to change the position of the label with respect to node then modify addBorderItem() method in the same class. Label offset can also be managed in the same method. By default, label is positioned to the south of node and offset is 20.

Remember to change the annotation to NOT generated otherwise method will be overwritten.

Invoking validation while saving diagram file

By default, diagram file can be validated by selecting Validate under Diagram menu. This adds an extra task for the user to validate the file which sometimes can be skipped from mind of the user easily. To avoid this, we will validate the model diagram while saving the file. Follow the steps to accomplish this:
    i. There is a method doSaveDocument() in ModelDocumentProvider class which is responsible for saving the diagram model. We will call validation from this method. Add following line: ValidateAction.runValidation((View) document.getContent()); as first line of the method. This will invoke validation as soon as file is saved. If you want to add some more functionality while saving, then this is the place to do it.

    ii. Change the annotation to @NOT generated.
Method doSaveDocument() is called when diagram is saved. Adding it as first line will invoke validations while saving it.

Till now we have done all the modifications for creating model on diagram editor. Now we will write code for generating XML file from model.

Step 7: Generate XML from model

The main objective of this article is to automate software development. We will generate an XML file from the diagram editor file which will be created by user. In real world, any type of file (java,xml,text,etc.) can be generated through this.

We will add a menu item which will appear when model file is right clicked. That menu item will be linked to an action class. A wizard in which user would be asked the destination path for generating XML will come up on selecting the menu item. Follow the steps to achieve the same:
    i. Define Extension point: Menu item is added by adding extension point. Refer to figure 9.

    Figure 9: Menu item.

    Extension point is used to define a new behaviour. Check the code snippet below to see the extension point.
    <extension point="org.eclipse.ui.popupMenus">
    <menu			       id="org.eclipse.ui.articles.action.contribution.navigator.subMenu"
    			 label="Generate XML from model"
    			tooltip="Generate XML">

    This piece of code is added in plugin.xml of main project.

    Define an action class for the extension point. We have BankAction class as the action class. This class would be responsible for opening a wizard which capture the destination information.

    An XML file will be generated at the captured destination path based on the model created by user on diagram editor. For model, we need to read the selected diagram editor file.

    Each active workbench window has the selection service which helps in tracking the current selection. Refer to the code snippet below.
    ISelection selection = BankPlugin.getDefault()
    .getWorkbench()				.getActiveWorkbenchWindow()

    Create a wizard: Create a wizard which would be responsible for holding the destination path (refer to figure 10).


    Figure 10: Wizard for capturing destination path.

    Override performFinish() method to add the logic for XML generation.
    IStructuredSelection structured = (IStructuredSelection) selection;
    Object object = structured.getFirstElement();
    if (object instanceof IFile) {
    	IFile file = (IFile) object;
    	IPath resourcePath = file.getFullPath();
    TransactionalEditingDomain editingDomain = 
    	ResourceSet resourceSet = editingDomain.getResourceSet();
    	Resource modelResource = resourceSet
    Application application = (Application) modelResource
    	XMLGenerator.generate(application, bean.getPath());

    In the code snippet shown above, application variable holds the complete model object. This model object can be utilized to generate any type of file.

    Complete model object and destination path is passed to method generate of XMLGenerator class.

    iii. Generation: XML file is generated by using StringBuffer and IO. Refer XMLGenerator.java. Apart from using plain IO, freemarker templates, JET (Java Emitter Templates ), etc can be used to generate it. Not only XML, but any type of artefact can be generated using this approach.


Demand for software applications is increasing day by day which is not met with the conventional way of development. A software factory automates the software development and hence speeds up the process of development. This article depicted the step by step process of creating a customized modeling solution which enables users to design the domain specific architecture and generate the relevant artifacts.

This solution enforces the implementation of organization best practices, coding standards, quality control as well as consistent approach. It also brings productivity improvement by tacking most of the common artifacts of SDLC.

Akansha Jain works as a Technical Analyst at SETLabs, the R&D division at Infosys Technologies Ltd. She has a number of years' experience in Java and Java EE application development, Eclipse plugin architecture, software factory, Web 2.0, and cloud computing.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date