[12764] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14058] | 3 | * Copyright (C) 2002-2016 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;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Optimization {
|
---|
| 31 | [Item("ResultParameter", "A parameter whose value is written to a result collection.")]
|
---|
| 32 | [StorableClass]
|
---|
[14058] | 33 | public sealed class ResultParameter<T> : LookupParameter<T>, IResultParameter<T> where T : class, IItem {
|
---|
| 34 | public override Image ItemImage { get { return VSImageLibrary.Exception; } }
|
---|
[14100] | 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 {
|
---|
[14058] | 42 | if (string.IsNullOrWhiteSpace(value)) throw new ArgumentNullException();
|
---|
[14136] | 43 | else if (!value.Equals(resultCollectionName)) {
|
---|
[14058] | 44 | resultCollectionName = value;
|
---|
| 45 | OnResultCollectionNameChanged();
|
---|
[12764] | 46 | }
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | [Storable]
|
---|
| 51 | private T defaultValue;
|
---|
| 52 | public T DefaultValue {
|
---|
| 53 | get { return defaultValue; }
|
---|
| 54 | set {
|
---|
| 55 | if (value != defaultValue) {
|
---|
| 56 | defaultValue = value;
|
---|
| 57 | OnDefaultValueChanged();
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | [StorableConstructor]
|
---|
| 63 | private ResultParameter(bool deserializing) : base(deserializing) { }
|
---|
| 64 | private ResultParameter(ResultParameter<T> original, Cloner cloner)
|
---|
| 65 | : base(original, cloner) {
|
---|
[14058] | 66 | resultCollectionName = original.resultCollectionName;
|
---|
[12764] | 67 | defaultValue = cloner.Clone(original.defaultValue);
|
---|
| 68 | }
|
---|
| 69 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 70 | return new ResultParameter<T>(this, cloner);
|
---|
| 71 | }
|
---|
[14058] | 72 | public ResultParameter() : this("Anonymous", string.Empty, "Results") { }
|
---|
| 73 | public ResultParameter(string name, string description) : this(name, description, "Results") { }
|
---|
[14100] | 74 |
|
---|
[14058] | 75 | public ResultParameter(string name, string description, string resultCollectionName)
|
---|
| 76 | : base(name, description, string.Empty) {
|
---|
| 77 | if (string.IsNullOrEmpty(resultCollectionName)) throw new ArgumentException("resultCollectionName");
|
---|
| 78 | this.resultCollectionName = resultCollectionName;
|
---|
[14100] | 79 | Hidden = false;
|
---|
[14059] | 80 | }
|
---|
[14058] | 81 | public ResultParameter(string name, string description, string resultCollectionName, T defaultValue)
|
---|
| 82 | : base(name, description, string.Empty) {
|
---|
| 83 | if (string.IsNullOrEmpty(resultCollectionName)) throw new ArgumentException("resultCollectionName");
|
---|
[12764] | 84 | if (defaultValue == null) throw new ArgumentNullException("defaultValue");
|
---|
[14058] | 85 | this.resultCollectionName = resultCollectionName;
|
---|
[12764] | 86 | this.defaultValue = defaultValue;
|
---|
[14100] | 87 | Hidden = false;
|
---|
[12764] | 88 | }
|
---|
[14059] | 89 |
|
---|
[14058] | 90 | protected override IItem GetActualValue() {
|
---|
| 91 | ResultCollection results;
|
---|
| 92 | if (CachedActualValue != null) {
|
---|
| 93 | results = CachedActualValue as ResultCollection;
|
---|
| 94 | if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection not found.");
|
---|
| 95 | } else {
|
---|
| 96 | var tmp = ResultCollectionName;
|
---|
| 97 | // verifyType has to be disabled, because the ResultCollection may not be identical to the generic type of the parameter
|
---|
| 98 | results = GetValue(ExecutionContext, ref tmp) as ResultCollection;
|
---|
| 99 | if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection with name " + tmp + " not found.");
|
---|
| 100 | CachedActualValue = results;
|
---|
| 101 | }
|
---|
[12764] | 102 |
|
---|
[14058] | 103 | IResult result;
|
---|
| 104 | if (!results.TryGetValue(ActualName, out result)) {
|
---|
| 105 | if (DefaultValue == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): Result not found and no default value specified.");
|
---|
[14100] | 106 | result = ItemDescription == Description ? new Result(ActualName, (T)DefaultValue.Clone()) : new Result(ActualName, Description, (T)DefaultValue.Clone());
|
---|
[14058] | 107 | results.Add(result);
|
---|
| 108 | }
|
---|
[12764] | 109 |
|
---|
[14058] | 110 | var resultValue = result.Value as T;
|
---|
[12764] | 111 | if (resultValue == null)
|
---|
[14058] | 112 | throw new InvalidOperationException(string.Format("Type mismatch. Result \"{0}\" does not contain a \"{1}\".", ActualName, typeof(T).GetPrettyName()));
|
---|
[12764] | 113 |
|
---|
| 114 | return resultValue;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[14058] | 117 | protected override void SetActualValue(IItem value) {
|
---|
| 118 | ResultCollection results;
|
---|
| 119 | if (CachedActualValue != null) {
|
---|
| 120 | results = CachedActualValue as ResultCollection;
|
---|
| 121 | if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection not found.");
|
---|
| 122 | } else {
|
---|
| 123 | var tmp = ResultCollectionName;
|
---|
| 124 | results = GetValue(ExecutionContext, ref tmp) as ResultCollection;
|
---|
| 125 | if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection with name " + tmp + " not found.");
|
---|
| 126 | CachedActualValue = results;
|
---|
| 127 | }
|
---|
[12764] | 128 |
|
---|
[14058] | 129 | IResult result;
|
---|
| 130 | if (!results.TryGetValue(ActualName, out result)) {
|
---|
[14100] | 131 | result = ItemDescription == Description ? new Result(ActualName, value) : new Result(ActualName, Description, value);
|
---|
[14058] | 132 | results.Add(result);
|
---|
| 133 | } else result.Value = value;
|
---|
[12764] | 134 | }
|
---|
| 135 |
|
---|
[14058] | 136 |
|
---|
| 137 | public event EventHandler ResultCollectionNameChanged;
|
---|
| 138 | private void OnResultCollectionNameChanged() {
|
---|
| 139 | var handler = ResultCollectionNameChanged;
|
---|
[12764] | 140 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 141 | OnToStringChanged();
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | public event EventHandler DefaultValueChanged;
|
---|
| 145 | private void OnDefaultValueChanged() {
|
---|
| 146 | EventHandler handler = DefaultValueChanged;
|
---|
| 147 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 148 | OnItemImageChanged();
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 | }
|
---|