[7228] | 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 | private string ResultName { get { return ResultNameParameter.Value.Value; } }
|
---|
| 29 | private string Formula { get { return FormulaParameter.Value.Value; } }
|
---|
| 30 |
|
---|
| 31 | #region Construction & Cloning
|
---|
| 32 | [StorableConstructor]
|
---|
| 33 | protected RunCollectionFormulaModifer(bool deserializing) : base(deserializing) { }
|
---|
| 34 | protected RunCollectionFormulaModifer(RunCollectionFormulaModifer original, Cloner cloner)
|
---|
| 35 | : base(original, cloner) {
|
---|
| 36 | RegisterEvents();
|
---|
| 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")));
|
---|
| 40 | Parameters.Add(new ValueParameter<StringValue>("Formula",
|
---|
| 41 | @"RPN formula for new value in postfix notation.
|
---|
| 42 |
|
---|
| 43 | This can contain the following elements:
|
---|
| 44 |
|
---|
| 45 | literals:
|
---|
| 46 | numbers, true, false, null and strings in single quotes
|
---|
| 47 | variables (run parameters or results):
|
---|
| 48 | unquoted or in double quotes if they contain special characters or whitespace
|
---|
| 49 | mathematical functions:
|
---|
| 50 | +, -, /, ^ (power), log
|
---|
| 51 | predicates:
|
---|
| 52 | ==, <, >, isnull, not
|
---|
| 53 | stack manipulation:
|
---|
| 54 | drop swap dup
|
---|
| 55 | string matching:
|
---|
| 56 | <string> <pattern> ismatch
|
---|
| 57 | string replacing:
|
---|
| 58 | <string> <pattern> <replacement> rename
|
---|
| 59 | conditionals:
|
---|
| 60 | <then> <else> <condition> if
|
---|
| 61 |
|
---|
| 62 | If the final value is null, the result variable is removed if it exists.",
|
---|
| 63 | new StringValue("1 1 +")));
|
---|
| 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() {
|
---|
| 86 | name = string.Format("{0} := {1}", ResultName, Formula);
|
---|
| 87 | OnNameChanged();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | public void Modify(List<IRun> runs) {
|
---|
| 91 | var calc = new Calculator { Formula = Formula };
|
---|
| 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;
|
---|
| 98 | try {
|
---|
| 99 | var value = calc.GetValue(variables);
|
---|
| 100 | if (value != null)
|
---|
| 101 | run.Results[ResultName] = value;
|
---|
| 102 | else
|
---|
| 103 | run.Results.Remove(ResultName);
|
---|
| 104 | } catch (Exception x) {
|
---|
| 105 | throw new Exception(string.Format("Calculation failed at Run {0}", run.Name), x);
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | }
|
---|
| 111 | }
|
---|