[3226] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 26 |
|
---|
| 27 | namespace HeuristicLab.Optimization {
|
---|
| 28 | /// <summary>
|
---|
| 29 | /// Represents a result which has a name and a data type and holds an IItem.
|
---|
| 30 | /// </summary>
|
---|
| 31 | [Item("Result", "A result which has a name and a data type and holds an IItem.")]
|
---|
| 32 | [StorableClass]
|
---|
| 33 | public sealed class Result : NamedItem, IResult {
|
---|
| 34 | public override bool CanChangeName {
|
---|
| 35 | get { return false; }
|
---|
| 36 | }
|
---|
| 37 | public override bool CanChangeDescription {
|
---|
| 38 | get { return false; }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | [Storable]
|
---|
| 42 | private Type dataType;
|
---|
| 43 | public Type DataType {
|
---|
| 44 | get { return dataType; }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | private IItem value;
|
---|
| 48 | [Storable]
|
---|
| 49 | public IItem Value {
|
---|
| 50 | get { return value; }
|
---|
| 51 | set {
|
---|
| 52 | if (this.value != value) {
|
---|
| 53 | if ((value != null) && (!dataType.IsInstanceOfType(value)))
|
---|
| 54 | throw new ArgumentException(
|
---|
| 55 | string.Format("Type mismatch. Value is not a \"{0}\".",
|
---|
| 56 | dataType.GetPrettyName())
|
---|
| 57 | );
|
---|
| 58 |
|
---|
| 59 | if (this.value != null) this.value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
|
---|
| 60 | this.value = value;
|
---|
| 61 | if (this.value != null) this.value.ToStringChanged += new EventHandler(Value_ToStringChanged);
|
---|
| 62 | OnValueChanged();
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | public Result()
|
---|
| 68 | : base("Anonymous") {
|
---|
| 69 | this.dataType = typeof(IItem);
|
---|
| 70 | this.value = null;
|
---|
| 71 | }
|
---|
| 72 | public Result(string name, Type dataType)
|
---|
| 73 | : base(name) {
|
---|
| 74 | this.dataType = dataType;
|
---|
| 75 | this.value = null;
|
---|
| 76 | }
|
---|
| 77 | public Result(string name, string description, Type dataType)
|
---|
| 78 | : base(name, description) {
|
---|
| 79 | this.dataType = dataType;
|
---|
| 80 | this.value = null;
|
---|
| 81 | }
|
---|
| 82 | public Result(string name, IItem value)
|
---|
| 83 | : base(name) {
|
---|
| 84 | this.dataType = value == null ? typeof(IItem) : value.GetType();
|
---|
| 85 | this.value = value;
|
---|
| 86 | if (this.value != null) this.value.ToStringChanged += new EventHandler(Value_ToStringChanged);
|
---|
| 87 | }
|
---|
| 88 | public Result(string name, string description, IItem value)
|
---|
| 89 | : base(name, description) {
|
---|
| 90 | this.dataType = value == null ? typeof(IItem) : value.GetType();
|
---|
| 91 | this.value = value;
|
---|
| 92 | if (this.value != null) this.value.ToStringChanged += new EventHandler(Value_ToStringChanged);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 96 | Result clone = new Result(Name, Description, (IItem)cloner.Clone(value));
|
---|
| 97 | cloner.RegisterClonedObject(this, clone);
|
---|
| 98 | clone.dataType = dataType;
|
---|
| 99 | return clone;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | public override string ToString() {
|
---|
| 103 | return string.Format("{0}: {1} ({2})", Name, Value == null ? "null" : Value.ToString(), DataType.GetPrettyName());
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | public event EventHandler ValueChanged;
|
---|
| 107 | private void OnValueChanged() {
|
---|
| 108 | if (ValueChanged != null)
|
---|
| 109 | ValueChanged(this, EventArgs.Empty);
|
---|
| 110 | OnToStringChanged();
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | private void Value_ToStringChanged(object sender, EventArgs e) {
|
---|
| 114 | OnToStringChanged();
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | }
|
---|