[12764] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16956] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[12764] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[14058] | 23 | using System.Drawing;
|
---|
[12764] | 24 | using HeuristicLab.Common;
|
---|
[14058] | 25 | using HeuristicLab.Common.Resources;
|
---|
[12764] | 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
[16956] | 28 | using HEAL.Attic;
|
---|
[12764] | 29 |
|
---|
| 30 | namespace HeuristicLab.Optimization {
|
---|
| 31 | [Item("ResultParameter", "A parameter whose value is written to a result collection.")]
|
---|
[16956] | 32 | [StorableType("CF10EF50-82B6-4A98-82C0-3C5ECED48904")]
|
---|
[14058] | 33 | public sealed class ResultParameter<T> : LookupParameter<T>, IResultParameter<T> where T : class, IItem {
|
---|
| 34 | public override Image ItemImage { get { return VSImageLibrary.Exception; } }
|
---|
[14429] | 35 | public override bool CanChangeDescription { get { return true; } }
|
---|
[12764] | 36 |
|
---|
| 37 | [Storable]
|
---|
[14058] | 38 | private string resultCollectionName;
|
---|
| 39 | public string ResultCollectionName {
|
---|
| 40 | get { return resultCollectionName; }
|
---|
[12764] | 41 | set {
|
---|
| 42 | if (value == null) throw new ArgumentNullException();
|
---|
[14058] | 43 | if (string.IsNullOrWhiteSpace(value)) throw new ArgumentNullException();
|
---|
[14429] | 44 | else if (!value.Equals(resultCollectionName)) {
|
---|
[14058] | 45 | resultCollectionName = value;
|
---|
| 46 | OnResultCollectionNameChanged();
|
---|
[12764] | 47 | }
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | [Storable]
|
---|
| 52 | private T defaultValue;
|
---|
| 53 | public T DefaultValue {
|
---|
| 54 | get { return defaultValue; }
|
---|
| 55 | set {
|
---|
| 56 | if (value != defaultValue) {
|
---|
| 57 | defaultValue = value;
|
---|
| 58 | OnDefaultValueChanged();
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | [StorableConstructor]
|
---|
[16956] | 64 | private ResultParameter(StorableConstructorFlag _) : base(_) { }
|
---|
[12764] | 65 | private ResultParameter(ResultParameter<T> original, Cloner cloner)
|
---|
| 66 | : base(original, cloner) {
|
---|
[14058] | 67 | resultCollectionName = original.resultCollectionName;
|
---|
[12764] | 68 | defaultValue = cloner.Clone(original.defaultValue);
|
---|
| 69 | }
|
---|
| 70 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 71 | return new ResultParameter<T>(this, cloner);
|
---|
| 72 | }
|
---|
[14058] | 73 | public ResultParameter() : this("Anonymous", string.Empty, "Results") { }
|
---|
| 74 | public ResultParameter(string name, string description) : this(name, description, "Results") { }
|
---|
[14429] | 75 |
|
---|
[14058] | 76 | public ResultParameter(string name, string description, string resultCollectionName)
|
---|
| 77 | : base(name, description, string.Empty) {
|
---|
| 78 | if (string.IsNullOrEmpty(resultCollectionName)) throw new ArgumentException("resultCollectionName");
|
---|
| 79 | this.resultCollectionName = resultCollectionName;
|
---|
[14429] | 80 | Hidden = false;
|
---|
[14059] | 81 | }
|
---|
[14058] | 82 | public ResultParameter(string name, string description, string resultCollectionName, T defaultValue)
|
---|
| 83 | : base(name, description, string.Empty) {
|
---|
| 84 | if (string.IsNullOrEmpty(resultCollectionName)) throw new ArgumentException("resultCollectionName");
|
---|
[12764] | 85 | if (defaultValue == null) throw new ArgumentNullException("defaultValue");
|
---|
[14058] | 86 | this.resultCollectionName = resultCollectionName;
|
---|
[12764] | 87 | this.defaultValue = defaultValue;
|
---|
[14429] | 88 | Hidden = false;
|
---|
[12764] | 89 | }
|
---|
[14059] | 90 |
|
---|
[14058] | 91 | protected override IItem GetActualValue() {
|
---|
| 92 | ResultCollection results;
|
---|
| 93 | if (CachedActualValue != null) {
|
---|
| 94 | results = CachedActualValue as ResultCollection;
|
---|
| 95 | if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection not found.");
|
---|
| 96 | } else {
|
---|
| 97 | var tmp = ResultCollectionName;
|
---|
| 98 | // verifyType has to be disabled, because the ResultCollection may not be identical to the generic type of the parameter
|
---|
| 99 | results = GetValue(ExecutionContext, ref tmp) as ResultCollection;
|
---|
| 100 | if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection with name " + tmp + " not found.");
|
---|
| 101 | CachedActualValue = results;
|
---|
| 102 | }
|
---|
[12764] | 103 |
|
---|
[14058] | 104 | IResult result;
|
---|
| 105 | if (!results.TryGetValue(ActualName, out result)) {
|
---|
| 106 | if (DefaultValue == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): Result not found and no default value specified.");
|
---|
[14429] | 107 | result = ItemDescription == Description ? new Result(ActualName, (T)DefaultValue.Clone()) : new Result(ActualName, Description, (T)DefaultValue.Clone());
|
---|
[14058] | 108 | results.Add(result);
|
---|
| 109 | }
|
---|
[12764] | 110 |
|
---|
[14058] | 111 | var resultValue = result.Value as T;
|
---|
[12764] | 112 | if (resultValue == null)
|
---|
[14058] | 113 | throw new InvalidOperationException(string.Format("Type mismatch. Result \"{0}\" does not contain a \"{1}\".", ActualName, typeof(T).GetPrettyName()));
|
---|
[12764] | 114 |
|
---|
| 115 | return resultValue;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[14058] | 118 | protected override void SetActualValue(IItem value) {
|
---|
| 119 | ResultCollection results;
|
---|
| 120 | if (CachedActualValue != null) {
|
---|
| 121 | results = CachedActualValue as ResultCollection;
|
---|
| 122 | if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection not found.");
|
---|
| 123 | } else {
|
---|
| 124 | var tmp = ResultCollectionName;
|
---|
| 125 | results = GetValue(ExecutionContext, ref tmp) as ResultCollection;
|
---|
| 126 | if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection with name " + tmp + " not found.");
|
---|
| 127 | CachedActualValue = results;
|
---|
| 128 | }
|
---|
[12764] | 129 |
|
---|
[14058] | 130 | IResult result;
|
---|
| 131 | if (!results.TryGetValue(ActualName, out result)) {
|
---|
[14429] | 132 | result = ItemDescription == Description ? new Result(ActualName, value) : new Result(ActualName, Description, value);
|
---|
[14058] | 133 | results.Add(result);
|
---|
| 134 | } else result.Value = value;
|
---|
[12764] | 135 | }
|
---|
| 136 |
|
---|
[14058] | 137 |
|
---|
| 138 | public event EventHandler ResultCollectionNameChanged;
|
---|
| 139 | private void OnResultCollectionNameChanged() {
|
---|
| 140 | var handler = ResultCollectionNameChanged;
|
---|
[12764] | 141 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 142 | OnToStringChanged();
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | public event EventHandler DefaultValueChanged;
|
---|
| 146 | private void OnDefaultValueChanged() {
|
---|
| 147 | EventHandler handler = DefaultValueChanged;
|
---|
| 148 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 149 | OnItemImageChanged();
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 | }
|
---|