Saturday, April 9, 2011

Apache Tuscany - Develop a simple tool that can be used to generate composite diagrams


Abstract: 
Apache Tuscany provides a comprehensive infrastructure to simplify the task of developing and managing Service Oriented Architecture (SOA) solutions based on Service Component Architecture (SCA) standard. Tuscany Java SCA is a lightweight runtime that is designed to run standalone or provisioned to different host environments.
Task is to implement a tool which generates composite diagrams from the composite files to illustrate the SCA artifacts and their wirings. SCA artifacts are composite, component, service, reference.
This tool can serve multiple purposes:
1) Help to document Tuscany's tutorials and samples.
2) Integrate with the SCA domain manager to visualize the SCA domain (contributions, composites, nodes etc).
Implementation Plan:
Composite XML should be generated using the Tuscany's in-memory representation of the composite model. It will then give as an input for the Composite Analyser. Composite Analyser is not a single object but the whole program itself as a single unit. Composite Analyser then analyses the XML document and grab the relevant DOM Elements such as Composite, Component, Service, Reference, Property, Wire, Text etc. and starts to build the SVG document using SVG DOM API of Apache Batik.
Basically Composite Analyser contains three objects, CompositeFileReader, LayoutBuilder, and SVGDocumentBuilder.
  • CompositeFileReader is responsible for reading the input composite XML file and provide the necessary details to LayoutBuilder.
  • LayoutBuilder then builds a layout which uses the space optimally and provides the details of positions and sizes of each artifact to SVGDocumentBuilder. I already researched on few layout building algorithms and tools (JGraphX) but further research will be done and will pick the most appropriate algorithm.
  • SVGDocumentBuilder creates the DOM Elements according to the layout and builds the final SVG composite diagram.
Since DOM elements (Composite, Component, Service, Reference, Property, Wire, Text etc.) will be used multiple times in order to build a single diagram, I am planning to create separate objects for those elements. Each object is responsible for creating its own element according to the requirement and giving it to the SVGDocumentBuilder. Most of the artifacts will hold a same structure and behaviour, therefore I have implemented following class diagram for the prototype.
In the prototype I built to create a SVG diagram using Apache Batik I used following SVG elements for each artifact mentioned above.
  • Composite, Component: “rect” SVG elements with rounded corners
  • Property: “rect” SVG element with equal height and width
  • Reference: “polygon” SVG element with 6 vertices and coordination of point B of the following sketch should be given to the addElement method.
  • Service: “polygon” SVG element with 6 vertices and coordination of point A of the following sketch should be given to the addElement method.
  • Wire: “polyline” SVG element used to connect a Reference and a Service object.
  • Text: “text” SVG element used to add a given text 
Following image shows a sample composite diagram which is built using Apache Batik as a prototype for this project after converting to PNG format. 
Deliverables:
  1. Code of the tool which will be built.
  2. Tests to verify the accuracy of the diagrams generated.
  3. User documentation on operation of the tool and sample diagrams generated. 
Time-line:
Till May 10
  • Read on Tuscany SCA Java, understand the design, and concentrate on project relevant parts
  • Read on Scalable Vector Graphics (SVG) 1.1
  • Read on Apache Batik and write examples to get familiar
  • Recognize all the artifacts of SCA.
  • Research on layout building algorithms and tools and find out the appropriate algorithm 
May 11 - May 24
  • Finalize the process view after getting the comments from the developers’ community and from my mentor.
  • Start initial implementations - building artifact structures 
May 24 - July 10
  • Preparing for the mid-term evaluation of the project.  
July 12 - August 15
  • Implement Composite Analyzer
  • Improve performance by using parallel design patterns.
  • Develop test cases to verify the accuracy of the generated diagrams. 
August 16 - August 22
  • Wrap up the work done, and polishing up the code.
  • Preparing for the final evaluation. 
August 26
  • Final evaluation deadline.
