Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/ScopePathLookupParameter.cs @ 16591

Last change on this file since 16591 was 16573, checked in by gkronber, 6 years ago

#2520: changed HeuristicLab.FLA addon to compile with new HL.Persistence

File size: 3.4 KB
Line 
1using System;
2using System.Text.RegularExpressions;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
6using HEAL.Attic;
7
8namespace HeuristicLab.Parameters {
9
10  [Item("ScopePathLookupParameter", "Start parameter lookup after freely navigating the scope tree using a path string.")]
11  [StorableType("3E5F3D67-61FD-427C-A822-6C623C0AA3DA")]
12  public class ScopePathLookupParameter<T> : LookupParameter<T>, ILookupParameter<T> where T : class, IItem {
13
14    #region Properties
15
16    public static Regex ValidPathRegex = new Regex("|(..|[a-zA-Z0-9_ ]+|\\{[0-9]+\\})(/(..|[a-zA-Z0-9_ ]+|\\{[0-9]+\\}))?");
17
18    [Storable]
19    private string path;
20    public string Path {
21      get { return path; }
22      set {
23        if (!ValidPathRegex.IsMatch(value))
24          throw new ArgumentException("Invalid Path");
25        if (value != null) {
26          path = value;
27          OnPathChanged();
28          OnToStringChanged();
29        }
30      }
31    }
32
33    #endregion
34
35    #region Constructors & Cloning
36
37    [StorableConstructor]
38    protected ScopePathLookupParameter(StorableConstructorFlag _) : base(_) { }
39    protected ScopePathLookupParameter(ScopePathLookupParameter<T> original, Cloner cloner)
40      : base(original, cloner) {
41      path = original.path;
42    }
43    public ScopePathLookupParameter()
44      : base() {
45      path = "";
46    }
47    public ScopePathLookupParameter(string name)
48      : base(name) {
49      path = "";
50    }
51    public ScopePathLookupParameter(string name, string description)
52      : base(name, description) {
53      path = "";
54    }
55    public ScopePathLookupParameter(string name, string description, string actualName)
56      : base(name, description, actualName) {
57      path = "";
58    }
59
60    public override IDeepCloneable Clone(Cloner cloner) {
61      return new ScopePathLookupParameter<T>(this, cloner);
62    }
63
64    #endregion
65
66    public static IScope NavigateScope(IScope scope, string path) {
67      foreach (var dir in path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries)) {
68        if (dir == "..")
69          scope = scope.Parent;
70        else if (dir.StartsWith("{")) {
71          scope = scope.SubScopes[int.Parse(dir.Substring(1, dir.Length-2))];
72        } else {
73          scope = scope.SubScopes.Find(s => s.Name == dir);
74        }
75      }
76      return scope;
77    }
78
79    protected override IItem GetActualValue() {
80      var originalContext = ExecutionContext as ExecutionContext;
81      ExecutionContext = new ExecutionContext(originalContext.Parent, originalContext.Operator, NavigateScope(originalContext.Scope, Path));
82      IItem result = base.GetActualValue();
83      ExecutionContext = originalContext;
84      return result;
85    }
86
87    protected override void SetActualValue(IItem value) {
88      var originalContext = ExecutionContext as ExecutionContext;
89      ExecutionContext = new ExecutionContext(originalContext.Parent, originalContext.Operator, NavigateScope(originalContext.Scope, Path));
90      base.SetActualValue(value);
91      ExecutionContext = originalContext;
92    }
93
94    public event EventHandler PathChanged;
95    protected virtual void OnPathChanged() {
96      EventHandler handler = PathChanged;
97      if (handler != null)
98        handler(this, EventArgs.Empty);
99    }
100
101    public override string ToString() {
102      return string.Format("{0} [{1}]", base.ToString(), Path);
103    }
104  } 
105}
Note: See TracBrowser for help on using the repository browser.