Friday, July 31, 2009

Customized ClassPath Contributor- An Eclipse Plug-in

When i first started using java (JDK 1.1), classpath management was an issue for me. Setting up a classpath in command line and then running a java program was so error-prone. Luckily, IDE's, such such as eclipse and netbeans, make a developer's life is more happier. Eclipse (actually eclipse framework) provides various functionalities for fast, productive, efficent, and happy programming, not just for Java, but also many other langues (C/C++, Php, Flex, ...)

Classpath management for java in eclipse , of course, is more easer than bare command line. But it still lacks of some functionalities. Profile based shared repository is one of these missing functionalities. So often in my current work, in eclipse, i find myself arranging classpaths after SVN branching or merging or changing workspace or checking out from SVN. My personal settings during these operations conflicts with other developers settings. There is not any profile (indiviual) based mechanism for settings and classpath ( I am awera that maven provides profile and central reposisty based but our projects has legacy ant scripts)

Therefore, We decided to create a central setting project (Settings) wh
ich contributes automatically and independently to our projects (for example, Beee & Booo) based on developer's profiles. Figure 1 depicts a sample workspace which contains two sample projects (Beee & Booo) and a setting project (Settings) which contains profiles (/Settings/profiles) and central shared libraries (/Settings/libs)


Figure 1: Settings projects and Sample projects (Beee & Booo ) in workspace

Let me talk a bit about Settings project. Setting project is a centralized configuration project and holds shared and indivual settings and libraries for each developer. Therefore, structure of the settings project would vary. But a setting project has following main directories:
  • Settings/libs: Contains common, share libraries
  • Settings/profiles: Holds indivual profiles.
Each profile (ex: deniz.turan) directory under "Settings/profiles" directory holds developer specific settings. Name of profiles directories are same as user's system (windows or linux) login names in order to discover automaticly profiles for developer.

Each profile has following subdirectories which would have specific or custom sub directories (such ants, libs)

  • Settings/profiles/deniz.turan/ants: Holds ant script related indivual properties and custom build scrtips: There are two important file here:
  • Settings/profiles/deniz.turan/libs: Holds profile based libraries. User can create sub library directories (with prefiex "lib_", ex: lib_1, lib_2, lib_3 ....) to arrange class path orders. Library class path order is natural order of String Class such as : lib_1 >lib_2> lib_3> ... > Settings/profiles/deniz.turan/libs (See figure 4 and 5)

Imprtant note: Profiles' libraries are in higher order in class path as demonstrated in below

Having a central setting project is not enough alone, unless the content of profiles (/Settings/profiles) and central common libraries (/Settings/libs) are automatically contributed to main projects for each developer independently. Therefore, i decided to develop an eclipse plug-in to contribute to classpath of a project from a setting project and profile, with a automatic manner. Of course, it is possible to add libraries or folders to project's build path in eclipse, but as i pointed out above, it causes conflicts during merges, branches or in new workspace or on other machines.

To develop plug-in, following two extension points from eclipse rcp framework are used:
  • org.eclipse.jdt.core.classpathContainerInitializer : Initialize and manage class path container (ex: "Deniz's Classpath", in sample source project, Figure 2 , 4 and 5)
  • org.eclipse.jdt.ui.classpathContainerPage : Configuration page for classpath container (Figure 3)
For an overview of JDT in eclipse, please click here

Below you can see some screen shots from Classpath Manager project and a sample workspace.

Figure 2: Classpath Container ("Deniz's Classpath Manager")


Figure 3: Classpath Container Page


Figure 4: After Classpath Manager contributes to a project's build path


Figure 5: Final workspace after Classpath Manager contributes resources from Settings project and profile (deniz.turan)


As figure 5 illustrates, content of deniz.turan profile in Settings project (/Settings/profiles/deniz.turan) and common libraries (/Settings/libs) are contributed to sample "Booo" project. As explained above, order of contributed libraries in classpath are based on first profile sub libraries (/Settings/profiles/deniz.turan/libs/lib_1), and then profiles libraray (/Settings/profile/deniz.turan/libs) and finally common libraries (/Settings/libs)

You can download source code of Classpath Manager eclipse plugin project with sample setting project. The source code is a prototype and implements basic idea of the classpathContainerInitializer and classpathContainerPage extensions. It is tested with eclipse 3.4 and 3.5.

Click here to download ource code.


Saturday, July 25, 2009

Fooled by Randomness, Nassim Nicholas Taleb

Think a book, whose author offends, mocks and bores its readers while he pretends he is the most clever, smart, intellectual and scientific guy in the world in each page of the book. It is not only written in a chaotic structure-- chapters, sections are not related to each other or very mixed up in terms of concepts and structure, the author also uses lots of buzzwords, redundant and wrong examples.

I am talking about
"Fooled by Randomness, The hidden Role of Chance in Life and in the Market", by Nassim Nicholas Taleb. Have a look at following quote from the book:

"What has more value? (a) a contract that pays you $1 million if the stock market goes down 10% on any given day in the next year; (b)a contract that pays you $1 million if the stock market goes down 10% on any given day in the next year due to a terrorist act. "

What is your answer? a or b ? Taleb claims:"I expect most people to select (b)." I am not sure IQ level of people around Taleb, but i reckon, most people would go option a. (I know that, in a normal distrubuted financial world, %10 changes in stock market is so low probability -- once every 73 to 603 trillion billion years-- , but in last 80 years, that incident happened over 2 times and five sigma deviation is over 73 times, which happens once in 7000 years if finance data is normally distrubuted. More info is here).

