Search This Blog

Thursday, February 25, 2010

Flex Camp 2010

Update: The presentations are online and can be found here.

The first Flex Camp 2010 is a fact and was a great success. Flex Camp is organized by the Dutch Flex User Group (FLUGR) and is a half-day Flex event with a variety of speakers and subjects. This time, the location of the event was Amsterdam. The following sessions were given:

Designer/Developer workflow with Flash Catalyst and Flash Builder

This session was all about how Flash Catalyst and Flash Builder approach the designer /developer workflow. In a traditional development environment a design usually takes the form of a Photoshop document which is sliced by the developer and incorporated in the Flex application. Flash Catalyst takes a new approach in that this product is able to import and slice the design itself. When imported, Flash Catalyst is able to convert individual elements to read-to-use Flex components.

One thing that is not incorporated in the product yet, is an iterative design/development cycle although this can partly be accomplished with the use of libraries.

Set your code on fire

Flex developers with a Java background are always disappointed by the lack of features of Flex Builder compared to working with Java editors. Luckily there are alternatives for Flex Builder and one of the alternatives is FDT from Powerflasher. During the session, an application was developed to show a lot of the capabilities of FDT. Very impressive. Some of FDT’s features are:


  • Rename and move refactoring

  • Extended use of code templates and quick fixes to:


    • Generate classes

    • Generate functions

    • Generate fields with getters and setters

    • Loop over arrays

    • Etc.


  • Superb code navigation possibilities like type hierarchy, dependency structure, outline view with search capabilities.

  • Debugger


Version 4 of FDT, which is coming in 2010, has even more features like a memory and performance profiler. To learn more about version 4 goto http://fdt.powerflasher.de/fdt4comfort

Flex Builder action script projects

Flex Builder is not only good in Flex projects but also suitable for Action script only projects. The speaker showed how he developed a pure Action script only website with Flex Builder. During the session a lot of practical tips were given which he encountered during the development of the website. Unfortunately, the site is not yet live at the time of writing. To be continued.

Flex in a JEE environment

In this session the speaker demonstrated a Flex application which is used to display information about geographical locations. The application integrates with the backend using web services and Blazeds and is deployed on a JBoss application server. The speaker showed how Maven is used to build and distribute the application and how the Flex application can be configured using server side services. It was a very practical and good session, especially for Java and Flex developers.

When all presentations are online, I will post the link to these presentations here.

Conclusion
Flex Camp 2010 was a big success. The subjects of the sessions were diverse enough to please everyone who attended Flex Camp. From Flex designer/developer to hardcore Java developer. On to the next Flex Camp!

Friday, October 30, 2009

Flex: How to create a different rollover color for the header in a datagrid component?

Update 01-02-2010: Monkey patching the Flex framework does not work when RSL's are used. This is because the code of the RSL is loaded in the first frame wether the monkey patched code is instantiated in the second frame and hence does not override the RSL classes. This can be solved by instantiating the monkey patched class in a custom preloader as described here. Depending on the size of the included classes, this can invalidate the choice to use RSL's in the first place, namely to reduce application size. To include the monkey patched class in a custom preloader depends solely on the patch itself and the increased download size. These two factors should be taken into account.


In my current project I have the following requirement for styling a DataGrid component:

The roll-over color of the header should be of a different color than the roll-over color of the datagrid rows. To make things even more interesting, the roll-over color must be a gradient instead of a solid color.

Unfortunately there is no easy way to accomplish this. We can set the default background color of the header with the headerColors style, but this color is overwritten when the mouse hovers over the header. We can also disable the rollover highlighting with the useRollOver property but this does not prevent from changing the rollover color of the header! Even if we could disable the header rollover color this way, it is not a solution because we want a different rollover color for the header and the rows containing the data.

After some digging in the Flex SDK source code, the solution I came up with is to provide a customized DataGridHeader class. Please note that I customized the Flex 3.2.0 SDK because I use the 3.2.0 SDK. If you use the Flex 3.4.0 SDK you should modify the DataGridHeader class of the 3.4.0 SDK instead. To customize the DataGridHeader class I did the following:
  1. Created a mx.controls.dataGridClasses package in my own source folder
  2. Copied the DataGridHeader.as file from the Flex 3.2.0 SDK to my own mx.controls.dataGridClasses package
  3. Located the mouseOver- and mouseDownHandlers and replaced the drawHeaderIndicator method with my own drawGradientBackGround method.
  4. Added two new style attributes to the DataGridHeader class, named: startColor and endColor which defines the start- and endcolor of the gradient fill. These attributes are queried in the drawGradientBackGround method. For simplicity I did not provide default values so the default color is black.
  5. Declared the startColor and endColor attributes in css and gave them a color value.
