Add beginnings of 'Fire' demo based on DROOLS example

pull/77/head
Will Gilbert 8 years ago
parent e1a6a66842
commit ad37c37e22

@ -22,6 +22,11 @@ NB: Use '--quiet' or '-q' to supress Gradle build output lines
./gradlew HelloWorld -q ./gradlew HelloWorld -q
Obligatory 'Hello, world' example where the input is evaluated by a rule. Obligatory 'Hello, world' example where the input is evaluated by a rule.
./gradlew Fire -q
Copied from DROOLS examples. Create some rooms with sprinklers, start a fire.
The sprinklers will turn on an alarm will be raised. Put out the fire, the
sprinklers will turn off and the alarm will be silenced.
./gradlew Shop -P person=Tommy -P age=15 ./gradlew Shop -P person=Tommy -P age=15
Rule to evaluate drinking age (US 21); Name and age can be passed in via the command line Rule to evaluate drinking age (US 21); Name and age can be passed in via the command line
or system properties; Default is 'Tom' at age '17'. or system properties; Default is 'Tom' at age '17'.
@ -113,6 +118,10 @@ task HelloWorld (dependsOn: 'classes', type: JavaExec) {
main = 'org.easyrules.samples.helloworld.Launcher' main = 'org.easyrules.samples.helloworld.Launcher'
} }
task Fire (dependsOn: 'classes', type: JavaExec) {
main = 'org.easyrules.samples.fire.Launcher'
}
task Scheduling (dependsOn: 'classes', type: JavaExec) { task Scheduling (dependsOn: 'classes', type: JavaExec) {
main = 'org.easyrules.samples.scheduling.Launcher' main = 'org.easyrules.samples.scheduling.Launcher'
} }

@ -0,0 +1,9 @@
package org.easyrules.samples.fire
@groovy.transform.TupleConstructor
@groovy.transform.EqualsAndHashCode
@groovy.transform.ToString
class Alarm {
String name
}

@ -0,0 +1,27 @@
package org.easyrules.samples.fire
import org.easyrules.annotation.Action
import org.easyrules.annotation.Condition
import org.easyrules.annotation.Rule
import org.easyrules.annotation.Priority
@Rule
class CancelAlarmRule {
def theWorld
@Condition
boolean when() {
theWorld.alarm != null && theWorld.fires.size() == 0
}
@Action
def then() {
theWorld.alarm = null
println( "Cancel the Alarm");
}
@Priority
int getPriority() { 0 }
}

@ -0,0 +1,27 @@
package org.easyrules.samples.fire
import org.easyrules.annotation.Action
import org.easyrules.annotation.Condition
import org.easyrules.annotation.Rule
import org.easyrules.annotation.Priority
@Rule
class EverythingOKRule {
def theWorld
@Condition
boolean when() {
def anyOn = theWorld.sprinklers.any{it.isOn()}
(anyOn == false) && (theWorld.alarm == null)
}
@Action
def then() {
println 'To Fire Station: Everything is OK'
}
@Priority
int getPriority() { 10 }
}

@ -0,0 +1,8 @@
package org.easyrules.samples.fire
@groovy.transform.TupleConstructor
@groovy.transform.EqualsAndHashCode
@groovy.transform.ToString
class Fire {
Room room;
}

