/*
Add this file to the other .ftl files in the 'ftl'
subdirectory of the Java template.
*/
/**
Provides access to executable code associated with
individual rules in the Statestep model.
The code to be executed when a rule applies is
that extracted from comment lines which begin with
"#:" (rather than just the "#" of ordinary
comments) in that rule's formula.

@author Michael Breen
*/
public class RuleCode
{
   private RuleCode() { }

   /**
   Executes the code, if any, that has been associated
   with a particular rule in the model.
   @param rule the rule for which the code extracted
   from the model should be run
   */
   public static void execute(Rule rule)
   {
<@code_for_rules/>
   }

   /*
   For convenience, you may wish to add methods here
   that can be invoked from the code embedded in the
   rules of the model.
   */

   static void hillwalk()
   {
      System.out.println("Method hillwalk() called.");
   }

   static void windsurf()
   {
      System.out.println("Method windsurf() called.");
   }
}
<#--------------------  Local Macros  -------------------->

<#--
For every rule in the model:
If executable code  for the rule is embedded in a comment
then output an 'if' statement to test for that particular
rule followed by the code extracted from the rule.
-->
<#macro code_for_rules>
   <#-- true until the first 'if' statement is written -->
   <#local first = true/>
   <#list doc.model.events.event as event>
      <#list event.rules.rule as rule>
         <#if rule.formula[0]?exists>
            <#local code_lines =
             rule.formula.text?matches("#:(.*)")
            />
            <#if code_lines?size != 0>
      <#if !first>else </#if>if (rule == Rule.${rule_id(rule)})
      {
               <#list code_lines as line>
         ${line?groups[1]}
               </#list>
      }
               <#local first = false/>
            </#if>
         </#if>
      </#list>
   </#list>
</#macro>
