Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Results/ResultParameter.cs @ 17594

Last change on this file since 17594 was 17594, checked in by mkommend, 4 years ago

#2521: Added first version of new results. The first algorithm that has been adapted for testing purposes is the hill climber.

File size: 6.8 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 HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Common.Resources;
27using HeuristicLab.Core;
28using HeuristicLab.Parameters;
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    [Storable]
63    public ResultCollection ResultCollection { get; set; }
64
65    //string IResultDefinition.Name { get => ActualName; set => ActualName = value; }
66    //T IResultDefinition<T>.Get(ResultCollection results) => ((IResultDefinition)this).Get(results) as T;
67    //IItem IResultDefinition.Get(ResultCollection results) => results[ActualName].Value;
68
69    [StorableConstructor]
70    private ResultParameter(StorableConstructorFlag _) : base(_) { }
71    private ResultParameter(ResultParameter<T> original, Cloner cloner)
72      : base(original, cloner) {
73      resultCollectionName = original.resultCollectionName;
74      defaultValue = cloner.Clone(original.defaultValue);
75      ResultCollection = cloner.Clone(original.ResultCollection);
76    }
77    public override IDeepCloneable Clone(Cloner cloner) {
78      return new ResultParameter<T>(this, cloner);
79    }
80
81    public ResultParameter(string name, string description) : this(name, description, "Results") { }
82    public ResultParameter(string name, string description, string resultCollectionName)
83      : base(name, description, string.Empty) {
84      if (string.IsNullOrEmpty(resultCollectionName)) throw new ArgumentException("resultCollectionName");
85      this.resultCollectionName = resultCollectionName;
86      Hidden = false;
87    }
88    public ResultParameter(string name, string description, string resultCollectionName, T defaultValue)
89      : base(name, description, string.Empty) {
90      if (string.IsNullOrEmpty(resultCollectionName)) throw new ArgumentException("resultCollectionName");
91      if (defaultValue == null) throw new ArgumentNullException("defaultValue");
92      this.resultCollectionName = resultCollectionName;
93      this.defaultValue = defaultValue;
94      Hidden = false;
95    }
96
97    protected override IItem GetActualValue() {
98      ResultCollection results = ResultCollection;
99      if (results == null) {
100        if (CachedActualValue != null) {
101          results = CachedActualValue as ResultCollection;
102          if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection not found.");
103        } else {
104          var tmp = ResultCollectionName;
105          // verifyType has to be disabled, because the ResultCollection may not be identical to the generic type of the parameter
106          results = GetValue(ExecutionContext, ref tmp) as ResultCollection;
107          if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection with name " + tmp + " not found.");
108          CachedActualValue = results;
109        }
110      }
111
112      IResult result;
113      if (!results.TryGetValue(ActualName, out result)) {
114        if (DefaultValue == null) return null;
115        result = ItemDescription == Description ? new Result(ActualName, (T)DefaultValue.Clone()) : new Result(ActualName, Description, (T)DefaultValue.Clone());
116        results.Add(result);
117      }
118
119      var resultValue = result.Value as T;
120      if (result.Value != null && resultValue == null)
121        throw new InvalidOperationException(string.Format("Type mismatch. Result \"{0}\" does not contain a \"{1}\".", ActualName, typeof(T).GetPrettyName()));
122
123      return resultValue;
124    }
125
126    protected override void SetActualValue(IItem value) {
127      ResultCollection results = ResultCollection;
128      if (results == null) {
129        if (CachedActualValue != null) {
130          results = CachedActualValue as ResultCollection;
131          if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection not found.");
132        } else {
133          var tmp = ResultCollectionName;
134          results = GetValue(ExecutionContext, ref tmp) as ResultCollection;
135          if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection with name " + tmp + " not found.");
136          CachedActualValue = results;
137        }
138      }
139
140      IResult result;
141      if (!results.TryGetValue(ActualName, out result)) {
142        result = ItemDescription == Description ? new Result(ActualName, value) : new Result(ActualName, Description, value);
143        results.Add(result);
144      } else result.Value = value;
145    }
146
147
148    public event EventHandler ResultCollectionNameChanged;
149    private void OnResultCollectionNameChanged() {
150      var handler = ResultCollectionNameChanged;
151      if (handler != null) handler(this, EventArgs.Empty);
152      OnToStringChanged();
153    }
154
155    public event EventHandler DefaultValueChanged;
156    private void OnDefaultValueChanged() {
157      EventHandler handler = DefaultValueChanged;
158      if (handler != null) handler(this, EventArgs.Empty);
159      OnItemImageChanged();
160    }
161  }
162}
Note: See TracBrowser for help on using the repository browser.