Free cookie consent management tool by TermsFeed Policy Generator

source: branches/crossvalidation-2434/HeuristicLab.Optimization/3.3/Results/ResultParameter.cs @ 14636

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

#2281: merged ResultParameter to trunk

File size: 6.3 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;
29
30namespace HeuristicLab.Optimization {
31  [Item("ResultParameter", "A parameter whose value is written to a result collection.")]
32  [StorableClass]
33  public sealed class ResultParameter<T> : LookupParameter<T>, IResultParameter<T> where T : class, IItem {
34    public override Image ItemImage { get { return VSImageLibrary.Exception; } }
35
36    [Storable]
37    private string resultCollectionName;
38    public string ResultCollectionName {
39      get { return resultCollectionName; }
40      set {
41        if (value == null) throw new ArgumentNullException();
42        if (string.IsNullOrWhiteSpace(value)) throw new ArgumentNullException();
43        else if (!resultCollectionName.Equals(value)) {
44          resultCollectionName = value;
45          OnResultCollectionNameChanged();
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) {
66      resultCollectionName = original.resultCollectionName;
67      defaultValue = cloner.Clone(original.defaultValue);
68    }
69    public override IDeepCloneable Clone(Cloner cloner) {
70      return new ResultParameter<T>(this, cloner);
71    }
72    public ResultParameter() : this("Anonymous", string.Empty, "Results") { }
73    public ResultParameter(string name) : this(name, string.Empty, "Results") { }
74    public ResultParameter(string name, string description) : this(name, description, "Results") { }
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;
79    }
80    public ResultParameter(string name, string description, string resultCollectionName, T defaultValue)
81      : base(name, description, string.Empty) {
82      if (string.IsNullOrEmpty(resultCollectionName)) throw new ArgumentException("resultCollectionName");
83      if (defaultValue == null) throw new ArgumentNullException("defaultValue");
84      this.resultCollectionName = resultCollectionName;
85      this.defaultValue = defaultValue;
86    }
87
88    protected override IItem GetActualValue() {
89      ResultCollection results;
90      if (CachedActualValue != null) {
91        results = CachedActualValue as ResultCollection;
92        if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection not found.");
93      } else {
94        var tmp = ResultCollectionName;
95        // verifyType has to be disabled, because the ResultCollection may not be identical to the generic type of the parameter
96        results = GetValue(ExecutionContext, ref tmp) as ResultCollection;
97        if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection with name " + tmp + " not found.");
98        CachedActualValue = results;
99      }
100
101      IResult result;
102      if (!results.TryGetValue(ActualName, out result)) {
103        if (DefaultValue == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): Result not found and no default value specified.");
104        result = new Result(ActualName, (T)DefaultValue.Clone());
105        results.Add(result);
106      }
107
108      var resultValue = result.Value as T;
109      if (resultValue == null)
110        throw new InvalidOperationException(string.Format("Type mismatch. Result \"{0}\" does not contain a \"{1}\".", ActualName, typeof(T).GetPrettyName()));
111
112      return resultValue;
113    }
114
115    protected override void SetActualValue(IItem value) {
116      ResultCollection results;
117      if (CachedActualValue != null) {
118        results = CachedActualValue as ResultCollection;
119        if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection not found.");
120      } else {
121        var tmp = ResultCollectionName;
122        results = GetValue(ExecutionContext, ref tmp) as ResultCollection;
123        if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection with name " + tmp + " not found.");
124        CachedActualValue = results;
125      }
126
127      IResult result;
128      if (!results.TryGetValue(ActualName, out result)) {
129        result = new Result(ActualName, value);
130        results.Add(result);
131      } else result.Value = value;
132    }
133
134
135    public event EventHandler ResultCollectionNameChanged;
136    private void OnResultCollectionNameChanged() {
137      var handler = ResultCollectionNameChanged;
138      if (handler != null) handler(this, EventArgs.Empty);
139      OnToStringChanged();
140    }
141
142    public event EventHandler DefaultValueChanged;
143    private void OnDefaultValueChanged() {
144      EventHandler handler = DefaultValueChanged;
145      if (handler != null) handler(this, EventArgs.Empty);
146      OnItemImageChanged();
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.