Thursday, February 27, 2014

WSO2 ESB 4.8.1 - Remote debugging with IntelliJ IDEA

Apache Synapse is a light-weight ESB written in Java. It is commercially supported by WSO2 ESB which is based on Apache Synapse. In this post I'll describe how to configure IntelliJ IDEA to debug the Apache Synapse code base within WSO2 ESB.

First you need to checkout the code base from svn location by running the following command on terminal:

svn checkout http://svn.wso2.org/repos/wso2/carbon/platform/branches/turing/dependencies/synapse/2.1.2-wso2v4 

After completing the checkout operation it is required to build this code base and configure it to the IDE (IntelliJ IDEA). Before building the project make sure following things.
  1. You got a live internet connection throughout the process to download the dependencies.
  2. You got Apache Maven and JDK installed in the system.
  3. Set the environment variable as follows to avoid Maven OutOfMemory error.      MAVEN_OPTS="-Xms768m -Xmx3072m -XX:MaxPermSize=1200m
To build the repository, first navigate in to the 2.1.2-wso2v4 directory through command line and run the following :

mvn clean install

This will also run all the unit tests. Then run:

mvn idea:idea 

to set up the repository as an IntelliJ IDEA project.

Now you can open the IDE (IntelliJ IDEA) and select 'File -> open' and navigate to the directory location. Now the directory will display as an IDEA project. Go ahead and open it.


Now select 'Run -> Edit Configurations...' in IDEA. In the pop up window select 'Add New Configuration' (the plus sign at the top left corner) and select 'Remote' from the drop down menu. Now give a meaningful name and save with the default configurations.


Now download the latest WSO2 ESB release (WSO2 ESB 4.8.1) from WSO2 website:


Extract it to a preferred location. Navigate to {Extracted Location}/wso2esb-4.8.1/bin through command line and run the following command:

sh wso2esb-samples.sh -sn 150 --debug 5005

The ESB will start to listen to port 5005. Now go to IntelliJ IDEA and add a debug point to an appropriate location (I prefer to add the debug point to line 50 in the class ProxyServiceMessageReciever.java as it is the method relevant to catching the SOAP packet that will be sent by the service). Then select " Run -> Debug 'SynapseESBServeDebug' " (The name will differ according to the name you have given to the remote debug configuration).

And now follow the WSO2 ESB documentation to run " ESB Sample 150: Introduction to Proxy Services " to run a sample proxy service. 


After your invoking the Axis2Client you will be able to see that the process has been paused at the break point that you have added earlier. Now your can use IntelliJIdea in the usual way to debug the code.


Thursday, April 25, 2013

Enhance Extdoc Plugin : Introduction


