sábado, 4 de abril de 2015

Creating custom Work Item Handler in BPM Suite/jBPM 6

Hi all! In this post I am going to share my experience creating a work item handler for BPM Suite 6.0.3 (which is very similar to jbpm as explained in a previous post).

The Hello World Work Item Handler

Let's create a really simple WorkItemHandler just to print "Hello World" in the console. Of course you can implement more feature and make it receive parameters, access database, web services, etc. But in this post we will keep it simple. Really simple.

First thing to do is to create a maven project for my WorkItemHandler. Here's the pom.xml of my Maven project:

Notice that we are importing jbpm dependency so we can find the java interface that is used to create work item handlers.

Now we can start coding. Let's create a class name HelloWorkItemHandler in package org.jugvale.jbpm, here is the simple code:

Now we can build our project and go to BPM Suite to configure it so we can use this workitem.

Registering the WorkItemHandler

The first thing to do is to make our artifact visible to BPM Suite. We can either import it in the maven used by BPM Suite or we can upload the JAR using the web application. In any case, the common step is to build our project.

After building it, decide if you want to install it in your maven repository so you can use it in the BPM Suite project as a dependency or you can upload the JAR to BPM Suite and make it a dependency of the BPM Suite project. That's what we are going to do.
  • So, after starting BPM Suite, and logging into business central, we can go to Authoring -> Artifact Repository  and upload the JAR with our Work Item Handler:

Now we create a simple project in BPM Suite. You can do this by going to Authoring -> Project Authoring and then New Item -> Project. Notice that I am considering that you have already a repository created.

Now that you have your project created, let's add the artifact we uploaded as a dependency of it. To do this, select the project and go to Tools -> Project Editor and on the Project Settings drop down menu, select Dependencies. Now you can specify the uploaded artifact as a dependency of your project using the Add from repository button and choose our artifact from the list that will show up and then we are done with this part. Don't forget the save the changes.

Remember that WorkItemHandlers are registered in the file META-INF/kmodule.xml of your project. However, we can do this visually! Go to Project Editor  and from the Project Settings dropdown button, choose Knowledge base and sessions. Add a new knowledge base, make it default and add a new ksession, make it default, edit it and add the work item handler. Here are some useful screenshots:

 Adding the kbase and the ksession:

Editing the ksession to add the work item handler:

Save it and now the project is already configured with a kbase and a ksession with our work item handler! See how our kmodule.xml looks like:

We are almost done.  Let's create a process to test the WorkItemHandler we created, use the menu New Item -> Business Process and do nothing for now, just close the process editor.Now let's simple edit the file WorkDefinitions.wid of our project to include metadata about the WorkItemHandler. Here we can set the icon and the name that will appears in the process editor. We simple only set a display name:

   "name" : "HelloWorkItemHandler",
    "displayName" : "Hello World!",
    "icon" : "defaultemailicon.gif"

Save the file, create a new process and MAGIC! Now you can use your custom work item handler and it will be appear in the process editor library under the Service Tasks pane. See my sample process:

If you build, deploy the project and run the process above, you see the "Hello World!" message in the logs:

00:25:16,904 INFO  [stdout] (http-localhost.localdomain/ Hello World!

That's our Hello World Work Item Handler! Of course you can also declare parameters for the Work Item and do more complex operations, such as access a Web Service, send email, send SMS, update a database....

Download the sources on github

domingo, 1 de fevereiro de 2015

Porting the StackerJavaFX game to Android

For too long I want to port a JavaFX application of mine to Android using JavaFX Ports. The day has come! In this post I will share with you my experiences porting the Stacker Game to Android. Actually at the time I write this sentence I just created a new project on Netbeans and it might even not work, I can even abandon this post, because at the moment I have 0% of the port done! UPDATE: It worked and was really easy!

From Java 8 to Java 7

Android doesn't support Java 8. We have projects to use Lambdas in Java 7, but I wanted to make it simple as possible to avoid any incompatibility, hence I decided to keep it only with Java 7 and JavaFX 2.2.
The saddest thing was to remove my beautiful lambdas to use the old anonymous classes from the code. Also, I noticed that JavaFX 8 is easier than JavaFX 2.2. The reason is that we have more constants and constructors in JavaFX 8 that we won't find in the 2.2 version. There were simple details to change, but Netbeans helped me, what I did was:

* Create a new JavaFX prject in Netbeans, but this time using Java 7;
* Copied the source from the JavaFX 8 project and pasted into the Java 7 project;
* Corrected the errors pointed by Netbeans.

I finished this in about 25 minutes. The main problem is that I ran into this Netbeans bug, but quickly solved by running it with Java 8, but compiling with Java 7.
I could continue programming in Netbeans, but as this was a first test, I deleted netbeans related files and created a gradle project.

First Port to Android

I carefully followed the instruction from the JavaFX ports site using the new build system, but I faced an issue with "Where Should I put my JAR?". This thread answered my question. The I noticed that I had to configure my project structure as a Gradle project. It worked very well!

After you configure your gradle application and correctly install Android tools, you just need to run too commands to have your application running in your phone:

$ gradle build
$ gradle installDebug

The harder part was to configure my phone to run in developer mode and then use the adb tool to configure the USB access.


I was really easy to port the JavaFX application to my Android phone and I am still impressed with with the performance while the app was running on the phone. Of course I would have to make a few adjustments if I want the app to be widely available, such as the size when the screen rotates...

* The source of the application ready to run on Android can be found on my github;
* A direct link to the APK can be found here.

sexta-feira, 23 de janeiro de 2015

Exporting JavaFX application as an animated GIF

To try to export an animation from a JavaFX app to an animated GIF, I started on this thread in StackOverflow, then this class from Eliot Kroo, which I am sharing on gist in case his site goes down.

Right now I am going to try his class on a JavaFX application and I will share the results here.

A sample animation

This is the animation I generated using my code :

The source for this animation is here:

Based on Eliot's class, I created one which will take an amount of screenshots based on the given period and write an animated GIF on the given path(well, right now I didn't create the source yet, hope I will be able to make it work):

This code have no great performance since the snapshot method is not "performatic". If we set a small period to collect the GIF's frames, we will face slowness.
That's all, folks! Now you can use the above code in your application to export GIT animations from your JavaFX applications!

quarta-feira, 21 de janeiro de 2015

Stacker Game in JavaFX

In this post I am going to share with you this simple game I created using JavaFX.

The Stacker game

The goal of the game is to reach the top by stacking rectangles. This is a well known game where when you reach the top you earn some gifts. Here is a video I took from youtube of someone playing this game:

Our game is a little different. We will will only stack 1 rectangle, but the rectangle can be adjacent of the below rect, not exactly on top of it. The number of rectangles to fill will be increasing according to the level and also the speed.

The game in Processing

The game was previously created in Processing, but most of the code I could reuse in the JavaFX version! You can find the source for the processing version in my github.


Creating games with JavaFX

JavaFX was always a platform for games since its 1.0 version. I remember we even had a blog specific for JavaFX script games. Since the end of JavaFX Script and with JavaFX 2, we have some samples that might be downloaded in Oracle's site.
To get start with games on JavaFX I recommend this series of posts by Carl Dea.
JavaFX have all resources we need to create games: animations, resources to detect collision, special effects, play sounds, etc...

Our stacker game implementation

The game is basically based on update a boolean matrix and read it to create a visual representation. When the user click on the game, we make a loop in the matrix to see if the block in the previous line  is true or if the block in the current line is adjacent to the block in the previous line.
The two important classes in our program is the Game Engine and the game itself. The game engine is responsible to make the game alive, because it will make the game update the screen on a given frequency and make it draw itself.
The game is responsible to draw and update the scenario according to time and user's input. Here is the code of these two classes:

There are classes that does nothing. We need to create a concrete class, so we did, we created the StackerGame  class. It contains the matrix that represents the game and will draw the grid and the rectangles according to the time, level and user's input.
The logic of the game is basically move a boolean in the matrix and fix it when the user clicks. If the user click was done when the rectangle had a block below it or adjacent, the game will continue, if not, we will call game over and when the user click again, the game will start from beginning. Note that the score is higher when the user makes a perfect stack and lower when the user stack using an adjacent block.  The game is drawn in a canvas, which is easier to control in our case(we are coming from Processing!).
Notice that we expose some variable using javaFX properties. These properties are used in our main application to display the score, level and eventually the game over label. See the all the code of these two classes:

Here is the game in action:

Possible Improvements

Unfortunately the game has no sounds and the effects are poor. Also, if the user reach the level 20, the game will bug! So, the possible improvements are:

* Improve the look of the game;
* Add sounds;
* Handle the end of the game event.


It was a simple demonstration of a game using JavaFX. As usual, it was done for fun, in a few hours... Don't judge possible bad code... The source is in my github.

JavaFX is fun, is enterprise, is good for game programming, for data visualizations, for learning Java...