A full working example can be downloaded from http://code.google.com/p/jc-examples/ as a zip file or from the svn repository. The following picture is taken from the sample project which demonstrates the gradient header color when the mouse is over the header. The mouse is over the name column in this case. Please note that this example uses the Flex 3.2.0 SDK.


If there are more straightforward solutions to this problem please share them.

Resources

Saturday, October 3, 2009

To inject or not to inject

Last week I was a couple of days at a client to look at some strange behavior in the application. The application was a calculator used by clients of the company to calculate services the client offers. The application was written in Java6 and used Tapestry for the view and a Spring/Hibernate/AspectJ combination for the backend which talked against an Oracle database. Tomcat was used as the runtime container for the application.

The first problem that had to be tackled was that sometimes the dependency injection did not work properly. Sometimes dependencies were injected, and other times not. When I looked at the application I noticed that an aspect was responsible for injecting repositories into domain objects. As soon as a domain object is deserialized or instantiated, the AnnotationBeanConfigurerAspect injected the repository. At least that was the intention. But the injection did not always take place. I further discovered that load-time weaving was used to inject the aspect’s behavior. I looked at the configuration and did not see anything that was wrong. The proper class loader in Tomcat was used and the necessary Spring configuration was present. What was causing this awkward behavior? Because time was limited I decided to switch to compile-time weaving. We compiled the classes with the AspectJ compiler and redeployed the application. This time the dependency injection was working as expected. We did not further investigate why load-time weaving did not work properly. If you have any suggestions please mention them.

The second problem was that after a specific amount of inactivity at the application side, the application crashed when it was first accessed. The stack trace revealed that it was a problem with the underlying database connection. The message indicated that the connection was already closed. What was happening was that Oracle closes all idle connections after a specific amount of time, in our case 30 minutes. This is configured in the sqlnet.ora file with a parameter called sqlnet.expire. To overcome this, we added the following configuration to the Commons DBCP connection pool:

testOnBorrow=true

validationQuery=SELECT 1 FROM DUAL

The result of this configuration is that all connections returned from the pool are first tested with the specified validation query. If the test fails the connection is discarded from the pool and a new one is returned. This solved our problem. I must mention that this configuration can have a negative impact on performance because every returned connection is checked for validity. Because of this, the simplest Oracle query is used to test the connection. Since the application did not have many concurrent transactions, this setting was acceptable.

If performance is an issue, I would recommend using an eviction strategy where a specified eviction policy periodically checks all idle connections for validity. See the Commons DBCP configuration guide for an explanation to configure this.

I hope some of you may find these solutions helpful in your own projects.

Tuesday, June 16, 2009

Melody composition using genetic algorithms

Introduction
Several techniques exist to create computer generated musical melodies. One of those techniques is genetic algorithms. Because the diversity of melodies over a specific range of notes is so large, genetic algorithms are a good candidate to help in composing melodies. This article describes how genetic algorithms can be used to compose musical melodies. this is explained following the steps needed to apply a genetic algorithm. These steps are:
  1. Define the genetical representation of the problem
  2. Determine the fitness function
  3. Determine the parameters used for the run
  4. Determine the termination criteria
The implementation of the genetic algorithm is done using the JGAP framework, a Java-based framework for implementing genetic algorithms. The generated melody itself is converted to MIDI which can then be played by the internal MIDI device or by a musical instrument attached to the computer

For more information about using genetic algorithms in Java please see my previous article which can be found here: introduction to genetic algorithms. For more information about music theory which is applied in this article please refer to the excellent site http://www.musictheory.net.

Define the genetical representation
Before the genetic algorithm can do it's work, the genetical representation of the problem must be defined. To make things slightly less complicated the following constraints are introduced:
  1. Only harmonic melodies are supported. This means that only one note is played at ones at any given time in the melody.
  2. The generated melodies do not adhere to a specific measure. It is just a sequence of notes.
