Was chatting to jstrachan today about where drools-groovy should be heading. He said that GroovyInterp is ok for the xml stuff we currently have and showed us some none-xml exmaples:
This example uses closures:
class RuleSet extends RuleSetSupport {
void loadRuleSet() {
with (rule("Cheese")) {
parameter("hello", String.class)
parameter("foo", int.class)
condition
{ hello -> hello.startsWith("foo") }
condition
{ hello, foo -> foo != hello }
consequence { hellow, foo ->
for (x in 1..100)
{
doSomething(hello, x)
}
}
}
}
Closures with bean properties:
class RuleSet extends RuleSetSuport {
void loadRuleSet() {
with (rule("Cheese")) {
@Property String hello
@Property int foo
condition { hello.startsWith("foo") }
condition { foo != hello }
consequence {
for (x in 1..100) { doSomething(hello, x) }
}
}
String doSomething(x, y)
{
println "Hey $x how are you $y"
}
}
String doSomething(x, y) { println "Hey $x how are you $y" }
}
Using a Map:
RuleSet ruleSet = new RuleSet()
ruleSet.addRule(
name : "Cheese"
parameters :
[
hello : String.class,
foo : int.class
],
conditions :
{ it.hello.startsWith("foo")}
consequence: { println "Hey" }
)
ruleSet.addRule(
name : "Cheese"
parameters :
[
hello : String.class,
foo : int.class
],
// multiple conditions
conditions :
[
{ it.hello.startsWith("foo")}
,
{ it.foo != it.bar }
]
consequence :
{ println "Hey" }
)
when groovy has inner class support it can do:
import org.drools.*
// assuming we had inner class support...
RuleBase ruleBase = [
new Rule("Cheese") {
@Property String hello
@Property int foo
void loadConditions() {
condition
{ hello.startsWith("foo") }
condition { foo != hello }
}
void consequence() {
// whatever
}
// arbitrary helper methods
String doSomething(x, y) {
println "Hey $x how are you $y"
}
},
new Rule("Beer") {
@Property long y = 1234
void loadConditions() {
condition { foo != hello }
}
void consequence() {
// whatever
}
},
]
or
import org.drools.*
// assuming we had inner class support...
RuleBase ruleBase = new RuleBase()
ruleBase.add new Rule("Cheese") {
@Property String hello
@Property int foo
void loadConditions() {
condition { hello.startsWith("foo") }
condition
{ foo != hello }
}
void consequence() {
// whatever
}
// arbitrary helper methods
String doSomething(x, y) {
println "Hey $x how are you $y"
}
}
ruleBase.add new Rule("Beer") {
@Property long y = 1234
void loadConditions() {
condition { foo != hello }
}
void consequence()
{
// whatever
}
}
This could be done quite easily in Groovy by creating your own "Builder", just like we did with AntBuilder, SwingBuilder or MarkupBuilder. The syntax would be almost as described as in this sample rule.
Here are a few classes in the groovy source tree that extend BuilderSupport to create similar functionalities:
The BuilderSupport class:
http://cvs.groovy.codehaus.org/groovy/groovy-core/src/main/groovy/util/BuilderSupport.java?rev=1.6&view=auto
The Swing builder
http://cvs.groovy.codehaus.org/groovy/groovy-core/src/main/groovy/swing/SwingBuilder.java?rev=1.7&view=auto
The Markup builder
http://cvs.groovy.codehaus.org/groovy/groovy-core/src/main/groovy/xml/MarkupBuilder.java?rev=1.7&view=auto
The Ant builder
http://cvs.groovy.codehaus.org/groovy/groovy-core/src/main/groovy/util/AntBuilder.java?rev=1.7&view=auto