ILOG JRules User Guide > Executing Rules > Tasks > Executing a Ruleset Using Rule Execution Server > Maintaining Execution Code Generators > Writing a New Generator Class

To create a new generator class, you can inherit from the following classes:

To write a new generator class if you inherit from the IlrFileGenerator class:

The following procedure describes what to do if you inherit from the IlrVelocityGenerator class and you want to use velocity as the engine to write the generated files.

To write a new generator class if you inherit from the IlrVelocityGenerator class:

  1. Redefine generateProjectContent(IJavaProject) or generateProjectContent(IProject):
    1. Redefine the method that corresponds to the kind of project you want to generate.
    2. Use the generateProjectContent method to generate all the files that you want. The following useful methods are provided:
    3. Start the implementation by calling the parent method.
public void generateProjectContent(IJavaProject javaProject) {
   super.generateProjectContent(javaProject);   
   generateVelocityFile("myDestinationDirectory", "",
      myAbsolutePathToTheTemplateDirectory, "myTemplate.vm",
      null, ".properties", "myTemplate.vm"); 
}
  1. Redefine the getTemplateDirectory method: this method indicates the path for the velocity template files. If you only use the generateVelocityFile(String path, String filePrefix, String templateDirectory, String templatePath, String newFilename, String extension, Object userData) method or the generateVelocityFileInSrc(String packagePrefix, String filePrefix, String templateDirectory, String templatePath, String newFilename, String extension, Object userData) method, which use the templateDirectory parameter to get the path of the velocity template files, then these methods can return null.
  2. Redefine the initializeContext(Object userData , VelocityContext context) method: this method will be called each time you call a generateVelocityFile or generateVelocityFileInSrc method in the generateProjectContent method. It sets your parameters relative to the RuleApp project in the generated file. Some get methods are provided to help you get and format the information in the generated file.
  3. A context.put must be called for each velocity parameter in your .vm file. For example:
protected VelocityContext initializeContext(Object userData , VelocityContext context) {
   if ( userData instanceof String ) { 
      String templateFile = (String) userData; 
      if ( templateFile.equals("myTemplate.vm") ) {  
         context.put("package", getPackagePrefix());   
         context.put("header", getFileHeader());    
         context.put("ruleapp-name", 
            getRuleAppProject().getDescriptor().getName());   
         context.put("ruleapp-version", 
            getRuleAppProject().getDescriptor().getVersion());
      }
   }  
   return context;
}

The following procedure describes what to do if you inherit from the IlrTraceFileGenerator class and you want to use velocity as the engine to write the generated files and use the trace filter properties file.

To write a new generator class if you inherit from the IlrTraceFileGenerator class:

  1. Redefine the same methods as for the IlrVelocityGenerator class.
  2. Call the generateProjectContent method the parent method.
  3. The trace filter properties file will be automatically generated by the generator wizard page, if the property is set correctly.
  4. To set the parameter values in the generated file, change your initializeContext method, as follows:
protected VelocityContext initializeContext(Object userData,
          VelocityContext context) { 
   if ( userData instanceof String ) { 
      String templateFile = (String) userData; 
      if ( templateFile.equals("myTemplate.vm") ) {  
         context.put("package", getPackagePrefix());   
         context.put("header", getFileHeader());    
         context.put("ruleapp-name",
            getRuleAppProject().getDescriptor().getName());   
         context.put("ruleapp-version", 
            getRuleAppProject().getDescriptor().getVersion());
      }
   }  else { 
      context = super.initializeContext(userData, context);
   } 
   return context;
}
  1. A wizard page is also provided to manage the trace filter properties file. For more information, see Writing a New Nested Wizard.

The following procedure describes what to do if you inherit from either the IlrPOJOGenerator class or the IlrWebServiceGenerator class. The steps are the same in each case.

To write a new generator class if you inherit from the IlrPOJOGenerator class or the IlrWebServiceGenerator class:

  1. Redefine the generateProjectContent(IJavaProject) method if you want to generate more files.
  2. Your implementation must call the parent method. For example:
public void generateProjectContent(IJavaProject javaProject) { 
   super.generateProjectContent(javaProject); 
   generateVelocityFile("myDestinationDirectory", "", 
                         myAbsolutePathToTheTemplateDirectory, 
                         "myTemplate.vm", null, ".properties",     
                         "myTemplate.vm"); 
}
  1. Redefine the initializeContext(Object userData , VelocityContext context) method to set the values of the velocity parameter in your new .vm file, as follows:
protected VelocityContext initializeContext(Object userData ,
                                            VelocityContext context) {
   if ( userData instanceof String ) {
      String templateFile = (String) userData; 
      if ( templateFile.equals("myTemplate.vm") ) { 
         context.put("package", getPackagePrefix()); 
         context.put("header", getFileHeader()); 
         context.put("ruleapp-name",
                      getRuleAppProject().getDescriptor().getName());
         context.put("ruleapp-version",
                      getRuleAppProject().getDescriptor().getVersion()); 
      } 
   }  else { 
      context = super.initializeContext(userData, context); 
      } 
   return context; 
} 
  1. Redefine the getPackagePrefix() method if you want to change the default package where the classes will be generated.
  2. By default, the package is the RuleApp project name.
  3. Redefine the getFilePrefix() method if you want your files to be generated with the same prefix.
  4. By default, no prefix is added.
  5. Redefine the getFileHeader() method if you want to change the header of the files.
  6. The default header is:
//      --------------------------------------------------------------------
//      This file was generated by ILOG JRules.
//      RuleApp: 
//      Date: 
//
//      N O T I C E
//
//      Changes to this file may cause incorrect behavior and will be
//      lost if the code is regenerated.
//     ---------------------------------------------------------------------

Related Concepts

Overview: Execution Code Generators
RuleApps and RuleApp Projects

Related Tasks

Invoking a Ruleset Using a Stateless Rule Session
Invoking a Ruleset Using a Stateful Rule Session
Invoking a Ruleset Using a Message-Driven Rule Bean