In a future version these constraints could be loosened.

A Melody can be seen as a composition of individual notes and rests. Those individual notes have properties which define how a certain note must be played. The following properties are used which are modified by the genetic algorithm:
  • Pitch
    The pitch determines which notes on the grand staff is played. Possible values are C,D,E,F,G,A and B and all possible variants using sharps (#) and flats (b).
  • Octave
    The octave determines in which octave a certain note is played. A piano has seven full octaves.
  • Duration
    The length determines how long a certain note is played. Possible values are, whole notes, half notes, quarter notes, eight notes, sixteenth notes.
Notes have more properties than pitch and duration alone. Velocity, for example, indicates how hard a note is played. These properties are not used in this article. Besides notes, there are also rests in a melody. Rests can only have a duration.

A solution in the search space of melody generation is a melody consisting of individual notes. A solution is represented as a chromosome with a fixed number of genes. The genes in the chromosome represent the individual notes and rests, each with it's own characteristics. A note can be represented by using a composite gene with three integer gene's. The three integer gene's represent pitch, octave and duration. Integer gene's are chosen in favor of a custom gene implementation to make mutation easy and simple across the individual gene's. Each gene is described in detail here:

Pitch: The pitch can be represented as a number from 1 to 12. Since there are twelve semitones in an octave, each semitone can be represented by a number from 1 to 12. Every following number is equal to one semitone. The mapping of the notes is:































































Value

Note

1

C

2

C# or Db

3

D

4

D# or Eb

5

E

6

F

7

F# or Gb

8

G

9

G# or Ab

10

A

11

A# or Bb

12

B

0

rest


Because all gene's are treated the same, a special case for the a rest in a melody is introduced. A rest has the value of 0 for the pitch. Since a rest is not associated with an octave, the octave property is ignored.

Octave: an octave can be represented by a number, the octave in which a certain note is played. The octave is indicated by a number from 1 to 7 where 1 represents the lowest octave on a piano keyboard and 7 the highest.

Duration: the duration can be represented by a number between 1 and 5. The mapping of the numbers are:































Value

Note

1

Whole note

2

Half note

3

Quarter note

4

Eighth note

5

Sixteenth note


The duration applies to both notes and rests. See the following code for the creation of the initial population of chromosomes:
Configuration.reset();
Configuration gaConf = new DefaultConfiguration();
gaConf.resetProperty(Configuration.PROPERTY_FITEVAL_INST);
gaConf.setFitnessEvaluator(new DeltaFitnessEvaluator());

gaConf.setPreservFittestIndividual(true);
gaConf.setKeepPopulationSizeConstant(false);

gaConf.setPopulationSize(40);
CompositeGene gene = new CompositeGene(gaConf);
// Add the pitch gene
gene.addGene(new IntegerGene(gaConf, 0, 12), false);
// Add the octave gene
gene.addGene(new IntegerGene(gaConf, 1, 7), false);
// Add the length (from 3 - 5 is from quarter to sixteenth)
gene.addGene(new IntegerGene(gaConf, 1, 5), false);

// A size of 16 represent 16 notes
IChromosome sampleChromosome = new Chromosome(gaConf, gene, 16);
gaConf.setSampleChromosome(sampleChromosome);

gaConf.setFitnessFunction(melodyFitnessFunction);

return Genotype.randomInitialGenotype(gaConf);
Determine the fitness function
The fitness function determines how good a specific melody is, relative to other melodies. The fitness function is the most complicated part of this problem since the fitness of a melody is subjective. Because of this nature, two approaches for fitness determination are presented.

Computer generated fitness
The computer generated fitness is purely based on certain algorithms to measure the fitness of a melody. Since there are a lot of different parameters, the fitness function combines the fitness value of several different strategies which can be easily added to the fitness function. Some of these parameters, but not limited to, are:
  • 80% of the notes in a melody may not have more than than 7 semitones difference.
  • A melody may not span more than 2 octaves
  • A melody must be in C-major (or minor scale)
  • Only 10% of a melody may consist of rests
  • Two consecutive notes may not lie more than 5 semitones from each other
  • ...
In the proof-of-concept implementation the following rules regarding the fitness of a melody are implemented. These rules are implemented as separate classes which all implement the MelodyFitnessStrategy interface. This makes it easy and straightforward to add more rules which measure the fitness of a melody.
  • ScaleStrategy
    Calculates if a given melody adheres to a specific scale, for example C major. The scale can be set as a parameter on this class.
  • IntervalStrategy
    Calculates if a given melody has one or more major and/or perfect intervals. The number of major and perfect intervals can be set as parameters on this class.
  • GlobalPitchDistributionStrategy
    Calculates if the lowest and highest pitch of a given melody fall within the margins specified by this class. The margin is indicated as the number of semitones and a percentage about how much of the notes must fall between the given semitones. These values can be set as parameters on this class.
  • RepeatingNotesStrategy
    Calculates if a given melody has repeating notes or rests. The maximum number of repeating notes and/or rests can be set as parameters on this class.
  • PropertionRestAndNotesStrategy
    Calculates the proportion between the notes and rests in a given melody. The propertion of notes/rests can be set as parameter on this class.
  • ParallelIntervalStrategy
    Calculates the number of parallel intervals in this melody. Some parallel intervals are supposed to sound good, like thirds and sixths. The number of good sounding parallel intervals can be set as a parameter on this class.
A builder class exists which helps in the creation of a valid fitness function.

Please note that all rules calculate the deviation between the generated melody and the specified rules. A lower fitness value means less deviation which means a better melody. In the future it is planned to add more strategies to calculate the fitness of a given melody. For example:
  • ContourStrategy. Strategy which calculates if a given melody has a specific contour in the pitch of the individual notes.
Below is the implementation of the ScaleStrategy:
public final class ScaleStrategy extends AbstractMelodyFitnessStrategy {
  private static final int ERROR_COUNT_WHEN_NOTE_NOT_ON_SCALE = 1;

  @Override
  // The more difference between current note and note in scale the higher the error count
  public double calculateErrors(IChromosome melody) {
     double errors = 0.0D;
     for (Gene gene : melody.getGenes()) {
        Note note = GeneNoteFactory.fromGene((CompositeGene) gene);
        if (Pitch.REST != note.getPitch() && !super.scale.contains(note.getPitch())) {
           errors += ERROR_COUNT_WHEN_NOTE_NOT_ON_SCALE;
        }
     }

     // Adhering to a given scale is quite important so square the result
     return (errors * errors) * 10;
  }

  public String toString() {
     return "[ScaleStrategy[scale: " + this.scale + "]]";
  }
}
A different approach
The above paragraph describes how the fitness of a melody can be computed based on the knowledge of music theory. Instead of using music theory to compute the fitness of a melody, a different approach can be used. This approach looks at the melody as audio data, an array of bytes, instead as a sequence of notes. From this audio data, different information can be extracted. For example, by using a Fast Fourier Transform (FFT), the audio data can be viewed in the frequency domain. By analyzing existing melodies using FFT, an algorithm can be constructed which measures the fitness of generated melodies based on this knowledge. A future version of the application may use the technique described here.

Human intervention based fitness
Since melodies are very subjective it is hard to come up with a computer based mechanism to measure how good a specific melody is. Another approach which can be used is a human based intervention fitness function. In this approach the user is represented with a fixed set of melodies generated by the genetic algorithm. The user selects two or three melodies which participate in the next evolution. Although this approach is not implemented in the current version, expect a future release to use this approach.

Determine the run parameters
When using a computer based fitness function, several parameters which affect the generated melody can be provided by the user. See the section about the computer generated fitness function which parameters can be supplied. Based on some sample runs, the number of evolutions to execute is 250. 250 evolutions seems like a good trade off between quality of the generated melody and computation time. The quality is measured in terms of the fitness value of the melody. With 250 evolutions, the fitness value of a 24-note melody almost always approaches zero.

Determine the termination criteria
The run ends when 250 evolutions are executed.

Some sample runs

The program can be executed in two different ways:
  1. By executing the MelodyGeneratorMain class file from the command line. Modify and recompile this class to alter the parameters of the run, for example the fitness function.
  2. Via the Swing UI. This is a very simple Swing UI build with Groovy's SwingBuilder. All of the parameters of the fitness function can be modified with this UI. Please note that this is a very simple UI implementation and not an example of how to write production quality Swing code.
To generate a melody from the UI, just click the generate button. When finished, the application plays back the melody and gives the option to replay, save or generate a new melody. Just experiment with the different settings and listen to the various generated melodies. In the UI you can specify the path to write the MIDI files to. Make sure this path exists on disk.

Conclusion
This article explains how genetic algorithms can be used to compose melodies. Genetic algorithms seem like a viable alternative for melody generation since they are very well suited to search for specific solutions in very large search spaces. In this case the search space are all possible combination of notes. The difficulty in generating melodies with genetic algorithms is the specification of the fitness function since this is very subjective.
Although the melodies generated by this program are nowhere near hit melodies, they can be used as inspiration when composing melodies. The performance of the algorithm can be improved by writing additional (and more complex) strategies to measure the fitness of a melody.

Let me know what ideas you have to measure the fitness of the generated melodies to improve the quality of the melodies.

The full source code of this application can be found on Google code, see the resources.
Resources

Monday, April 20, 2009

J-Spring 2009 part 2

The second keynote was given by Jos Warmer, a Model Driven Architecture evangelist. Jos has a lot of experience with MDA and you can see that from his talk. His talk was about Mod4j. According to the website: “Mod4j (Modelling for Java) is an open source DSL-based environment for developing administrative enterprise applications.”

Mod4j is an environment in which DSL’s are used to generate specific parts of a Java application. At the moment three DSL’s are implemented (and a fourth is on its way) which focuses on specific architectural layers of the application. These DSL’s are:

  • Business Domain DSL
  • Data Contract DSL
  • Service DSL

The business domain DSL implements the concepts to specify the business objects in your application. These are: business objects with properties, associations between business objects and constraints. Consider the following example:

association Order order one -> many OrderLine orderLines ordered;

The above sample DSL generates a one-to-many association from the Order to the OrderLines class including the necessary Hibernate mapping files. A lot of boilerplate code is generated by using the business domain DSL.

The Service DSL is used to specify services for your domain objects. Examples are the CRUD operations of a specific domain object and finder methods. The code generator for this DSL takes care of generating the required Java code. Consider the following code:

from RecordShopDataContract import SimpleCustomerDto;
create createCustomer for SimpleCustomerDto ;
read readCustomer for SimpleCustomerDto ;
update updateCustomer for SimpleCustomerDto ;
delete deleteCustomer for SimpleCustomerDto ;

The preceding code generates the CRUD methods for the SimpleCustomerDto in the service class.

What’s nice about Mod4j is that no visual models are used for code generation. Domain Specific Languages are used instead. The DSL’s can be put in a version control system where you are able to use the version control system’s merge and history abilities; something which is hard to implement when using visual models. There are also a lot of extension points to modify the generated code. But when you find yourself modifying too much of the generated code, you must ask yourself if the code generator should be modified instead. With mod4j this is possible because it is open source. What’s also nice is that there is an Eclipse plug-in to facilitate Mod4j development. With this plug-in you get code completion on the provided DSL’s.

Although I am not a big fan of Model Driven Architecture tools, I like the concept and principles of Mod4j mainly because coding is central. When should you use Mod4j? I find this a difficult question. When working on a large multi-layered application in Java with technologies like Hibernate, Mod4j can make sure that a consistent architecture is used across all layers. New developers can be more easily brought up to speed.

But when looking at the DSL’s used for generating code, I immediately thought of the Grails framework, based on Groovy. Grails uses a lot of DSL’s. Consider the following Grails example which creates an Author business object:

class Author {
static hasMany = [ books : Book ]
String name
static constraints = {
name(size:5..15,blank:false)
}
}

The above code is the Author business object in Grails with a one-to-many association to the Book class. It also implements a constraint on the name property. Because of the dynamic nature of Groovy, you get the CRUD operations, dynamic finders and the relationship handling for free. If you want to customize the mappings you can modify the Hibernate mapping files if you want.

So the DSL Grails uses is what I call an executable DSL whereas the DSL’s that Mod4j uses are used to generate Java code.

Despite this there is still place for a technology like Mod4j. Not everyone is in the position to use Grails (or Rails) to facility high speed web development. When plain Java web development is used, Mod4j provides a viable alternative to speed-up this development. I will definitely keep an eye out for Mod4j!

Service Oriented Architecture

The next session I went to was about Service Oriented Architectures from Tijs Rademakers. His presentation turned out to be a very practical session which included a lot of his own experiences when implementing Service Oriented Architectures. I like this approach very much. He clarified some misunderstandings when using ESB’s. When an ESB is used, often a lot of code has to be written inside the ESB. Think of transformation and routing logic. This code has to be maintained as well which is often underestimated.

Tijs also talked about how to handle XML messages in a Service Oriented Architecture. I always thought that Groovy has superior XML handling capabilities compared to Java. Tijs mentioned Groovy as one of the most effective ways for handling XML. I agree! So when working with XML in Java (Service Oriented Architecture or not), consider using the Groovy language for the XML handling logic.

Conclusion
Despite the economical situation, 4 people of my company (including me) went to the J-Spring. At QNH we find it very important to continue to invest in knowledge. Individuals are the most important part of a company in which you should invest.

I found the J-Spring 2009 a very good day with lots of good speakers and sessions with a great variety of subjects well worth the investment. If you’re a Dutch Java developer and was not part of J-Spring, make sure you will be at J-Fall later this year.

Keep up the good work!

Friday, April 17, 2009

J-Spring 2009 part 1

April 15th I went to the Dutch Java user group conference named J-Spring. This is the first of two blog entries about this conference. The J-Spring is a popular 1 day conference about Java and related technologies. It is a good place to get an overview about the current state of technology and meet fellow developers.

The introductory keynote was given by Sherali Karimov from Atlassian who talked about how agile is implemented at Atlassian.

Two things in his presentation I found particular interesting. These were:

  • The disturbed
  • Blitz tests

The disturbed
We are all familiar with it. Sometimes you and your team members get so many questions from different people that your own work suffers from it. At Atlassian they introduced the role: the disturbed. Whenever someone has the disturbed role, he/she is expected NOT to do any work but instead answer all questions from outside. The other team members can focus on delivering. The disturbed role is changed every week to another team member. Every week a new team member is responsible for answering questions from people outside the team. Besides shielding the team from outside questions, the disturbed role has another advantage. By switching team members to answer questions, gradually each team member gains the knowledge to answer those questions. It is an effective and practical way to share knowledge within the team.

Blitz tests
Another concept that was introduced is Blitz tests. Atlassian uses there own products inside there own company. Whenever a new release of a particular product is released, it is deployed on the company’s production environment. People from within the company can subscribe to test the new functionality. The experience at Atlassian was that a lot of people subscribed and were eager to test the new functionality. They gave a lot of good feedback to the development team which resulted in higher quality software. I believe you gain a lot when you have the possibility to test every new release by certain people in your own company before releasing the product to your customers.

JEE and JavaFX
The next session I went to was from Paul Bakker. This session was about the future of web development with JEE6 and JavaFX. Paul gave a very complete overview about all the technologies involved in JEE6 and JavaFX. A couple of technologies used in JEE6 are JSF 2.0, JAX-RS and JPA 2.0. After giving a brief overview about every technology, Paul showed how to build a web application with JSF and JavaFX together. Basically, JavaFX was used for the highly interactive parts and JSF for the HTML forms. JavaScript was used to act as a bridge between the HTML page and the JavaFX applet. He also showed how to pass parameters between the applet and the HTML page and vice versa. This seems like an effective way to combine JavaFX with regular web development. JEE6 and JavaFX definitely look very promising!

Look out for the second post of the J-Spring which will highlight some interesting points from the other sessions I attended.

Monday, April 6, 2009

J-Spring

Next week, April 15th, I am going to the Dutch Java User Group conference named J-Spring. This one-day conference is a good place in helping to keep your knowledge up-to-date. There are a lot of interesting sessions which you can attend and a lot of fellow developers to have discussions with. I Am planning to attend the following sessions:

  1. Developing web applications of the future: Combining JavaFX & JEE 6
  2. Java Programming in a Multicore World
  3. SOA, it's a hard knock life
  4. REST, the web as the database?
  5. Hop on Board the Java Troubleshooting Platform

I will post my experience of the J-Spring next week.