Search This Blog

Tuesday, November 13, 2012

Sample generated melodies

Based on some questions, I uploaded a couple of sample melodies in MP3 format which are generated by my genetic algorithms melody generator. The generated sample melodies can be found here: http://code.google.com/p/melodycomposition/downloads/list.

The original article of the melody generator can be found here: Melody generator

Thursday, October 18, 2012

Kinect within VMWare

Recently Microsoft announces the availability of the Kinect 1.6 SDK. One of the big enhancements in this release is the support for the Kinect device within a virtual machine (VMWare or Parallels)!

For a client we developed a Kinect application which was shown at the Dutch Floriade festival. More details about this project in a next post.

The problem with the old Kinect SDK was the inability to use the Kinect within a virtual machine. Because of this I had to create a bootcamp partition and install Windows on it just for the sole purpose of Kinect development. The 1.6 SDK solves this!

Below are the results of my tests with the Kinect within VMWare. I used the following system configuration to test on:
  1. MacBook pro 2011 with 2Ghz core i7 and 8GB RAM..
  2. OSX Mountain Lion.
  3. VMWare Fusion 5 running Windows 7.
  4. Visual Studio Express 2012.
  5. Kinect 1.6 SDK.
First I tested the above setup with the Xbox Kinect. This did not work which I more or less expected since Mircosoft wants to push the Kinect for Windows for Kinect development.

Next I tested was the Kinect for Windows. After I connected the device I started the Shape Game sample application. The device was found and worked immediately! I also tested the Kinect with our own application and this also worked as expected.

Ont thing I noticed was that both applications did not run as smooth as when I ran the application under bootcamp. After examing my virtual machine settings I noticed I had given it only 1 cpu core (out of the available 8). This could be the problem. When I changed the maximum number of cpu cores the virtual machine may use to 4, things ran much smoother. There was no noticeable difference when running within a virtual machine or bootcamp. 

For more information about the 1.6 SDK see: http://msdn.microsoft.com/en-us/library/jj663803.aspx#SDK_1pt6_M2.

Conclusion
The new Kinect 1.6 SDK does work within a virtual machine if the Kinect for Windows is used.


Thursday, October 4, 2012

IntelliJ SSR: replace constructors

Suppose you have a large codebase which uses a particular class named SomeClass (nice descriptive name :)). SomeClass has one constructor:

public SomeClass(String a, String b, String c, String d) {
    // Do something
}

This constructor is used at various places in the codebase.

At some point in time an update to SomeClass is made which introduced another constructor:

public SomeClass(String a, String b) {
    // Do something
}


Lets assume you want all calls to the 4 argument constructor to be replaced by the 2 argument constructor. The arguments of the 4 argument constructor should be used in the 2 argument constructor in the following way: new SomeClass(1, 2, 3, 4) becomes new SomeClass(3, 1).

See the following example:


public class UsageOfSomeClass {
    private SomeClass instance_1 = new SomeClass("a", "b", "c", "d");
    private SomeClass instance_2 = new SomeClass("d", "e", "f", "g");
    private SomeClass instance_3 = new SomeClass("h", "i", "j", "k");

    public static void main(String[] args) {
        SomeClass someClass = new SomeClass("1", "2", "3", "4");
    }
}

Should become:

public class UsageOfSomeClass {
    private SomeClass instance_1 = new SomeClass("c", "a");
    private SomeClass instance_2 = new SomeClass("f", "d");
    private SomeClass instance_3 = new SomeClass("j", "h");

    public static void main(String[] args) {
        SomeClass someClass = new SomeClass("3", "1");
    }
}

You can achive the following for an entire codebase with one single structural search and replace action. Use the following templates to achieve this:

Search template: new SomeClass($arg1$, $arg2$, $arg3$, $arg4$)
Replace tempalte: new SomeClass($arg3$, $arg1$)

Hit find and Replace All. Thats it!

Sunday, September 9, 2012

IntelliJ: HTML and structural search and replace

I was asked to do some quick fix on some website. The website used a lot of table structures for laying out the page. To give an impression, the page had more than 50 tables each with 4 columns and 20+ rows.

There was no external CSS used. All styling of the tables was done with attributes or inline style elements. The styling was also not consistent throughout the page so each table looked slightly different.

Here is an example:

<table>
    <tr>
        <td width="200px">Some text</td>
        <td width="221px">More text</td>
        <td width="300px" height="100px">Example</td>
        <td style="width: 210px;">HelloWorld</td>
        <td style="width: 190px;">More text</td>
        <td id="test" style="width: 222px;"></td>
        <td id="test2" class="right" style="width: 200px;">Java</td>
    </tr>
