[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>
|
---|
[3822] | 33 | [Item("ScopeTreeLookupParameter", "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 | }
|
---|
[3740] | 57 | public ScopeTreeLookupParameter(string name, int depth)
|
---|
| 58 | : base(name) {
|
---|
| 59 | this.depth = depth;
|
---|
| 60 | }
|
---|
[3659] | 61 | public ScopeTreeLookupParameter(string name, string description)
|
---|
| 62 | : base(name, description) {
|
---|
| 63 | depth = 1;
|
---|
| 64 | }
|
---|
[3740] | 65 | public ScopeTreeLookupParameter(string name, string description, int depth)
|
---|
| 66 | : base(name, description) {
|
---|
| 67 | this.depth = depth;
|
---|
| 68 | }
|
---|
[3659] | 69 | public ScopeTreeLookupParameter(string name, string description, string actualName)
|
---|
| 70 | : base(name, description, actualName) {
|
---|
| 71 | depth = 1;
|
---|
| 72 | }
|
---|
[3740] | 73 | public ScopeTreeLookupParameter(string name, string description, string actualName, int depth)
|
---|
| 74 | : base(name, description, actualName) {
|
---|
| 75 | this.depth = depth;
|
---|
| 76 | }
|
---|
[3659] | 77 | [StorableConstructor]
|
---|
| 78 | protected ScopeTreeLookupParameter(bool deserializing) : base(deserializing) { }
|
---|
| 79 |
|
---|
| 80 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 81 | ScopeTreeLookupParameter<T> clone = (ScopeTreeLookupParameter<T>)base.Clone(cloner);
|
---|
| 82 | clone.depth = depth;
|
---|
| 83 | return clone;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[2757] | 86 | protected override IItem GetActualValue() {
|
---|
[3659] | 87 | IEnumerable<IScope> scopes = new IScope[] { ExecutionContext.Scope };
|
---|
| 88 | for (int i = 0; i < depth; i++)
|
---|
| 89 | scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b));
|
---|
| 90 |
|
---|
[3687] | 91 | string name = TranslatedName;
|
---|
[3659] | 92 | List<T> values = new List<T>();
|
---|
[2740] | 93 | IVariable var;
|
---|
[2756] | 94 | T value;
|
---|
[3659] | 95 | foreach (IScope scope in scopes) {
|
---|
| 96 | scope.Variables.TryGetValue(name, out var);
|
---|
[2756] | 97 | if (var != null) {
|
---|
| 98 | value = var.Value as T;
|
---|
[2852] | 99 | if ((var.Value != null) && (value == null))
|
---|
[2756] | 100 | throw new InvalidOperationException(
|
---|
| 101 | string.Format("Type mismatch. Variable \"{0}\" does not contain a \"{1}\".",
|
---|
| 102 | name,
|
---|
| 103 | typeof(T).GetPrettyName())
|
---|
| 104 | );
|
---|
[3659] | 105 | values.Add(value);
|
---|
[2756] | 106 | }
|
---|
[2740] | 107 | }
|
---|
[3659] | 108 | return new ItemArray<T>(values);
|
---|
[2740] | 109 | }
|
---|
[2757] | 110 | protected override void SetActualValue(IItem value) {
|
---|
| 111 | ItemArray<T> values = value as ItemArray<T>;
|
---|
| 112 | if (values == null)
|
---|
| 113 | throw new InvalidOperationException(
|
---|
| 114 | string.Format("Type mismatch. Value is not a \"{0}\".",
|
---|
| 115 | typeof(ItemArray<T>).GetPrettyName())
|
---|
| 116 | );
|
---|
| 117 |
|
---|
[3659] | 118 | IEnumerable<IScope> scopes = new IScope[] { ExecutionContext.Scope };
|
---|
| 119 | for (int i = 0; i < depth; i++)
|
---|
| 120 | scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b));
|
---|
| 121 |
|
---|
| 122 | if (scopes.Count() != values.Length) throw new InvalidOperationException("Number of values is not equal to number of scopes.");
|
---|
| 123 |
|
---|
[3687] | 124 | string name = TranslatedName;
|
---|
[3659] | 125 | int j = 0;
|
---|
[2740] | 126 | IVariable var;
|
---|
[3659] | 127 | foreach (IScope scope in scopes) {
|
---|
| 128 | scope.Variables.TryGetValue(name, out var);
|
---|
| 129 | if (var != null) var.Value = values[j];
|
---|
| 130 | else scope.Variables.Add(new Variable(name, values[j]));
|
---|
| 131 | j++;
|
---|
[2740] | 132 | }
|
---|
| 133 | }
|
---|
[3659] | 134 |
|
---|
| 135 | public event EventHandler DepthChanged;
|
---|
| 136 | protected virtual void OnDepthChanged() {
|
---|
| 137 | EventHandler handler = DepthChanged;
|
---|
| 138 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 139 | }
|
---|
[2740] | 140 | }
|
---|
| 141 | }
|
---|