Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Results/Result.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: 5.6 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.Core;
27
28namespace HeuristicLab.Optimization {
29  /// <summary>
30  /// Represents a result which has a name and a data type and holds an IItem.
31  /// </summary>
32  [Item("Result", "A result which has a name and a data type and holds an IItem.")]
33  public class Result : ResultDefinition, IResult, IStorableContent {
34    public string Filename { get; set; }
35    public override Image ItemImage {
36      get {
37        if (value != null) return value.ItemImage;
38        else return base.ItemImage;
39      }
40    }
41
42    [Storable]
43    private IItem value;
44    public IItem Value {
45      get => value;
46      set => SetValue(value);
47    }
48
49    public bool HasValue => Value != null;
50
51    [StorableConstructor]
52    protected Result(StorableConstructorFlag _) : base(_) { }
53    [StorableHook(HookType.AfterDeserialization)]
54    private void AfterDeserialization() {
55      RegisterValueEvents();
56    }
57
58    protected Result(Result original, Cloner cloner)
59      : base(original, cloner) {
60      value = cloner.Clone(original.value);
61      RegisterValueEvents();
62    }
63    public override IDeepCloneable Clone(Cloner cloner) {
64      return new Result(this, cloner);
65    }
66
67    public Result(string name, Type dataType) : this(name, string.Empty, dataType) { }
68    public Result(string name, string description, Type dataType) : base(name, description, dataType) {
69      value = null;
70    }
71
72    public Result(string name, IItem value) : this(name, string.Empty, value.GetType(), value) { }
73    public Result(string name, string description, IItem value) : this(name, description, value.GetType(), value) { }
74    public Result(string name, string description, Type dataType, IItem value) : base(name, description, dataType) {
75      this.value = value;
76      RegisterValueEvents();
77    }
78
79    private void SetValue(IItem newValue) {
80      if (value == newValue) return;
81      if (newValue == null) throw new ArgumentNullException(nameof(Value));
82      if (!DataType.IsInstanceOfType(newValue))
83        throw new ArgumentException(string.Format("Type mismatch. Value is not a \"{0}\".", DataType.GetPrettyName()));
84
85      DeregisterValueEvents();
86      value = newValue;
87      RegisterValueEvents();
88      OnValueChanged();
89    }
90
91    public virtual void Reset() {
92      DeregisterValueEvents();
93      value = null;
94      OnValueChanged();
95    }
96
97    public override string ToString() {
98      if (value != null)
99        return string.Format("{0}: {1}", Name, value.ToString());
100
101      return base.ToString();
102    }
103
104    public event EventHandler ValueChanged;
105    private void OnValueChanged() {
106      ValueChanged?.Invoke(this, EventArgs.Empty);
107      OnItemImageChanged();
108      OnToStringChanged();
109    }
110
111    private void RegisterValueEvents() {
112      if (value == null) return;
113
114      value.ItemImageChanged += Value_ItemImageChanged;
115      value.ToStringChanged += Value_ToStringChanged;
116    }
117    private void DeregisterValueEvents() {
118      if (value == null) return;
119
120      value.ItemImageChanged -= Value_ItemImageChanged;
121      value.ToStringChanged -= Value_ToStringChanged;
122    }
123    private void Value_ItemImageChanged(object sender, EventArgs e) {
124      OnItemImageChanged();
125    }
126    private void Value_ToStringChanged(object sender, EventArgs e) {
127      OnToStringChanged();
128    }
129  }
130
131  /// <summary>
132  /// Represents a result which has a name and a data type and holds an IItem.
133  /// </summary>
134  [Item("Result", "A typed result which has a name and a data type and holds a value of type T.")]
135  [StorableType("BA883E2F-1E0B-4F05-A31A-7A0973CB63A3")]
136  public sealed class Result<T> : Result, IResult<T>, IStorableContent
137    where T : IItem {
138
139    public new T Value {
140      get { return (T)base.Value; }
141      set { base.Value = value; }
142    }
143
144    [StorableConstructor]
145    private Result(StorableConstructorFlag _) : base(_) { }
146    private Result(Result<T> original, Cloner cloner) : base(original, cloner) {
147    }
148    public override IDeepCloneable Clone(Cloner cloner) {
149      return new Result<T>(this, cloner);
150    }
151
152    public Result(string name) : this(name, typeof(T)) { }
153    public Result(string name, Type dataType) : this(name, string.Empty, dataType) { }
154    public Result(string name, string description, Type dataType) : base(name, description, dataType) { }
155
156    public Result(string name, T value) : this(name, string.Empty, value.GetType(), value) { }
157    public Result(string name, string description, T value) : this(name, description, value.GetType(), value) { }
158    public Result(string name, string description, Type dataType, IItem value) : base(name, description, dataType, value) { }
159  }
160}
Note: See TracBrowser for help on using the repository browser.