Wednesday, 15 September 2010

groovy - Lazy GString evaluation while being executed by Eval or GroovyShell -



groovy - Lazy GString evaluation while being executed by Eval or GroovyShell -

for monitoring system, need evaluate custom checks represented boolean expressions. example, have simple pseudocode status (they can more complex):

if (webservice unavailable) , (there no emails planned downtime)

if possible, i'd utilize short-circuit evaluation here, if webservice available (and first status false), sec part won't evaluated, overall result false anyway. due this, can avoid connecting mail service server (and other time-consuming operations) when it's unnecessary.

here illustration of i'm trying achieve:

class webservicedowncheck { boolean check() { println 'checking webservice' homecoming false } } class nodowntimeemailcheck { boolean check() { //shouldn't executed println 'checking emails' homecoming true } } //using string evaluation, because conditions constructed during runtime eval.me("${new webservicedowncheck().check()} && ${new nodowntimeemailcheck().check()}")

the output is:

checking webservice checking emails //shouldn't there result: false

nodowntimeemailcheck().check() shouldn't executed, because first status false. i've tried:

using groovyshell instead of eval changing check() methods fields , applying @lazy transformation changing ${new webservicedowncheck().check()} ${ -> new webservicedowncheck().check()} (saw here)

seems need lazy mechanism allow me initialize gstring while eval (or groovyshell) executing it. maybe lazy binding groovyshell.

i wonder, somehow possible without writing custom string evaluation logic?

combine 1 expression, this:

eval.me("${new webservicedowncheck().check() && new nodowntimeemailcheck().check()}")

and first evaluated if result false.

groovy eval lazy-evaluation groovyshell

No comments:

Post a Comment