Conditional Generation

Posts   
 
    
Cadmium avatar
Cadmium
User
Posts: 153
Joined: 19-Sep-2003
# Posted on: 28-Jul-2006 18:02:21   

I need to have a few template files conditionally generated (sometimes templateA, sometimes templateB, sometimes none) based on some user input stored in the taskcache.

These are going to be lpt templates and I figured the best way to do this was either a custom TaskPerformer and try to pass them into the LptParser.DotNetTemplateEngine or somehow conditionally add a task during generaton, but I can't quite figure out the best way to do it.

Cadmium avatar
Cadmium
User
Posts: 153
Joined: 19-Sep-2003
# Posted on: 28-Jul-2006 18:23:53   

This works great:


      Task task = new Task("test","SD.LLBLGen.Pro.LptParser","SD.LLBLGen.Pro.LptParser.DotNetTemplateEngine");
      task.Performer = new SD.LLBLGen.Pro.LptParser.DotNetTemplateEngine(); 
      foreach( DictionaryEntry parameter in parameters ) {
        task.Parameters.Add(parameter.Key, parameter.Value);
      }
      executingGenerator.TasksToExecute.Add(task);

except for


'62' is not a valid value for 'value'. 'value' should be between 'minimum' and 'maximum'.

So I'm guessing that's not the reccomended way of doing things stuck_out_tongue_winking_eye

Cadmium avatar
Cadmium
User
Posts: 153
Joined: 19-Sep-2003
# Posted on: 28-Jul-2006 18:29:21   

However this does work (this is within a custom taskperformer):


      Task task = new Task("test","SD.LLBLGen.Pro.LptParser","SD.LLBLGen.Pro.LptParser.DotNetTemplateEngine");
      task.Performer = new SD.LLBLGen.Pro.LptParser.DotNetTemplateEngine(); 
      foreach( DictionaryEntry parameter in parameters ) {
        task.Parameters.Add(parameter.Key, parameter.Value);
      }
      task.Perform(executingGenerator, taskDefinition.TaskLogNode);

Any reason I shouldn't do it that way?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 28-Jul-2006 19:02:24   

A derived class from DotnetTemplateEngine, which checks the taskcache and selects the right templateID, then calls its base class? (if none, it exists) ?

Frans Bouma | Lead developer LLBLGen Pro
Cadmium avatar
Cadmium
User
Posts: 153
Joined: 19-Sep-2003
# Posted on: 28-Jul-2006 19:32:22   

Otis wrote:

A derived class from DotnetTemplateEngine, which checks the taskcache and selects the right templateID, then calls its base class? (if none, it exists) ?

Great, it saves me a few lines. Here's my code if anyone's interested (in this case I decided to check against the project custom properties rather than the task cache, but it's a simple change):


using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;
using System.Xml;

using SD.LLBLGen.Pro.GeneratorCore;
using SD.LLBLGen.Pro.ApplicationCore;
using SD.LLBLGen.Pro.ApplicationCore.Templates;
using SD.LLBLGen.Pro.LptParser;

namespace Mozaic.WebGen.TaskPerformers {

  public class ConditionalLPTGenerator : DotNetTemplateEngine {

    #region Class Member Declarations
    private bool    _performResult;
    #endregion

    #region Class Property Declarations
    /// <summary>
    /// Reflects the result of the already executed <see cref="Perform"/> method. Should return false when
    /// <see cref="Perform"/> hasn't been called yet. Other tasks can use this property to check what the result
    /// was of a given task.
    /// </summary>
    public override bool PerformResult { 
      get { return _performResult;}
    }
    #endregion
    
    public ConditionalLPTGenerator() {
    }

    public override void Perform(IGenerator executingGenerator, ITask taskDefinition) {
      Perform(executingGenerator, taskDefinition, null);
    }

    public override void Perform(IGenerator executingGenerator, ITask taskDefinition, Hashtable parameters) {

      if(parameters==null) {
        throw new GeneratorAbortException("No parameters have been specified. Aborting generator.", taskDefinition);
      }

      if(parameters.Count <= 0) {
        throw new GeneratorAbortException("No parameters have been specified. Aborting generator.", taskDefinition);
      }

      if(!parameters.ContainsKey("conditionalProperty")) {
        throw new GeneratorAbortException("Mandatory parameter 'conditionalProperty' not found. Aborting generator.", taskDefinition);
      }

      string conditionalProperty = ((ITaskParameter)parameters["conditionalProperty"]).Value;
      string propertyToCompare = string.Empty;
      string compareValue = string.Empty;
      bool negate = false;

      if (executingGenerator.ProjectDefinition.CustomProperties.ContainsKey(conditionalProperty)) {
        propertyToCompare = executingGenerator.ProjectDefinition.CustomProperties[conditionalProperty] as string;
      }

      if(parameters.ContainsKey("compareValue")) {
        compareValue = ((ITaskParameter)parameters["compareValue"]).Value;
      }
      else {
        propertyToCompare = propertyToCompare.ToLower();
        compareValue = "true";
      }

      if(parameters.ContainsKey("negate")) {
        string value=((ITaskParameter)parameters["negate"]).Value;
        negate=(value.ToLower(CultureInfo.InvariantCulture)=="true");
      }

      if ((propertyToCompare == compareValue) != negate) {
        base.Perform(executingGenerator,taskDefinition, parameters);
      }
      else {
        base.LogLine("Conditional task was skipped.", "SettingsCreator", true, true);
        base.AddNewLogNode(taskDefinition.TaskLogNode, LogNodeType.ActionDescription, "Conditional task was skipped. Conditions not met.");
      }

      _performResult = true;
    }

  }
}


Usage is simple. You pass in all of the normal parameters for the task, in addition you have to pass "conditionalProperty" which is the key for your custom property. By default it checks for a "true" value, but you have the option of passing in an expected value "compareValue", and you can also pass in "negate" if you want to ... negate the statement simple_smile