Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/31/11 17:42:00 (13 years ago)
Author:
epitzer
Message:

Extend calculator to support other data types, literals, stack manipulations and conditionals (#1622)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Optimization/3.3/RunCollectionFormulaModifer.cs

    r6670 r6687  
    2020    public ValueParameter<StringValue> ResultNameParameter {
    2121      get { return (ValueParameter<StringValue>)Parameters["ResultName"]; }
    22     }   
    23    
     22    }
     23
    2424    public ValueParameter<StringValue> FormulaParameter {
    2525      get { return (ValueParameter<StringValue>)Parameters["Formula"]; }
    26     }   
     26    }
    2727
    28     #region Construction & Cloning   
     28    private string ResultName { get { return ResultNameParameter.Value.Value; } }
     29    private string Formula { get { return FormulaParameter.Value.Value; } }
     30
     31    #region Construction & Cloning
    2932    [StorableConstructor]
    3033    protected RunCollectionFormulaModifer(bool deserializing) : base(deserializing) { }
     
    3538    public RunCollectionFormulaModifer() {
    3639      Parameters.Add(new ValueParameter<StringValue>("ResultName", "The name of the result to be generated by this formula.", new StringValue("Calc.Value")));
    37       Parameters.Add(new ValueParameter<StringValue>("Formula", "RPN formula for new value. This can contain existing run parameters or results (optionally in quotes if they contain whitespace), numbers and arithmetic operators in postfix notation.", new StringValue("1 1 +")));
     40      Parameters.Add(new ValueParameter<StringValue>("Formula",
     41@"RPN formula for new value in postfix notation.
     42
     43This can contain the following elements:
     44
     45literals:
     46  numbers, true, false, null and strings in single quotes
     47variables (run parameters or results):
     48  unquoted or in double quotes if they contain special characters or whitespace
     49mathematical functions:
     50  +, -, /, ^ (power), log
     51predicates:
     52  ==, <, >, isnull, not
     53stack manipulation:
     54  drop swap dup
     55string matching:
     56  <string> <pattern> ismatch
     57string replacing:
     58  <string> <pattern> <replacement> rename
     59conditionals:
     60  <then> <else> <condition> if
     61
     62If the final value is null, the result variable is removed if it exists.",
     63        new StringValue("1 1 +")));
    3864      UpdateName();
    3965      RegisterEvents();
     
    5884
    5985    private void UpdateName() {
    60       name = string.Format("{0} := {1}", ResultNameParameter.Value.Value, FormulaParameter.Value.Value);
     86      name = string.Format("{0} := {1}", ResultName, Formula);
    6187      OnNameChanged();
    6288    }
    6389
    6490    public void Modify(List<IRun> runs) {
    65       var calc = new Calculator {Formula = FormulaParameter.Value.Value};     
     91      var calc = new Calculator { Formula = Formula };
    6692      foreach (var run in runs) {
    6793        var variables = new Dictionary<string, IItem>();
     
    7096        foreach (var result in run.Results)
    7197          variables[result.Key] = result.Value;
    72         run.Results[ResultNameParameter.Value.Value] = calc.GetValue(variables);       
    73       }     
     98        var value = calc.GetValue(variables);
     99        if (value != null)
     100          run.Results[ResultName] = value;
     101        else
     102          run.Results.Remove(ResultName);
     103      }
    74104    }
    75    
     105
    76106  }
    77107}
Note: See TracChangeset for help on using the changeset viewer.