</table>

Without rewriting the whole site at once I wanted to remove all the inline styles so I could use css to style all the tables the same. That at least looked more attractive in the short run while in the long run we could focus on a complete redesign of the site.

The first thing I wanted to do was replace all the td elements with inline styling with plain, empty td elements. As written in my previous posts, IntelliJ's structural search and replace features fits this use case nicely.

In the structural search and replace dialog I used the following:

Search template:

<td $width$="$value$" $style$="$value$">$text$</td>

Replace template:
<td>$text$</td>

In the Edit variables dialog I specified a minimum count of zero for the $width$, $style$ and $text$ variable. The $value$ variable has a minimum count of one. For all variables I used a maximum count of one.

The template searches for all td elements with zero or one width or style attributes (or both) and with or without text. The replace template replaces the td found with the search template in a plain td. The output of the HTML after the search and replace is run is:

<table>
    <tr>
        <td>Some text</td>
        <td>More text</td>
        <td>Example</td>
        <td>HelloWorld</td>
        <td>More text</td>
        <td></td>
        <td>Java</td>
    </tr>
</table>

After this replacement I could just insert the appropriate CSS and style all tables consistently. Without structural search and replace I think such scenario's take considerably longer to execute.

Hope you enjoyed it.

If you have other interesting structural search and replace use cases please share them.

Saturday, August 11, 2012

IntelliJ: customize the editor

In this post I explain the modifications I make in the configuration of the IntelliJ editor and project window. With these modifications I find it even more easier to work with source code.

I make the following modifications to the IntelliJ editor settings:

Show method separators
This option shows horizontal lines between methods with which seperate methods are easily identified. To enable method separators do the following:
  1. Open the settings window
  2. Open Editor -> appearance and check Show method separators.
High light usages of element at caret
This option highlights all usages of the element where the caret is positioned. To enable this option:
  1. Open the settings window
  2. Click Editor and check Highlight usages of element ar caret.
Highlight current scope
This option highlights the current scope of a code block in the left gutter. To enable this option:
  1. Open the settings window
  2. Click Editor and check Highlight current scope.
Disable allow placement of caret after end of line
To disable the placement of the caret after the end of a line do the following:
  1. Open the settings window
  2. Click Editor and uncheck Allow placement of caret after end of line.
Use and show soft wraps
Soft wraps are useful when the editor window is smaller than the actual line length that can be displayed. The lines that exceed the editor window wraps to the following line.
  1. Open the settings window
  2. Click Editor and check use soft wraps in editor and Show all soft wraps.
Tab limit
The tab limit limits the maximum open tabs so the tabs remain manageable. To limit the maximum open tabs:
  1. Open the settings window
  2. Click Editor -> Editor Tabs and specify a tab limit.
Optimize imports on the fly
This option automatically optimizes the imports when you are typing code. To enable this option:
  1. Open the settings window
  2. Click Editor -> Auto Import and check Optimize imports on the fly
The results of the above modifications can be seen in the screenshot below. I made the editor window smaller than usual to demonstrate the soft wraps.


In the project window I make the following modifications:

Autoscroll to source
This option lets me autoscroll to the editor window when a file is clicked.

Flatten packages
Abbreviate qualified package names
The above two option give a nice compact but complete overview of the package structure with abbreviated names. This can be seen in the following screenshot:



Let me know which options you use.

Sunday, August 5, 2012

IntelliJ: Final parameters and variables


Different teams require different coding styles and source code metrics. We have for example a rule which states that all method parameters should be final. Without going to discuss this rule (since your mileage may vary) I will show how IntelliJ can be used to enforce this rule.

There are two use cases here:
  1. Adding the final keyword to all methods generated with IntelliJ.
  2. Add the final keyword to existing methods.

To add the final keyword to parameters of generated methods do the following:
  1. Open up the Project Settings dialog and navigate to Code Style -> Java.
  2. Click the Code Generation tab.
  3. In here you will find the Final modifier section.
  4. Check the "Make generated parameters final" option.

If you now generate methods, IntelliJ will insert the final keyword before every parameter.

