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