Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP.Grammar.Editor/HeuristicLab.Optimization/3.3/RunCollectionFormulaModifer.cs @ 6675

Last change on this file since 6675 was 6675, checked in by mkommend, 13 years ago

#1479: Integrated trunk changes.

File size: 3.0 KB
Line 
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"]; }
22    }   
23   
24    public ValueParameter<StringValue> FormulaParameter {
25      get { return (ValueParameter<StringValue>)Parameters["Formula"]; }
26    }   
27
28    #region Construction & Cloning   
29    [StorableConstructor]
30    protected RunCollectionFormulaModifer(bool deserializing) : base(deserializing) { }
31    protected RunCollectionFormulaModifer(RunCollectionFormulaModifer original, Cloner cloner)
32      : base(original, cloner) {
33      RegisterEvents();
34    }
35    public RunCollectionFormulaModifer() {
36      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 +")));
38      UpdateName();
39      RegisterEvents();
40    }
41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new RunCollectionFormulaModifer(this, cloner);
43    }
44    [StorableHook(HookType.AfterDeserialization)]
45    private void AfterDeserialization() {
46      RegisterEvents();
47    }
48    #endregion
49
50    private void RegisterEvents() {
51      ResultNameParameter.ToStringChanged += Parameter_NameChanged;
52      FormulaParameter.ToStringChanged += Parameter_NameChanged;
53    }
54
55    void Parameter_NameChanged(object sender, EventArgs e) {
56      UpdateName();
57    }
58
59    private void UpdateName() {
60      name = string.Format("{0} := {1}", ResultNameParameter.Value.Value, FormulaParameter.Value.Value);
61      OnNameChanged();
62    }
63
64    public void Modify(List<IRun> runs) {
65      var calc = new Calculator {Formula = FormulaParameter.Value.Value};     
66      foreach (var run in runs) {
67        var variables = new Dictionary<string, IItem>();
68        foreach (var param in run.Parameters)
69          variables[param.Key] = param.Value;
70        foreach (var result in run.Results)
71          variables[result.Key] = result.Value;
72        run.Results[ResultNameParameter.Value.Value] = calc.GetValue(variables);       
73      }     
74    }
75   
76  }
77}
Note: See TracBrowser for help on using the repository browser.