Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/HeuristicLab.Optimization/3.3/Results/ResultParameter.cs @ 16455

Last change on this file since 16455 was 16075, checked in by abeham, 6 years ago

#2457: updated to trunk r15861

File size: 6.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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    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 (value == null) throw new ArgumentNullException();
43        if (string.IsNullOrWhiteSpace(value)) throw new ArgumentNullException();
44        else if (!value.Equals(resultCollectionName)) {
45          resultCollectionName = value;
46          OnResultCollectionNameChanged();
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]
64    private ResultParameter(bool deserializing) : base(deserializing) { }
65    private ResultParameter(ResultParameter<T> original, Cloner cloner)
66      : base(original, cloner) {
67      resultCollectionName = original.resultCollectionName;
68      defaultValue = cloner.Clone(original.defaultValue);
69    }
70    public override IDeepCloneable Clone(Cloner cloner) {
71      return new ResultParameter<T>(this, cloner);
72    }
73    public ResultParameter() : this("Anonymous", string.Empty, "Results") { }
74    public ResultParameter(string name, string description) : this(name, description, "Results") { }
75
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;
80      Hidden = false;
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      Hidden = false;
89    }
90
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      }
103
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.");
107        result = ItemDescription == Description ? new Result(ActualName, (T)DefaultValue.Clone()) : new Result(ActualName, Description, (T)DefaultValue.Clone());
108        results.Add(result);
109      }
110
111      var resultValue = result.Value as T;
112      if (resultValue == null)
113        throw new InvalidOperationException(string.Format("Type mismatch. Result \"{0}\" does not contain a \"{1}\".", ActualName, typeof(T).GetPrettyName()));
114
115      return resultValue;
116    }
117
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      }
129
130      IResult result;
131      if (!results.TryGetValue(ActualName, out result)) {
132        result = ItemDescription == Description ? new Result(ActualName, value) : new Result(ActualName, Description, value);
133        results.Add(result);
134      } else result.Value = value;
135    }
136
137
138    public event EventHandler ResultCollectionNameChanged;
139    private void OnResultCollectionNameChanged() {
140      var handler = ResultCollectionNameChanged;
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}
Note: See TracBrowser for help on using the repository browser.