1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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.Parameters;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using HeuristicLab.PluginInfrastructure;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Optimization {
|
---|
30 | [Item("ResultParameter", "A parameter whose value is written to a result collection.")]
|
---|
31 | [StorableClass]
|
---|
32 | [NonDiscoverableType]
|
---|
33 | public sealed class ResultParameter<T> : LookupParameter<ResultCollection>, IResultParameter<T> where T : class, IItem, new() {
|
---|
34 |
|
---|
35 | [Storable]
|
---|
36 | private string resultName;
|
---|
37 | public string ResultName {
|
---|
38 | get { return resultName; }
|
---|
39 | set {
|
---|
40 | if (value == null) throw new ArgumentNullException();
|
---|
41 | if (string.IsNullOrWhiteSpace(value)) {
|
---|
42 | resultName = Name;
|
---|
43 | OnResultNameChanged();
|
---|
44 | } else if (!resultName.Equals(value)) {
|
---|
45 | resultName = value;
|
---|
46 | OnResultNameChanged();
|
---|
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 | public T ResultValue {
|
---|
64 | get { return GetResultValue(); }
|
---|
65 | set { SetResultValue(value); }
|
---|
66 | }
|
---|
67 |
|
---|
68 | [StorableConstructor]
|
---|
69 | private ResultParameter(bool deserializing) : base(deserializing) { }
|
---|
70 | private ResultParameter(ResultParameter<T> original, Cloner cloner)
|
---|
71 | : base(original, cloner) {
|
---|
72 | resultName = original.resultName;
|
---|
73 | defaultValue = cloner.Clone(original.defaultValue);
|
---|
74 | }
|
---|
75 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
76 | return new ResultParameter<T>(this, cloner);
|
---|
77 | }
|
---|
78 | public ResultParameter() : this("Anonymous") { }
|
---|
79 | public ResultParameter(string name)
|
---|
80 | : base(name, string.Empty, "Results") {
|
---|
81 | resultName = Name;
|
---|
82 | defaultValue = new T();
|
---|
83 | }
|
---|
84 | public ResultParameter(string name, string description) : this(name, description, new T()) { }
|
---|
85 | public ResultParameter(string name, string description, T defaultValue)
|
---|
86 | : base(name, description, "Results") {
|
---|
87 | if (defaultValue == null) throw new ArgumentNullException("defaultValue");
|
---|
88 | resultName = Name;
|
---|
89 | this.defaultValue = defaultValue;
|
---|
90 | }
|
---|
91 | public ResultParameter(string name, string description, string resultName) : this(name, description, new T(), resultName) { }
|
---|
92 | public ResultParameter(string name, string description, T defaultValue, string resultName)
|
---|
93 | : base(name, description, "Results") {
|
---|
94 | if (defaultValue == null) throw new ArgumentNullException("defaultValue");
|
---|
95 | this.resultName = string.IsNullOrWhiteSpace(resultName) ? Name : resultName;
|
---|
96 | this.defaultValue = defaultValue;
|
---|
97 | }
|
---|
98 |
|
---|
99 | public override string ToString() {
|
---|
100 | if (Name.Equals(ResultName)) return Name;
|
---|
101 | return Name + ": " + ResultName;
|
---|
102 | }
|
---|
103 |
|
---|
104 | private T GetResultValue() {
|
---|
105 | var results = ActualValue;
|
---|
106 | if (results == null)
|
---|
107 | throw new InvalidOperationException("ResultParameter (" + ResultName + "): ResultCollection " + ActualName + " not found.");
|
---|
108 |
|
---|
109 | if (!results.ContainsKey(ResultName)) results.Add(new Result(ResultName, (T)DefaultValue.Clone()));
|
---|
110 | var resultValue = results[ResultName].Value as T;
|
---|
111 | if (resultValue == null)
|
---|
112 | throw new InvalidOperationException(string.Format("Type mismatch. Result \"{0}\" does not contain a \"{1}\".", ResultName, typeof(T).GetPrettyName()));
|
---|
113 |
|
---|
114 | return resultValue;
|
---|
115 | }
|
---|
116 |
|
---|
117 | private void SetResultValue(T value) {
|
---|
118 | var results = ActualValue;
|
---|
119 | if (results == null)
|
---|
120 | throw new InvalidOperationException("ResultParameter (" + ResultName + "): ResultCollection " + ActualName + " not found.");
|
---|
121 |
|
---|
122 | if (!results.ContainsKey(ResultName))
|
---|
123 | results.Add(new Result(ResultName, value));
|
---|
124 | else results[ResultName].Value = value;
|
---|
125 | }
|
---|
126 |
|
---|
127 | public event EventHandler ResultNameChanged;
|
---|
128 | private void OnResultNameChanged() {
|
---|
129 | var handler = ResultNameChanged;
|
---|
130 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
131 | OnToStringChanged();
|
---|
132 | }
|
---|
133 |
|
---|
134 | public event EventHandler DefaultValueChanged;
|
---|
135 | private void OnDefaultValueChanged() {
|
---|
136 | EventHandler handler = DefaultValueChanged;
|
---|
137 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
138 | OnItemImageChanged();
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|