-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from will-gilbert/master
Implemented DROOLS 'Fire' demo as an EasyRules demo
- Loading branch information
Showing
13 changed files
with
310 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Launcher.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package org.easyrules.samples.fire | ||
|
||
import org.easyrules.samples.fire.rules.* | ||
import org.easyrules.samples.fire.beans.* | ||
|
||
|
||
import static org.easyrules.core.RulesEngineBuilder.aNewRulesEngine as EasyRules | ||
|
||
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 the rooms for each name; Install a sprinkler system in each room; Add to the World | ||
def rooms = [:] | ||
def theWorld = new TheWorld() | ||
|
||
names.each { name -> | ||
rooms[name] = new Room(name) | ||
theWorld.sprinklers << new Sprinkler(rooms[name]) | ||
} | ||
|
||
|
||
// Create the rules engine | ||
def rulesEngine = EasyRules() | ||
.named("Fire Alarm Demo") | ||
.build() | ||
|
||
// Register all of the rules | ||
rulesEngine.registerRule(new EverythingOKRule(theWorld:theWorld)) | ||
rulesEngine.registerRule(new RaiseAlarmRule(theWorld:theWorld)) | ||
rulesEngine.registerRule(new CancelAlarmRule(theWorld:theWorld)) | ||
rulesEngine.registerRule(new ThereIsAnAlarmRule(theWorld:theWorld)) | ||
rulesEngine.registerRule(new TurnSprinklerOnRule(theWorld:theWorld)) | ||
rulesEngine.registerRule(new TurnSprinklerOffRule(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() | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/TheWorld.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.easyrules.samples.fire | ||
|
||
@groovy.transform.TupleConstructor | ||
@groovy.transform.EqualsAndHashCode | ||
@groovy.transform.ToString(includePackage = false, includeNames = true) | ||
class TheWorld { | ||
|
||
def sprinklers = [] | ||
def alarm = null | ||
def fires = [] | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Alarm.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.easyrules.samples.fire.beans | ||
|
||
|
||
@groovy.transform.TupleConstructor | ||
@groovy.transform.EqualsAndHashCode | ||
@groovy.transform.ToString(includePackage = false, includeNames = true) | ||
class Alarm { | ||
def address | ||
} |
8 changes: 8 additions & 0 deletions
8
easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Fire.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.easyrules.samples.fire.beans | ||
|
||
@groovy.transform.TupleConstructor | ||
@groovy.transform.EqualsAndHashCode | ||
@groovy.transform.ToString(includePackage = false, includeNames = true) | ||
class Fire { | ||
def room; | ||
} |
8 changes: 8 additions & 0 deletions
8
easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Room.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.easyrules.samples.fire.beans | ||
|
||
@groovy.transform.TupleConstructor | ||
@groovy.transform.EqualsAndHashCode | ||
@groovy.transform.ToString(includePackage = false, includeNames = true) | ||
class Room { | ||
def name; | ||
} |
9 changes: 9 additions & 0 deletions
9
easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Sprinkler.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.easyrules.samples.fire.beans | ||
|
||
@groovy.transform.TupleConstructor | ||
@groovy.transform.EqualsAndHashCode | ||
@groovy.transform.ToString(includePackage = false, includeNames = true) | ||
class Sprinkler { | ||
def room; | ||
def on = false; | ||
} |
27 changes: 27 additions & 0 deletions
27
easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/CancelAlarmRule.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.easyrules.samples.fire.rules | ||
|
||
import org.easyrules.annotation.Action | ||
import org.easyrules.annotation.Condition | ||
import org.easyrules.annotation.Rule | ||
import org.easyrules.annotation.Priority | ||
|
||
@Rule(description='All the fires are out, cancel the alarm') | ||
class CancelAlarmRule { | ||
|
||
def theWorld | ||
|
||
@Condition | ||
boolean when() { | ||
theWorld.alarm && theWorld.fires.size() == 0 | ||
} | ||
|
||
@Action | ||
def then() { | ||
println( "Cancel the Alarm"); | ||
theWorld.alarm = null | ||
} | ||
|
||
@Priority | ||
int getPriority() { 1 } | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/EverythingOKRule.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.easyrules.samples.fire.rules | ||
|
||
import org.easyrules.annotation.Action | ||
import org.easyrules.annotation.Condition | ||
import org.easyrules.annotation.Rule | ||
import org.easyrules.annotation.Priority | ||
|
||
@Rule(description='No alarm, nothing to see here; This need to be last rule considered') | ||
class EverythingOKRule { | ||
|
||
def theWorld | ||
|
||
@Condition | ||
boolean when() { | ||
theWorld.alarm == null | ||
} | ||
|
||
@Action | ||
def then() { | ||
println 'At the Fire Station: Everything is OK' | ||
} | ||
|
||
@Priority | ||
int getPriority() { 15 } | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/RaiseAlarmRule.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.easyrules.samples.fire.rules | ||
|
||
import org.easyrules.samples.fire.beans.Alarm | ||
|
||
import org.easyrules.annotation.Action | ||
import org.easyrules.annotation.Condition | ||
import org.easyrules.annotation.Rule | ||
import org.easyrules.annotation.Priority | ||
|
||
@Rule(description='A fire will raise an alarm') | ||
class RaiseAlarmRule { | ||
|
||
def theWorld | ||
|
||
@Condition | ||
boolean when() { | ||
theWorld.fires.size() > 0 | ||
} | ||
|
||
@Action | ||
def then() { | ||
theWorld.alarm = new Alarm('123 Main Street') | ||
println( "Raise the Alarm"); | ||
} | ||
|
||
@Priority | ||
int getPriority() { 2 } | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/ThereIsAnAlarmRule.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.easyrules.samples.fire.rules | ||
|
||
import org.easyrules.annotation.Action | ||
import org.easyrules.annotation.Condition | ||
import org.easyrules.annotation.Rule | ||
import org.easyrules.annotation.Priority | ||
|
||
@Rule(description='The alarm is detected at the fire station') | ||
class ThereIsAnAlarmRule { | ||
|
||
def theWorld | ||
|
||
@Condition | ||
boolean when() { | ||
theWorld.alarm != null | ||
} | ||
|
||
@Action | ||
def then() { | ||
println "At the Fire Station: There is an Alarm at ${theWorld.alarm.address}" | ||
} | ||
|
||
@Priority | ||
int getPriority() { 5 } | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
...rules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/TurnSprinklerOffRule.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.easyrules.samples.fire.rules | ||
|
||
import org.easyrules.annotation.Action | ||
import org.easyrules.annotation.Condition | ||
import org.easyrules.annotation.Rule | ||
import org.easyrules.annotation.Priority | ||
|
||
@Rule(description='The fires are out, turn off all of the sprinklers') | ||
class TurnSprinklerOffRule { | ||
|
||
def theWorld | ||
|
||
@Condition | ||
boolean when() { | ||
theWorld.fires.size() == 0 | ||
} | ||
|
||
@Action | ||
def then() { | ||
theWorld.sprinklers.each { sprinkler -> | ||
if(sprinkler.on) { | ||
sprinkler.on = false | ||
println "Turn sprinkler off in room: ${sprinkler.room.name}" | ||
} | ||
} | ||
} | ||
|
||
@Priority | ||
int getPriority() { 0 } | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/TurnSprinklerOnRule.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.easyrules.samples.fire.rules | ||
|
||
import org.easyrules.annotation.Action | ||
import org.easyrules.annotation.Condition | ||
import org.easyrules.annotation.Rule | ||
import org.easyrules.annotation.Priority | ||
|
||
@Rule(description='A fire has been detected in a room, turn on the sprinkler in the room; Highest priority rule') | ||
class TurnSprinklerOnRule { | ||
|
||
def theWorld | ||
|
||
@Condition | ||
boolean when() { | ||
theWorld.fires.size() > 0 | ||
} | ||
|
||
@Action | ||
def then() { | ||
theWorld.fires.each { fire -> | ||
|
||
def sprinkler = theWorld.sprinklers.find { sprinkler -> | ||
sprinkler.room.name == fire.room.name | ||
|
||
} | ||
|
||
if(sprinkler) { | ||
sprinkler.on = true | ||
println "Turn sprinkler on in room: ${sprinkler.room.name}" | ||
} | ||
} | ||
} | ||
|
||
@Priority | ||
int getPriority() { 0 } | ||
|
||
} |