Short description: The current Extdoc view provides extended documentation on classes and methods which has been data mind from code. But there is no way for users to influence the documentation yet. The major goal of this project is to enhance Extdoc view such that user can involve for the documentation (that is users can create, annotate and rate contributions to the documentation of classes and methods.


Abstract
The current Extdoc view provides extended documentation on classes and methods which has been data mind from code. But there is no way for users to influence the documentation yet. The major goal of this project is to enhance Extdoc view such that user can involve for the documentation (that is users can create, annotate and rate contributions to the documentation of classes and methods.
Detailed Information
Writing documentation is one of those tasks that programmers are typically not really interested in. It is time-consuming, wearisome dull task which has almost no immediate rewards. So as a consequence, most of the times documentation is incomplete or out-dated.
But at the same time good and comprehensive documentation is critical for the success of a library or framework. There are tools which can generate documentation (Javadoc comments) by extracting the obvious information from the source code. But the thing is for an instant it may be seems like helpful and valuable but finally this kind of documentation will only pleases the project manager since it improves the source code to documentation ratio. But in the developers point of view these kind of documentation is less helpful and rather useless.
However there are many other resources out there such as tutorial sites, Code-search engines and code snippet repositories which offers valuable information about an API (how to use a certain API or deal with errors). The current Extended Documentation platform is exclusively developed to serve the purpose of aggregate this wide range of information sources available on the web into a single view in eclipse.
The figure below shows the current Extdoc view.


The existing view of Extension documentation is great. But as you can see, there are no user feedback components. Without that functionalities the Extdoc view is rather incomplete. So the proposed idea of the project is to enhance the Extdoc platform which enables the functionality for the users to involve in the documentation. The figure below is a mock-up screenshot of the user interface of Extdoc view after adding the functionalities.

There by clicking on the log in text user will be navigated to a log in window which will be like this.




There user can authenticate himself by providing his/her Google, Yahoo likewise ID. It will be openId based authentication. By successfully log in to the system user will be able navigated to the Extdoc view which the user interface will be like this.






There user have the privileges to edit/create, annotate and rate the existing documentation.

Deliverables
Final product deliverable will be the Extended documentation platform with a very solid restful API for commenting and editing API elements and a very tight editing integration into the IDE.
  • Users will be authenticated based on openId authentication.
  • OData protocol will be used for backend communication.
  • The database back end will be SQL-DB.





Saturday, February 16, 2013

Memoization

Introduction

I dreamt of blogging my knowledge for about two years. It is too long time, but finally today it comes alive. So as a start I thought of blogging about my favorite field, "Algorithms".
Wikipedia defines an algorithm as follows,

"In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning."

How ever there are various types of algorithms used for various purposes in the fields of mathematics and computer science. For example, 'Graph algorithms' are practically used in searching in a map for a path connecting to destinations, and 'searching and sorting' algorithms are used in data analysing. And there are many more types of different and important algorithms which programmers used. But here I'm going to explain about 'Memoization algorithms'. To understand what is memoization first we have to take a look at what is 'dynamic programming'.

Introduction to Dynamic Programming(DP)

This method is some what similar to divide-and-conquer method(hopefully you will have an idea what is 'divide and conquer approach'), as this method also solves the main problem by solving the subproblems. In divide and conquer method, first it divide the main problem into disjoint subproblems and solves those subproblems recursively, and then it combine those solutions to solve the original problem. 
Here in dynamic programming(DP) approach, it works efficient when subproblems overlap(ie : when subproblems share subproblems). While divide and conquer algorithm repeatedly solve the common subproblems, a DP algorithm solves each subproblem only once and save it's answer on a table and retrieve that answer when the same subproblem appears again, rather than re-computing it. DP thus uses additional memory to save computation time

So as a summery, unlike divide and conquer, in dynamic programming,
  • Subproblems overlap
  • Subproblems may share subproblems
  • Can store results and re-use them
  • Divide and conquer performs redundant calculations

Typically when developing a DP algorithm, we follow four main steps
  1.  Characterize the structure of an optimal solution.
  2.  Recursively define the value of an optimal solution.
  3.  Compute the value of an optimal solution, usually the bottom-up fashion.
  4.  Construct an optimal solution from computed information.

There are usually two similar methods to implement a dynamic programming approach.

The first approach is 'top-down with memoization'. In this approach, we write the procedure recursively in a natural manner, but modified to save the result of each subproblem (usually in an array or hash table). The procedure now first checks to see whether it has previously solved this subproblem. If so, it returns the saved value, saving further computation at this level; if not, the procedure computes the value in the usual manner. We say that the recursive procedure has been memoized; it “remembers” what results it has computed previously.

The second approach is the bottom-up method. This approach typically depends on some natural notion of the “size” of a subproblem, such that solving any particular subproblem depends only on solving “smaller” subproblems. We sort the subproblems by size and solve them in size order, smallest first. When solving a particular subproblem, we have already solved all of the smaller subproblems its solution depends upon, and we have saved their solutions. We solve each subproblem only once, and when we first see it, we have already solved all of its prerequisite subproblems.

These two approaches yield algorithms with the same asymptotic running time, except in unusual circumstances where the top-down approach does not actually recurse to examine all possible subproblems. The bottom-up approach often has much better constant factors, since it has less overhead for procedure calls.

However in this article, we'll only discuss further about 'memoization algorithms'.

Memoization

Wikipedia defines memoization as follows,

"In computing, memoization is an optimization technique used primarily to speed up computer programs by having function calls avoid repeating the calculation of results for previously processed inputs. Memoization has also been used in other contexts (and for purposes other than speed gains), such as in simple mutually recursive descent parsing in a general top-down parsing algorithm that accommodates ambiguity and left recursion in polynomial time and space. Although related to caching, memoization refers to a specific case of this optimization, distinguishing it from forms of caching such as buffering or page replacement. In the context of some logic programming languages, memoization is also known as tabling"

However I think it would be easy to understand this concept if we try this using an example. So I choose problem  67 of Project Euler.net . The problem is to find a maximum path sum of a given pyramid of numbers.

eg :
By starting at the top of the tringle below and moving adjacent numbers on the row below, the maximum total from top to bottom is 23. 
3
7 4
4 6
8 5 9 3

That is, 3 + 7 + 4 + 9 = 23.

Anyway the problem is to find the maximum path of this triangle.

Yes, it is possible to find the solution for this tringle using brute force method(by trying out every route). But as there are 2^(99) sifferent paths in total, so lets assume you could check one trillion (1012) routes every second. Then it would take over twenty billion years to check them all.

So now you can understand that it is not literally possible method, so here is the solution for this problem using 'memoization algorithm' coded in java language;



public class Euler67{
public void solution() throws FileNotFoundException{
File f=new File("input.txt");
Scanner sc=new Scanner(f);
int[] tree = new int[10000];
int tmp=sc.nextInt();
int k=0;
while(tmp!=-1){
tree[k]=tmp;
k++;
tmp=sc.nextInt();
}
int[] val=new int[tree.length];
val[0]=0;
int[] trn=new int[10000];
trn[0]=0;
for(int i=1;i<10000;i++){
trn[i]=trn[i-1]+i;
}
val[0]=0;
int j=1;
int one,two;
val[1]=tree[0];
for(int i=2;i<val.length;i++){

one=trn[j-1]-trn[j]+i;
two=one-1;

if(one-1==trn[j-1]){
val[i]=val[one]+tree[i-1];
}else if(two==trn[j]){
val[i]=val[two]+tree[i-1];

}else{
val[i]=Math.max((val[one]+tree[i-1]), (val[two]+tree[i-1]));

}


if(i>=trn[j+1]){
j++;

}

}
Arrays.sort(val);
System.out.println(val[9999]);

}
}

Here the array 'tree' holds the input pyramid of numbers; and the array 'trn' holds the answer for the subproblem(because of optimal substructure). So the answer for this will be stored automatically at the end of this trn array. And this method will compute it in 160705210 nano seconds(0.16 seconds) instead of twenty billion years ;) .

Conclusion

So I think now you have some understanding of dynamic programming and memoization, how they works and how efficient they are. If you have any doubt regarding this article(topic) don't hesitate to comment below, I'll answer them asap!! :-D :-D