Monday, May 16, 2011

Adding ECF real time editor sharing support to Ceditor - Eclipse

ECF ( Eclipse Communication Framework ) currently gives  support of real-time editor sharing to the Default Text Editor and The Java code editor . Recently I worked to enable this feature for C editor. With the help of EclipseWiki , I  created a plugin( with a local update site ). It will act as an update site for eclipse on local pc.

I used to build the project
  • Eclipse Helios 3.6.2
  • Ubuntu 10.10   
Minimum requirement for the plugin is Eclipse 3.6 . This plugin may not work in Windows platform . I didn't test it . I used the plugin with Eclipse CDT 7.0.2 .

But before all this you have to install ECF componants in your IDE ( Eclipse Helios or CDT ). To do that go here.

After your ECF installation is done , time is now for adding the real time editor sharing support for the C editor.

  • Extract the file to a directory you prefer.
  • Open the IDE and select help -> Install New Software
  • Click Add .
  • Select Local . Add the following directory .                    /The/directory/where/you/extracted/the/file/ECFc.site.
  • Add a suitable name. Hit OK.
  • Follow the installing guidline and ignore the warning message.     
Additional Info:
  
 I found that in other pc there occurs a problem regarding site . To overcome the situation, open site.xml in ECFc.site directory. Update the url field in the following line


<description url="file:///home/rafee/Desktop/ECFc.site">

Replace url field value as url="file://directory/where/you/extracted/ECFc.site". The path of the directory should be absolute. Hopefully it works for you then ( If you are facing any problem regarding site ). I 
found it works for me in that way.


Thursday, February 3, 2011

Observer pattern - in an investigative way




Think of a situation when a crime has occurred and the criminal has run away. CID ( Crime Investigation Department ) reached the spot and experiencing the situation , informed the Police Control Room. One of the CID officer using a phone call can easily do this. On response to this request , the  control room informs all the units working on the highways. This informing mechanism is quite simple  and clear.But this can be made complicated by doing the following ... 

One of the officer of CID always remains on phone for doing all the communication works. Then the intension of the officer to be in CID  will be hampered ( He/ She is there for doing investigations , not to remain busy on the phone ).

Observer  pattern is similar to the situation described above. Consider CID as a unit similar to the highway unit that are to be informed.  The Police Control room works as a central communicator . Then all the units are informed on request coming from any of them. ( Sometimes it is not necessary to wait for a request  coming from a unit , Control Room can itself genarate message and notify all.)

In observer pattern there is a Subject( Police Control Room ) who maintains state integrity among all the Observers( units ) In case of any  change in  Stateinformation  ).


Now let's assume that criminal has been traced . A unit informs the control room that they have located the criminal . Then the control room informs

--- CID , to ensure that criminal has been traced.

--- Other units to stop searching ( to prevent any wastage of effort for searching )

In the case above each units ( Observers ) has two states . Let us denote it like the following:

Searching: when all the units are searching on request came from the  Police Control Room.
and 
Free: when a unit has traced the criminal or no notification from the Police Control Room came.

The police control room (Subject) maintains all the units at any of the above state at a given instance of time. 

this is the whole picture of  the observer pattern.  This pattern offers loose coupling .

Monday, January 31, 2011

0/1-Kanpsack Algorithm



Suppose you have been ordered to bring some elements from a place where there are m boxes full of precious elements.
( each box contains elements of only one type. ) . Each element has a weight w_i and a value p_i , where i denotes the box number.

You are told  all the parameters ( weight and value ) of the boxes in advance . But you can not carry more than n units of weight  with your knapsack .Because your boss has given this to you. Now the problem is to find out   what is the highest amount of profit you can make with the knapsack you have. ( The more you make profit, the more your boss become impressed.)

Now take the pencil  and paper and figure out what you should do . Consider one box at a time. Say the box number is k .(Assume that there is no box whose number is higher than k ) You can make two things with the k^{th} box,  

Case 1: Pick an element from the box . It will cause your knapsack size to be reduced by w_k , adding p_k to your total amount of profit.

Case 2: Do not pick an element from the box. That will cause the number of boxes you are considering , reduced by one that is k-1. ( Be careful , when you are not taking an element from on box , you are discarding the box from your consideration. In future you will not be able to take any element from that box )

let profit( i , j) denote the profit you can make when you have i sized knapsack and j boxes. Then according to the first one of the cases above , you have 

                    profit( i-w_j , j  )+p_j  , 

that is your knapsack size is shorten by w_j and you have added p_j to the total amount of profit possible by the reduced knapsack.

Other case says the following ,

                               profit( i , j - 1)

that is , you are assuming that you have only j-1 boxes .

The problem is a maximization problem. So you have to take the maximum of the two situation . Then the solution becomes ,

profit(i,j) = max \left\{ profit(i - w_j , i) + p_j \\ profit( i , j - 1)

and your destination is to find profit(n,m). Here Knapsack size means how much of weight it can hold. 


Thursday, January 20, 2011

Insert blob files into Oracle using Java




In this article  I will discuss how to insert a image into database table using PreparedStatement interface of java. All other blob files can be handled in same way. You will need ojdbc6.jar as the oracle driver. If you try to use ojdbc14.jar you may get the following exception :

Exception in thread "main" java.lang.AbstractMethodError:
 Theoracle.jdbc.driver.T4CPreparedStatement.setBinaryStream(ILjava/io/InputStream;)V


Now let's create a database table using the following sql command.

create table imagetable ( imageid numeric(5) , image blob);

Now use the following java code to insert a image in the table.

Class.forName("oracle.jdbc.driver.OracleDriver"); 
Connection connect = DriverManager.getConnection  
                       ("jdbc:oracle:thin:@IP_ADDRESS:PORT_NUMBER/SERVICE_NAME" ,"username","password"); 
String filename = "Your filename here" ; 
InputStream input = new FileInputStream(filename); 
PreparedStatement pstmt = connect.prepareStatement("insert into imagetable values(?,?)"); 
pstmt.setBinaryStream(2, input); 
pstmt.setInt(1, 1); 
pstmt.execute();


Also change the connection parameters according to your's.