Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/HeuristicLab.Optimization/3.3/Results/ResultParameter.cs @ 14058

Last change on this file since 14058 was 14058, checked in by abeham, 8 years ago

#2431: Refactored ResultsParameter

File size: 6.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using System.Drawing;
24using HeuristicLab.Common;
25using HeuristicLab.Common.Resources;
26using HeuristicLab.Core;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Optimization {
32  [Item("ResultParameter", "A parameter whose value is written to a result collection.")]
33  [StorableClass]
34  [NonDiscoverableType]
35  public sealed class ResultParameter<T> : LookupParameter<T>, IResultParameter<T> where T : class, IItem {
36    public override Image ItemImage { get { return VSImageLibrary.Exception; } }
37
38    [Storable]
39    private string resultCollectionName;
40    public string ResultCollectionName {
41      get { return resultCollectionName; }
42      set {
43        if (value == null) throw new ArgumentNullException();
44        if (string.IsNullOrWhiteSpace(value)) throw new ArgumentNullException();
45        else if (!resultCollectionName.Equals(value)) {
46          resultCollectionName = value;
47          OnResultCollectionNameChanged();
48        }
49      }
50    }
51
52    [Storable]
53    private T defaultValue;
54    public T DefaultValue {
55      get { return defaultValue; }
56      set {
57        if (value != defaultValue) {
58          defaultValue = value;
59          OnDefaultValueChanged();
60        }
61      }
62    }
63
64    [StorableConstructor]
65    private ResultParameter(bool deserializing) : base(deserializing) { }
66    private ResultParameter(ResultParameter<T> original, Cloner cloner)
67      : base(original, cloner) {
68      resultCollectionName = original.resultCollectionName;
69      defaultValue = cloner.Clone(original.defaultValue);
70    }
71    public override IDeepCloneable Clone(Cloner cloner) {
72      return new ResultParameter<T>(this, cloner);
73    }
74    public ResultParameter() : this("Anonymous", string.Empty, "Results") { }
75    public ResultParameter(string name) : this(name, string.Empty, "Results") { }
76    public ResultParameter(string name, string description) : this(name, description, "Results") { }
77    public ResultParameter(string name, string description, string resultCollectionName)
78      : base(name, description, string.Empty) {
79      if (string.IsNullOrEmpty(resultCollectionName)) throw new ArgumentException("resultCollectionName");
80      this.resultCollectionName = resultCollectionName;
81    }
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");
85      if (defaultValue == null) throw new ArgumentNullException("defaultValue");
86      this.resultCollectionName = resultCollectionName;
87      this.defaultValue = defaultValue;
88    }
89   
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      }
102
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.");
106        result = new Result(ActualName, (T)DefaultValue.Clone());
107        results.Add(result);
108      }
109
110      var resultValue = result.Value as T;
111      if (resultValue == null)
112        throw new InvalidOperationException(string.Format("Type mismatch. Result \"{0}\" does not contain a \"{1}\".", ActualName, typeof(T).GetPrettyName()));
113
114      return resultValue;
115    }
116
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      }
128
129      IResult result;
130      if (!results.TryGetValue(ActualName, out result)) {
131        result = new Result(ActualName, value);
132        results.Add(result);
133      } else result.Value = value;
134    }
135
136
137    public event EventHandler ResultCollectionNameChanged;
138    private void OnResultCollectionNameChanged() {
139      var handler = ResultCollectionNameChanged;
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}
Note: See TracBrowser for help on using the repository browser.