Community Interactions:
Apache Tuscany developers’ community is the main community behind this project and I highly appreciate comments/ ideas of the expert developers of Tuscany and consider those as a great opportunity to learn and contribute more and more to the improvement of Tuscany.
Biography:
I am Nirmal Fernando, final year undergraduate at Department of Computer Science and Engineering at University of Moratuwa, Sri Lanka. I am very competent at Java programming language, OOP, XML, XSL and data structures and algorithms. I am familiar with SOA concepts and web services.
I participated in GSoC 2010 for Apache Derby (RDBMS in Java) project and successfully finished the project. This is a sample of the work (final output) which I've done for Derby last summer (http://nirmalfdo.blogspot.com/p/my-work-at-gsoc-2010.html).
You can find my profile and recommendations at LinkedIn (http://www.linkedin.com/profile/view?id=54105394&trk=tab_pro).
I am looking forward to have an exciting summer with Apache Tuscany, and I consider this is a great opportunity to me, to apply my knowledge and skills into a real world application which benefits many people around the globe. 
Thanks!

Thursday, January 27, 2011

Using Java Executor Framework...

Java Executor Framework provides a solution using a thread pool to the following problems of using threads directly:

  • Creating a new thread causes some performance overhead.
  • Too many threads can lead to reduced performance, as the CPU needs to switch between these threads.
  • You cannot easily control the number of threads, therefore you may run into out of memory errors due to too many threads.
Here's a code snippet on how you can use it:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestExecutor {
   
    private static final int NTHREDS = 10;

    /**
     * @param args
     */
    public static void main(String[] args) {

        long l=System.currentTimeMillis();
        ExecutorService executor = Executors.newFixedThreadPool(NTHREDS);
        for (int i = 0; i < 10000; i++) {
            Runnable worker = new Test(i);
            executor.execute(worker);
        }
        // This will make the executor accept no new threads
        // and finish all existing threads in the queue
        executor.shutdown();
        // Wait until all threads are finish
        while (!executor.isTerminated()) {

        }
        System.out.println("Total time: "+ (System.currentTimeMillis()-l));
    }
public class Test implements Runnable {

    private int id;
   
    public Test(int id) {
        this.id = id;
    }

    @Override
    public void run() {
        int sum=id;
        for(int j=0; j<10000000;j++)
            sum += j;
        System.out.println("My sum = "+sum);
    }
}

You may note that I have used a thread pool with ten threads, you can change the size of the pool according to your need.

You may wonder what actually applying concurrency does to your application's performance. Here's a comparison of CPU usage without and with using threads.



My application without using threads!

Application is running in the time gap

of 40 - 0 seconds in the CPU history

graph. You may note that CPU

utilization is 30% & 100%. i.e. not

effectively utilized.







My application using threads!

Application is running in the time gap

of 25 - 0 seconds in the CPU history

graph. You may note that CPU

utilization is 100% & 100%. Thus the

both CPUs are effectively utilized.





There is another way that you can get the advantage of Java Executor Framework, I will elaborate on that in my next post! :)

Happy Threading!! :D

Tuesday, January 25, 2011

Sorting a hashmap by values?

Hash map is a really useful data structure when it comes to coding. In some instances you may need to sort the keys of the hash map according to the values.

Here's a way (there may be many other ways), how you can retrieve the  list of keys after sorting the hash map according to values.

public ArrayList<String> sortHashMapByValues(HashMap<String, Integer> passedMap) {

        ArrayList<String> sortedRels = new ArrayList<String>();
        ArrayList<Integer> mapValues = new ArrayList<Integer>(passedMap.values());
        Collections.sort(mapValues);

        for(int i : mapValues){
            for ( Iterator it = passedMap.entrySet().iterator(); it.hasNext(); ) {
                Map.Entry e = (Map.Entry) it.next();
                if(((Integer)e.getValue()) == i){
                    sortedRels.add((String)e.getKey());
                    passedMap.remove(e.getKey());
                    break;
                }
            }
        }
        return sortedRels;
    }


Hope this is help someone!

Thursday, January 13, 2011

The Hymn Composed to Honour His Eminence Rev. Dr. Malcom Cardinal Ranjith

à¶šුà¶½ වදිà¶± රතු à¶šුමරුà¶±ේ - à¶±ිදුà¶š් à·ƒුවයෙà¶±් යෙà·„ෙà¶±් à·€ැජඹේà·€ා
à¶½ොවම දිනනා à·„ිමියනේ - සත් වරම් දෙà·€් à¶¶ෙà¶½ෙà¶±් à¶±ැà·„ැà·€ේà·€ා
à¶šිà¶­ු à·„ිමිà¶œෙ රතු à¶´ුà¶­à¶±ුà·€ේ - ආයුà¶¶ෝà·€ේà·€ා //

ලක à¶‘à¶šà¶½ු à¶šà¶½ දහම් පහනයි
මේ à¶¶ිමේ මිà¶«ි à¶šිà¶»ුà¶½ ඔබමයි
සමිදු ආ' à·ƒිà¶»ි 'ලකට à¶œෙà¶± දෙà¶±
ලක් මවගෙ à¶´ෙම්à¶¶à¶» à¶´ුà¶­ාමයි

