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 |
|
---|
22 | using System;
|
---|
23 | using HEAL.Attic;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Parameters {
|
---|
28 | /// <summary>
|
---|
29 | /// A parameter whose value is retrieved from the scope.
|
---|
30 | /// </summary>
|
---|
31 | [Item("LookupParameter", "A parameter whose value is retrieved from or written to a scope.")]
|
---|
32 | [StorableType("84FE5F33-94B8-4E30-B1CB-CD15314FB83B")]
|
---|
33 | public class LookupParameter<T> : ContextParameter, ILookupParameter<T> where T : class, IItem {
|
---|
34 | [Storable]
|
---|
35 | private string actualName;
|
---|
36 | public string ActualName {
|
---|
37 | get { return actualName; }
|
---|
38 | set {
|
---|
39 | if (value == null) throw new ArgumentNullException();
|
---|
40 | if (string.IsNullOrWhiteSpace(value)) {
|
---|
41 | actualName = Name;
|
---|
42 | OnActualNameChanged();
|
---|
43 | } else if (!actualName.Equals(value)) {
|
---|
44 | actualName = value;
|
---|
45 | OnActualNameChanged();
|
---|
46 | }
|
---|
47 | }
|
---|
48 | }
|
---|
49 | public string TranslatedName {
|
---|
50 | get {
|
---|
51 | string translatedName = Name;
|
---|
52 | GetValueParameterAndTranslateName(ExecutionContext, ref translatedName);
|
---|
53 | return translatedName;
|
---|
54 | }
|
---|
55 | }
|
---|
56 | public new T ActualValue {
|
---|
57 | get { return (T)base.ActualValue; }
|
---|
58 | set { base.ActualValue = value; }
|
---|
59 | }
|
---|
60 |
|
---|
61 | [StorableConstructor]
|
---|
62 | protected LookupParameter(StorableConstructorFlag _) : base(_) { }
|
---|
63 | protected LookupParameter(LookupParameter<T> original, Cloner cloner)
|
---|
64 | : base(original, cloner) {
|
---|
65 | actualName = original.actualName;
|
---|
66 | }
|
---|
67 | public LookupParameter()
|
---|
68 | : base("Anonymous", typeof(T)) {
|
---|
69 | this.actualName = Name;
|
---|
70 | }
|
---|
71 | public LookupParameter(string name)
|
---|
72 | : base(name, typeof(T)) {
|
---|
73 | this.actualName = Name;
|
---|
74 | }
|
---|
75 | public LookupParameter(string name, string description)
|
---|
76 | : base(name, description, typeof(T)) {
|
---|
77 | this.actualName = Name;
|
---|
78 | }
|
---|
79 | public LookupParameter(string name, string description, string actualName)
|
---|
80 | : base(name, description, typeof(T)) {
|
---|
81 | this.actualName = string.IsNullOrWhiteSpace(actualName) ? Name : actualName;
|
---|
82 | }
|
---|
83 |
|
---|
84 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
85 | return new LookupParameter<T>(this, cloner);
|
---|
86 | }
|
---|
87 |
|
---|
88 | public override string ToString() {
|
---|
89 | if (Name.Equals(ActualName))
|
---|
90 | return Name;
|
---|
91 | else
|
---|
92 | return Name + ": " + ActualName;
|
---|
93 | }
|
---|
94 |
|
---|
95 | protected static IValueParameter GetValueParameterAndTranslateName(IExecutionContext executionContext, ref string translatedName) {
|
---|
96 | IValueParameter valueParam;
|
---|
97 | ILookupParameter lookupParam;
|
---|
98 | IExecutionContext currentExecutionContext = executionContext;
|
---|
99 |
|
---|
100 | while (currentExecutionContext != null) {
|
---|
101 | IParameter param = null;
|
---|
102 | while (currentExecutionContext != null && !currentExecutionContext.Parameters.TryGetValue(translatedName, out param))
|
---|
103 | currentExecutionContext = currentExecutionContext.Parent;
|
---|
104 | if (currentExecutionContext == null) break;
|
---|
105 |
|
---|
106 | valueParam = param as IValueParameter;
|
---|
107 | lookupParam = param as ILookupParameter;
|
---|
108 |
|
---|
109 | if (valueParam == null && lookupParam == null)
|
---|
110 | throw new InvalidOperationException(
|
---|
111 | string.Format("Parameter look-up chain broken. Parameter \"{0}\" is neither an \"{1}\" nor an \"{2}\".",
|
---|
112 | translatedName, typeof(IValueParameter).GetPrettyName(), typeof(ILookupParameter).GetPrettyName())
|
---|
113 | );
|
---|
114 |
|
---|
115 | if (valueParam != null) {
|
---|
116 | if (valueParam.Value != null) return valueParam;
|
---|
117 | else if (lookupParam == null) return valueParam;
|
---|
118 | }
|
---|
119 | translatedName = lookupParam.ActualName;
|
---|
120 |
|
---|
121 | currentExecutionContext = currentExecutionContext.Parent;
|
---|
122 | }
|
---|
123 | return null;
|
---|
124 | }
|
---|
125 | protected static IVariable LookupVariable(IScope scope, string name) {
|
---|
126 | IVariable variable = null;
|
---|
127 | while (scope != null && !scope.Variables.TryGetValue(name, out variable))
|
---|
128 | scope = scope.Parent;
|
---|
129 | return scope != null ? variable : null;
|
---|
130 | }
|
---|
131 |
|
---|
132 | protected override IItem GetActualValueFromContext() {
|
---|
133 | string translatedName = Name;
|
---|
134 | var value = GetValue(ExecutionContext, ref translatedName);
|
---|
135 | if (value != null && !(value is T))
|
---|
136 | throw new InvalidOperationException(
|
---|
137 | string.Format("Type mismatch. Variable \"{0}\" does not contain a \"{1}\".",
|
---|
138 | translatedName,
|
---|
139 | typeof(T).GetPrettyName())
|
---|
140 | );
|
---|
141 | return value;
|
---|
142 | }
|
---|
143 |
|
---|
144 | protected static IItem GetValue(IExecutionContext executionContext, ref string name) {
|
---|
145 | // try to get value from context stack
|
---|
146 | IValueParameter param = GetValueParameterAndTranslateName(executionContext, ref name);
|
---|
147 | if (param != null) return param.Value;
|
---|
148 |
|
---|
149 | // try to get variable from scope
|
---|
150 | IVariable var = LookupVariable(executionContext.Scope, name);
|
---|
151 | return var != null ? var.Value : null;
|
---|
152 | }
|
---|
153 |
|
---|
154 | protected override void SetActualValue(IItem value) {
|
---|
155 | if (!(value is T))
|
---|
156 | throw new InvalidOperationException(
|
---|
157 | string.Format("Type mismatch. Value is not a \"{0}\".",
|
---|
158 | typeof(T).GetPrettyName())
|
---|
159 | );
|
---|
160 | CachedActualValue = value;
|
---|
161 |
|
---|
162 | string translatedName = Name;
|
---|
163 | SetValue(ExecutionContext, ref translatedName, value);
|
---|
164 | }
|
---|
165 |
|
---|
166 | protected static void SetValue(IExecutionContext executionContext, ref string name, IItem value) {
|
---|
167 | // try to set value in context stack
|
---|
168 | IValueParameter param = GetValueParameterAndTranslateName(executionContext, ref name);
|
---|
169 | if (param != null) {
|
---|
170 | param.Value = value;
|
---|
171 | return;
|
---|
172 | }
|
---|
173 |
|
---|
174 | // try to set value in scope
|
---|
175 | IVariable var = LookupVariable(executionContext.Scope, name);
|
---|
176 | if (var != null) {
|
---|
177 | var.Value = value;
|
---|
178 | return;
|
---|
179 | }
|
---|
180 |
|
---|
181 | // create new variable
|
---|
182 | executionContext.Scope.Variables.Add(new Variable(name, value));
|
---|
183 | }
|
---|
184 |
|
---|
185 | public event EventHandler ActualNameChanged;
|
---|
186 | protected virtual void OnActualNameChanged() {
|
---|
187 | EventHandler handler = ActualNameChanged;
|
---|
188 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
189 | OnToStringChanged();
|
---|
190 | }
|
---|
191 | }
|
---|
192 | }
|
---|