Besides adding the final keyword to every parameter in generated methods you can also add the final keyword to parameters in existing methods. To do this, do the following:
  1. Open up the settings dialog and click Inspections (in Project settings)
  2. Click copy to copy the Project default inspections and name the profile anything you want.
  3. Click the Rest to Empty button to disable all inspections. Disabling all inspections is a quick way to only focus on a specific inspection if you want to apply just that specific inspection.
  4. Enter 'final' in the search box
  5. Under the "Code style issues" check the following rule: "Local variable or parameter can be final".
  6. On the left make sure the option "Report method parameters" and/or "Report Local Variables".
  7. Click OK.
  8. Click Analyse -> Inspect code in the menu.
  9. Make sure you select the Whole project and select the profile created in step 2.
  10. Click OK.
  11. In the inspection dialog you see all methods where the parameter could be final.
  12. Right click the inspection and click "Apply fix 'Accept suggested final modifier'" to add the final modifier to all method parameters where it can be final.

Please note that the inspection does not add the final keyword to parameters which are reassigned since this gives compile errors. You could argue if this should be included in the inspection as well. I personally feel this can be handy to add final to all parameters regardless if they are changed or not. You can then afterwards fix all compile time issues you may have.

Monday, July 30, 2012

IntelliJ productivity tips


This is the first post in a series of many where I demonstrate a particular feature of IntelliJ. I use IntelliJ on a daily basis and knowing when to use particular features really helps improving productivity. Especially if you just start using IntelliJ you barely scratch the surface of what is possible. These series of posts help to get the most out of the IDE.

An often overlooked but powerful feature in IntelliJ is the structural search and replace. Suppose that there are lots of new instances of a particular class created. Sometimes it is better to use a static factory method instead.

For example: a rather large codebase I was working on had lots of new Integer() statements:

public class SomeClass {
            public static final Integer VARIABLE_1 = new Integer(10);
            public static final Integer VARIABLE_2 = new Integer(20);
            public static final Integer VARIABLE_3 = new Integer(30);
}

If the codebase has 10's, 100's or even 1000's of such statements, you can replace all of them with one single search and replace statement using structural search and replace. In short: with structural search and replace you can search/replace your entire codebase with knowledge about the code.

The above new instance creation can be replaced by doing the following:

  1. Open up the Structural Replace dialog by pressing Ctrl - Shift - M (on Windows) or Command - Shift - M (on OSX)
  2. In the Search template dialog enter the following: new Integer($arguments$)
  3. In the Replacement template enter the following: Integer.valueOf($arguments$)
  4. Make sure you specify java as file type
  5. In the Edit Variables dialog set the minimum and maximum count of the $arguments$ parameter to 1.
  6. The above search template finds all occurrences of new Integer() with exactly one argument and replaces it with Integer.valueOf() taking into account the arguments.
  7. Click find.
  8. In the Find dialog you see all occurrences of new Integer($arguments$) that were found.
  9. Click Do Replace all to replace all new Integer() to Integer.valueOf().
For an in-depth explanation of structural search and replace see http://www.jetbrains.com/idea/documentation/ssr.html.

I hope you enjoy this series. If you have other useful tips please share them!

Tuesday, May 1, 2012

Increasing user expectations


Users are getting increasingly more demanding about the services and functionality that (online) organizations have to offer. This is especially true for organizations where users arrange most or all of their services online. Some examples are Internet banking, online insurance and travel agencies.

Users expect this functionality:

  • To be easy to use
  • That new features are added continuously and provide real value
  • Will work on their mobile devices like phones and tablets
  • Is tailored to their personal needs

This functionality is most of the time designed within the company itself and eventually, when developed and tested, deployed in production. In this model, the company expects that the functionality will meet the needs of most or all users. I believe this model is not sufficient anymore in the ever-increasing expectations of those users.

One of the models to cope with this is to let users/developers themselves build the functionality they need. We also see a lot of this on the Internet where users build their own application/gadgets by using provided API’s. A prime example of this is of course Facebook.

What if we extend this model to the enterprise? What if, for example, Internet Banking companies expose (some) of their functionality through API’s and let users develop functionality they really need? What kind of applications would arise out of this?

I do not think this is a question of how but when. As some or most of you already know, Google owns a banking license. What does it mean for regular banks if Google or perhaps Facebook adopts such kind of platform for banking and/or insurance products? I think a whole new generation of users will make the transition to those companies. This indeed may have a large impact on the customer base of traditional organizations.

Before organizations are ready to make such transitions, a large number of questions must be addressed:

  • How do we secure access to API’s and data?
  • Which data do we want to expose?
  • Why would developers want to build applications for us? How do we compensate developers who are building great applications for us?
  • What about legal issues?
  • What technology do we use?
  • How do we change the mindset of our own people?
  • Etc.

