Saturday, April 23, 2011

Try a Drools example in less than a minute

Now that Drools 5.2.0.M2 is released, it's very easy to try an example yourself:
  1. Download a download zip (this may take more than a minute):
    1. Drools
    2. Drools & jBPM integration
    3. Drools Planner
  2. Unzip it
  3. Double click on the runExamples script:
    1. Linux/Mac: /examples/runExamples.sh
    2. Windows: /examples/runExamples.bat

And you'll see one of these 3 windows:
Just chose on an example to run it.

Release 5.2.0.M2 of Drools (Expert, Fusion, Planner) and Guvnor

We're happy to announce the release of drools 5.2 Milestone 2.
Read the new and noteworthy changes for this milestone:
Release CR1 will follow very soon.

Friday, April 22, 2011

Drools Backward Chaining Unification Syntax Discussion - Rebellion in the ranks

We are having some lively debate on the unification syntax for Drools backward chaining. I want to make sure we get the widest possible input on this, so blogging it some more.

At the simplest level people have seen this:
Location(t : thing, l : location)
Edible(t : thing)
And said why do this, if the behaviour is the same as '==' (see below), why introduce something new.
Location(t : thing, l : location)
Edible(thing == t)
Lets move from ground facts to a query, defined as:
query niceFood( String t, String l )
Location(t : thing, l : location)
Edible(t : thing)
end
Query parameter behaviour on whether the parameter is an in our an out variable is defined by the caller, not the defintition itself. That means we have no idea of the behaviour for t or l at compile time, i.e. in or out, bound or unbound. So using '==' (see below) for those patterns wouldn't make sense if the caller left t or l unbound, especially so if that query called another query passing that unbound variable down. You would in essence be using '==' to pass down an unbound value to a nested query.
query niceFood( String t, String l )
Location( thing == t, location == l)
Edible( thing == t )
end
For this reason when dealing with queries we need to think in terms of unification across variables, which is bi-directional, and not filters on a field value. Remember as well that the unification process can span to nested patterns and queries.
query niceFood( String t, String l )
Location( t : thing, l : location)
Edible( t : thing )
end
Here is a recursive query, where a query calls itself. Again at compile time we have no way of knowing whether x or y is an in or an out var, that all depends on how the query is called at runtime. So we use the unification binding symbol, instead of the '==' symbol.
query isContainedIn( String x, String y )
Location( x : thing, y : location)
or
( Location(z : thing, location y) and ?isContainedIn( x : x, z : y) )
end
The unification symbol in essence means the value, once bound, will be the same across all locations.

If it's still not clear what unification is, please read Adventures in Prolog. http://www.amzi.com/AdventureInProlog/a1start.php especially http://www.amzi.com/AdventureInProlog/a5rules.php. There is a wealth of explanations on how a prolog engine works for unification on the web. You could also try the Mandarax manual that also executes derivation queries with Java objects.

The main complaint with this is that it introduces potential spelling mistakes. If I unify in 3 places for a rule and one of those misses a letter, the rule will not execute correctly. Prolog provides warnings when a unification only appears once, which catches the most trivial cases, but not all. The reverse is also true that someone might use a variable name for a binding not realising it was already a binding, again with the rule not executing as expected.

From the one or two emails I've had it's been suggested the sky will fall in and we'll all be going to hell in a hand basket.

So I'd please invite you all to discuss alternatives. Don't just email me telling me that if I do this the world is gonna end and I've destroyed Drools and everyone's reputation, that doesn't help and it makes the debate emotional and awkward. Work with me through alternative suggestions based on levels of merit, discussing the pros and cons. Make sure you actually understand what unification is, so that you extrapalate through any suggestions. Another aspect to consider when proposing any alternatives is that we need to keep things regular across the positional syntax too.
  • Regular.
  • Introduces no new syntax, and thus no new symbols.
  • Natural and logical extended behaviour to our existing binding syntax and behaviour.
  • Backwards compatible.
  • People can ignore it if they wish, they never need know it exists.
    • Caveat it does introduce the spelling mistake issue to them.
I'd be very interested to see if there have ever been any proposals in the prolog world to address this.

As a way to get people's mind thinking, lets work through one possible suggestion:

As alternatives we could leave ':' for it's traditional role as a single binding and instead introduce a new symbol purely for unification. POSL uses '->' for slot fillers so lets use that. http://ruleml.org/submission/ruleml-shortation.html
query niceFood( String t, String l )
Location( thing -> t, location -> l)
Edible( thing -> t)
end
This however doesn't solve the problem, you can still get spelling mistakes, the only difference is the problem has shifted to only those using unification and not those using normal ':' bindings and constraints. In essence we've introduced a new symbol, not to remove the problem, but shift onto other people. This does nothing for positional though, where the problem will remain.

