Add beginnings of 'Fire' demo based on DROOLS example
parent
e1a6a66842
commit
ad37c37e22
@ -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…
Reference in New Issue