@ -0,0 +1,75 @@
package org.easyrules.samples.fire
import static org.easyrules.core.RulesEngineBuilder.aNewRulesEngine
import org.easyrules.api.RulesEngine
class Launcher {
static void main(String... args) {
def label = 'FIRE ALARM'.replaceAll(/./){it+' '}
def width = 80
println """${'='*width}
|${label.center width }
|${'='*width}""".stripMargin()
// Define some room names
def names = ['Kitchen', 'Bedroom', 'Office', 'Livingroom']
// Create rooms for each name; Install a sprinkler in each room; Add to rules engine
def rooms = new HashMap<String,Room>()
def sprinklers = []
names.each { name ->
def room = new Room(name)
rooms[name] = room
sprinklers << new Sprinkler(room)
}
def theWorld = new TheWorld(sprinklers)
// Create a rules engine
RulesEngine rulesEngine = aNewRulesEngine()
.named("Fire Alarm Demo")
.build()
// Register rules
rulesEngine.registerRule(new EverythingOKRule(theWorld:theWorld))
rulesEngine.registerRule(new RaiseAlarmRule(theWorld:theWorld))
rulesEngine.registerRule(new ThereIsAnAlarmRule(theWorld:theWorld))
rulesEngine.registerRule(new CancelAlarmRule(theWorld:theWorld))
rulesEngine.registerRule(new TurnSprinklerOnRule(theWorld:theWorld))
// Fire the rules
rulesEngine.fireRules()
pause('Start some fires')
def kitchenFire = new Fire( rooms['Kitchen'] )
def officeFire = new Fire( rooms['Office'] )
theWorld.fires << kitchenFire
theWorld.fires << officeFire
// Fire the rules
rulesEngine.fireRules()
pause('Put out the fires')
theWorld.fires.remove kitchenFire
theWorld.fires.remove officeFire
// Fire the rules
rulesEngine.fireRules()
}
public static void pause(message) {
println "\n>>>>>> Press enter to '$message' <<<<<<"
def keyboard = new Scanner(System.in)
keyboard.nextLine()
}
}

@ -0,0 +1,27 @@
package org.easyrules.samples.fire
import org.easyrules.annotation.Action
import org.easyrules.annotation.Condition
import org.easyrules.annotation.Rule
import org.easyrules.annotation.Priority
@Rule
class RaiseAlarmRule {
def theWorld
@Condition
boolean when() {
theWorld.fires.size() > 0
}
@Action
def then() {
theWorld.alarm = new Alarm()
println( "Raise the Alarm");
}
@Priority
int getPriority() { 0 }
}

@ -0,0 +1,8 @@
package org.easyrules.samples.fire
@groovy.transform.TupleConstructor
@groovy.transform.EqualsAndHashCode
@groovy.transform.ToString
class Room {
String name;
}

@ -0,0 +1,11 @@
package org.easyrules.samples.fire
@groovy.transform.TupleConstructor
@groovy.transform.EqualsAndHashCode
@groovy.transform.ToString
class Sprinkler {
Room room;
boolean on;
boolean isOn() {on}
}

@ -0,0 +1,15 @@
package org.easyrules.samples.fire
@groovy.transform.TupleConstructor
@groovy.transform.EqualsAndHashCode
@groovy.transform.ToString
class TheWorld {
Sprinkler[] sprinklers
Alarm alarm = null
def fires = []
TheWorld(sprinklers) {
this.sprinklers = sprinklers
}
}

@ -0,0 +1,26 @@
package org.easyrules.samples.fire
import org.easyrules.annotation.Action
import org.easyrules.annotation.Condition
import org.easyrules.annotation.Rule
import org.easyrules.annotation.Priority
@Rule
class ThereIsAnAlarmRule {
def theWorld
@Condition
boolean when() {
theWorld.alarm != null
}
@Action
def then() {
println 'To Fire Station: There is an Alarm'
}
@Priority
int getPriority() { 0 }
}

@ -0,0 +1,28 @@
package org.easyrules.samples.fire
import org.easyrules.annotation.Action
import org.easyrules.annotation.Condition
import org.easyrules.annotation.Rule
import org.easyrules.annotation.Priority
@Rule
class TurnSprinklerOnRule {
def theWorld
@Condition
boolean when() {
theWorld.fires.size() > 0
}
@Action
def then() {
theWorld.fires.each{ fire ->
println "Turn sprinkler on in room: ${fire.room.name}"
}
}
@Priority
int getPriority() { 0 }
}
Loading…
Cancel
Save