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