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
datasource.url=jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000 | |
datasource.username=sa | |
datasource.pooled=true | |
datasource.driverClassName=org.h2.Driver | |
datasource.username=sa | |
datasource.password= | |
datasource.dbCreate=create-drop | |
datasource.url=jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000 | |
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
configurations.all { | |
resolutionStrategy.cacheChangingModulesFor 0, 'seconds' | |
} |
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
def bar(name) { | |
"bar: $name" | |
} | |
def foo(k, name = bar(k)) { | |
"$name" | |
} | |
assert foo("ha") == "bar: ha" |
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
public interface RulesEngine {} |
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
class Student { | |
enum DormHall { | |
Honors, Preferred, Normal | |
} | |
def firstName | |
def lastName | |
def gpa | |
// We only want dormHall to be assigned when it is deliberate, so throw an exception when | |
// it is accidentally assigned outside of our rules engine |
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
class RulesEngineFactory<E extends RulesEngine> { | |
private final E engine | |
public RulesEngineFactory(final Class<E> c) { | |
this.engine = (E)c.newInstance() | |
this.engine.engine = this.engine | |
} | |
def getObject(final def obj) { | |
this.engine.obj = obj | |
this.engine |
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
class EnrollmentRulesEngine extends RulesEngineSupport<Student> implements RulesEngine { | |
def dormHallRule = { obj -> | |
if (obj.gpa >= 3.5) { | |
obj.@dormHall = Student.DormHall.Honors | |
} else if (obj.gpa >= 3.0) { | |
obj.@dormHall = Student.DormHall.Preferred | |
} else { | |
obj.@dormHall = Student.DormHall.Normal | |
} | |
} |
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
class RulesEngineSupport<E> { | |
RulesEngine engine | |
E obj | |
// Deduce the rule list for a given RulesEngine instance | |
def getRules() { | |
def rules = [] | |
engine.class.declaredFields.each { | |
def field = engine."${it.name}" | |
if (!it.isSynthetic() && field instanceof Closure && it.name.endsWith("Rule")) { |
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
class EnrollmentProcessor { | |
static def enrollmentRulesFactory = new RulesEngineFactory<EnrollmentRulesEngine>(EnrollmentRulesEngine) | |
def studentService | |
def annualDormHallAssignment() { | |
def students = Student.list(/* do some filtering maybe to make sure they are enrolling for this year,etc. */) | |
def errors = [] | |
students.each { student -> | |
try { | |
def studentEnrollmentRulesEngine = enrollmentRulesFactory.getObject(student) |
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
/** | |
* | |
* Structure: | |
* + module: model | |
* - class: com.danveloper.model.Student | |
* | |
**/ | |
class Student { | |
enum DormHall { | |
Honors, Preferred, Normal |