From e1a6a6684215cc5553193c77edb6a584409d4771 Mon Sep 17 00:00:00 2001 From: Will Gilbert Date: Tue, 16 May 2017 07:17:39 -0400 Subject: [PATCH 1/4] In the 'build.gradle' move the classpath definition to a common task; DRY - Don't Repeat Yourself\! --- easyrules-gradle/build.gradle | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/easyrules-gradle/build.gradle b/easyrules-gradle/build.gradle index 1975acba..3fd28380 100644 --- a/easyrules-gradle/build.gradle +++ b/easyrules-gradle/build.gradle @@ -23,7 +23,7 @@ NB: Use '--quiet' or '-q' to supress Gradle build output lines Obligatory 'Hello, world' example where the input is evaluated by a rule. ./gradlew Shop -P person=Tommy -P age=15 - Rule to evaluate drinking age (US 21); Nmae 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'. ./gradlew Scheduling -q @@ -94,11 +94,11 @@ sourceSets { tasks.withType(JavaExec) { standardInput = System.in + classpath = sourceSets.main.runtimeClasspath } task FizzBuzz (dependsOn: 'classes', type: JavaExec) { main = 'org.easyrules.samples.fizzbuzz.FizzBuzz' - classpath = sourceSets.main.runtimeClasspath } task FizzBuzzER (dependsOn: 'classes', type: JavaExec) { @@ -107,31 +107,26 @@ task FizzBuzzER (dependsOn: 'classes', type: JavaExec) { } task Simple (dependsOn: 'classes', type: JavaExec) { main = 'org.easyrules.samples.simple.Launcher' - classpath = sourceSets.main.runtimeClasspath } task HelloWorld (dependsOn: 'classes', type: JavaExec) { main = 'org.easyrules.samples.helloworld.Launcher' - classpath = sourceSets.main.runtimeClasspath } task Scheduling (dependsOn: 'classes', type: JavaExec) { main = 'org.easyrules.samples.scheduling.Launcher' - classpath = sourceSets.main.runtimeClasspath } task Shop (dependsOn: 'classes', type: JavaExec) { main = 'org.easyrules.samples.shop.Launcher' - classpath = sourceSets.main.runtimeClasspath args = [ findProperty('person') ?: 'Tom', - findProperty('age') ?: '14' + findProperty('age') ?: '17' ] } task Spring (dependsOn: 'classes', type: JavaExec) { main = 'org.easyrules.samples.spring.Launcher' - classpath = sourceSets.main.runtimeClasspath } From ad37c37e2211c3640402e38382de87925319b4cc Mon Sep 17 00:00:00 2001 From: Will Gilbert Date: Tue, 16 May 2017 09:47:48 -0400 Subject: [PATCH 2/4] Add beginnings of 'Fire' demo based on DROOLS example --- easyrules-gradle/build.gradle | 9 +++ .../org/easyrules/samples/fire/Alarm.groovy | 9 +++ .../samples/fire/CancelAlarmRule.groovy | 27 +++++++ .../samples/fire/EverythingOKRule.groovy | 27 +++++++ .../org/easyrules/samples/fire/Fire.groovy | 8 ++ .../easyrules/samples/fire/Launcher.groovy | 75 +++++++++++++++++++ .../samples/fire/RaiseAlarmRule.groovy | 27 +++++++ .../org/easyrules/samples/fire/Room.groovy | 8 ++ .../easyrules/samples/fire/Sprinkler.groovy | 11 +++ .../easyrules/samples/fire/TheWorld.groovy | 15 ++++ .../samples/fire/ThereIsAnAlarmRule.groovy | 26 +++++++ .../samples/fire/TurnSprinklerOnRule.groovy | 28 +++++++ 12 files changed, 270 insertions(+) create mode 100644 easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Alarm.groovy create mode 100644 easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/CancelAlarmRule.groovy create mode 100644 easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/EverythingOKRule.groovy create mode 100644 easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Fire.groovy create mode 100644 easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Launcher.groovy create mode 100644 easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/RaiseAlarmRule.groovy create mode 100644 easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Room.groovy create mode 100644 easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Sprinkler.groovy create mode 100644 easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/TheWorld.groovy create mode 100644 easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/ThereIsAnAlarmRule.groovy create mode 100644 easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/TurnSprinklerOnRule.groovy diff --git a/easyrules-gradle/build.gradle b/easyrules-gradle/build.gradle index 3fd28380..6b695c44 100644 --- a/easyrules-gradle/build.gradle +++ b/easyrules-gradle/build.gradle @@ -22,6 +22,11 @@ NB: Use '--quiet' or '-q' to supress Gradle build output lines ./gradlew HelloWorld -q 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 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'. @@ -113,6 +118,10 @@ task HelloWorld (dependsOn: 'classes', type: JavaExec) { main = 'org.easyrules.samples.helloworld.Launcher' } +task Fire (dependsOn: 'classes', type: JavaExec) { + main = 'org.easyrules.samples.fire.Launcher' +} + task Scheduling (dependsOn: 'classes', type: JavaExec) { main = 'org.easyrules.samples.scheduling.Launcher' } diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Alarm.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Alarm.groovy new file mode 100644 index 00000000..5c908e6d --- /dev/null +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Alarm.groovy @@ -0,0 +1,9 @@ +package org.easyrules.samples.fire + + +@groovy.transform.TupleConstructor +@groovy.transform.EqualsAndHashCode +@groovy.transform.ToString +class Alarm { + String name +} diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/CancelAlarmRule.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/CancelAlarmRule.groovy new file mode 100644 index 00000000..9ef5f8d0 --- /dev/null +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/CancelAlarmRule.groovy @@ -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 } + +} diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/EverythingOKRule.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/EverythingOKRule.groovy new file mode 100644 index 00000000..d2427cab --- /dev/null +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/EverythingOKRule.groovy @@ -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 } + +} diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Fire.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Fire.groovy new file mode 100644 index 00000000..c64d6787 --- /dev/null +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Fire.groovy @@ -0,0 +1,8 @@ +package org.easyrules.samples.fire + +@groovy.transform.TupleConstructor +@groovy.transform.EqualsAndHashCode +@groovy.transform.ToString +class Fire { + Room room; +} diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Launcher.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Launcher.groovy new file mode 100644 index 00000000..f426d120 --- /dev/null +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Launcher.groovy @@ -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() + 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() + } + +} diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/RaiseAlarmRule.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/RaiseAlarmRule.groovy new file mode 100644 index 00000000..dfcd4754 --- /dev/null +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/RaiseAlarmRule.groovy @@ -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 } + +} diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Room.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Room.groovy new file mode 100644 index 00000000..995e6456 --- /dev/null +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Room.groovy @@ -0,0 +1,8 @@ +package org.easyrules.samples.fire + +@groovy.transform.TupleConstructor +@groovy.transform.EqualsAndHashCode +@groovy.transform.ToString +class Room { + String name; +} diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Sprinkler.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Sprinkler.groovy new file mode 100644 index 00000000..435a8a77 --- /dev/null +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Sprinkler.groovy @@ -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} +} diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/TheWorld.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/TheWorld.groovy new file mode 100644 index 00000000..e6cb030d --- /dev/null +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/TheWorld.groovy @@ -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 + } +} diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/ThereIsAnAlarmRule.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/ThereIsAnAlarmRule.groovy new file mode 100644 index 00000000..23b15ba0 --- /dev/null +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/ThereIsAnAlarmRule.groovy @@ -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 } + +} diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/TurnSprinklerOnRule.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/TurnSprinklerOnRule.groovy new file mode 100644 index 00000000..b4ea2549 --- /dev/null +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/TurnSprinklerOnRule.groovy @@ -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 } + +} From d4aedf6d29d4c7daf7c1aa9267aa96a9691d112b Mon Sep 17 00:00:00 2001 From: Will Gilbert Date: Tue, 16 May 2017 12:20:48 -0400 Subject: [PATCH 3/4] Move rules and beans the folders; Add rule descriptions --- .../easyrules/samples/fire/Launcher.groovy | 27 +++++++------- .../easyrules/samples/fire/TheWorld.groovy | 7 +--- .../samples/fire/TurnSprinklerOnRule.groovy | 28 -------------- .../samples/fire/{ => beans}/Alarm.groovy | 4 +- .../samples/fire/{ => beans}/Fire.groovy | 4 +- .../samples/fire/{ => beans}/Room.groovy | 4 +- .../samples/fire/{ => beans}/Sprinkler.groovy | 8 ++-- .../fire/{ => rules}/CancelAlarmRule.groovy | 8 ++-- .../fire/{ => rules}/EverythingOKRule.groovy | 9 ++--- .../fire/{ => rules}/RaiseAlarmRule.groovy | 8 ++-- .../{ => rules}/ThereIsAnAlarmRule.groovy | 8 ++-- .../fire/rules/TurnSprinklerOffRule.groovy | 31 ++++++++++++++++ .../fire/rules/TurnSprinklerOnRule.groovy | 37 +++++++++++++++++++ 13 files changed, 110 insertions(+), 73 deletions(-) delete mode 100644 easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/TurnSprinklerOnRule.groovy rename easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/{ => beans}/Alarm.groovy (67%) rename easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/{ => beans}/Fire.groovy (67%) rename easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/{ => beans}/Room.groovy (67%) rename easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/{ => beans}/Sprinkler.groovy (56%) rename easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/{ => rules}/CancelAlarmRule.groovy (72%) rename easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/{ => rules}/EverythingOKRule.groovy (64%) rename easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/{ => rules}/RaiseAlarmRule.groovy (67%) rename easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/{ => rules}/ThereIsAnAlarmRule.groovy (60%) create mode 100644 easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/TurnSprinklerOffRule.groovy create mode 100644 easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/TurnSprinklerOnRule.groovy diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Launcher.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Launcher.groovy index f426d120..b6704723 100644 --- a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Launcher.groovy +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Launcher.groovy @@ -1,8 +1,10 @@ package org.easyrules.samples.fire -import static org.easyrules.core.RulesEngineBuilder.aNewRulesEngine +import org.easyrules.samples.fire.rules.* +import org.easyrules.samples.fire.beans.* -import org.easyrules.api.RulesEngine + +import static org.easyrules.core.RulesEngineBuilder.aNewRulesEngine as EasyRules class Launcher { @@ -19,29 +21,28 @@ class Launcher { // 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() - def sprinklers = [] + // 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 -> - def room = new Room(name) - rooms[name] = room - sprinklers << new Sprinkler(room) + rooms[name] = new Room(name) + theWorld.sprinklers << new Sprinkler(rooms[name]) } - def theWorld = new TheWorld(sprinklers) - // Create a rules engine - RulesEngine rulesEngine = aNewRulesEngine() + // Create the rules engine + def rulesEngine = EasyRules() .named("Fire Alarm Demo") .build() - // Register rules + // Register all of the 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 ThereIsAnAlarmRule(theWorld:theWorld)) rulesEngine.registerRule(new TurnSprinklerOnRule(theWorld:theWorld)) + rulesEngine.registerRule(new TurnSprinklerOffRule(theWorld:theWorld)) // Fire the rules rulesEngine.fireRules() diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/TheWorld.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/TheWorld.groovy index e6cb030d..7b6e43cd 100644 --- a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/TheWorld.groovy +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/TheWorld.groovy @@ -5,11 +5,8 @@ package org.easyrules.samples.fire @groovy.transform.ToString class TheWorld { - Sprinkler[] sprinklers - Alarm alarm = null + def sprinklers = [] + def alarm = null def fires = [] - TheWorld(sprinklers) { - this.sprinklers = sprinklers - } } diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/TurnSprinklerOnRule.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/TurnSprinklerOnRule.groovy deleted file mode 100644 index b4ea2549..00000000 --- a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/TurnSprinklerOnRule.groovy +++ /dev/null @@ -1,28 +0,0 @@ -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 } - -} diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Alarm.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Alarm.groovy similarity index 67% rename from easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Alarm.groovy rename to easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Alarm.groovy index 5c908e6d..7ed4d68b 100644 --- a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Alarm.groovy +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Alarm.groovy @@ -1,9 +1,9 @@ -package org.easyrules.samples.fire +package org.easyrules.samples.fire.beans @groovy.transform.TupleConstructor @groovy.transform.EqualsAndHashCode @groovy.transform.ToString class Alarm { - String name + def address } diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Fire.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Fire.groovy similarity index 67% rename from easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Fire.groovy rename to easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Fire.groovy index c64d6787..5fcbe519 100644 --- a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Fire.groovy +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Fire.groovy @@ -1,8 +1,8 @@ -package org.easyrules.samples.fire +package org.easyrules.samples.fire.beans @groovy.transform.TupleConstructor @groovy.transform.EqualsAndHashCode @groovy.transform.ToString class Fire { - Room room; + def room; } diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Room.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Room.groovy similarity index 67% rename from easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Room.groovy rename to easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Room.groovy index 995e6456..79f8f8b7 100644 --- a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Room.groovy +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Room.groovy @@ -1,8 +1,8 @@ -package org.easyrules.samples.fire +package org.easyrules.samples.fire.beans @groovy.transform.TupleConstructor @groovy.transform.EqualsAndHashCode @groovy.transform.ToString class Room { - String name; + def name; } diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Sprinkler.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Sprinkler.groovy similarity index 56% rename from easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Sprinkler.groovy rename to easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Sprinkler.groovy index 435a8a77..969e76cd 100644 --- a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/Sprinkler.groovy +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Sprinkler.groovy @@ -1,11 +1,9 @@ -package org.easyrules.samples.fire +package org.easyrules.samples.fire.beans @groovy.transform.TupleConstructor @groovy.transform.EqualsAndHashCode @groovy.transform.ToString class Sprinkler { - Room room; - boolean on; - - boolean isOn() {on} + def room; + def on = false; } diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/CancelAlarmRule.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/CancelAlarmRule.groovy similarity index 72% rename from easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/CancelAlarmRule.groovy rename to easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/CancelAlarmRule.groovy index 9ef5f8d0..cbe9936a 100644 --- a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/CancelAlarmRule.groovy +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/CancelAlarmRule.groovy @@ -1,24 +1,24 @@ -package org.easyrules.samples.fire +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 +@Rule(description='All the fires are out, cancel the alarm') class CancelAlarmRule { def theWorld @Condition boolean when() { - theWorld.alarm != null && theWorld.fires.size() == 0 + theWorld.alarm && theWorld.fires.size() == 0 } @Action def then() { - theWorld.alarm = null println( "Cancel the Alarm"); + theWorld.alarm = null } @Priority diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/EverythingOKRule.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/EverythingOKRule.groovy similarity index 64% rename from easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/EverythingOKRule.groovy rename to easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/EverythingOKRule.groovy index d2427cab..2aa6da5b 100644 --- a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/EverythingOKRule.groovy +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/EverythingOKRule.groovy @@ -1,19 +1,18 @@ -package org.easyrules.samples.fire +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 +@Rule(description='No alarm, nothing to see here; This need to be last rule considered') class EverythingOKRule { def theWorld @Condition boolean when() { - def anyOn = theWorld.sprinklers.any{it.isOn()} - (anyOn == false) && (theWorld.alarm == null) + theWorld.alarm == null } @Action @@ -22,6 +21,6 @@ class EverythingOKRule { } @Priority - int getPriority() { 10 } + int getPriority() { 15 } } diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/RaiseAlarmRule.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/RaiseAlarmRule.groovy similarity index 67% rename from easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/RaiseAlarmRule.groovy rename to easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/RaiseAlarmRule.groovy index dfcd4754..d99259d6 100644 --- a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/RaiseAlarmRule.groovy +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/RaiseAlarmRule.groovy @@ -1,11 +1,13 @@ -package org.easyrules.samples.fire +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 +@Rule(description='A fire will raise an alarm') class RaiseAlarmRule { def theWorld @@ -17,7 +19,7 @@ class RaiseAlarmRule { @Action def then() { - theWorld.alarm = new Alarm() + theWorld.alarm = new Alarm('123 Main Street') println( "Raise the Alarm"); } diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/ThereIsAnAlarmRule.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/ThereIsAnAlarmRule.groovy similarity index 60% rename from easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/ThereIsAnAlarmRule.groovy rename to easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/ThereIsAnAlarmRule.groovy index 23b15ba0..574b7091 100644 --- a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/ThereIsAnAlarmRule.groovy +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/ThereIsAnAlarmRule.groovy @@ -1,11 +1,11 @@ -package org.easyrules.samples.fire +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 +@Rule(description='The alarm is detected at the fire station') class ThereIsAnAlarmRule { def theWorld @@ -17,10 +17,10 @@ class ThereIsAnAlarmRule { @Action def then() { - println 'To Fire Station: There is an Alarm' + println "To Fire Station: There is an Alarm at ${theWorld.alarm.address}" } @Priority - int getPriority() { 0 } + int getPriority() { 1 } } diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/TurnSprinklerOffRule.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/TurnSprinklerOffRule.groovy new file mode 100644 index 00000000..4dfebbd7 --- /dev/null +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/TurnSprinklerOffRule.groovy @@ -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 alarm has been cancelled, turn off all of the sprinklers') +class TurnSprinklerOffRule { + + def theWorld + + @Condition + boolean when() { + theWorld.alarm == null + } + + @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() { 5 } + +} diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/TurnSprinklerOnRule.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/TurnSprinklerOnRule.groovy new file mode 100644 index 00000000..074c05fa --- /dev/null +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/TurnSprinklerOnRule.groovy @@ -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 } + +} From 731142c6b678a23103e7dc6e63a3076f401fd8fb Mon Sep 17 00:00:00 2001 From: Will Gilbert Date: Tue, 16 May 2017 12:37:51 -0400 Subject: [PATCH 4/4] Create easier to read 'toString' output; Set priorities to match real life; Sprinklers first then alarm --- .../main/groovy/org/easyrules/samples/fire/TheWorld.groovy | 2 +- .../groovy/org/easyrules/samples/fire/beans/Alarm.groovy | 2 +- .../groovy/org/easyrules/samples/fire/beans/Fire.groovy | 2 +- .../groovy/org/easyrules/samples/fire/beans/Room.groovy | 2 +- .../org/easyrules/samples/fire/beans/Sprinkler.groovy | 2 +- .../org/easyrules/samples/fire/rules/CancelAlarmRule.groovy | 2 +- .../easyrules/samples/fire/rules/EverythingOKRule.groovy | 2 +- .../org/easyrules/samples/fire/rules/RaiseAlarmRule.groovy | 2 +- .../easyrules/samples/fire/rules/ThereIsAnAlarmRule.groovy | 4 ++-- .../samples/fire/rules/TurnSprinklerOffRule.groovy | 6 +++--- 10 files changed, 13 insertions(+), 13 deletions(-) diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/TheWorld.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/TheWorld.groovy index 7b6e43cd..69acfba9 100644 --- a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/TheWorld.groovy +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/TheWorld.groovy @@ -2,7 +2,7 @@ package org.easyrules.samples.fire @groovy.transform.TupleConstructor @groovy.transform.EqualsAndHashCode -@groovy.transform.ToString +@groovy.transform.ToString(includePackage = false, includeNames = true) class TheWorld { def sprinklers = [] diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Alarm.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Alarm.groovy index 7ed4d68b..1d0459fb 100644 --- a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Alarm.groovy +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Alarm.groovy @@ -3,7 +3,7 @@ package org.easyrules.samples.fire.beans @groovy.transform.TupleConstructor @groovy.transform.EqualsAndHashCode -@groovy.transform.ToString +@groovy.transform.ToString(includePackage = false, includeNames = true) class Alarm { def address } diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Fire.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Fire.groovy index 5fcbe519..23ab6bc6 100644 --- a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Fire.groovy +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Fire.groovy @@ -2,7 +2,7 @@ package org.easyrules.samples.fire.beans @groovy.transform.TupleConstructor @groovy.transform.EqualsAndHashCode -@groovy.transform.ToString +@groovy.transform.ToString(includePackage = false, includeNames = true) class Fire { def room; } diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Room.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Room.groovy index 79f8f8b7..4057d5d3 100644 --- a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Room.groovy +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Room.groovy @@ -2,7 +2,7 @@ package org.easyrules.samples.fire.beans @groovy.transform.TupleConstructor @groovy.transform.EqualsAndHashCode -@groovy.transform.ToString +@groovy.transform.ToString(includePackage = false, includeNames = true) class Room { def name; } diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Sprinkler.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Sprinkler.groovy index 969e76cd..7ac51693 100644 --- a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Sprinkler.groovy +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/beans/Sprinkler.groovy @@ -2,7 +2,7 @@ package org.easyrules.samples.fire.beans @groovy.transform.TupleConstructor @groovy.transform.EqualsAndHashCode -@groovy.transform.ToString +@groovy.transform.ToString(includePackage = false, includeNames = true) class Sprinkler { def room; def on = false; diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/CancelAlarmRule.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/CancelAlarmRule.groovy index cbe9936a..744557fb 100644 --- a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/CancelAlarmRule.groovy +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/CancelAlarmRule.groovy @@ -22,6 +22,6 @@ class CancelAlarmRule { } @Priority - int getPriority() { 0 } + int getPriority() { 1 } } diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/EverythingOKRule.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/EverythingOKRule.groovy index 2aa6da5b..c435c8cf 100644 --- a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/EverythingOKRule.groovy +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/EverythingOKRule.groovy @@ -17,7 +17,7 @@ class EverythingOKRule { @Action def then() { - println 'To Fire Station: Everything is OK' + println 'At the Fire Station: Everything is OK' } @Priority diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/RaiseAlarmRule.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/RaiseAlarmRule.groovy index d99259d6..38c6cb5e 100644 --- a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/RaiseAlarmRule.groovy +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/RaiseAlarmRule.groovy @@ -24,6 +24,6 @@ class RaiseAlarmRule { } @Priority - int getPriority() { 0 } + int getPriority() { 2 } } diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/ThereIsAnAlarmRule.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/ThereIsAnAlarmRule.groovy index 574b7091..f5c0a077 100644 --- a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/ThereIsAnAlarmRule.groovy +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/ThereIsAnAlarmRule.groovy @@ -17,10 +17,10 @@ class ThereIsAnAlarmRule { @Action def then() { - println "To Fire Station: There is an Alarm at ${theWorld.alarm.address}" + println "At the Fire Station: There is an Alarm at ${theWorld.alarm.address}" } @Priority - int getPriority() { 1 } + int getPriority() { 5 } } diff --git a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/TurnSprinklerOffRule.groovy b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/TurnSprinklerOffRule.groovy index 4dfebbd7..f78a2f5d 100644 --- a/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/TurnSprinklerOffRule.groovy +++ b/easyrules-gradle/src/main/groovy/org/easyrules/samples/fire/rules/TurnSprinklerOffRule.groovy @@ -5,14 +5,14 @@ import org.easyrules.annotation.Condition import org.easyrules.annotation.Rule import org.easyrules.annotation.Priority -@Rule(description='The alarm has been cancelled, turn off all of the sprinklers') +@Rule(description='The fires are out, turn off all of the sprinklers') class TurnSprinklerOffRule { def theWorld @Condition boolean when() { - theWorld.alarm == null + theWorld.fires.size() == 0 } @Action @@ -26,6 +26,6 @@ class TurnSprinklerOffRule { } @Priority - int getPriority() { 5 } + int getPriority() { 0 } }