[6663] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using HeuristicLab.Common;
|
---|
| 6 | using HeuristicLab.Core;
|
---|
| 7 | using HeuristicLab.Data;
|
---|
| 8 | using HeuristicLab.Parameters;
|
---|
| 9 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 10 |
|
---|
| 11 | namespace 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) {
|
---|
[6670] | 33 | RegisterEvents();
|
---|
[6663] | 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 | }
|
---|