Wednesday, October 2, 2013

Java to UML transformation extensios - Part I

Hi folk, 

In this post, I will show you how to make a Java to UML transformation extension using IBM Rational Software Architect (RSA).

But first, in terms of modeling, what is a transformation?

A transformation is:
  • A mechanism that takes a set of source elements and changes them into a new set of target elements.
  • A set of transforms that you can configure, specify as public in the plug-in manifest file, and run using a transformation configuration.

So, with the transformation extensions you can extend and enhance custom transformations or the transformations that are available in the IBM Rational modeling products.
The transformation framework extensibility is based on the Eclipse plug-in architecture. Transformation extensions are plug-ins that contain the following items:
  • An extension to this extension point: com.ibm.xtools.transform.core.transformationExtensions
  • One or more TransformationExtension elements, which defines the rules, extractors, or transforms that enhance the functionality of the extended transformation
  • One or more ExtendTransform elements, which define the transform to extend  

For example, I'll use two rules and an extractor as show the following image:


- RuleA: select interfaces contains in Java Project
- ExtractorB: extract information from the interface methods
- RuleB: select methods that take input parameters

Concepts:
  • Transformation context: contains the values for all of the parameters of the transformation
  • Source: the initial object, or set of objects, passed into the transformation operation
  • Target container: A reference to the locations where any generated artifacts are located

First thing to check is the packages installations. Run the IBM Installation Manager and be sure that Transformation authoring is selected.

 


Ok, let's get to work.

Procedure to create a Plug-in Project transformation extension:

  1. Open RSA and create a new project. File -> New -> Project...
  2. Select Other -> Plug-in Development -> Plug-in Project and click Next
  3. Write in Project name: org.jif.transform.java2uml and click Next
  4. Write Versión 1.0.0 and in Rich Client Application select No
  5. Click Next
  6. Select Custom plug-in wizard from templates and click Next
  7. Select Transformation Extensions and click Next
  8. In Target transformation ID, select com.ibm.xtools.transform.java.uml.Java-to-UML. Click Next
  9. Insert the 3 transformation elements for the example as shows following image
    • RuleA
    • ExtractorB
    • RuleC
  10. Click Finish
  11. In the Plug-in Manifest Editor (Plug-in Development perspective), click on tab Extensions
  12. Select true in the enabled property in Extension Element Details

Now, we can launch an Eclipse application to set up the test environment for the example.

Procedure to transform Java to UML:
  1. In the Plug-in Manifest Editor (Plug-in Development perspective), click on icon Launch an Eclipse application 
  2. Create a new Java Project. File -> New -> Project...
  3. Select Java -> Java Project and click Next
  4. Write in Project name: Java Program and click Finish
  5. Create 3 classes and 2 Interfaces: 
    • org.jif.test.fix1
      • ClassA
      • InterfaceA
    • org.jif.test.fix2
      • ClassB
      • InterfaceB
    • org.jif.test.fix3
      • ClassC
  6. Define classes and interfaces as:
    • InterfaceA has method getName() and return value as String
    • InterfaceB has method calculate(int val) and return value as int
    • ClassA implements InterfaceA and add a private string attribute name and the override method from interface
    • ClassB implements InterfaceB and add the override method from interface
    • ClassC has method print(String text) and not return value
  7. Open the Modeling perspective
  8. Create a new project. File -> New -> Project...
  9. Select General -> Project and click Next
  10. Write in Project name: UML Project and click Finish
  11. Create a model UML, File -> New -> Model
  12. Select Standard template and click Next
  13. Select General -> Blank Package and in File name: org
    •  The model org, you can delete later. What we need is a model container.
  14. Browse Destination folder and select UML Project
  15. Click Finish
  16. Create a new Transform, File -> New -> Transformation Configuration 
    • Write in Name: java2uml 
    • Select Configuration file destination: Java Program
    • Select the Java to UML transformation
    • Select Protocol: Conceptual
  17. Click Next
  18. We have to set the source and target:
    • Specify the elements that the transformation transforms, inside of Java Project, select src folder
    • Specify the model destination project for the transformation output, select UML Project
  19. Click Finish 
  20. Open the transformation overview with the Transformation Configuration Editor
  21. Click on Extension tab to check if the Java to UML transformation extension is checked
  22. Now, in the Main tab, we can run the extension with the following options checked:
    • Generate UML operations for each Java getter/setter method
    • Generate UML a flat UML package for each Java package
    • Generate a debug log
  23. To see the transformation, create a class diagram and add all UML elements transformed 



Now we have created the skeleton of the transformation, I will now extend the rules and extraction.

Easy right? Now the fun begins

See Java to UML transformation extensios - Part II 

(coming soon, you have to be patient)


 


Friday, August 30, 2013

Composite pattern using Rational Software Architect

In software engineering, the design patterns are reusable solutions to a commonly occurring problem within a given context. Currently I am involved in a UML modeling project that needs a design pattern for objects that have the same composition, same code and tree structured data.