We could introduce a "var" prefix, that says if the identifier is not defined in the query parameters it must have a "var" prefix before it the first time it is unified against. That would ensure that no spelling mistakes occur. I'll introduce a new query for this, where "location" is determine by the Here fact. Now location is no longer query argument, but determined by unifying against Here. As 'l' was not declared in th query parameters we had to prefix it with 'var', so that later uses can unify against it.
query niceFood( String t, String l )
Location( thing -> t, location -> l)
Edible( thing -> t)
end

query getCurrentFood(String food)
Here(var location -> l)
?niceFood( t -> food, location -> l)
end
This solution seems to solve the problem, but we have introduced a new symbol and a new prefix. Also does this work for positionl? lets try:
query niceFood( String t, String l )
Location( t, l)
Edible( t)
end

query getCurrentFood(String food)
Here( var l; )
?niceFood( food, l; )
end
I believe it does, although we'll need to check that the parser can handle "var l" for positional without creating ambiguity. There is the issue though that it is possible to unify on patterns themselves:
var p <- Person()
p <- Person()

So the <- would need to work in place of : on the pattern bindings, and 'var' would also be needed. I reversed it to show that Person is unifying into p. Another aspect to consider is that a unification variable, once bound can be treated like a normal field constraint, which may cause confusion as effectively you have two ways of defining variables. This means people can use -> as a complete replacement, depending on their personal preferences - that may not be a good idea....
  • Regular.
  • Introduces new syntax and new symbols.
  • Existing ':' is preserved, at the expense of introducing new concepts to learn.
  • Backwards compatible
  • People can ignore it if they wish, they never need know it exists.
  • People may end up using -> as a replacement for : for simple cases, confusing people.

What do people think about this as an alternative? Another suggestion has been to add "out" as a prefix on a query parameter, to enforce that a parameter can only be used for passing stuff out, and not bi-directional. Please keep the alternative suggestions coming, but remember there are no points for being dramatic.

Monday, April 18, 2011

Backward Chaining emerges in Drools

We have a first cut of Prolog style derivation queries working. We have adopted the RuleML's POSL (Positional Slotted Language) approach to mixing positional terms and OO named arg arguments. This allows you to work fully in OO named arguments or fully in prolog positional terms or mix in between. The best of both worlds :) Note again this all works directly of java Pojos. We introduce an @Position annotation for fields, to allow positional mapping in rules.

Just to explain positional arguments are ones where you don't need to specify the field name, as the position maps to a known named field. i.e. Person( name == "mark" ) can be rewritten as Person( "mark"; ). The ; is important so that we know when that everything before it is a positional argument. Otherwise we might assume it was a boolean expression, which is how it could be interpretted after the ;

If I had the following query:
query niceFood( String thing, String location )
Location(thing, location) // Location and Edible are ground facts
Edible(thing)
end
I could invoke it in the following ways:

//$food is positional output variable and "kitchen" an input literal
?niceFood($food, "kitchen";)

// both are output variables
?niceFood($food, $place;)

// Named arguments. Argument food is bound to output variable $food.
// Argument place is bound to input literal "kitchen"
?niceFood($food : food, place : "kitchen" )

// Mixed, first argument is positional, second argument is named
?niceFood($food; place : "kitchen" )


The ? symbpol is necessary to tell Drools that is a pull based query. This will also (TBD) allow you to pull against ground fact patterns, instead of always being reactive to their changes. Drools is a reactive engine and data can be pushed (reactive) or pulled (queried). We don't yet support it but eventually we will allow "open" derivation queries, where if you do not include the ?, it will continue to propagate new results as they appear in underlying query results.

A variable is considered an output variable if it has not been previous declared.

So let's look at what Nani' Search looks like in Drools. Nani Search is an example adventure program from "Adventure's in Prolog". Here is a sample of queries and rules from the example. I've added a reactive rule to output what can be seen when Here is updated.
query niceFood( String thing, String location ) 
Location(thing, location)
Edible(thing)
end

query connect( String x, String y )
Door(x, y;)
or
Door(y, x;)
end

query look(String place, List things, List food, List exits)
things : List() from accumulate( Location(thing, place;) ,
collectList( thing ) )
food : List() from accumulate( ?niceFood(thing, place;) ,
collectList( thing ) )
exits : List() from accumulate( ?connect(place, exit;) ,
collectList( exit ) )
end

