Free cookie consent management tool by TermsFeed Policy Generator

Changeset 836 for branches


Ignore:
Timestamp:
11/27/08 00:52:23 (16 years ago)
Author:
gkronber
Message:

worked on #285 (Cloning could be improved by creating objects at the bottom of the cloning chain with 'new' instead of the top with Activator.CreateInstance())

Location:
branches/CloningRefactorBranch
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • branches/CloningRefactorBranch/HeuristicLab.Core/AtomicOperation.cs

    r776 r836  
    4747    /// <summary>
    4848    /// Initializes a new instance of <see cref="AtomicOperation"/>.
     49    /// Empty constructor is needed for the persistence mechanism.
    4950    /// </summary>
    5051    public AtomicOperation() { }
  • branches/CloningRefactorBranch/HeuristicLab.Core/ConstrainedItemBase.cs

    r776 r836  
    4747
    4848    /// <summary>
    49     /// Clones the current instance (deep clone).
     49    /// Copy constructor for deep cloning.
    5050    /// </summary>
    51     /// <remarks>Calls <see cref="StorableBase.Clone
    52     /// (System.Collections.Generic.IDictionary&lt;System.Guid, object&gt;)"/>
    53     /// of base class <see cref="ItemBase"/>.<br/>
    54     /// Deep clone through <see cref="Auxiliary.Clone"/> method of helper class
    55     /// <see cref="Auxiliary"/>.</remarks>
    56     /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
    57     /// <returns>The cloned object as <see cref="ConstrainedItemBase"/>.</returns>
    58     public override object Clone(IDictionary<Guid, object> clonedObjects) {
    59       ConstrainedItemBase clone = (ConstrainedItemBase)base.Clone(clonedObjects);
    60       clone.myConstraints.Clear();
    61       foreach (IConstraint constraint in Constraints)
    62         clone.AddConstraint((IConstraint)Auxiliary.Clone(constraint, clonedObjects));
    63       return clone;
     51    /// <param name="original"></param>
     52    /// <param name="clonedObjects"></param>
     53    protected ConstrainedItemBase(ConstrainedItemBase original, IDictionary<Guid, object> clonedObjects)
     54      : base(original, clonedObjects) {
     55      myConstraints = new List<IConstraint>();
     56      foreach (IConstraint constraint in original.myConstraints)
     57        AddConstraint((IConstraint)Auxiliary.Clone(constraint, clonedObjects));
    6458    }
    6559
     
    145139    }
    146140
    147     #region Persistence Methods 
     141    #region Persistence Methods
    148142    /// <summary>
    149143    /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
     
    157151    /// (Needed to avoid cycles.)</param>
    158152    /// <returns>The saved <see cref="XmlNode"/>.</returns>
    159     public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
     153    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    160154      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
    161155      if (Constraints.Count > 0) {
     
    176170    /// <param name="restoredObjects">The dictionary of all already restored objects.
    177171    /// (Needed to avoid cycles.)</param>
    178     public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
     172    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    179173      base.Populate(node, restoredObjects);
    180174      XmlNode constraintsNode = node.SelectSingleNode("Constraints");
  • branches/CloningRefactorBranch/HeuristicLab.Core/EngineBase.cs

    r776 r836  
    118118
    119119    /// <summary>
     120    /// Copy constructor.
     121    /// </summary>
     122    /// <param name="original"></param>
     123    /// <param name="clonedObjects"></param>
     124    protected EngineBase(EngineBase original, IDictionary<Guid, object> clonedObjects)
     125      : base(original, clonedObjects) {
     126      this.myOperatorGraph = (IOperatorGraph)Auxiliary.Clone(original.OperatorGraph, clonedObjects);
     127      this.myGlobalScope = (IScope)Auxiliary.Clone(original.GlobalScope, clonedObjects);
     128      this.myExecutionTime = original.ExecutionTime;
     129      IOperation[] operations = new IOperation[original.ExecutionStack.Count];
     130      original.ExecutionStack.CopyTo(operations, 0);
     131      for (int i = operations.Length - 1; i >= 0; i--)
     132        this.myExecutionStack.Push((IOperation)Auxiliary.Clone(operations[i], clonedObjects));
     133      this.myRunning = original.Running;
     134      this.myCanceled = original.Canceled;
     135    }
     136
     137    /// <summary>
    120138    /// Clones the current instance (deep clone).
    121139    /// </summary>
     
    125143    /// <returns>The cloned object as <see cref="EngineBase"/>.</returns>
    126144    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    127       EngineBase clone = (EngineBase)base.Clone(clonedObjects);
    128       clone.myOperatorGraph = (IOperatorGraph)Auxiliary.Clone(OperatorGraph, clonedObjects);
    129       clone.myGlobalScope = (IScope)Auxiliary.Clone(GlobalScope, clonedObjects);
    130       clone.myExecutionTime = ExecutionTime;
    131       IOperation[] operations = new IOperation[ExecutionStack.Count];
    132       ExecutionStack.CopyTo(operations, 0);
    133       for (int i = operations.Length - 1; i >= 0; i--)
    134         clone.myExecutionStack.Push((IOperation)Auxiliary.Clone(operations[i], clonedObjects));
    135       clone.myRunning = Running;
    136       clone.myCanceled = Canceled;
    137       return clone;
    138      
     145      return new EngineBase(this, clonedObjects);
    139146    }
    140147
  • branches/CloningRefactorBranch/HeuristicLab.Core/ItemBase.cs

    r776 r836  
    3030  /// </summary>
    3131  public abstract class ItemBase : StorableBase, IItem {
     32    /// <summary>
     33    /// Creates an instance of an ItemBase.
     34    /// </summary>
     35    protected ItemBase() { }
     36
     37    /// <summary>
     38    /// Copy constructor for deep cloning of items.
     39    /// </summary>
     40    /// <param name="original"></param>
     41    /// <param name="clonedObjects"></param>
     42    protected ItemBase(ItemBase original, IDictionary<Guid, object> clonedObjects) : base(original, clonedObjects) { }
     43
    3244    /// <summary>
    3345    /// Creates a new instance of <see cref="ItemBaseView"/> for
  • branches/CloningRefactorBranch/HeuristicLab.Core/OperatorBase.cs

    r776 r836  
    105105
    106106    /// <summary>
    107     /// Clones the current instance (deep clone).
    108     /// </summary>
    109     /// <remarks>Clones also sub operators, variables and variable infos.</remarks>
    110     /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
    111     /// <returns>The cloned object as <see cref="OperatorBase"/>.</returns>
    112     public override object Clone(IDictionary<Guid, object> clonedObjects) {
    113       OperatorBase clone = (OperatorBase)base.Clone(clonedObjects);
    114       clone.myName = Name;
    115       clone.mySubOperators.Clear();
    116       for (int i = 0; i < SubOperators.Count; i++)
    117         clone.AddSubOperator((IOperator)Auxiliary.Clone(SubOperators[i], clonedObjects));
    118       clone.myVariableInfos.Clear();
    119       foreach (IVariableInfo variableInfo in myVariableInfos.Values)
    120         clone.AddVariableInfo((IVariableInfo)Auxiliary.Clone(variableInfo, clonedObjects));
    121       clone.myVariables.Clear();
    122       foreach (IVariable variable in myVariables.Values)
    123         clone.AddVariable((IVariable)Auxiliary.Clone(variable, clonedObjects));
    124       return clone;
     107    /// Copy constructor
     108    /// </summary>
     109    /// <param name="original"></param>
     110    /// <param name="clonedObjects"></param>
     111    protected OperatorBase(OperatorBase original, IDictionary<Guid, object> clonedObjects)
     112      : base(original, clonedObjects) {
     113      this.myName = original.Name;
     114      this.mySubOperators = new List<IOperator>();
     115      for (int i = 0; i < original.SubOperators.Count; i++)
     116        AddSubOperator((IOperator)Auxiliary.Clone(original.SubOperators[i], clonedObjects));
     117      this.myVariableInfos = new Dictionary<string, IVariableInfo>();
     118      foreach (IVariableInfo variableInfo in original.myVariableInfos.Values)
     119        AddVariableInfo((IVariableInfo)Auxiliary.Clone(variableInfo, clonedObjects));
     120      this.myVariables = new Dictionary<string, IVariable>();
     121      foreach (IVariable variable in original.myVariables.Values)
     122        AddVariable((IVariable)Auxiliary.Clone(variable, clonedObjects));
    125123    }
    126124
     
    628626    /// <param name="persistedObjects">The dictionary of all already persisted objects. (Needed to avoid cycles.)</param>
    629627    /// <returns>The saved <see cref="XmlNode"/>.</returns>
    630     public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
     628    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    631629      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
    632630      XmlAttribute nameAttribute = document.CreateAttribute("Name");
     
    660658    /// <param name="node">The <see cref="XmlNode"/> where the operation is saved.</param>
    661659    /// <param name="restoredObjects">A dictionary of all already restored objects. (Needed to avoid cycles.)</param>
    662     public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
     660    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    663661      base.Populate(node, restoredObjects);
    664662      myName = node.Attributes["Name"].Value;
  • branches/CloningRefactorBranch/HeuristicLab.Core/OperatorGroup.cs

    r776 r836  
    7171
    7272    /// <summary>
     73    /// Copy constructor for deep cloning
     74    /// </summary>
     75    /// <param name="original"></param>
     76    /// <param name="clonedObjects"></param>
     77    protected OperatorGroup(OperatorGroup original, IDictionary<Guid, object> clonedObjects) : base(original, clonedObjects) {
     78      this.myName = original.myName;
     79      mySubGroups = new List<IOperatorGroup>();
     80      myOperators = new List<IOperator>();
     81      foreach (IOperatorGroup group in original.SubGroups)
     82        AddSubGroup((IOperatorGroup)Auxiliary.Clone(group, clonedObjects));
     83      foreach (IOperator op in original.Operators)
     84        AddOperator((IOperator)Auxiliary.Clone(op, clonedObjects));
     85    }
     86
     87    /// <summary>
    7388    /// Clones the current instance (deep clone).
    7489    /// </summary>
     
    7893    /// <returns>The cloned object as <see cref="OperatorGroup"/>.</returns>
    7994    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    80       OperatorGroup clone = (OperatorGroup)base.Clone(clonedObjects);
    81       clone.myName = Name;
    82       foreach (IOperatorGroup group in SubGroups)
    83         clone.AddSubGroup((IOperatorGroup)Auxiliary.Clone(group, clonedObjects));
    84       foreach (IOperator op in Operators)
    85         clone.AddOperator((IOperator)Auxiliary.Clone(op, clonedObjects));
     95      OperatorGroup clone = new OperatorGroup(this, clonedObjects);
    8696      return clone;
    8797    }
  • branches/CloningRefactorBranch/HeuristicLab.Core/Scope.cs

    r776 r836  
    3232    private IScope parent;
    3333
    34     private string myName; 
     34    private string myName;
    3535    /// <summary>
    3636    /// Gets the name of the current scope.
     
    7575      : this() {
    7676      if (name != null) myName = name;
     77    }
     78
     79    /// <summary>
     80    /// Copy constructor to create deep clones.
     81    /// </summary>
     82    /// <param name="original"></param>
     83    /// <param name="clonedObjects"></param>
     84    protected Scope(Scope original, IDictionary<Guid, object> clonedObjects)
     85      : base(original, clonedObjects) {
     86      this.myName = original.Name;
     87      foreach (IVariable variable in original.myVariables.Values)
     88        AddVariable((IVariable)Auxiliary.Clone(variable, clonedObjects));
     89      foreach (KeyValuePair<string, string> alias in original.myAliases)
     90        AddAlias(alias.Key, alias.Value);
     91      for (int i = 0; i < original.SubScopes.Count; i++)
     92        AddSubScope((IScope)Auxiliary.Clone(original.SubScopes[i], clonedObjects));
    7793    }
    7894
     
    238254        RemoveVariable(variableNames[j]);
    239255
    240       KeyValuePair<string, string>[] aliases = new KeyValuePair<string,string>[myAliases.Count];
     256      KeyValuePair<string, string>[] aliases = new KeyValuePair<string, string>[myAliases.Count];
    241257      myAliases.CopyTo(aliases, 0);
    242258      for (int j = 0; j < aliases.Length; j++)
     
    249265    /// <inheritdoc/>
    250266    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    251       Scope clone = (Scope)base.Clone(clonedObjects);
    252       clone.myName = Name;
    253 
    254       foreach (IVariable variable in myVariables.Values)
    255         clone.AddVariable((IVariable)Auxiliary.Clone(variable, clonedObjects));
    256       foreach (KeyValuePair<string, string> alias in myAliases)
    257         clone.AddAlias(alias.Key, alias.Value);
    258       for (int i = 0; i < SubScopes.Count; i++)
    259         clone.AddSubScope((IScope)Auxiliary.Clone(SubScopes[i], clonedObjects));
    260 
    261       return clone;
     267      return new Scope(this, clonedObjects);
    262268    }
    263269
     
    369375    /// <param name="persistedObjects">The dictionary of all already persisted objects. (Needed to avoid cycles.)</param>
    370376    /// <returns>The saved <see cref="XmlNode"/>.</returns>
    371     public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
     377    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    372378      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
    373379      XmlAttribute nameAttribute = document.CreateAttribute("Name");
     
    409415    /// <param name="restoredObjects">The dictionary of all already restored objects.
    410416    /// (Needed to avoid cycles.)</param>
    411     public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
     417    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    412418      base.Populate(node, restoredObjects);
    413419      myName = node.Attributes["Name"].Value;
  • branches/CloningRefactorBranch/HeuristicLab.Core/StorableBase.cs

    r776 r836  
    4646
    4747    /// <summary>
     48    ///  Copy constructor to create a deep clone of the original object
     49    /// </summary>
     50    /// <param name="original">The object to be cloned</param>
     51    protected StorableBase(StorableBase original, IDictionary<Guid, object> clonedObjects) : this() {
     52      clonedObjects.Add(myGuid, this);
     53    }
     54
     55    /// <summary>
    4856    /// Clones the current instance (deep clone).
    4957    /// </summary>
     
    5462    }
    5563    /// <summary>
    56     /// Clones the current instance with the <see cref="M:Activator.CreateInstance"/>
    57     /// method of <see cref="Activator"/>.
     64    /// Default implementation of clone throws NotSupportedException.
     65    /// All actually cloneable objects must override this method.
    5866    /// </summary>
    5967    /// <param name="clonedObjects">All already cloned objects.</param>
    60     /// <returns>The clone.</returns>
     68    /// <returns>Nothing</returns>
    6169    public virtual object Clone(IDictionary<Guid, object> clonedObjects) {
    62       object clone = Activator.CreateInstance(this.GetType());
    63       clonedObjects.Add(Guid, clone);
    64       return clone;
     70      throw new NotSupportedException("Can't clone abstract type StorableBase.");
    6571    }
    6672
     
    7480    /// <param name="persistedObjects">The dictionary of all already persisted objects. (Needed to avoid cycles.)</param>
    7581    /// <returns>The saved <see cref="XmlNode"/>.</returns>
    76     public virtual XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
     82    public virtual XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    7783      XmlNode node = document.CreateNode(XmlNodeType.Element, name, null);
    7884      XmlAttribute typeAttribute = document.CreateAttribute("Type");
     
    9197    /// <param name="restoredObjects">The dictionary of all already restored objects.
    9298    /// (Needed to avoid cycles.)</param>
    93     public virtual void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
     99    public virtual void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    94100      myGuid = new Guid(node.Attributes["GUID"].Value);
    95101    }
  • branches/CloningRefactorBranch/HeuristicLab.Data/ConstrainedDoubleData.cs

    r763 r836  
    5656    }
    5757
     58    /// <summary>
     59    /// Copy constructor for deep cloning of ConstrainedDoubleData items.
     60    /// </summary>
     61    /// <param name="original"></param>
     62    /// <param name="clonedObjects"></param>
     63    protected ConstrainedDoubleData(ConstrainedDoubleData original, IDictionary<Guid, object> clonedObjects) : base(original, clonedObjects) { }
     64
    5865    /// <inheritdoc cref="ConstrainedObjectData.TrySetData(object)"/>
    5966    public virtual bool TrySetData(double data) {
     
    8087    /// <returns>The cloned instance as <see cref="ConstrainedDoubleData"/>.</returns>
    8188    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    82       ConstrainedDoubleData clone = (ConstrainedDoubleData)base.Clone(clonedObjects);
    83       return clone;
     89      return new ConstrainedDoubleData(this, clonedObjects);
    8490    }
    8591
  • branches/CloningRefactorBranch/HeuristicLab.Data/ConstrainedIntData.cs

    r763 r836  
    5656    }
    5757
     58    /// <summary>
     59    /// Copy constructor for deep cloning of ConstrainedDoubleData items.
     60    /// </summary>
     61    /// <param name="original"></param>
     62    /// <param name="clonedObjects"></param>
     63    protected ConstrainedIntData(ConstrainedIntData original, IDictionary<Guid, object> clonedObjects) : base(original, clonedObjects) { }
     64
    5865    /// <inheritdoc cref="ConstrainedObjectData.TrySetData(object)"/>
    5966    public virtual bool TrySetData(int data) {
     
    8087    /// <returns>The clone instance as <see cref="ConstrainedIntData"/>.</returns>
    8188    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    82       ConstrainedIntData clone = (ConstrainedIntData)base.Clone(clonedObjects);
    83       return clone;
     89      return new ConstrainedIntData(this, clonedObjects);
    8490    }
    8591
Note: See TracChangeset for help on using the changeset viewer.