Unfortunately, every time I visit customers I find, in most of them, that the software code quality is poor. I will not analyze in this post why this happens because there are many influencing factors such as cost, inexperienced programmers, misdirection, etc..

What I know, is that many programmers do not use development methodologies including the use of pattern matching.

Anyway, I will explain how to use the composite pattern in a few steps, modeling in UML and programming in Java using the based Eclipse tool Rational Software Architect for WebSphere (RSA).

As brief introduction, the composite pattern describes that a group of objects are to be treated in the same way as a single instance of an object. The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies.


More info on Wikipedia: Composite pattern.

For now I leave the theory aside and I will explain the procedures. I divided the procedures into three steps: modeling, transformation and code execution.

Procedure to create the UML model:
  1. Open RSA and open the Modeling perspective
  2. Create a new project. File -> New -> Project...
  3. Select General -> Project and click Next
  4. Write in Project name: Composite Pattern and click Finish


  5. Create a model UML, File -> New -> Model
  6. Select Standard template and click Next
  7. Select General -> Blank Package and in File name: org
  8. Browse Destination folder and select Composite Pattern, click Finish


  9. On package org, add the following UML packages in cascaded: jif, develop and composite
  10. Add the following UML classes in composite package: Composite class, Component class and Leaf class and IComponent interface
  11. Add the following UML interface in composite package: IComponent interface
  12. Open the Main freeform diagram and drag the UML elements and Save the changes


  13. Add in IComponent interface the following operations: getName with String value return and without inputs parameters, getParent with IComponent value return and without input parameters, add without return and IComponent as input parameter, remove without return and IComponent as input parameter and getChilds with java.util.Set value return and without input parameters
  14. Repeat same operations described in the previous step for Composite class
  15. In the Component class add the following attribute name as String type and protected
  16. Add in Leaf class the following operations: getName with String value return and without inputs parameters, getParent with Component value return and without input parameters
  17. Create an Interface Realization from Component to IComponent
  18. Create a Generalizarion from Composite to Component
  19. Create a Generalization from Leaf to Component
  20. Create a Composition Association from Composite to Component


  21. Save changes

Procedure to transform UML to Java:

  1. Create a Java project, New -> Other, select in the wizard window the Java Project option and then click Next
  2. Write the Project name: Composite and click Finish
  3. In menu, Modeling -> Transform -> New Configuration
  4. Write in name uml2java and select in Configuration file destination the java project Composite
  5. In Java Transformations select UML to Java


  6. Click Next
  7. Select the Source, Composite Pattern -> Models -> org
  8. Select the Target, Composite -> src
  9. Click Finish
  10. Run the transformation to generate de Java source code

Procedure to run the example:
  1. Resolve the warnings that transformation has left:
    Set is a raw type. References to generic type Set<E> should be parameterized to: 
    Set<IComponent>
    - In Composite class, initialize the attribute component: 
    private Set<IComponent> component = new HashSet<IComponent>();
    - Remove the @generated tags
  2. Implement the Composite class:
    - Create constructor

    public Composite(String name) {
    super.name = name;
    }
    - Add the return value in the getName method
    public String getName() {
    return super.name;
    }
    - Add array components
    public void add(IComponent component) {
    this.component.add(component);
    }
    - Add the return value in the getChilds method
            public Set<IComponent> getChilds() {
    return component;
    }

  3. Implement the Leaf class:
    - Create constructor

    public Leaf(String name) {
    super.name = name;
    }
    - Add the return value in the getName method
    public String getName() {
    return super.name;
    }
  4. Create a class (Test.java) to run an example composite pattern:
    - In the main method, 

    IComponent componentA = new Composite("ComponentA");
    IComponent componentB = new Composite("ComponentB");
    IComponent componentC = new Composite("ComponentC");
    IComponent componentD = new Composite("ComponentD");
    IComponent leafA = new Leaf("leafA");
    IComponent leafB = new Leaf("leafB");
    IComponent leafC = new Leaf("leafC");
    IComponent leafD = new Leaf("leafD");
    componentD.add(leafD);
    componentC.add(leafC);
    componentB.add(leafB);
    componentB.add(componentC);
    componentA.add(leafA);
    componentA.add(componentB);
    componentA.add(componentD);

It takes a structure as shown in the following diagram:


Note: At the end of the post you can find the source files for the sample.

When running the test class generates the following structure:


ComponentA
 ComponentD
  leafD
 leafA
 ComponentB
  ComponentC
   leafC
  leafB

Composite Pattern example applied to automotive engineering:

Here I leave an example of how it can be applied in the real world.


Source Files:

- IComponent.java

package jif.develop.composite;

import java.util.Set;

/** 
 * @author Jorge Iglesias
 */
public interface IComponent {
public String getName();
public IComponent getParent();
public void add(IComponent component);
public void remove(IComponent component);
public Set<IComponent> getChilds();
}

