Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/27/10 03:55:16 (14 years ago)
Author:
swagner
Message:

Operator architecture refactoring (#95)

  • implemented ideas which came up during today's presentation of HeuristicLab.Core and related plugins
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Core/3.3/Scope.cs

    r2664 r2687  
    3333  [Item("Scope", "A scope which contains variables and sub-scopes.")]
    3434  [Creatable("Test")]
    35   public class Scope : NamedItem {
     35  public sealed class Scope : NamedItem, IScope {
    3636    [Storable]
    37     private Scope parent;
     37    private IScope parent;
     38    public IScope Parent {
     39      get { return parent; }
     40      set {
     41        if (parent != null) parent.SubScopes.Remove(this);
     42        parent = value;
     43        if ((parent != null) && !parent.SubScopes.Contains(this)) parent.SubScopes.Add(this);
     44      }
     45    }
    3846
    3947    private VariableCollection variables;
     
    7987    }
    8088
    81     public Variable Lookup(string name, bool recursive) {
    82       return Lookup(name, recursive, true);
    83     }
    84     public Variable Lookup(string name, bool recursive, bool throwOnError) {
    85       Variable variable;
    86 
    87       if (this.variables.TryGetValue(name, out variable)) {
    88         return variable;
    89       } else if (recursive) {
    90         Scope scope = this.parent;
    91         while (scope != null) {
    92           if (scope.variables.TryGetValue(name, out variable))
    93             return variable;
    94           scope = scope.parent;
    95         }
    96       }
    97       if (throwOnError)
    98         throw new ArgumentException("Variable " + name + " not found");
    99       else
    100         return null;
    101     }
    102 
    10389    /// <inheritdoc/>
    10490    public void Clear() {
     
    10995    /// <inheritdoc/>
    11096    public override IDeepCloneable Clone(Cloner cloner) {
    111       Scope clone = (Scope)base.Clone(cloner);
    112       clone.parent = (Scope)cloner.Clone(parent);
     97      Scope clone = new Scope();
     98      cloner.RegisterClonedObject(this, clone);
     99      clone.name = name;
     100      clone.description = description;
     101      clone.parent = (IScope)cloner.Clone(parent);
    113102      clone.Variables = (VariableCollection)cloner.Clone(variables);
    114103      clone.SubScopes = (ScopeList)cloner.Clone(subScopes);
     
    119108    private void RegisterSubScopesEvents() {
    120109      if (subScopes != null) {
    121         subScopes.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<Scope>>(SubScopes_ItemsAdded);
    122         subScopes.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<Scope>>(SubScopes_ItemsRemoved);
    123         subScopes.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<Scope>>(SubScopes_ItemsReplaced);
    124         subScopes.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<Scope>>(SubScopes_CollectionReset);
     110        subScopes.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
     111        subScopes.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
     112        subScopes.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
     113        subScopes.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
    125114        subScopes.Changed += new ChangedEventHandler(SubScopes_Changed);
    126115      }
     
    128117    private void DeregisterSubScopesEvents() {
    129118      if (subScopes != null) {
    130         subScopes.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<Scope>>(SubScopes_ItemsAdded);
    131         subScopes.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<Scope>>(SubScopes_ItemsRemoved);
    132         subScopes.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<Scope>>(SubScopes_ItemsReplaced);
    133         subScopes.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<Scope>>(SubScopes_CollectionReset);
     119        subScopes.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
     120        subScopes.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
     121        subScopes.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
     122        subScopes.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
    134123        subScopes.Changed -= new ChangedEventHandler(SubScopes_Changed);
    135124      }
    136125    }
    137     private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<Scope>> e) {
    138       foreach (IndexedItem<Scope> item in e.Items)
    139         item.Value.parent = this;
     126    private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
     127      foreach (IndexedItem<IScope> item in e.Items)
     128        item.Value.Parent = this;
    140129    }
    141     private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<Scope>> e) {
    142       foreach (IndexedItem<Scope> item in e.Items)
    143         item.Value.parent = null;
     130    private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
     131      foreach (IndexedItem<IScope> item in e.Items)
     132        item.Value.Parent = null;
    144133    }
    145     private void SubScopes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<Scope>> e) {
    146       foreach (IndexedItem<Scope> oldItem in e.OldItems)
    147         oldItem.Value.parent = null;
    148       foreach (IndexedItem<Scope> item in e.Items)
    149         item.Value.parent = this;
     134    private void SubScopes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
     135      foreach (IndexedItem<IScope> oldItem in e.OldItems)
     136        oldItem.Value.Parent = null;
     137      foreach (IndexedItem<IScope> item in e.Items)
     138        item.Value.Parent = this;
    150139    }
    151     private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<Scope>> e) {
    152       foreach (IndexedItem<Scope> oldItem in e.OldItems)
    153         oldItem.Value.parent = null;
    154       foreach (IndexedItem<Scope> item in e.Items)
    155         item.Value.parent = this;
     140    private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
     141      foreach (IndexedItem<IScope> oldItem in e.OldItems)
     142        oldItem.Value.Parent = null;
     143      foreach (IndexedItem<IScope> item in e.Items)
     144        item.Value.Parent = this;
    156145    }
    157146    private void SubScopes_Changed(object sender, ChangedEventArgs e) {
Note: See TracChangeset for help on using the changeset viewer.