Wednesday, October 22, 2014

scala & drools : project skelon updated

Scala & drools project skeleton updated with latest release of Drools (6.1), more features added (logging, test cases wired, drl errors catched up), and rules clean up.
Some advices coming from experiences with big scala drools projects (hundreds of rules) :
  • Scala case classes are perfect for your rules
  • Use only java collections within the classes used by your rules. Avoid the scala collections in that precise case but rely on collection.JavaConversions._ implicits to hide that restriction.
  • In knowledge bases only use declarative classes (declare) for internal usage, such as intermediary reasoning state.
  • To change the java compiler used by drools, use the following system property : "". It will replace ECJ by Apache Janino, but you will have to provide the dependency yourself : "org.codehaus.janino" % "janino" % "2.5.16" (Use only this release, at least up to Drools 6.1)

Sunday, July 13, 2014

Using Stream to simplify execution alternatives...

First thing to remember before using Stream to simplify execution alternatives : Stream(...) will evaluate all its arguments before the stream is created, but if you use the #:: notation, only the first one will be evaluated at stream creation.
Now using this to simplify conditions : So everything is working fine, and "oups" was not printed on the console.
But one of the best usage I found of this pattern, is when we want to get some information that may be available from several sources : This time we have a piece of code that is very easy to read !
Try to do the same job using only traditional "if-then-else" blocks, or pattern matching.

Traditional approach examples : Using if block suffer from a bug, the first fromX function returning a non empty result will be evaluated twice (we should have use a temporary value to keep the result of from evaluation). The pattern matching variant is very difficult to read.

Sunday, April 27, 2014

java.library.path change at runtime, using scala

Based on this article "Changing Java Library Path at Runtime" on, here is the scala release : And a small function to take a resource and save it to the chosen destination (based on Scala IO) :

Sunday, March 2, 2014

Akka actors based, generic primes calculation parallel algorithm

Actors programming is an interesting solution to take benefits of all your server CPU core to compute primes number while still being able to compute their order positions.

Here the principle is to create an actor router, which will load balance primes computation, CheckerActors. The load balancing is based on the size of each load balanced actors, a new task will be given to the actor with the lowest mailbox size.

All checked values are then sent to a single actor which will reorder received values and attribute them their respective positions. This actor, the ValuesManagerActor, is also responsible of managing the load, and check that final destination of all results can manage the result flow.

Here all results are sent to PrinterActor, this actor output results on the standard ouput, if you try here to print all results this actor may not as fast as the received result flow, so its mailbox will grow, up to the jvm maximum heap size thus generating OutOfMemory exception. That's why here I've added an acknowledge mechanism that verify we do not have too many results sent and not taken into account. If we have too many results not taken into account then ValuesManagerActor suspend the load balancing to CheckerActors.

My primes project at github for experiments and tests.

How long time to get the 500000th prime (sbt console) (first results, jvm, Intel Core i7 2.3 Ghz, 4 CPUs) :

Algo Duration Comments
Classic61s pgen.primes.take(500000).last
Parallel22s pgen.primesPar.take(500000).last
Actor (ack on each response)38s with fork-join-executor
Actor (ack every 5000 responses)35s with fork-join-executor
Actor (ack on each response)33s with thread-pool-executor
Actor (ack every 5000 responses)30s with thread-pool-executor

Actors based algorithm is quite faster than a classic sequential one, but not as fast as using a parallel collection in order to parallelize the classic algorithm.

Although my implementation is not as fast as the parallel solution (I'll have to investigate that point), it has a major benefits, it can be distributed across several computers. It'll be the subject of a new article.

Saturday, February 1, 2014

Generic primes generator

An example of how to implement a generic primes generator that you can use with any numerical type. Of course, performances will be impacted by your choice :

  • howlongfor(intPrimes.drop(25000).head) // res0: (String, Int) = (459ms,287137)
  • howlongfor(longPrimes.drop(25000).head) // res1: (String, Long) = (1349ms,287137)
  • howlongfor(bigIntPrimes.drop(25000).head) // res2: (String, BigInt) = (4361ms,287137)

My primes project can be found here.

Simplified source code