Thursday, April 19, 2007

JBoss rules

JBoss rules is a library. It provide the DRL file to store the rules.
DRL file is represent one package.
Under DRL file it contains many rule with a identify name with package.

JBoss IDE for JBoss rules (necessary install in Eclipse 3.2.x) . It just add some views and DRL and DSL Editor. (so i think it just good for help you to check the syntax).

the programming steps:
1. PackageBuilder -> Package
2. RunBaseFactory -> RunBase -> add -> Package
3. RunBase -> new -> WorkingMemory
4. WorkingMemory -> assertObject for fireAllRules().

It will use all assertObjects in WorkingMemory to run for all rules except you have add filter for call method of fireAllRules();

DRL sytax:
rule :
rule "name"
while "condition"
then "process"
end

e.g.
rule "Your First Rule"
salience 10
when
$m : Man(age <> 25)
then
System.out.println("first : " + $m.getName());
mans.add($m);
end

rule "Your Second Rule"
salience 5
when
$m : Man(age > 25, $name : name)
then
System.out.println("second : " + $name);
end

note: if miss salience .. then all rule by default salience = 0.
If fireAllRules() "Your Second Rule" execute first and then "Your First Rule"

query:
query "name"
"condition"
end

e.g. DRL
query "test"
$man Man(age > 25);
end

e.g. Java
QueryResults qrs = WorkingMemory.getQueryResults("test");
qrs.size();

It could return all valid the query objects.

Personal comment:
From example most rule only check about the assertObject and update the state of object ... but never return anything ..... such as only true/false.
If you have other thing out of set state, you need to define global object to return.