[2740] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2790] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2740] | 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;
|
---|
[3659] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[2756] | 25 | using HeuristicLab.Common;
|
---|
[2740] | 26 | using HeuristicLab.Core;
|
---|
[2996] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2740] | 28 |
|
---|
| 29 | namespace HeuristicLab.Parameters {
|
---|
| 30 | /// <summary>
|
---|
[3659] | 31 | /// A generic parameter representing instances of type T which are collected from or written to scope tree.
|
---|
[2740] | 32 | /// </summary>
|
---|
[3659] | 33 | [Item("ScopeTreeLookupParameter<T>", "A generic parameter representing instances of type T which are collected from or written to scope tree.")]
|
---|
[3017] | 34 | [StorableClass]
|
---|
[3663] | 35 | public class ScopeTreeLookupParameter<T> : LookupParameter<ItemArray<T>>, IScopeTreeLookupParameter<T> where T : class, IItem {
|
---|
[3659] | 36 | [Storable]
|
---|
| 37 | private int depth;
|
---|
| 38 | public int Depth {
|
---|
| 39 | get { return depth; }
|
---|
| 40 | set {
|
---|
| 41 | if (value < 0) throw new ArgumentException("Depth must be larger than or equal to 0.");
|
---|
| 42 | if (depth != value) {
|
---|
| 43 | depth = value;
|
---|
| 44 | OnDepthChanged();
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
[2740] | 48 |
|
---|
[3659] | 49 | public ScopeTreeLookupParameter()
|
---|
| 50 | : base() {
|
---|
| 51 | depth = 1;
|
---|
| 52 | }
|
---|
| 53 | public ScopeTreeLookupParameter(string name)
|
---|
| 54 | : base(name) {
|
---|
| 55 | depth = 1;
|
---|
| 56 | }
|
---|
| 57 | public ScopeTreeLookupParameter(string name, string description)
|
---|
| 58 | : base(name, description) {
|
---|
| 59 | depth = 1;
|
---|
| 60 | }
|
---|
| 61 | public ScopeTreeLookupParameter(string name, string description, string actualName)
|
---|
| 62 | : base(name, description, actualName) {
|
---|
| 63 | depth = 1;
|
---|
| 64 | }
|
---|
| 65 | [StorableConstructor]
|
---|
| 66 | protected ScopeTreeLookupParameter(bool deserializing) : base(deserializing) { }
|
---|
| 67 |
|
---|
| 68 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 69 | ScopeTreeLookupParameter<T> clone = (ScopeTreeLookupParameter<T>)base.Clone(cloner);
|
---|
| 70 | clone.depth = depth;
|
---|
| 71 | return clone;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[2757] | 74 | protected override IItem GetActualValue() {
|
---|
[2818] | 75 | string name = LookupParameter<ItemArray<T>>.TranslateName(Name, ExecutionContext);
|
---|
[3659] | 76 |
|
---|
| 77 | IEnumerable<IScope> scopes = new IScope[] { ExecutionContext.Scope };
|
---|
| 78 | for (int i = 0; i < depth; i++)
|
---|
| 79 | scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b));
|
---|
| 80 |
|
---|
| 81 | List<T> values = new List<T>();
|
---|
[2740] | 82 | IVariable var;
|
---|
[2756] | 83 | T value;
|
---|
[3659] | 84 | foreach (IScope scope in scopes) {
|
---|
| 85 | scope.Variables.TryGetValue(name, out var);
|
---|
[2756] | 86 | if (var != null) {
|
---|
| 87 | value = var.Value as T;
|
---|
[2852] | 88 | if ((var.Value != null) && (value == null))
|
---|
[2756] | 89 | throw new InvalidOperationException(
|
---|
| 90 | string.Format("Type mismatch. Variable \"{0}\" does not contain a \"{1}\".",
|
---|
| 91 | name,
|
---|
| 92 | typeof(T).GetPrettyName())
|
---|
| 93 | );
|
---|
[3659] | 94 | values.Add(value);
|
---|
[2756] | 95 | }
|
---|
[2740] | 96 | }
|
---|
[3659] | 97 | return new ItemArray<T>(values);
|
---|
[2740] | 98 | }
|
---|
[2757] | 99 | protected override void SetActualValue(IItem value) {
|
---|
| 100 | ItemArray<T> values = value as ItemArray<T>;
|
---|
| 101 | if (values == null)
|
---|
| 102 | throw new InvalidOperationException(
|
---|
| 103 | string.Format("Type mismatch. Value is not a \"{0}\".",
|
---|
| 104 | typeof(ItemArray<T>).GetPrettyName())
|
---|
| 105 | );
|
---|
| 106 |
|
---|
[2818] | 107 | string name = LookupParameter<ItemArray<T>>.TranslateName(Name, ExecutionContext);
|
---|
[3659] | 108 |
|
---|
| 109 | IEnumerable<IScope> scopes = new IScope[] { ExecutionContext.Scope };
|
---|
| 110 | for (int i = 0; i < depth; i++)
|
---|
| 111 | scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b));
|
---|
| 112 |
|
---|
| 113 | if (scopes.Count() != values.Length) throw new InvalidOperationException("Number of values is not equal to number of scopes.");
|
---|
| 114 |
|
---|
| 115 | int j = 0;
|
---|
[2740] | 116 | IVariable var;
|
---|
[3659] | 117 | foreach (IScope scope in scopes) {
|
---|
| 118 | scope.Variables.TryGetValue(name, out var);
|
---|
| 119 | if (var != null) var.Value = values[j];
|
---|
| 120 | else scope.Variables.Add(new Variable(name, values[j]));
|
---|
| 121 | j++;
|
---|
[2740] | 122 | }
|
---|
| 123 | }
|
---|
[3659] | 124 |
|
---|
| 125 | public event EventHandler DepthChanged;
|
---|
| 126 | protected virtual void OnDepthChanged() {
|
---|
| 127 | EventHandler handler = DepthChanged;
|
---|
| 128 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 129 | }
|
---|
[2740] | 130 | }
|
---|
| 131 | }
|
---|