[2756] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2790] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2756] | 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.Parameters {
|
---|
| 28 | /// <summary>
|
---|
| 29 | /// A parameter whose value is retrieved from the scope.
|
---|
| 30 | /// </summary>
|
---|
[3822] | 31 | [Item("LookupParameter", "A parameter whose value is retrieved from or written to a scope.")]
|
---|
[3017] | 32 | [StorableClass]
|
---|
[2756] | 33 | public class LookupParameter<T> : Parameter, 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 (!actualName.Equals(value)) {
|
---|
| 41 | actualName = value;
|
---|
| 42 | OnActualNameChanged();
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
[3687] | 46 | public string TranslatedName {
|
---|
| 47 | get {
|
---|
| 48 | string translatedName;
|
---|
| 49 | GetValueParameterAndTranslateName(out translatedName);
|
---|
| 50 | return translatedName;
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
[2757] | 53 | public new T ActualValue {
|
---|
[2987] | 54 | get {
|
---|
| 55 | if (cachedActualValue == null) cachedActualValue = GetActualValue();
|
---|
| 56 | return (T)cachedActualValue;
|
---|
| 57 | }
|
---|
| 58 | set {
|
---|
| 59 | cachedActualValue = value;
|
---|
| 60 | SetActualValue(value);
|
---|
| 61 | }
|
---|
[2756] | 62 | }
|
---|
| 63 |
|
---|
[4722] | 64 | [StorableConstructor]
|
---|
| 65 | protected LookupParameter(bool deserializing) : base(deserializing) { }
|
---|
| 66 | protected LookupParameter(LookupParameter<T> original, Cloner cloner)
|
---|
| 67 | : base(original, cloner) {
|
---|
| 68 | actualName = original.actualName;
|
---|
| 69 | }
|
---|
[2756] | 70 | public LookupParameter()
|
---|
| 71 | : base("Anonymous", typeof(T)) {
|
---|
[3080] | 72 | this.actualName = Name;
|
---|
[2756] | 73 | }
|
---|
| 74 | public LookupParameter(string name)
|
---|
| 75 | : base(name, typeof(T)) {
|
---|
[3080] | 76 | this.actualName = Name;
|
---|
[2756] | 77 | }
|
---|
| 78 | public LookupParameter(string name, string description)
|
---|
| 79 | : base(name, description, typeof(T)) {
|
---|
[3080] | 80 | this.actualName = Name;
|
---|
[2756] | 81 | }
|
---|
[3080] | 82 | public LookupParameter(string name, string description, string actualName)
|
---|
| 83 | : base(name, description, typeof(T)) {
|
---|
| 84 | this.actualName = actualName == null ? string.Empty : actualName;
|
---|
| 85 | }
|
---|
[2756] | 86 |
|
---|
| 87 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 88 | return new LookupParameter<T>(this, cloner);
|
---|
[2756] | 89 | }
|
---|
| 90 |
|
---|
| 91 | public override string ToString() {
|
---|
[3688] | 92 | if (Name.Equals(ActualName))
|
---|
| 93 | return Name;
|
---|
| 94 | else
|
---|
| 95 | return Name + ": " + ActualName;
|
---|
[2756] | 96 | }
|
---|
| 97 |
|
---|
[3075] | 98 | private IValueParameter GetValueParameterAndTranslateName(out string actualName) {
|
---|
| 99 | IValueParameter valueParam;
|
---|
| 100 | ILookupParameter lookupParam;
|
---|
| 101 | IExecutionContext currentExecutionContext = ExecutionContext;
|
---|
[2773] | 102 |
|
---|
[3075] | 103 | actualName = Name;
|
---|
| 104 | while (currentExecutionContext != null) {
|
---|
| 105 | valueParam = currentExecutionContext.Parameters[actualName] as IValueParameter;
|
---|
| 106 | lookupParam = currentExecutionContext.Parameters[actualName] as ILookupParameter;
|
---|
[2773] | 107 |
|
---|
[3075] | 108 | if ((valueParam == null) && (lookupParam == null))
|
---|
| 109 | throw new InvalidOperationException(
|
---|
| 110 | string.Format("Parameter look-up chain broken. Parameter \"{0}\" is not an \"{1}\" or an \"{2}\".",
|
---|
| 111 | actualName, typeof(IValueParameter).GetPrettyName(), typeof(ILookupParameter).GetPrettyName())
|
---|
| 112 | );
|
---|
[2773] | 113 |
|
---|
[3075] | 114 | if (valueParam != null) {
|
---|
| 115 | if (valueParam.Value != null) return valueParam;
|
---|
| 116 | else if (lookupParam == null) return valueParam;
|
---|
[2773] | 117 | }
|
---|
[3075] | 118 | if (lookupParam != null) actualName = lookupParam.ActualName;
|
---|
| 119 |
|
---|
| 120 | currentExecutionContext = currentExecutionContext.Parent;
|
---|
| 121 | while ((currentExecutionContext != null) && !currentExecutionContext.Parameters.ContainsKey(actualName))
|
---|
| 122 | currentExecutionContext = currentExecutionContext.Parent;
|
---|
[2773] | 123 | }
|
---|
| 124 | return null;
|
---|
| 125 | }
|
---|
[2756] | 126 | private IVariable LookupVariable(string name) {
|
---|
| 127 | IScope scope = ExecutionContext.Scope;
|
---|
| 128 | while ((scope != null) && !scope.Variables.ContainsKey(name))
|
---|
| 129 | scope = scope.Parent;
|
---|
[3077] | 130 | return scope != null ? scope.Variables[name] : null;
|
---|
[2756] | 131 | }
|
---|
[2757] | 132 | protected override IItem GetActualValue() {
|
---|
[2773] | 133 | string name;
|
---|
[2805] | 134 | // try to get value from context stack
|
---|
[3075] | 135 | IValueParameter param = GetValueParameterAndTranslateName(out name);
|
---|
[3091] | 136 | if (param != null) return param.Value;
|
---|
[2805] | 137 |
|
---|
| 138 | // try to get variable from scope
|
---|
| 139 | IVariable var = LookupVariable(name);
|
---|
| 140 | if (var != null) {
|
---|
[2852] | 141 | if (!(var.Value is T))
|
---|
[2805] | 142 | throw new InvalidOperationException(
|
---|
| 143 | string.Format("Type mismatch. Variable \"{0}\" does not contain a \"{1}\".",
|
---|
| 144 | name,
|
---|
| 145 | typeof(T).GetPrettyName())
|
---|
| 146 | );
|
---|
[2852] | 147 | return var.Value;
|
---|
[2756] | 148 | }
|
---|
| 149 | return null;
|
---|
| 150 | }
|
---|
[2757] | 151 | protected override void SetActualValue(IItem value) {
|
---|
[2852] | 152 | if (!(value is T))
|
---|
[2757] | 153 | throw new InvalidOperationException(
|
---|
| 154 | string.Format("Type mismatch. Value is not a \"{0}\".",
|
---|
| 155 | typeof(T).GetPrettyName())
|
---|
| 156 | );
|
---|
[2805] | 157 | // try to set value in context stack
|
---|
[2773] | 158 | string name;
|
---|
[3075] | 159 | IValueParameter param = GetValueParameterAndTranslateName(out name);
|
---|
[2805] | 160 | if (param != null) {
|
---|
[2852] | 161 | param.Value = value;
|
---|
[2805] | 162 | return;
|
---|
[2773] | 163 | }
|
---|
[2805] | 164 |
|
---|
| 165 | // try to set value in scope
|
---|
| 166 | IVariable var = LookupVariable(name);
|
---|
| 167 | if (var != null) {
|
---|
[2852] | 168 | var.Value = value;
|
---|
[2805] | 169 | return;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | // create new variable
|
---|
| 173 | ExecutionContext.Scope.Variables.Add(new Variable(name, value));
|
---|
[2756] | 174 | }
|
---|
| 175 |
|
---|
| 176 | public event EventHandler ActualNameChanged;
|
---|
[4332] | 177 | protected virtual void OnActualNameChanged() {
|
---|
| 178 | EventHandler handler = ActualNameChanged;
|
---|
| 179 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[2932] | 180 | OnToStringChanged();
|
---|
[2756] | 181 | }
|
---|
| 182 | }
|
---|
| 183 | }
|
---|