Introduction
The State example is an easy to follow example that shows how the modifying of one fact can cause another rule to fire, it also demonstrates Conflict resolution with Salience values.
Instructions for Running the State Example
In order to build the Drools project from source, you must do the following:
- Install Maven, Java and CVS. There are good instructions for doing so in my OpenEJB article.
- Check out the Project from CVS
- Navigate to the top-level drools directory:
- Build the project (this might take a few minutes the first time):
- Change into the
drools-examplesdirectory and run the State example:
Understanding the State Example
Each State class has fields for its name and its current state. The two possible states for each objects are:
- NOTRUN
- FINISHED
In the example we have four objects: A, B, C and D. Initially all are set to state NOTRUN. A Bootstrap process fires setting A to state FINISHED which then causes B to change to state FINISHED. C and D are both dependent on B - causing a conflict which is resolved by setting salience values.
Instantiate four State objects and assert them into the Working Memory. Notice we set dynamic to true, this means we do not need to use drools.modifyObject( fact ) instead this is done automatically via the JavaBean PropertyChangeListener.
We need to bootstrap the system by checking for the A object with a state of NOTRUN. This will cause the AtoB rule to fire.
After the bootstrap process with A being set to state FINISHED, AtoB fires causing B to be set to state FINISHED.
With AtoB fired and B now at state FINISHED, we now have a conflict. Both rules BtoC and DtoC are activated and eligible to fire. Because we want BtoC to fire first we give it a higher salience value.
Finally BtoD fires.

2 Comments
Hide/Show CommentsMay 06, 2005
Peng Gao
Notice below statement in semantics.base.ClassFieldObjectTypeFactory:
clazz.getMethod( "get" + fieldName.toUpperCase( ).charAt( 0 ) +
fieldName.substring( 1 ),
( Class[] ) null );
For private int UID, you'd define public int getUID(), otherwise NoSuchMethodException exception will be raised.
Nov 14, 2008
John Klassa
Is the point of the example that we want A, B, C and D to finish in that order? Otherwise, it wouldn't matter whether BtoC or BtoD fires first, right? That is, it would be perfectly acceptable for both to fire at the same time, and thereby let C and D finish (in either order) once B is finished.
(Quite new to this, obviously. Trying to wrap my head around things that have to be done in certain ways, vs. things that happened to have been done a certain way to make a particular point.
)