- Component.java
package jif.develop.composite;

import java.util.Set;

/** 
 * @author Jorge Iglesias
 */
public class Component implements IComponent {

protected String name;

public String getName() {
return null;
}

public IComponent getParent() {
return null;
}

public void add(IComponent component) {
}

public void remove(IComponent component) {
}

public Set<IComponent> getChilds() {
return null;
}
}

- Composite.java
package jif.develop.composite;

import java.util.HashSet;
import java.util.Set;

/** 
 * @author Jorge Iglesias
 */
public class Composite extends Component {

private Set<IComponent> component = new HashSet<IComponent>();

public Composite(String name) {
super.name = name;
}

public String getName() {
return super.name;
}

public IComponent getParent() {
//TODO: to be implemented
return null;
}

public void add(IComponent component) {
this.component.add(component);
}

public void remove(IComponent component) {
//TODO: to be implemented
}

public Set<IComponent> getChilds() {
return component;
}
}

- Leaf.java
package jif.develop.composite;

/** 
 * @author Jorge Iglesias
 */
public class Leaf extends Component {

public Leaf(String name) {
super.name = name;
}

public String getName() {
return super.name;
}

public IComponent getParent() {
return null;
}
}
- Test.java
package jif.develop.composite;

/** 
 * @author Jorge Iglesias
 */
public class Test {

public static void main(String[] args) {
IComponent componentA = new Composite("ComponentA");
IComponent componentB = new Composite("ComponentB");
IComponent componentC = new Composite("ComponentC");
IComponent componentD = new Composite("ComponentD");
IComponent leafA = new Leaf("leafA");
IComponent leafB = new Leaf("leafB");
IComponent leafC = new Leaf("leafC");
IComponent leafD = new Leaf("leafD");
componentD.add(leafD);
componentC.add(leafC);
componentB.add(leafB);
componentB.add(componentC);
componentA.add(leafA);
componentA.add(componentB);
componentA.add(componentD);
printTree(componentA,0);
}
static void printTree(IComponent c, int depth) {
String d = "";
for (int i = 0; i < depth; i++) {
d+=" "; 
}
System.out.println(d + c.getName());
if (c instanceof Composite) {
if (c.getChilds().size() > 0) {
depth++;

for (IComponent component : c.getChilds()) {
printTree(component, depth);
}
}
}
}
}





Friday, June 7, 2013

Check Percentage of CPU usage - Linux

Hi folk,

A couple of days ago, I did not know that one of my CPU was running at 100% capacity because a process was "hung".





So, I had to make a Kill -9, but to my surprise, the next day I had the same problem.

Once I solved the problem I kept thinking, how many times did this happen with other process on my laptop and I have not noticed?

To stay informed I have created the following bash script to check the top 10 processes.

#!/bin/sh
# Check Percentage of CPU usage
# Jorge Iglesias

# Skip first lines from top command with head -17
# PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
# Select top ten proccess with -n 10
top -n1 -b | head -17 | tail -n 10 > top.txt

while read line
do
    ind=0
    pid=""

    for line in $line;
    do
        if [ $ind -eq 0 ]    # index of PID
        then
            pid=$line
          
        fi

        if [ $ind -eq 8 ]    # index of %CPU
        then
            min=80.0     # min value alert
            if [ 1 -eq `echo "${line} > ${min}" | bc` ]
            then
                notify-send -t 5000 -i error "Percentage of CPU usage" "The %CPU usage for process <b>id $pid</b> is <b>$line%</b> \n\nPlease review and kill the process."      
            fi
            break;         # break line, only read to %CPU value
        fi
        ((ind+=1))
    done
done < top.txt

rm top.txt                # delete temp file


I scheduled the script to run every 10 minutes and when it detects that a process up to 80% then is reported on screen.

 
You can watch the process using the top command.


Now, you decide if you really want to "kill" the process or not :-)

I hope this helps!!

Cheers.

Monday, May 6, 2013

Notify-send Java Class for Linux

Hi folk,

I will share this Java class that I use to display tools information on my Linux operating system.

Sample:




Notify-send is a little, simple On-Screen Display application. It uses the notify bubble pop-up Linux system.

 /**
 * Utils
 *
 * Created on Nov 25, 2010
 *
 * @version 1.0
 * @author Jorge Iglesias, (jorge.iglesias@es.ibm.com)
 *
 * @ Copyright IBM Software Services Rational Spain. 2011. All rights reserved.
 */

package com.ibm.issr.util.linux;

import java.io.IOException;

public class Notify {
  
    public static void sendInfo(String text) {
        send("info",text);
    }

    public static void sendError(String text) {
        send("error",text);
    }

    private static void send(String type, String text) {
        String[] command = new String[] { "notify-send", "-t", "20000", "-i", type, text };
        try {
            Runtime.getRuntime().exec(command);
        } catch (IOException e) {
            System.err.println(e.getMessage());
        }
    }
}