And the most important question: when are organizations ready to adopt this model and radically change the way applications are delivered to end-users? Of course this change does not have to be a big bang but can and should be made incrementally.

From a technology point of view, one of the ways to implement such eco-system is by adopting the Open Social specification. The Open Social specification defines:

  • A social data model, which provides a standard representation of people their relationships and activities also known as a connected graph of people.
  • A set of standard API’s to access people, their relationships and activities available in REST and JavaScript API’s. Besides the JavaScript API for web applications, the REST API is particular interesting as enabler of mobile applications.
  • A standard to develop gadgets that encapsulate reusable application logic.

As an example, iGoogle is one of the platforms based on Open Social (in fact, Google donated their iGadgets container to the community which is Apache Shindig). IBM and Atlassian are also incorporating Open Social technology in their own products.

Conclusion


Since users become more and more demanding about the services and functionality that (online) organizations offer, organizations need to find other ways to provide that functionality instead of the traditional specification-build-deploy cycle. A possible way to do this is to let users themselves build the functionality they need. Open Social could be the technological enabler to facilitate this.

However, the ultimate question remains: if and when are organizations ready to adopt a radical new strategy to provide functionality to its end-users.

What is your opinion about this?

Tuesday, April 10, 2012

Flex: unittest a view with radio buttons

At a recent project where I do some coaching, we wanted to unit test the validation logic present in view components. The validation logic is implemented as an array of validators which are triggered after some specific events take place. To test this we basically did the following:

  1. Created a unit test (we are using FlexUnit)
  2. Instantiate and initialize the view in the unit test
  3. Populate the UI components with values to test
  4. Trigger the validators
All seems well until we wanted to populate the selectedValue property of a RadioButtonGroup. This did not seem to work from within our unit tests. According to the documentation the selectedValue property can both be set and read from within code. Testing this logic inside a standalone MXML component and setting the selectedValue property did work. Unfortunately this did not help in our unit test because there it did not work.

Looking at the source code of the RadioButtonGroup I noticed that when setting the selectedValue property, the code loops over the associated RadioButtons. In our unit test the associated RadioButtons are not set (I expected that they were set actually). After digging some further I noticed the addInstance(instance:RadioButton):void method. This method apparently adds a RadioButton to the RadioButtonGroup. But where is it called? Somewhere by the Flex framework but where exactly was not obvious. Notice that this method is declared in the mx_internal namespace which means that this functionally may change in a future release. It also means however that we can still call this code ourselves.

To fix our test, the mx_internal namespace is added so the addInstance method can be called from within the test. With the addInstance method all RadioButtons which belong to the RadioButtonGroup can be added to the group. See the example below.

   1:  <?xml version="1.0"?>
   2:  <mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml">
   3:      <mx:RadioButtonGroup id="genderRadioButtonGroup"/>
   4:   
   5:      <mx:RadioButton id="male" groupName="genderRadioButtonGroup" label="male"></mx:RadioButton>
   6:      <mx:RadioButton id="female" groupName="genderRadioButtonGroup" label="female"></mx:RadioButton>
   7:  </mx:VBox>

And the code of the unit test:
   1:  package {
   2:  import flexunit.framework.TestCase;
   3:   
   4:  import mx.core.mx_internal;
   5:   
   6:  use namespace mx_internal;
   7:   
   8:  public class RadioButtonTest extends TestCase {
   9:      public function RadioButtonTest() {
  10:      }
  11:   
  12:      public function testPopulateRadioButton():void {
  13:   
  14:          var myForm:MyForm = new MyForm();
  15:          // Initialize the UI component so that all child components are added to the form.
  16:          myForm.initialize();
  17:   
  18:          // Explicitly add the radiobuttons to the group, please note the use namespace mx_internal or else this
  19:          // method can not be called.
  20:          myForm.genderRadioButtonGroup.addInstance(myForm.male);
  21:          myForm.genderRadioButtonGroup.addInstance(myForm.female);
  22:   
  23:          myForm.genderRadioButtonGroup.selectedValue = "male";
  24:   
  25:          assertEquals("male", myForm.genderRadioButtonGroup.selectedValue);
  26:      }
  27:  }
  28:  }

Conclusion
Using the mx_internal namespace is not encouraged but sometimes can help to work around certain limitations, especially in test code. In the case of the RadioButtonGroup I would assumed things worked differently but unfortunately they did not. I doubt that this will be fixed in a future release regarding the state of Flex at the moment.

If you have another solution to the above problem please share it!

Thursday, March 29, 2012

Continuously looping video in WPF

