Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/19/10 02:15:10 (14 years ago)
Author:
swagner
Message:

Operator architecture refactoring (#95)

  • worked on operators and SGA
  • improved performance
Location:
trunk/sources/HeuristicLab.Core/3.3
Files:
12 edited

Legend:

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

    r2790 r2830  
    2121
    2222using System;
    23 using System.Collections.Generic;
    24 using System.Text;
    25 using System.Xml;
    2623using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2724
  • trunk/sources/HeuristicLab.Core/3.3/Item.cs

    r2790 r2830  
    2020#endregion
    2121
    22 using System;
    23 using System.Collections.Generic;
    24 using System.ComponentModel;
    25 using System.Text;
    26 using System.Xml;
    2722using System.Drawing;
    28 using System.Resources;
     23using HeuristicLab.Common.Resources;
    2924using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    30 using HeuristicLab.Common.Resources;
    3125
    3226namespace HeuristicLab.Core {
     
    6155    protected virtual void OnChanged(ChangedEventArgs e) {
    6256      if ((e.RegisterChangedObject(this)) && (Changed != null))
    63           Changed(this, e);
     57        Changed(this, e);
    6458    }
    6559  }
  • trunk/sources/HeuristicLab.Core/3.3/ItemArray.cs

    r2790 r2830  
    2121
    2222using System;
     23using System.Linq;
    2324using System.Collections;
    2425using System.Collections.Generic;
     
    5556    public ItemArray() : base() { }
    5657    public ItemArray(int length) : base(length) { }
    57     public ItemArray(T[] array) : base(array) { }
    58     public ItemArray(IEnumerable<T> collection) : base(collection) { }
     58    public ItemArray(T[] array) : base(array) {
     59      foreach (T item in this)
     60        item.Changed += new ChangedEventHandler(Item_Changed);
     61    }
     62    public ItemArray(IEnumerable<T> collection) : base(collection) {
     63      foreach (T item in this)
     64        item.Changed += new ChangedEventHandler(Item_Changed);
     65    }
    5966
    6067    public object Clone() {
     
    6269    }
    6370
    64     public IDeepCloneable Clone(Cloner cloner) {
    65       T[] items = new T[Length];
    66       for (int i = 0; i < items.Length; i++)
    67         items[i] = (T)cloner.Clone(this[i]);
    68       ItemArray<T> clone = (ItemArray<T>)Activator.CreateInstance(this.GetType(), new object[] { items });
     71    public virtual IDeepCloneable Clone(Cloner cloner) {
     72      ItemArray<T> clone = (ItemArray<T>)Activator.CreateInstance(this.GetType(), this.Select(x => (T)cloner.Clone(x)));
    6973      cloner.RegisterClonedObject(this, clone);
    7074      return clone;
  • trunk/sources/HeuristicLab.Core/3.3/ItemCollection.cs

    r2790 r2830  
    2121
    2222using System;
     23using System.Linq;
    2324using System.Collections;
    2425using System.Collections.Generic;
     
    5657    public ItemCollection() : base() { }
    5758    public ItemCollection(int capacity) : base(capacity) { }
    58     public ItemCollection(IEnumerable<T> collection) : base(collection) { }
     59    public ItemCollection(IEnumerable<T> collection) : base(collection) {
     60      foreach (T item in this)
     61        item.Changed += new ChangedEventHandler(Item_Changed);
     62    }
    5963
    6064    public object Clone() {
     
    6266    }
    6367
    64     public IDeepCloneable Clone(Cloner cloner) {
    65       List<T> items = new List<T>();
    66       foreach (T item in this)
    67         items.Add((T)cloner.Clone(item));
    68       ItemCollection<T> clone = (ItemCollection<T>)Activator.CreateInstance(this.GetType(), items);
     68    public virtual IDeepCloneable Clone(Cloner cloner) {
     69      ItemCollection<T> clone = (ItemCollection<T>)Activator.CreateInstance(this.GetType(), this.Select(x => (T)cloner.Clone(x)));
    6970      cloner.RegisterClonedObject(this, clone);
    7071      return clone;
  • trunk/sources/HeuristicLab.Core/3.3/ItemList.cs

    r2790 r2830  
    2121
    2222using System;
     23using System.Linq;
    2324using System.Collections;
    2425using System.Collections.Generic;
     
    5657    public ItemList() : base() { }
    5758    public ItemList(int capacity) : base(capacity) { }
    58     public ItemList(IEnumerable<T> collection) : base(collection) { }
     59    public ItemList(IEnumerable<T> collection) : base(collection) {
     60      foreach (T item in this)
     61        item.Changed += new ChangedEventHandler(Item_Changed);
     62    }
    5963
    6064    public object Clone() {
     
    6266    }
    6367
    64     public IDeepCloneable Clone(Cloner cloner) {
    65       List<T> items = new List<T>();
    66       foreach (T item in this)
    67         items.Add((T)cloner.Clone(item));
    68       ItemList<T> clone = (ItemList<T>)Activator.CreateInstance(this.GetType(), items);
     68    public virtual IDeepCloneable Clone(Cloner cloner) {
     69      ItemList<T> clone = (ItemList<T>)Activator.CreateInstance(this.GetType(), this.Select(x => (T)cloner.Clone(x)));
    6970      cloner.RegisterClonedObject(this, clone);
    7071      return clone;
  • trunk/sources/HeuristicLab.Core/3.3/ItemSet.cs

    r2790 r2830  
    2121
    2222using System;
     23using System.Linq;
    2324using System.Collections;
    2425using System.Collections.Generic;
     
    5556
    5657    public ItemSet() : base() { }
    57     public ItemSet(IEnumerable<T> collection) : base(collection) { }
     58    public ItemSet(IEnumerable<T> collection) : base(collection) {
     59      foreach (T item in this)
     60        item.Changed += new ChangedEventHandler(Item_Changed);
     61    }
    5862
    5963    public object Clone() {
     
    6165    }
    6266
    63     public IDeepCloneable Clone(Cloner cloner) {
    64       List<T> items = new List<T>();
    65       foreach (T item in this)
    66         items.Add((T)cloner.Clone(item));
    67       ItemSet<T> clone = (ItemSet<T>)Activator.CreateInstance(this.GetType(), items);
     67    public virtual IDeepCloneable Clone(Cloner cloner) {
     68      ItemSet<T> clone = (ItemSet<T>)Activator.CreateInstance(this.GetType(), this.Select(x => (T)cloner.Clone(x)));
    6869      cloner.RegisterClonedObject(this, clone);
    6970      return clone;
  • trunk/sources/HeuristicLab.Core/3.3/NamedItem.cs

    r2793 r2830  
    7474    /// </summary>
    7575    protected NamedItem() {
    76       name = ItemName;
    77       description = ItemDescription;
     76      name = string.Empty;
     77      description = string.Empty;
    7878    }
    7979    /// <summary>
     
    8383    /// <param name="name">The name of the current instance.</param>
    8484    /// <param name="value">The value of the current instance.</param>
    85     protected NamedItem(string name)
    86       : this() {
     85    protected NamedItem(string name) {
    8786      if (name == null) throw new ArgumentNullException();
    8887      this.name = name;
     88      description = string.Empty;
    8989    }
    90     protected NamedItem(string name, string description)
    91       : this(name) {
     90    protected NamedItem(string name, string description) {
     91      if (name == null) throw new ArgumentNullException();
     92      this.name = name;
    9293      this.description = description;
    9394    }
  • trunk/sources/HeuristicLab.Core/3.3/NamedItemCollection.cs

    r2790 r2830  
    2121
    2222using System;
     23using System.Linq;
    2324using System.Collections.Generic;
    2425using System.Drawing;
     
    4344    private object RestoreEvents {
    4445      get { return null; }
    45       set {
    46         foreach (T item in this) {
    47           item.NameChanging += new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
    48           item.NameChanged += new EventHandler(Item_NameChanged);
    49           item.Changed += new ChangedEventHandler(Item_Changed);
    50         }
    51       }
     46      set { RegisterItemEvents(this); }
    5247    }
    5348
    5449    public NamedItemCollection() : base() { }
    5550    public NamedItemCollection(int capacity) : base(capacity) { }
    56     public NamedItemCollection(IEnumerable<T> collection) : base(collection) { }
     51    public NamedItemCollection(IEnumerable<T> collection) : base(collection) {
     52      RegisterItemEvents(this);
     53    }
    5754
    5855    public object Clone() {
    5956      return Clone(new Cloner());
    6057    }
    61     public IDeepCloneable Clone(Cloner cloner) {
    62       List<T> items = new List<T>();
    63       foreach (T item in this)
    64         items.Add((T)cloner.Clone(item));
    65       NamedItemCollection<T> clone = (NamedItemCollection<T>)Activator.CreateInstance(this.GetType(), items);
     58    public virtual IDeepCloneable Clone(Cloner cloner) {
     59      NamedItemCollection<T> clone = (NamedItemCollection<T>)Activator.CreateInstance(this.GetType(), this.Select(x => (T)cloner.Clone(x)));
    6660      cloner.RegisterClonedObject(this, clone);
    6761      return clone;
     
    8680
    8781    protected override void OnItemsAdded(IEnumerable<T> items) {
    88       foreach (T item in items) {
    89         item.NameChanging += new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
    90         item.NameChanged += new EventHandler(Item_NameChanged);
    91         item.Changed += new ChangedEventHandler(Item_Changed);
    92       }
     82      RegisterItemEvents(items);
    9383      base.OnItemsAdded(items);
    9484    }
    9585    protected override void OnItemsRemoved(IEnumerable<T> items) {
    96       foreach (T item in items) {
    97         item.NameChanging -= new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
    98         item.NameChanged -= new EventHandler(Item_NameChanged);
    99         item.Changed -= new ChangedEventHandler(Item_Changed);
    100       }
     86      DeregisterItemEvents(items);
    10187      base.OnItemsRemoved(items);
    10288    }
     
    10894    #endregion
    10995    protected override void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
    110       foreach (T oldItem in oldItems) {
    111         oldItem.NameChanging -= new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
    112         oldItem.NameChanged -= new EventHandler(Item_NameChanged);
    113         oldItem.Changed -= new ChangedEventHandler(Item_Changed);
    114       }
     96      DeregisterItemEvents(oldItems);
     97      RegisterItemEvents(items);
     98      base.OnCollectionReset(items, oldItems);
     99    }
     100    protected override void OnPropertyChanged(string propertyName) {
     101      base.OnPropertyChanged(propertyName);
     102      OnChanged();
     103    }
     104
     105    private void RegisterItemEvents(IEnumerable<T> items) {
    115106      foreach (T item in items) {
    116107        item.NameChanging += new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
     
    118109        item.Changed += new ChangedEventHandler(Item_Changed);
    119110      }
    120       base.OnCollectionReset(items, oldItems);
    121111    }
    122     protected override void OnPropertyChanged(string propertyName) {
    123       base.OnPropertyChanged(propertyName);
    124       OnChanged();
     112    private void DeregisterItemEvents(IEnumerable<T> items) {
     113      foreach (T item in items) {
     114        item.NameChanging -= new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
     115        item.NameChanged -= new EventHandler(Item_NameChanged);
     116        item.Changed -= new ChangedEventHandler(Item_Changed);
     117      }
    125118    }
    126119
  • trunk/sources/HeuristicLab.Core/3.3/Scope.cs

    r2790 r2830  
    2020#endregion
    2121
    22 using System;
    23 using System.Collections.Generic;
    24 using System.Text;
    25 using System.Xml;
     22using HeuristicLab.Collections;
    2623using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    27 using HeuristicLab.Collections;
    2824
    2925namespace HeuristicLab.Core {
     
    9995      clone.Name = Name;
    10096      clone.Description = Description;
    101       clone.parent = (IScope)cloner.Clone(parent);
    102       clone.Variables = (VariableCollection)cloner.Clone(variables);
    103       clone.SubScopes = (ScopeList)cloner.Clone(subScopes);
     97      if (variables.Count > 0) clone.Variables = (VariableCollection)cloner.Clone(variables);
     98      if (subScopes.Count > 0) clone.SubScopes = (ScopeList)cloner.Clone(subScopes);
    10499      return clone;
    105100    }
  • trunk/sources/HeuristicLab.Core/3.3/ScopeList.cs

    r2790 r2830  
    2020#endregion
    2121
    22 using System;
    23 using System.Collections;
    2422using System.Collections.Generic;
    25 using System.Collections.ObjectModel;
    26 using System.Text;
    27 using System.Drawing;
     23using System.Linq;
    2824using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    29 using HeuristicLab.Common.Resources;
    30 using HeuristicLab.Collections;
    3125
    3226namespace HeuristicLab.Core {
     
    3428  [Item("ScopeList", "Represents a list of scopes.")]
    3529  [Creatable("Test")]
    36   public class ScopeList : ItemList<IScope> {
     30  public sealed class ScopeList : ItemList<IScope> {
    3731    public ScopeList() : base() { }
    3832    public ScopeList(int capacity) : base(capacity) { }
    3933    public ScopeList(IEnumerable<IScope> collection) : base(collection) { }
     34
     35    public override IDeepCloneable Clone(Cloner cloner) {
     36      ScopeList clone = new ScopeList(this.Select(x => (IScope)cloner.Clone(x)));
     37      cloner.RegisterClonedObject(this, clone);
     38      return clone;
     39    }
    4040  }
    4141}
  • trunk/sources/HeuristicLab.Core/3.3/Variable.cs

    r2818 r2830  
    5353    public Variable()
    5454      : base("Anonymous") {
    55       Value = null;
     55      this.value = null;
    5656    }
    5757    /// <summary>
     
    6161    /// <param name="name">The name of the current instance.</param>
    6262    /// <param name="value">The value of the current instance.</param>
     63    public Variable(string name)
     64      : base(name) {
     65      this.value = null;
     66    }
     67    public Variable(string name, string description)
     68      : base(name, description) {
     69      this.value = null;
     70    }
    6371    public Variable(string name, IItem value)
    6472      : base(name) {
    65       Value = value;
     73      this.value = value;
     74      this.value.Changed += new ChangedEventHandler(Value_Changed);
     75    }
     76    public Variable(string name, string description, IItem value)
     77      : base(name, description) {
     78      this.value = value;
     79      this.value.Changed += new ChangedEventHandler(Value_Changed);
    6680    }
    6781
     
    7286    /// <returns>The cloned object as <see cref="Variable"/>.</returns>
    7387    public override IDeepCloneable Clone(Cloner cloner) {
    74       Variable clone = new Variable();
     88      Variable clone = new Variable(Name, Description, (IItem)cloner.Clone(value));
    7589      cloner.RegisterClonedObject(this, clone);
    76       clone.Name = Name;
    77       clone.Description = Description;
    78       clone.Value = (IItem)cloner.Clone(value);
    7990      return clone;
    8091    }
  • trunk/sources/HeuristicLab.Core/3.3/VariableCollection.cs

    r2790 r2830  
    2121
    2222using System.Collections.Generic;
     23using System.Linq;
    2324using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2425
     
    2728  [Item("VariableCollection", "Represents a collection of variables.")]
    2829  [Creatable("Test")]
    29   public class VariableCollection : NamedItemCollection<IVariable> {
     30  public sealed class VariableCollection : NamedItemCollection<IVariable> {
    3031    public VariableCollection() : base() { }
    3132    public VariableCollection(int capacity) : base(capacity) { }
    3233    public VariableCollection(IEnumerable<IVariable> collection) : base(collection) { }
     34
     35    public override IDeepCloneable Clone(Cloner cloner) {
     36      VariableCollection clone = new VariableCollection(this.Select(x => (IVariable)cloner.Clone(x)));
     37      cloner.RegisterClonedObject(this, clone);
     38      return clone;
     39    }
    3340  }
    3441}
Note: See TracChangeset for help on using the changeset viewer.