rule reactiveLook when
Here( place : location)
?look(place, things, food, exits;)
then
System.out.println( " You are in the " + place );
System.out.println( " You can see " + things );
System.out.println( " You can eat" + food );
System.out.println( " You can go to " + exits );
end
This gives the output:
-----Output----
You are in the kitchen
You can see [crackers, broccoli, apple]
You can eat[crackers, apple]
You can go to [[kitchen, dining room], [kitchen, cellar]]


Recursion is an important part of derivation queries and allows for search of trees:
query isContainedIn( String x, String y ) 
Location(x, y;)
or
( Location(z, y;) and ?isContainedIn(x, z;) )
end

If you insert the following objects, you can determine that the key is located in the envelope, which is located on the desk and the desk is located in the office.
    insert( new Location("apple", "kitchen") );
insert( new Location("desk", "office") );
insert( new Location("flashlight", "desk") );
insert( new Location("envelope", "desk") );
insert( new Location("key", "envelope") );

There is still lots more to do. The basics are now in place and usable and we'llpublish missing features in the mailing list soon. Note that this is not opportunistic backward chaining, which is still planned. You can think of opportunistic BC as lazy object creation or field initialisation.

Friday, April 15, 2011

First look - Drools Planner

James Taylor did a first look on Drools Planner: read his blog post.

Thursday, April 14, 2011

BPMN2 Visual Editor M3 release

We at Red Hat has been working on the next generation Eclipse tooling for BPMN2 with Codehoop. Here is a video from the most recent M3 release, it's looking very sexy and Codehoop have done an excellent job on this.
http://vimeo.com/22021856

You can read more about the BPMN2 editor here:
http://kverlaen.blogspot.com/2011/02/new-bpmn-20-eclipse-editor.html

Wednesday, April 13, 2011

Drools & jBPM Workshops (May 2011) - New York and Washington DC

After JBoss World we have two Drools and jBPM5 workshops in May for New York and Washington. These are free events open to all in the public:

The events will be a mixture of talks with lots of time for questions and discussions.

Speakers:
Mark Proctor - Drools co-creator and Project Lead
Kris Verlaenen - jBPM Project Lead
Edson Tirelli - Drools technical lead.

Event: New York - Registration page and agenda
Date:
Tuesday, May 10, 2011
Time: 8:30am – 5:30pm (breakfast, lunch, afternoon snack provided)
Cost: Free but register as seats are very limited.
Location: Affinia Manhattan Hotel
371 Seventh Avenue,
New York, NY 10001

Event: Washington - Registration page and agenda
Date:
Thursday, May 12, 2011
Time: 8:30am – 4:00pm
Cost: Free but register as seats are very limited.
Location: Renaissance Washington
999 Ninth Street, NW
Washington, DC 2000

Thursday, April 07, 2011

Drools & jBPM Community versus Product

A question that comes up often is what is the difference between community and product versions - or .org vs .com for short. What is the difference between Drools and BRMS.

As there is no product for jBPM5 yet, I'll focus on Drools and BRMS - but the arguments are the same.

Branding
First let me explain the brandings and what relates to what and is made up of what. JBoss BRMS is the branded product offering from Red Hat; for which you can buy support subscriptions and professional services, such as training and on-site consultancy. JBoss itself is a Red Hat brand for it's Java middleware.

JBoss BRMS is made up from a number of community projects - Drools Expert, Fusion and Flow and Guvnor. Flow is already deprecated and has been incorporated into jBPM5; Drools will work with jBPM5 going forward.

Multi-Year support
Red Hat commits to supporting a product release over a large number of years, the length varies from product to product. Cumulative patch releases ensure you get select bug fixes and mature features that have been more extensively tested, without the risk of new bleeding edge features. And the assurance that we will be there for the long term. .org does not do maintenance releases it's a continuously forward moving R&D project, living on the bleeding edge. If you want the fixes you are going to get all the new features too, and the risks that come with them. Getting community support for previous .org releases can be hard and introduces elements of risk - Red Hat engineers will not burden themselves answering or helping on .org legacy releases, leaving you to the mercy of a volunteer based community. This is important to ensure that research continues at a fast pace - see Unburdening R&D.

Red Hat provides additional detailed notes on this, see:
JBoss Enterprise Middleware product update and support policy


Sanity through Sanitization
A common fear with OSS is due to the transparency people get to the ongoing R&D. From casual inspection this can seem quite hairy as end users are exposed to all the unstable, unfinished and experimental works. This leaves a lot of uncertainty and fear in using OSS in a production environment.

