Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3087_Ceres_Integration/HeuristicLab.Optimization/3.3/Results/ResultParameter.cs @ 17945

Last change on this file since 17945 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 6.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
29
30namespace HeuristicLab.Optimization {
31  [Item("ResultParameter", "A parameter whose value is written to a result collection.")]
32  [StorableType("CF10EF50-82B6-4A98-82C0-3C5ECED48904")]
33  public sealed class ResultParameter<T> : LookupParameter<T>, IResultParameter<T> where T : class, IItem {
34    public override Image ItemImage { get { return VSImageLibrary.Exception; } }
35    public override bool CanChangeDescription { get { return true; } }
36
37    [Storable]
38    private string resultCollectionName;
39    public string ResultCollectionName {
40      get { return resultCollectionName; }
41      set {
42        if (string.IsNullOrWhiteSpace(value)) throw new ArgumentNullException();
43        else if (!value.Equals(resultCollectionName)) {
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(StorableConstructorFlag _) : base(_) { }
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, string description) : this(name, description, "Results") { }
74
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      Hidden = false;
80    }
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");
84      if (defaultValue == null) throw new ArgumentNullException("defaultValue");
85      this.resultCollectionName = resultCollectionName;
86      this.defaultValue = defaultValue;
87      Hidden = false;
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 = ItemDescription == Description ? new Result(ActualName, (T)DefaultValue.Clone()) : new Result(ActualName, Description, (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 = ItemDescription == Description ? new Result(ActualName, value) : new Result(ActualName, Description, 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.