Taleb is a trader and scholar, works in a fixed income (bonds) financial company. He has background in science (PhD) and like many other PhD graduate, he bored in academia after some time and started to work in finance sector. In his book, he claims that probability theory is not a natural or trivial concepts for many people to comprehend. I do agree with him in this claim. But he takes his claims further and try to establish a theory and life style based on randomness (rare events) . He claims that he is expert on random events and he takes advantages of these random events in his life and business.

He exemplifies his ideas with "fictitious" characters who works as traders in finance (fixed income or equity markets). These characters are generally quite extreme and opposite of each other. For example, he kicks off the book with stock and bond market traders. The stock market trader, Steve, does not have any sound education and a risk taker and get successful so quickly. On the other hand, the bond market trader, Bob, has a degree in probability and he does not take much risk in his business (to be honest, there is not much risk in fixed income market compare to stock market). Because of calculated and not risk taking style, Bob is not rich as much as Steve.

Taleb claims that the success of the stock market trader, Steve, is based on randomness, in another words just "luck". He claims that even if you put 10.000 monkeys in stock market as trader, and monkeys trade randomly, by the end of 5 years, there will be at least a rich monkey. He also claims that after some lengthy time (10 years), all of these monkeys will disappear (The clever one would run away when he has some money, but most of them lose all of their money before they are kicked out). Taleb has a point in analogy, i think. Similar to many aspect of life, randomness also has a part in stock market. But i think, his claims that without proper analysis, research and hard work in stock market, someone would get rich randomly is just ridiculous. He dismisses that the participant of stock markets are intelligent, agile and very adaptive people. Stock market has chaotic aspects, but not totally random. It responses a deterministic way to some events (for example, if a small company merges or bought by a big company, it's share will surge). And the job of traders is to predict (or to speculate) these events in order to make profit.

I found many so-called scientific or intellectual ideas of Talebs, especially in probability theory is very mixed up. For example, he ignores the main reason of filters in statistics or engineering (filtering outliners and noise). While he claims that he takes advantages of these outliners (randomness, noise) in his real life and business, he complaints about the source of the noise for example, media and journalist (He has issue with media, TV, papers too). He contradicts himself, and gives mixed messages in different part of the book.

In brief, in his book, Taleb comes cross as an arrogant, geek person who claims 'if you disagree with me, you're an idiot and I will ignore and laugh at you.' He wrote the book for just sake of writing a book, and before clearing and organising the ideas in his mind. His writing style and personality kills the some of his nice ideas. By the end of book, i felt big disappointment. It was worst ever book i read in a long time. Luckily, the current book in my hand ("The Ascent of Money: A Financial History of the World") , is making me to forget yucky taste of this book.

Saturday, July 18, 2009

Better Annotation in Java Around a Testing Framework

Recently in a pet project, i got involved extensively with annotation in java. I wanted to create a testing framework in which unit tests are created based on given methods level annotations. The testing concept is similar to EasyMock, but rather than creating mock objects, i wanted to capture real objects during "runtime" and then create tests cases based on annotation and output/input parameters. By the way, with "Runtime" i don't mean production environment. When a QA or developer run (tests) the product, tests cases and its inputs are created and stored.

After clearing the concept in my mind, i decided to do a prototype. In the first prototype, i wanted to implement @AssertNotNull unit test case for method's input parameters and output value.

By deploying method level java byte code injection with javassist and a simple annotation (@AssertNotNull), unit tests are create for methods and inputs and outputs of methods are serialized. Once i catch the input objects (parameters of the method) with injected byte code at the beginning and end of methods, i serialized them in XML format with XStream . Injected byte codes also call a TestGenerator service to generate unit tests based on the defined annotation and serialized input/outputs. In unit test phrase, I was planning to deserialize these input objects and uses as input to automatically created tests. First prototype was successful. Unit tests (@AssertNotNull) are created for output object and input parameters of a given annotated method. It was a big step to create unit tests automatically with real object rather than mock objects like EasyMock.

As a next step, i decided to implement a prototype for more complex test cases which has two inputs, for example @AssertEquals. When i was prototyping @AssertEquals annotation, i noticed that annotation in java are really so basic. They do not provide so much functionalities for advance usage such as:

  • Hierarchy: One of the main concept in OO is missing in annotation creation.You are not allowed to extend an annotation. For example, i would like to create a base annotation type (let say @Test) and many sub annotation types (@AssertTrue, @AssertNotNull, @AssertEquals) which extend @Tests. This approach, Hierarchy in annotation, would save lots of computation and coding time if reflection is used to probe which methods are annotated with a type of annotation (@Test) rather than a specific annotation (@AssertTrue). Of course, as a solution to lack of Hierarchy in annotation, methods or classes can be annotated both with super type and specific type annotation (both for example, @AssertNotNull and @Test). But isn't it an ugly code ?
  • Multiple annotation: A method or class can be annotated only once with a specific annotation. Even it is not much common in real life, as in my test framework, it should be possible to annotate a method with different parameters. For example, i would like to annotate a method with multiple @AssertNotEqual each of which uses different parameters. People who comes cross that issue, generally creates a container annotation For example in JPA, If you would like to annotate an entity with multiple @NamedQuery, you have to declare these annotations in @NamedQueries.
  • Simple Type : Types of fields in annotation are so restricted. Only primitive, String and Class types are allowed.
I think annotation in java should be revised. Similar to enums they should to be enhanced for a versatile and advanced usage. I am aware that these issues can be resolved with some other ways as i mentioned above, and it is not trivial to implement comprehensive annotation, but i still think that java deserves better annotation design.

By the way, i am still working on this pet project, test framework! Once i finished it, i will create a blog here with more detail.