Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
2 replies Latest Post - ‏2013-02-04T19:51:35Z by SystemAdmin
SystemAdmin
SystemAdmin
2736 Posts
ACCEPTED ANSWER

Pinned topic GROOVY -- Modular Closures for Builders (FactoryBuilderSupport)

‏2013-01-21T23:50:44Z |
The following is an attempt to modularize my DSL files because
there is a nested closure that is identically defined in
many of these DSL files.
Current (working) Non-Modular Implementation

JAVA CODE:


Binding groovyBinding = 

new Binding(); groovyBinding.setVariable("builder", builder); 

final GroovyShell interp = 

new GroovyShell(groovyBinding); PatientNode result = (PatientNode)(

new GroovyShell(groovyBinding).evaluate( "builder." + stringBuilder.toString()));


patient.groovy


patient 
{ demographics 
{ element(dbID:
'DateOfDeath', name:
'DateOfDeathDocument$DateOfDeath') 
{ applicable 
{ allOf 
{ condition 
{ eq(elementOcId:
'VitalStatus', value:
'DECEASED') 
} 
} 
} 
//... many more elements here ... 
} 
} drugs() 
{ drug() 
{ element(dbID:
'DrugName', name:
'DrugNameDocument$DrugName') 
{
} 
//... many more elements here .... 
} 
} 
}


The above works, but the problem is that the entire closure drug(){...}
is used in many other DSL files.

So, what I would like to do is pull the drug(){...} closure out of patient.groovy and put it
into a new drugs.groovy. The following is what I modified the above to in order to modularize the
drug closure, but I'm not understanding part of it.

Modified (broken) Modular Implementation

Modified Java Code:


Closure drugClosure = (Closure)

new GroovyShell().evaluate( "
{->" + drugStringBuilder.toString() + "
}" ); drugClosure.setResolveStrategy(Closure.DELEGATE_FIRST); Binding groovyBinding = 

new Binding(); groovyBinding.setVariable("builder", builder); groovyBinding.setVariable("drugClosure", drugClosure); PatientNode result = (PatientNode)(

new GroovyShell(groovyBinding).evaluate( "builder." + stringBuilder.toString()));


New drugs.groovy


drug() 
{ element(dbID:
'DrugName', name:
'DrugNameDocument$DrugName') 
{
} 
//... many more elements here .... 
}

Modified patient.groovy


patient 
{ demographics 
{ element(dbID:
'DateOfDeath', name:
'DateOfDeathDocument$DateOfDeath') 
{ applicable 
{ allOf 
{ condition 
{ eq(elementOcId:
'VitalStatus', value:
'DECEASED') 
} 
} 
} 
//... many more elements here ... 
} 
} drugs() 
{ drugClosure 
} 
}


I think things are breaking down in the new drugs() {...} closure. drugClosure is defined in
a separate file and I'm having a heck of a time trying to find information that explains how to
include/reference/inject that into patient {...}
Any help would be much appreciated.

Thanks,
John
Updated on 2013-02-04T19:51:35Z at 2013-02-04T19:51:35Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    2736 Posts
    ACCEPTED ANSWER

    Re: GROOVY -- Modular Closures for Builders (FactoryBuilderSupport)

    ‏2013-02-04T19:50:26Z  in response to SystemAdmin
    I found the solution:

    The the builder class needs a new method I named as 'include(Closure c, Object args)' method within the builder.

    BUILDER Class:

    
    
    
    public 
    
    class BlahBlahFactoryBuilder 
    
    extends FactoryBuilderSupport 
    { 
    
    public BlahBlahBcrFactoryBuilder(
    
    boolean init) 
    { 
    
    super(init); 
    }   
    
    public 
    
    void include(Closure closure, Object[] args) 
    { Closure clone = (Closure)closure.clone(); clone.setDelegate(
    
    this); clone.setResolveStrategy(Closure.DELEGATE_FIRST); clone.call(args); 
    }
    


    Modified patient.groovy

    
    drugs() 
    { drugClosure 
    }
    
    • SystemAdmin
      SystemAdmin
      2736 Posts
      ACCEPTED ANSWER

      Re: GROOVY -- Modular Closures for Builders (FactoryBuilderSupport)

      ‏2013-02-04T19:51:35Z  in response to SystemAdmin
      Correction:

      
      drugs() 
      { include drugClosure 
      }