Closed source companies get to do all their R&D behind closed doors, and end users are only exposed and aware of the highly polished marketing.

.com addresses this issue by removing or demarcating experimental or unstable features. To ensure there is a level of sanity and trust in what you are using. The product is typically 3 to 6 months behind in features, but offers strong levels of stability as a result. .com ensures you never have the dreaded "skip x.0 releases, wait for the x.1" which .org releases come with.

Cross platform and version compatibility
Released products are tested across the range of JBoss and Red Hat products and also multiple versions. So if you are using JBoss BRMS you'll know it'll work on the range of AS services, or the JBoss SOA stack.

We also check compatibility across other platforms such as Websphere and even IBM z/OS.

.org community releases do not go through this compatibility matrix level of testing. If our unit tests pass on on hudson, we are release it.

Direct impact on Roadmap
By being a .com customer you have a direct line to influence the roadmap and urgent bugs. Resources are prioritized for customers versus community jira's or mailing lists posts. It is also the only way to get Red Hat engineering resources for help on legacy releases, they will ignore .org legacy issues in the community.

Community Independence
By separating .org from .com it ensures that R&D can be bottom up user and community driven. This ensures a healthy eco system for ideas and innovation and collaboration - compared to a top down marketing driven model. The life cycle of .com and .org feeding into each other ensures the best of both worlds and help's maintain an important but delicate balance of innovation versus sanity and stability.

I should add that .org is still relevant and important for .com customers as it provides an open environment for them to get involved and upstream their work, which may eventually end up in the product.

Unburden R&D
Everything that goes into ensuring a great product for the enterprise version entails a lot of work. You need a lot of resources that have meticulous attention to detail. Their priorities will be different to the R&D developers priorities. Stability and maintenance are the enemy of innovation. To ensure we have continued innovation at a project level it's important that we isolate R&D from these pressures. So everything about the product process is about freeing up the .org R&D developers so they can focus on ideas and innovations.

If you have any further questions on JBoss BRMS product, then please contact sales@jboss.org

Wednesday, April 06, 2011

Integrate Oryx Designer and Guvnor in your own applications

Shortly after we started blogging about the Guvnor integration with Oryx Designer for jBPM5, we received a number of inquiries on how to use it in custom applications and be able to create/edit jBPM5 processes outside of Guvnor while at the same time leverage Guvnor as the central JCR repository for all your jBPM5 assets.

This video shows a custom Seam2/RichFaces application which utilizes the Guvnor Standalone Editor feature and the recently added AtomPub Interface to fully integrate Oryx Designer and Guvnor to create/edit jBPM5 processes without having to do any work in the Guvnor UI.

You can download the demo app source here.

Monday, April 04, 2011

JUnit testing your jBPM5 processes

Even though business processes aren't code (we even recommend you to make them as high-level as possible and to avoid adding implementation details), they also have a life cycle like other development artefacts. And since business processes can be updated dynamically, testing them (so that you don't break any use cases when doing a modification) is really important as well.

When unit testing your process, you test whether the process behaves as expected in specific use cases, for example test the output based on the existing input. To simplify unit testing, jBPM5 includes a helper class that you can use to greatly simplify your junit testing, by offering:
  • helper methods to create a new knowledge base and session for a given (set of) process(es)
    • you can select whether you want to use persistence or not
  • assert statements to check
    • the state of a process instance (active, completed, aborted)
    • which node instances are currently active
    • which nodes have been triggered (to check the path that has been followed)
    • get the value of variables
    • etc.
For example, conside the following hello world process containing a start event, a script task and an end event. The following junit test will create a new session, start the process and then verify whether the process instance completed successfully and whether these three nodes have been executed.


public class MyProcessTest extends JbpmJUnitTestCase {

public void testProcess() {
// create your session and load the given process(es)
StatefulKnowledgeSession ksession = createKnowledgeSession("sample.bpmn");
// start the process
ProcessInstance processInstance = ksession.startProcess("com.sample.bpmn.hello");
// check whether the process instance has completed successfully
assertProcessInstanceCompleted(processInstance.getId(), ksession);
// check whether the given nodes were executed during the process execution
assertNodeTriggered(processInstance.getId(), "StartProcess", "Hello", "EndProcess");
}
}

