Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/ScopePathLookupParameter.cs @ 7403

Last change on this file since 7403 was 7128, checked in by epitzer, 13 years ago

#1696 Integrate fitness landscape analysis plugins from Heureka! repository.

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