දරු à¶šැලගෙ දුà¶š් à¶±ිවන à·ƒිà·ƒිලයි
à·ƒෙà¶­් සරණ දුà¶±් à¶œිà¶±ි à¶±ිà·€ාලයි
දම් දහර අම දහර à·€ිලසිà¶±්
දෙà·€් දනට à¶±ිà¶­ි දෙà¶±්à¶±ෙ ඔබමයි

දෙà·€් වදන à¶´à¶« නලට මුà·ƒුමයි
à·ƒුදු සභය à¶œිà¶»ි මුදුà¶±ෙ à·„ිරවේයි
à¶¶ැà¶§à·…ු à¶šැà¶½ à¶±ිà·ƒි මග à¶»ැà¶œෙà¶± යන
ලක් සසුà¶±ෙ à¶±ායක à¶‘à¶©ේරයි

-Author unknown (let me know if you know)

Sunday, January 9, 2011

Seven Steps to Get Accepted to GSoC


SevenStepsToGetAcceptedToGSoC -

Tuesday, December 21, 2010

Bad smells in code ?? :O


I think it is really important that you identify the "bad smells" in a code that you have written, simply since they are not good to have.

There are number of such "bad smells" which have been recognized and expected, programmers to be aware of. You can check most of them from here. 

I had a research on the internet for a Eclipse plug-in in order to identify so called "bad smells". Luckily I found this wonderful plug-in called "JDeodorant". 

JDeodorant is capable of recognizing four main types of "bad smells" that can be found in your code namely God Class, Long Method, Type Checking and Feature Envy. I have created a screen cast on "how to use JDeodorant?" and here it is.


Hope this post made your attention towards possible "bad smells" in your code, and hopefully correcting them (Oh I forgot to tell, using JDeodorant you can correct most of these bad smells as well ).

Happy Coding!! :)

Work done in the period of November 20th – December 3rd

Within this period I managed to serialize the whole drools rules file as a Knowledge Base Object, and observed a significant improvement (approximately 10s) but still far less than the execution time of the current RelEx2Frame. Also that serialization needed JVM stack size to be increased to 2MB. Still we felt that the performance is not up to the requirement so we decided to split the drools rules file according to 100 rules per file basis and Nisansa did that task. Danaja came up with a design which is focused on applying concurrency and parallelism for the RelEx2Frame system, and was accepted by all the members of the team as the basic design which will be altered and improved after further analysis.

In the current RelEx2Frame there is a significant limitation of the concepts or the words that are detected. Statistical learning methods can be used to reduce this limitation. One approach is to use an existing application and the other would be to implement statistical learner. Google Sets [1] is one of the existing applications that we are considering. During this period I have implemented an application which accesses Google Sets and generate new set of words for a given combination of words (<4). I have used an existing library called ‘XGoogle’ [2] written in Python programming language which provides an interface to access Google Sets. Since I was not familiar with Python, I had to learn Python and which I successfully managed to do. We will keep the results came out from this application and will compare with the results from our statistical learner to choose the most appropriate set of words.

Preparing the design document was the major work that we had done during this period, since it was due on 3rd December. All of us contributed to the design document in several ways and I contributed by writing design constraints, design decisions and designing rule learning component. Design constraints part involved basically three sub sections, namely Operating Environment, End-user Environment and Performance Requirements. Design decisions consisted with major decisions, some of which were already taken and others yet to be taken. Programming language selection, rule engine selection, caching knowledge bases, statistical learning of concepts and selecting the best suited data mining algorithm were the main design considerations discussed there.

Designing the rule learner was the most challenging task to me. I read many documents [3-5] on existing rule learners, existing rule induction algorithms, data mining techniques etc.  After considerable amount of literature survey I came up with the architecture for the statistical rule learner using data mining techniques, which will be altered and improved as it requires. Chamilka reviewed it and made few suggestions.

We were successfully managed to submit the design document on 3rd of December.
  

[1] “Google sets labs,” [Online]. Available: http://labs.google.com/sets
[2] “XGoogle,” [Online]. Available: http://www.catonmat.net/blog/python-library-for-google-sets
[3] K. Mhashilkar. “Data Mining Technology,” [Online]. Available: http://www.executionmih.com/data-mining/technology-architecture-application-frontend.php
[4] J. Grzymala-Busse, “Three strategies to rule induction from data with numerical attributes,” presented at the International Workshop on Rough Sets in Knowledge Discovery (RSKD 2003), associated with the European Joint Conferences on Theory and Practice of Software 2003, Warsaw, Poland, April 5–13, 2003.
[5] “Rule Learner,” [Online]. Available: http://openrules.com/RuleLearner.htm