[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"]; }
|
---|
[6784] | 22 | }
|
---|
| 23 |
|
---|
[6663] | 24 | public ValueParameter<StringValue> FormulaParameter {
|
---|
| 25 | get { return (ValueParameter<StringValue>)Parameters["Formula"]; }
|
---|
[6784] | 26 | }
|
---|
[6663] | 27 |
|
---|
[6784] | 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")));
|
---|
[6784] | 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 +")));
|
---|
[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() {
|
---|
[6784] | 86 | name = string.Format("{0} := {1}", ResultName, Formula);
|
---|
[6663] | 87 | OnNameChanged();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | public void Modify(List<IRun> runs) {
|
---|
[6784] | 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;
|
---|
[6784] | 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 | }
|
---|
[6784] | 105 |
|
---|
[6663] | 106 | }
|
---|
| 107 | }
|
---|