Introduction
This is the most basic of examples with just three rules. Because of the simplicity the DebugWorkingMemoryEventListener has been included, it also demonstrates the use of imports and functions.
Running Hello World Example
Change into the drools-examples directory and run the Hello World example:
D:\java\workspaces\drools-2.0>maven helloworld-java
__ __
| \/ |__ _Apache__ ___
| |\/| / _` \ V / -_) ' \ ~ intelligent projects ~
|_| |_\__,_|\_/\___|_||_| v. 1.0
(bunch of output)
helloworld-java:
[java] Using drl: helloworld.java.drl
[java] FIRE RULES(Hello)
[java] ----------
[java] [ActivationCreated: rule=Debug; tuple={[]}]
[java] [ConditionTested: rule=Hello World; condition=[Condition: hello.equals("Hello")]; passed=true; tuple={[]}]
[java] [ActivationCreated: rule=Hello World; tuple={[]}]
[java] [ConditionTested: rule=Goodbye Cruel World; condition=[Condition: goodbye.equals("Goodbye")]; passed=false; tuple={[]}]
[java] [ObjectAsserted: handle=[fid:1]; object=Hello]
[java] Hello World
[java] [ActivationFired: rule=Hello World; tuple={[]}]
[java] [ActivationFired: rule=Debug; tuple={[]}]
[java] Asserted object: Hello
[java]
[java]
[java] FIRE RULES(GoodBye)
[java] ----------
[java] [ActivationCreated: rule=Debug; tuple={[]}]
[java] [ConditionTested: rule=Hello World; condition=[Condition: hello.equals("Hello")]; passed=false; tuple={[]}]
[java] Goodbye Cruel World
[java] Asserted object: Goodbye
[java] [ConditionTested: rule=Goodbye Cruel World; condition=[Condition: goodbye.equals("Goodbye")]; passed=true; tuple={[]}]
[java] [ActivationCreated: rule=Goodbye Cruel World; tuple={[]}]
[java] [ObjectAsserted: handle=[fid:2]; object=Goodbye]
[java] [ActivationFired: rule=Goodbye Cruel World; tuple={[]}]
[java] [ActivationFired: rule=Debug; tuple={[]}]
BUILD SUCCESSFUL
Total time: 27 seconds
Finished at: Mon Mar 21 01:59:38 CET 2005
Understanding the Hello World Example
First the drl specifies the imports:
<java:import>java.lang.Object</java:import> <java:import>java.lang.String</java:import>
Although the print statements could be embedded in the consequence they have been move to functions for demonstration purposes:
<java:functions> public void helloWorld(String hello) { System.err.println(hello + " World"); } public void goodbyeWorld(String goodbye) { System.out.println(goodbye + " Cruel World"); } </java:functions>
This is the first declared rule and it matches any String object asserted into the system with the value equal to "Hello" and then calls a function in the consequence passing the fact as a parameter.
<rule name="Hello World"> <parameter identifier="hello"> <class>String</class> </parameter> <java:condition>hello.equals("Hello")</java:condition> <java:consequence> helloWorld( hello ); </java:consequence> </rule>
The exact same as above except this time its matching "Goodbye":
<rule name="Goodbye Cruel World"> <parameter identifier="goodbye"> <class>String</class> </parameter> <java:condition>goodbye.equals("Goodbye")</java:condition> <java:consequence> goodbyeWorld( goodbye ); </java:consequence> </rule>
A Debug rule has been added that matches any Object and prints a statements informing the user of the object asserted:
<rule name="Debug"> <parameter identifier="object"> <class>Object</class> </parameter> <java:consequence> System.out.println("Asserted object: " + object); </java:consequence> </rule>
Because we have added a DebugWorkingMemoryEventListener it also prints out debug statements as facts are asserted, matched and rules fired:
workingMemory = ruleBase.newWorkingMemory( );
workingMemory.addEventListener(new DebugWorkingMemoryEventListener( ) );