Testing processes that don't interact with the environment like this one are really easy to test but in general aren't that realistic ;) Real-life business processes typically include wait states, invocation of external services, etc. One of the advantages of our domain-specific process approach however is that you can easily specify different implementation of the node depending on the context. This means that, when you are unit testing your business process, you can register test handlers that can be used to verify whether specific services are requested correctly, and provide test responses for those services. For example, imagine you have an email node or a human task as part of your process. When unit testing, you don't want to send out an actual email but rather test whether the email that is requested contains the correct information (for example the right to email, a personalized body, etc.).

The following example describes how a process that sends out an email could be tested. This test case in particular will test whether an exception is raised when the email could not be sent (which is simulated by notifying the engine that the sending the email could not be completed). The test case uses a test handler that simply registers when an email was requested (and allows you to test the data related to the email like from, to, etc.). Once the engine has been notified the email could not be sent (using abortWorkItem(..)), the unit test verifies that the process handles this case successfully by logging this and generating an error, which aborts the process instance in this case.


public void testProcess2() {
// create your session and load the given process(es)
StatefulKnowledgeSession ksession = createKnowledgeSession("sample2.bpmn");
// register a test handler for "Email"
TestWorkItemHandler testHandler = new TestWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Email", testHandler);
// start the process
ProcessInstance processInstance = ksession.startProcess("com.sample.bpmn.hello2");
assertProcessInstanceActive(processInstance.getId(), ksession);
assertNodeTriggered(processInstance.getId(), "StartProcess", "Email");
// check whether the email has been requested
WorkItem workItem = testHandler.getWorkItem();
assertNotNull(workItem);
assertEquals("Email", workItem.getName());
assertEquals("me@mail.com", workItem.getParameter("From"));
assertEquals("you@mail.com", workItem.getParameter("To"));
// notify the engine the email has been sent
ksession.getWorkItemManager().abortWorkItem(workItem.getId());
assertProcessInstanceAborted(processInstance.getId(), ksession);
assertNodeTriggered(processInstance.getId(), "Gateway", "Failed", "Error");
}

You can configure whether you want to execute the junit tests using persistence or not (by default using an in-memory H2 database which is started by the junit test itself and by using a history log), simply by passing a boolean whether you want to use persistence or not when calling the super constructor.

We will extend the set of out-of-the-box assert statements over time, but if you do need additional assertions for your use cases, you can already extend the set yourself by looking at the implemention of the existing ones and simply tweaking where necessary. And let us know if you create some reusable ones, we'll add them to the code base!

If you combine these test features with the advanced debugging capabilities, (as for example shown in this screencast) you can use this to walk through the process one step at a time and figure out what's happening internally. We're working on extending this even further to support full simulation and testing capabilities. This would allow you easily define and run various test scenarios, replay an execution log, etc. The foundation is already there (like a simulation clock and the definition of execution paths), but we're still working on the user interface that would make this useable by not just developers but also business users.

Expert System in a Gaming Enviroment

Found this blog on expert systems for game environments, thought others might like it:
http://wiki.bath.ac.uk/display/BISAI/Expert+System+in+a+Gaming+Enviroment

Intro paragraph snippet:
"Following on from the research this section will explain how an expert system function works within the gaming environment. The goal of this expert system is to select which weapon to use during game play, of which this depends on a number of different variables that are changing every frame, therefore the expert system will make a decision.

This page will explain through the following:

  • Analysis of the gaming environment, listing variables, expert knowledge and static data.
  • Assumptions made in the creation of the expert system e.g. confidence factor, forward chaining
  • How the facts are derived from game play itself.
  • How the inference engine decides which rules to run using data from working memory and knowledge base.
  • Weapon Selection example using the expert system

Depicting the diagram shown in Types of gaming AI under expert system, there are a few requirements into developing a weapon selection AI system, and they are as follows:

  • From game play what are the input variables, or changing environmental data, i.e. target range, target type, weapon range.
  • What is the system going to output and why? i.e. type of weapon, why choose a specific weapon, what are the weapon characteristics.

The answers to these questions are derived from the game expert, in this case the makers of this coursework. Therefore the known facts are weapon characteristics (highlighted in the table 1 below ), format of target range weapon range, and which weapon is best for achieving its goal i.e to eradicate the target.

The main reason for developing an AI is to satisfy the objectives and as the gaming environment constantly changes, the bot will constantly have to re-evaluate the situation much like a human player. Creation of an AI will improve game play and provide a challenge with the bot making its own decisions on weapon selection."

All presentations online - including BRMS and BPMS event (London) slides

All the Drools and jBPM presentations are now centralized on this page:
So, if you're looking for nice slides, video's or podcasts about Drools (Expert, Fusion, Planner), jBPM or Guvnor, just take a look there.

Most of the slides of the BRMS and BPMS event in London have already been added.