Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/23/09 16:43:34 (15 years ago)
Author:
swagner
Message:

Refactored cloning (#806)

Location:
trunk/sources/HeuristicLab.Core/3.3
Files:
2 added
3 deleted
17 edited

Legend:

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

    r1823 r2526  
    6969    /// </summary>
    7070    /// <remarks>The operator and the scope objects are cloned with the
    71     /// <see cref="HeuristicLab.Core.Auxiliary.Clone"/> method of the <see cref="Auxiliary"/> class.</remarks>
     71    /// <see cref="HeuristicLab.Core.cloner.Clone"/> method of the <see cref="Auxiliary"/> class.</remarks>
    7272    /// <param name="clonedObjects">All already cloned objects. (Needed to avoid cycles.)</param>
    7373    /// <returns>The cloned object as <see cref="AtomicOperation"/>.</returns>
    74     public override object Clone(IDictionary<Guid, object> clonedObjects) {
     74    public override IItem Clone(ICloner cloner) {
    7575      AtomicOperation clone = new AtomicOperation();
    76       clonedObjects.Add(Guid, clone);
    77       clone.myOperator = (IOperator)Auxiliary.Clone(Operator, clonedObjects);
    78       clone.myScope = (IScope)Auxiliary.Clone(Scope, clonedObjects);
     76      cloner.RegisterClonedObject(this, clone);
     77      clone.myOperator = (IOperator)cloner.Clone(Operator);
     78      clone.myScope = (IScope)cloner.Clone(Scope);
    7979      return clone;
    8080    }
  • trunk/sources/HeuristicLab.Core/3.3/CompositeOperation.cs

    r1823 r2526  
    8181    /// </summary>
    8282    /// <remarks>All operations of the current instance are cloned, too (deep clone), with the
    83     /// <see cref="HeuristicLab.Core.Auxiliary.Clone"/> method of the class <see cref="Auxiliary"/>.</remarks>
     83    /// <see cref="HeuristicLab.Core.cloner.Clone"/> method of the class <see cref="Auxiliary"/>.</remarks>
    8484    /// <param name="clonedObjects">A dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
    8585    /// <returns>The cloned operation as <see cref="CompositeOperation"/>.</returns>
    86     public override object Clone(IDictionary<Guid, object> clonedObjects) {
     86    public override IItem Clone(ICloner cloner) {
    8787      CompositeOperation clone = new CompositeOperation();
    88       clonedObjects.Add(Guid, clone);
     88      cloner.RegisterClonedObject(this, clone);
    8989      clone.myExecuteInParallel = ExecuteInParallel;
    9090      for (int i = 0; i < Operations.Count; i++)
    91         clone.AddOperation((IOperation)Auxiliary.Clone(Operations[i], clonedObjects));
     91        clone.AddOperation((IOperation)cloner.Clone(Operations[i]));
    9292      return clone;
    9393    }
  • trunk/sources/HeuristicLab.Core/3.3/EngineBase.cs

    r2474 r2526  
    127127    /// Clones the current instance (deep clone).
    128128    /// </summary>
    129     /// <remarks>Deep clone through <see cref="Auxiliary.Clone"/> method of helper class
     129    /// <remarks>Deep clone through <see cref="cloner.Clone"/> method of helper class
    130130    /// <see cref="Auxiliary"/>.</remarks>
    131131    /// <param name="clonedObjects">Dictionary of all already clone objects. (Needed to avoid cycles.)</param>
    132132    /// <returns>The cloned object as <see cref="EngineBase"/>.</returns>
    133     public override object Clone(IDictionary<Guid, object> clonedObjects) {
    134       EngineBase clone = (EngineBase)base.Clone(clonedObjects);
    135       clone.myOperatorGraph = (IOperatorGraph)Auxiliary.Clone(OperatorGraph, clonedObjects);
    136       clone.myGlobalScope = (IScope)Auxiliary.Clone(GlobalScope, clonedObjects);
     133    public override IItem Clone(ICloner cloner) {
     134      EngineBase clone = (EngineBase)base.Clone(cloner);
     135      clone.myOperatorGraph = (IOperatorGraph)cloner.Clone(OperatorGraph);
     136      clone.myGlobalScope = (IScope)cloner.Clone(GlobalScope);
    137137      clone.myExecutionTime = ExecutionTime;
    138138      IOperation[] operations = new IOperation[ExecutionStack.Count];
    139139      ExecutionStack.CopyTo(operations, 0);
    140140      for (int i = operations.Length - 1; i >= 0; i--)
    141         clone.myExecutionStack.Push((IOperation)Auxiliary.Clone(operations[i], clonedObjects));
     141        clone.myExecutionStack.Push((IOperation)cloner.Clone(operations[i]));
    142142      clone.myRunning = Running;
    143143      clone.myCanceled = Canceled;
  • trunk/sources/HeuristicLab.Core/3.3/HeuristicLab.Core-3.3.csproj

    r2524 r2526  
    9898  </ItemGroup>
    9999  <ItemGroup>
    100     <Compile Include="Auxiliary.cs" />
    101100    <Compile Include="AtomicOperation.cs" />
    102101    <Compile Include="CompositeOperation.cs" />
     102    <Compile Include="Cloner.cs" />
     103    <Compile Include="Interfaces\ICloner.cs" />
    103104    <Compile Include="Interfaces\IEditable.cs" />
    104105    <Compile Include="Interfaces\IOperation.cs" />
     
    119120    <Compile Include="Interfaces\IRandom.cs" />
    120121    <Compile Include="OperatorGraph.cs" />
    121     <Compile Include="StorableBase.cs" />
    122122    <Compile Include="Scope.cs" />
    123123    <Compile Include="EngineBase.cs" />
    124124    <Compile Include="Interfaces\IEngine.cs" />
    125125    <Compile Include="Interfaces\IOperator.cs" />
    126     <Compile Include="Interfaces\IStorable.cs" />
    127126    <Compile Include="PersistenceManager.cs" />
    128127    <Compile Include="HeuristicLabCorePlugin.cs" />
  • trunk/sources/HeuristicLab.Core/3.3/Interfaces/IItem.cs

    r2520 r2526  
    2828  /// Interface to represent (almost) every HeuristicLab object (an object, an operator,...).
    2929  /// </summary>
    30   public interface IItem : IStorable {
     30  public interface IItem : ICloneable {
     31    /// <summary>
     32    /// Creates a deep clone of this item.
     33    /// </summary>
     34    /// <param name="cloner">The cloner which is responsible for keeping track of all already
     35    /// cloned objects.</param>
     36    /// <returns>A clone of this instance.</returns>
     37    IItem Clone(ICloner cloner);
     38
    3139    /// <summary>
    3240    /// Fires a new <c>Changed</c> event.
  • trunk/sources/HeuristicLab.Core/3.3/Interfaces/IOperatorGraph.cs

    r2474 r2526  
    4949    /// </summary>
    5050    /// <param name="guid">The unique id of the operator to remove.</param>
    51     void RemoveOperator(Guid guid);
    52     /// <summary>
    53     /// Gets the operator with the specified <paramref name="guid"/>.
    54     /// </summary>
    55     /// <param name="guid">The unique id of the operator.</param>
    56     /// <returns>The searched operator.</returns>
    57     IOperator GetOperator(Guid guid);
     51    void RemoveOperator(IOperator op);
    5852    /// <summary>
    5953    /// Clears the current instance.
  • trunk/sources/HeuristicLab.Core/3.3/Interfaces/IOperatorGroup.cs

    r776 r2526  
    2929  /// Interface to represent a group of operators.
    3030  /// </summary>
    31   public interface IOperatorGroup : IStorable {
     31  public interface IOperatorGroup : IItem {
    3232    /// <summary>
    3333    /// Gets or sets the name of the current instance.
  • trunk/sources/HeuristicLab.Core/3.3/Interfaces/IScope.cs

    r2474 r2526  
    130130    void ReorderSubScopes(int[] sequence);
    131131    /// <summary>
    132     /// Gets the sub scope with the given <paramref name="guid"/>.
    133     /// </summary>
    134     /// <param name="guid">The unique identifier of the sub scope.</param>
    135     /// <returns>The sub scope with the given <paramref name="guid"/>.</returns>
    136     IScope GetScope(Guid guid);
    137     /// <summary>
    138132    /// Gets the sub scope with the given <paramref name="name"/>.
    139133    /// </summary>
  • trunk/sources/HeuristicLab.Core/3.3/ItemBase.cs

    r2520 r2526  
    3131  /// </summary>
    3232  [EmptyStorableClass]
    33   public abstract class ItemBase : StorableBase, IItem {
     33  public abstract class ItemBase : IItem {
     34    /// <summary>
     35    /// Creates a deep clone of this instance.
     36    /// </summary>
     37    /// <remarks>
     38    /// This method is the entry point for creating a deep clone of a whole object graph.
     39    /// </remarks>
     40    /// <returns>A clone of this instance.</returns>
     41    public object Clone() {
     42      return Clone(new Cloner());
     43    }
     44
     45    /// <summary>
     46    /// Creates a deep clone of this instance.
     47    /// </summary>
     48    /// <remarks>This method should not be called directly. It is used for creating clones of
     49    /// objects which are contained in the object that is currently cloned.</remarks>
     50    /// <param name="cloner">The cloner which is responsible for keeping track of all already
     51    /// cloned objects.</param>
     52    /// <returns>A clone of this instance.</returns>
     53    public virtual IItem Clone(ICloner cloner) {
     54      ItemBase clone = (ItemBase)Activator.CreateInstance(this.GetType());
     55      cloner.RegisterClonedObject(this, clone);
     56      return clone;
     57    }
     58
    3459    /// <summary>
    3560    /// Gets the string representation of the current instance.
  • trunk/sources/HeuristicLab.Core/3.3/OperatorBase.cs

    r2524 r2526  
    131131    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
    132132    /// <returns>The cloned object as <see cref="OperatorBase"/>.</returns>
    133     public override object Clone(IDictionary<Guid, object> clonedObjects) {
    134       OperatorBase clone = (OperatorBase)base.Clone(clonedObjects);
     133    public override IItem Clone(ICloner cloner) {
     134      OperatorBase clone = (OperatorBase)base.Clone(cloner);
    135135      clone.myName = Name;
    136136      clone.mySubOperators.Clear();
    137137      for (int i = 0; i < SubOperators.Count; i++)
    138         clone.AddSubOperator((IOperator)Auxiliary.Clone(SubOperators[i], clonedObjects));
     138        clone.AddSubOperator((IOperator)cloner.Clone(SubOperators[i]));
    139139      clone.myVariableInfos.Clear();
    140140      foreach (IVariableInfo variableInfo in myVariableInfos.Values)
    141         clone.AddVariableInfo((IVariableInfo)Auxiliary.Clone(variableInfo, clonedObjects));
     141        clone.AddVariableInfo((IVariableInfo)cloner.Clone(variableInfo));
    142142      clone.myVariables.Clear();
    143143      foreach (IVariable variable in myVariables.Values)
    144         clone.AddVariable((IVariable)Auxiliary.Clone(variable, clonedObjects));
     144        clone.AddVariable((IVariable)cloner.Clone(variable));
    145145      return clone;
    146146    }
  • trunk/sources/HeuristicLab.Core/3.3/OperatorGraph.cs

    r2520 r2526  
    3434
    3535    [Storable]
    36     private IDictionary<Guid, IOperator> myOperators;
     36    private IDictionary<IOperator, IOperator> myOperators;
    3737    /// <summary>
    3838    /// Gets all operators of the current instance.
     
    6262    /// </summary>
    6363    public OperatorGraph() {
    64       myOperators = new Dictionary<Guid, IOperator>();
     64      myOperators = new Dictionary<IOperator, IOperator>();
    6565    }
    6666
     
    6868    /// Clones the current instance (deep clone).
    6969    /// </summary>
    70     /// <remarks>Deep clone through <see cref="Auxiliary.Clone"/> method of helper class
     70    /// <remarks>Deep clone through <see cref="cloner.Clone"/> method of helper class
    7171    /// <see cref="Auxiliary"/>.</remarks>
    7272    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
    7373    /// <returns>The cloned object as <see cref="OperatorGraph"/>.</returns>
    74     public override object Clone(IDictionary<Guid, object> clonedObjects) {
     74    public override IItem Clone(ICloner cloner) {
    7575      OperatorGraph clone = new OperatorGraph();
    76       clonedObjects.Add(Guid, clone);
     76      cloner.RegisterClonedObject(this, clone);
    7777      foreach (IOperator op in Operators)
    78         clone.AddOperator((IOperator)Auxiliary.Clone(op, clonedObjects));
     78        clone.AddOperator((IOperator)cloner.Clone(op));
    7979      if (InitialOperator != null)
    80         clone.myInitialOperator = (IOperator)Auxiliary.Clone(InitialOperator, clonedObjects);
     80        clone.myInitialOperator = (IOperator)cloner.Clone(InitialOperator);
    8181      return clone;
    8282    }
     
    8585    /// <remarks>Calls <see cref="OnOperatorAdded"/>.</remarks>
    8686    public void AddOperator(IOperator op) {
    87       if (!myOperators.ContainsKey(op.Guid)) {
    88         myOperators.Add(op.Guid, op);
     87      if (!myOperators.ContainsKey(op)) {
     88        myOperators.Add(op, op);
    8989        OnOperatorAdded(op);
    9090
     
    9595    /// <inheritdoc/>
    9696    /// <remarks>Calls <see cref="OnOperatorRemoved"/>.</remarks>
    97     public void RemoveOperator(Guid guid) {
    98       IOperator op = GetOperator(guid);
    99       if (op != null) {
     97    public void RemoveOperator(IOperator op) {
     98      if (myOperators.ContainsKey(op)) {
    10099        foreach (IOperator o in Operators) {
    101100          int i = 0;
     
    109108        if (InitialOperator == op)
    110109          InitialOperator = null;
    111         myOperators.Remove(op.Guid);
     110        myOperators.Remove(op);
    112111        OnOperatorRemoved(op);
    113112      }
    114113    }
    115114    /// <inheritdoc/>
    116     public IOperator GetOperator(Guid guid) {
    117       IOperator op;
    118       if (myOperators.TryGetValue(guid, out op))
    119         return op;
    120       else
    121         return null;
    122     }
    123     /// <inheritdoc/>
    124115    public void Clear() {
    125       Guid[] guids = new Guid[Operators.Count];
     116      IOperator[] ops = new IOperator[Operators.Count];
    126117      int i = 0;
    127118      foreach (IOperator op in Operators) {
    128         guids[i] = op.Guid;
     119        ops[i] = op;
    129120        i++;
    130121      }
    131       for (int j = 0; j < guids.Length; j++)
    132         RemoveOperator(guids[j]);
     122      for (int j = 0; j < ops.Length; j++)
     123        RemoveOperator(ops[j]);
    133124    }
    134125
  • trunk/sources/HeuristicLab.Core/3.3/OperatorGroup.cs

    r1823 r2526  
    3030  /// Representation of a group of operators (can also include subgroups).
    3131  /// </summary>
    32   public class OperatorGroup : StorableBase, IOperatorGroup {
     32  public class OperatorGroup : ItemBase, IOperatorGroup {
    3333
    3434    [Storable]
     
    8080    /// Clones the current instance (deep clone).
    8181    /// </summary>
    82     /// <remarks>Deep clone with <see cref="Auxiliary.Clone"/> method of helper class
     82    /// <remarks>Deep clone with <see cref="cloner.Clone"/> method of helper class
    8383    /// <see cref="Auxiliary"/>.</remarks>
    8484    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
    8585    /// <returns>The cloned object as <see cref="OperatorGroup"/>.</returns>
    86     public override object Clone(IDictionary<Guid, object> clonedObjects) {
    87       OperatorGroup clone = (OperatorGroup)base.Clone(clonedObjects);
     86    public override IItem Clone(ICloner cloner) {
     87      OperatorGroup clone = (OperatorGroup)base.Clone(cloner);
    8888      clone.myName = Name;
    8989      foreach (IOperatorGroup group in SubGroups)
    90         clone.AddSubGroup((IOperatorGroup)Auxiliary.Clone(group, clonedObjects));
     90        clone.AddSubGroup((IOperatorGroup)cloner.Clone(group));
    9191      foreach (IOperator op in Operators)
    92         clone.AddOperator((IOperator)Auxiliary.Clone(op, clonedObjects));
     92        clone.AddOperator((IOperator)cloner.Clone(op));
    9393      return clone;
    9494    }
  • trunk/sources/HeuristicLab.Core/3.3/OperatorLibrary.cs

    r2520 r2526  
    5151    /// Clones the current instance (deep clone).
    5252    /// </summary>
    53     /// <remarks>Deep clone through <see cref="Auxiliary.Clone"/> method of helper class
     53    /// <remarks>Deep clone through <see cref="cloner.Clone"/> method of helper class
    5454    /// <see cref="Auxiliary"/>.</remarks>
    5555    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
    5656    /// <returns>The cloned object as <see cref="OperatorLibrary"/>.</returns>
    57     public override object Clone(IDictionary<Guid, object> clonedObjects) {
     57    public override IItem Clone(ICloner cloner) {
    5858      OperatorLibrary clone = new OperatorLibrary();
    59       clonedObjects.Add(Guid, clone);
    60       clone.myGroup = (IOperatorGroup)Auxiliary.Clone(Group, clonedObjects);
     59      cloner.RegisterClonedObject(this, clone);
     60      clone.myGroup = (IOperatorGroup)cloner.Clone(Group);
    6161      return clone;
    6262    }
  • trunk/sources/HeuristicLab.Core/3.3/PersistenceManager.cs

    r1895 r2526  
    4141    /// <param name="instance">The object that should be saved.</param>
    4242    /// <param name="filename">The name of the file where the <paramref name="object"/> should be saved.</param>
    43     public static void Save(IStorable instance, string filename) {
     43    public static void Save(IItem instance, string filename) {
    4444      XmlGenerator.Serialize(instance, filename, 0);
    4545    }
    4646
    47     public static void SaveCompressed(IStorable instance, string filename) {
     47    public static void SaveCompressed(IItem instance, string filename) {
    4848      XmlGenerator.Serialize(instance, filename, 9);
    4949    }
     
    5555    /// <param name="instance">The object that should be saved.</param>
    5656    /// <param name="stream">The (file) stream where the object should be saved.</param>
    57     public static void Save(IStorable instance, Stream stream) {
     57    public static void Save(IItem instance, Stream stream) {
    5858      XmlGenerator.Serialize(instance, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()));     
    5959    }
     
    6565    /// <param name="filename">The filename of the file where the data is saved.</param>
    6666    /// <returns>The loaded object.</returns>
    67     public static IStorable Load(string filename) {
    68       return (IStorable)XmlParser.Deserialize(filename);
     67    public static IItem Load(string filename) {
     68      return (IItem)XmlParser.Deserialize(filename);
    6969    }
    7070    /// <summary>
     
    7575    /// <param name="stream">The stream from where to load the data.</param>
    7676    /// <returns>The loaded object.</returns>
    77     public static IStorable Load(Stream stream) {
    78       return (IStorable)XmlParser.Deserialize(stream);
     77    public static IItem Load(Stream stream) {
     78      return (IItem)XmlParser.Deserialize(stream);
    7979    }
    8080
  • trunk/sources/HeuristicLab.Core/3.3/Scope.cs

    r2520 r2526  
    217217    }
    218218    /// <inheritdoc/>
    219     public IScope GetScope(Guid guid) {
    220       if (Guid == guid) return this;
    221       else {
    222         for (int i = 0; i < mySubScopes.Count; i++) {
    223           IScope s = mySubScopes[i].GetScope(guid);
    224           if (s != null) return s;
    225         }
    226       }
    227       return null;
    228     }
    229     /// <inheritdoc/>
    230219    public IScope GetScope(string name) {
    231220      if (Name == name) return this;
     
    260249
    261250    /// <inheritdoc/>
    262     public override object Clone(IDictionary<Guid, object> clonedObjects) {
    263       Scope clone = (Scope)base.Clone(clonedObjects);
     251    public override IItem Clone(ICloner cloner) {
     252      Scope clone = (Scope)base.Clone(cloner);
    264253      clone.myName = Name;
    265254
    266255      foreach (IVariable variable in myVariables.Values)
    267         clone.AddVariable((IVariable)Auxiliary.Clone(variable, clonedObjects));
     256        clone.AddVariable((IVariable)cloner.Clone(variable));
    268257      foreach (KeyValuePair<string, string> alias in myAliases)
    269258        clone.AddAlias(alias.Key, alias.Value);
    270259      for (int i = 0; i < SubScopes.Count; i++)
    271         clone.AddSubScope((IScope)Auxiliary.Clone(SubScopes[i], clonedObjects));
     260        clone.AddSubScope((IScope)cloner.Clone(SubScopes[i]));
    272261
    273262      return clone;
  • trunk/sources/HeuristicLab.Core/3.3/Variable.cs

    r2520 r2526  
    9595    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
    9696    /// <returns>The cloned object as <see cref="Variable"/>.</returns>
    97     public override object Clone(IDictionary<Guid, object> clonedObjects) {
     97    public override IItem Clone(ICloner cloner) {
    9898      Variable clone = new Variable();
    99       clonedObjects.Add(Guid, clone);
     99      cloner.RegisterClonedObject(this, clone);
    100100      clone.myName = Name;
    101101      if (Value != null)
    102         clone.myValue = (IItem)Auxiliary.Clone(Value, clonedObjects);
     102        clone.myValue = (IItem)cloner.Clone(Value);
    103103      return clone;
    104104    }
  • trunk/sources/HeuristicLab.Core/3.3/VariableInfo.cs

    r2520 r2526  
    136136    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
    137137    /// <returns>The cloned object as <see cref="VariableInfo"/>.</returns>
    138     public override object Clone(IDictionary<Guid, object> clonedObjects) {
     138    public override IItem Clone(ICloner cloner) {
    139139      VariableInfo clone = new VariableInfo();
    140       clonedObjects.Add(Guid, clone);
     140      cloner.RegisterClonedObject(this, clone);
    141141      clone.myActualName = ActualName;
    142142      clone.myFormalName = FormalName;
Note: See TracChangeset for help on using the changeset viewer.