Monday, December 01, 2008

Fluent Process API

A recent addition to Drools Flow allows users to create processes using what could be called a "fluent API". While it has always been possible to create processes using our graphical editor or the underlying XML, we do acknowledge that some people simply prefer creating process definitions using an API. The underlying example shows how easy it is now to create a simple "Hello World" example process using this API:

RuleFlowProcessFactory factory =
RuleFlowProcessFactory.createProcess("org.drools.process");
factory
// header
.name("My process").packageName("org.drools")
// nodes
.startNode(1).name("Start").done()
.actionNode(2).name("Action")
.action("java", "System.out.println(\"Hello World\");").done()
.endNode(3).name("End").done()
// connections
.connection(1, 2)
.connection(2, 3);
RuleFlowProcess process = factory.validate().getProcess();

Many thanks go out to salaboy for doing a large part of the work and adding the different factories to create the various types of nodes supported in RuleFlow. This also shows how easy it is to contribute to the Drools project if you want to: come join us at our #drools irc channel, describe what you want to do and we'll help you as much as possible and welcome contributions !

Kris

2 comments:

  1. I wouldn't call this api fluent just because it's chainable. A fluent api should read more like english.

    I believe a "connectedTo" operation to connect nodes during creation instead of an explicit connection(id,id) would help a lot. In the example it could/should replace done() - nodes with multiples in/out are probably a problem.

    ReplyDelete
  2. True, we didn't really try hard to make readable English sentences. But this api is still targeted towards normal java developers, so then the question would be: what would the people who use this api prefer?

    Regarding the connections, we tried to find a simple solution that supports all use cases. But the good thing is we could support multiple ways. Could you give a more concrete example on how you would like it to work?

    ReplyDelete