At the moment I am building a Kinect based application using the Windows Presentation Foundation framework for a client. More details on this application later.

In that application we needed to show a continuously looping video. After looking through some documentation I used the following XAML construct to play and loop the video continuously:

   1:  <MediaElement LoadedBehavior="Play">
   2:      <MediaElement.Triggers>
   3:          <EventTrigger RoutedEvent="MediaElement.Loaded">
   4:              <EventTrigger.Actions>
   5:                  <BeginStoryboard>
   6:                      <Storyboard>
   7:                          <MediaTimeline Source="Instructions.avi" RepeatBehavior="Forever" Storyboard.TargetName="video" />
   8:                      </Storyboard>
   9:                  </BeginStoryboard>
  10:              </EventTrigger.Actions>
  11:          </EventTrigger>
  12:      </MediaElement.Triggers>
  13:  </MediaElement>

Unfortunately this solution is not reliable. After some time it can took a while before the video starts playing again after it has ended. Sometimes the video stops playing completely. Some searching revealed that the MediaElement indeed is not reliable enough. Some alternatives where suggested, for example http://wpfmediakit.codeplex.com/.

The mediakit can indeed be a solution but there is a simpler way without adding a dependency on a third party framework. Instead of using a StoryBoard and a MediaTimeLine you can also manage the repetition yourself. The following code demonstrates this.

   1:  <MediaElement x:Name="instructionVideo" Width="320" Height="240" Source="Videos/Instructions.mp4" LoadedBehavior="Manual" Loaded="instructionVideo_Loaded" MediaEnded="instructionVideo_MediaEnded" VerticalAlignment="Bottom" HorizontalAlignment="Center" Canvas.Top="40"/>

The above XAML fragment is accompanied by the following code-behind:

   1:   private void instructionVideo_Loaded(object sender, RoutedEventArgs e)
   2:   {
   3:       instructionVideo.Play();
   4:   }
   5:   
   6:   private void instructionVideo_MediaEnded(object sender, RoutedEventArgs e)
   7:   {
   8:       instructionVideo.Position = TimeSpan.FromSeconds(0);
   9:   }

Instead of letting WPF manage the video, we specify a LoadedBehavior of Manual which means we can manage the starting and stopping of the video ourselves. To actually start the video the Loaded event handler is used which calls the Play method on the instructionVideo element. When the video reaches the end, the MediaEnded event is dispatched. In this event the Position is set back to 0 so the video starts playing from the beginning again. This proved to be a reliable way of continuously looping videos.

PS. the video in questions is in mp4 format and about 20 seconds in length which must loop all day long.

Monday, January 23, 2012

Monitoring an IBM JVM with VisualVM


JDK6 update 7 and onward include a tool called VisualVM. VisualVM is a visual tool with monitoring and profiling capabilities for the JVM. With VisualVM you can:
  1. Monitor heap usage
  2. Monitor CPU usage
  3. Monitor Threads
  4. Initiate garbage collections
  5. Profile CPU and memory
  6. And more…

Although VisualVM is distributed with the Oracle JDK, it can also be used to monitor IBM JVM’s. VisualVM is not able to connect to the IBM JVM locally. JMX must be used instead.

To enable JMX monitoring on the IBM JVM open the WebSphere administrative console and:
  1. navigate to: Server -> Server Types -> WebSphere application servers ->[SERVER_NAME]
  2. Expand Java and Process Management and click Process definition
  3. Click Java Virtual Machine
  4. In the Generic JVM arguments field append the following properties: -Djavax.management.builder.initial= -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=1099
  5. Restart the server


To start monitoring the JVM with VisualVM start VisualVM by navigating to [JDK_HOME]\bin and start jvisualvm.exe (please note that when VisualVM is downloaded as a separate package the executable is called visualvm.exe instead). In VisualVM click File -> Add JMX Connection. Specify localhost:1099 in the connection field and click OK.

If everything went OK, you should see the localhost:1099 connection under the Local node in the tree on the left. Double-click this node to start monitoring. See the following screenshot for an example:



When using a JMX connection to monitor the JVM please be aware that not all functionality can be used compared to monitoring a local JVM. Profiling memory is for example not possible.

The above configuration was tested on:
  • Windows 7 64-bit
  • IBM JDK1.6 64 bit
  • WebSphere Application Server version 7.0

Note: Before JDK6 update 7, VisualVM can also be downloaded separately from http://visualvm.java.net/download.html
Note: To actually test if port 1099 is listening for connections use (on Windows) the netstat –a command and check wether the port is present and listening.