Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Optimization/3.3/RunCollectionFormulaModifer.cs @ 9420

Last change on this file since 9420 was 6760, checked in by epitzer, 13 years ago

#1530 integrate changes from trunk

File size: 3.6 KB
RevLine 
[6663]1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Common;
6using HeuristicLab.Core;
7using HeuristicLab.Data;
8using HeuristicLab.Parameters;
9using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
10
11namespace HeuristicLab.Optimization {
12
13  [Item("RunCollection Formula Modifier", "Modifies a RunCollection by adding results using the given formula.")]
14  [StorableClass]
15  public class RunCollectionFormulaModifer : ParameterizedNamedItem, IRunCollectionModifier {
16
17    public override bool CanChangeName { get { return false; } }
18    public override bool CanChangeDescription { get { return false; } }
19
20    public ValueParameter<StringValue> ResultNameParameter {
21      get { return (ValueParameter<StringValue>)Parameters["ResultName"]; }
[6687]22    }
23
[6663]24    public ValueParameter<StringValue> FormulaParameter {
25      get { return (ValueParameter<StringValue>)Parameters["Formula"]; }
[6687]26    }
[6663]27
[6687]28    private string ResultName { get { return ResultNameParameter.Value.Value; } }
29    private string Formula { get { return FormulaParameter.Value.Value; } }
30
31    #region Construction & Cloning
[6663]32    [StorableConstructor]
33    protected RunCollectionFormulaModifer(bool deserializing) : base(deserializing) { }
34    protected RunCollectionFormulaModifer(RunCollectionFormulaModifer original, Cloner cloner)
35      : base(original, cloner) {
[6670]36      RegisterEvents();
[6663]37    }
38    public RunCollectionFormulaModifer() {
39      Parameters.Add(new ValueParameter<StringValue>("ResultName", "The name of the result to be generated by this formula.", new StringValue("Calc.Value")));
[6687]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 +")));
[6663]64      UpdateName();
65      RegisterEvents();
66    }
67    public override IDeepCloneable Clone(Cloner cloner) {
68      return new RunCollectionFormulaModifer(this, cloner);
69    }
70    [StorableHook(HookType.AfterDeserialization)]
71    private void AfterDeserialization() {
72      RegisterEvents();
73    }
74    #endregion
75
76    private void RegisterEvents() {
77      ResultNameParameter.ToStringChanged += Parameter_NameChanged;
78      FormulaParameter.ToStringChanged += Parameter_NameChanged;
79    }
80
81    void Parameter_NameChanged(object sender, EventArgs e) {
82      UpdateName();
83    }
84
85    private void UpdateName() {
[6687]86      name = string.Format("{0} := {1}", ResultName, Formula);
[6663]87      OnNameChanged();
88    }
89
90    public void Modify(List<IRun> runs) {
[6687]91      var calc = new Calculator { Formula = Formula };
[6663]92      foreach (var run in runs) {
93        var variables = new Dictionary<string, IItem>();
94        foreach (var param in run.Parameters)
95          variables[param.Key] = param.Value;
96        foreach (var result in run.Results)
97          variables[result.Key] = result.Value;
[6687]98        var value = calc.GetValue(variables);
99        if (value != null)
100          run.Results[ResultName] = value;
101        else
102          run.Results.Remove(ResultName);
103      }
[6663]104    }
[6687]105
[6663]106  }
107}
Note: See TracBrowser for help on using the repository browser.