Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/09/09 00:57:49 (15 years ago)
Author:
swagner
Message:

Refactoring of the operator architecture (#95)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Operator Architecture Refactoring/HeuristicLab.Core/3.3/OperatorBase.cs

    r1823 r2033  
    3030  /// The base class for all operators.
    3131  /// </summary>
    32   public abstract class OperatorBase : ConstrainedItemBase, IOperator {
     32  public abstract class OperatorBase : ItemBase, IOperator {
    3333
    3434    [Storable]
     
    130130    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
    131131    /// <returns>The cloned object as <see cref="OperatorBase"/>.</returns>
    132     public override object Clone(IDictionary<Guid, object> clonedObjects) {
     132    public override object Clone(IDictionary<long, object> clonedObjects) {
    133133      OperatorBase clone = (OperatorBase)base.Clone(clonedObjects);
    134134      clone.myName = Name;
     
    162162      OnSubOperatorAdded(subOperator, mySubOperators.Count - 1);
    163163    }
    164     /// <inheritdoc cref="IOperator.TryAddSubOperator(HeuristicLab.Core.IOperator)"/>
    165     /// <param name="subOperator">The sub operator to add.</param>
    166     /// <remarks>Calls <see cref="OnSubOperatorAdded"/>.</remarks>
    167     public virtual bool TryAddSubOperator(IOperator subOperator) {
    168       mySubOperators.Add(subOperator);
    169       if (IsValid()) {
    170         OnSubOperatorAdded(subOperator, mySubOperators.Count - 1);
    171         return true;
    172       } else {
    173         mySubOperators.RemoveAt(mySubOperators.Count - 1);
    174         return false;
    175       }
    176     }
    177     /// <inheritdoc cref="HeuristicLab.Core.IOperator.TryAddSubOperator(HeuristicLab.Core.IOperator,
    178     /// out System.Collections.Generic.ICollection&lt;HeuristicLab.Core.IConstraint&gt;)"/>
    179     /// <param name="subOperator">The sub operator to add.</param>
    180     /// <remarks>Calls <see cref="OnSubOperatorAdded"/>.</remarks>
    181     public virtual bool TryAddSubOperator(IOperator subOperator, out ICollection<IConstraint> violatedConstraints) {
    182       mySubOperators.Add(subOperator);
    183       if (IsValid(out violatedConstraints)) {
    184         OnSubOperatorAdded(subOperator, mySubOperators.Count - 1);
    185         return true;
    186       } else {
    187         mySubOperators.RemoveAt(mySubOperators.Count - 1);
    188         return false;
    189       }
    190     }
    191164    /// <inheritdoc cref="HeuristicLab.Core.IOperator.AddSubOperator(HeuristicLab.Core.IOperator, int)"/>
    192165    /// <param name="subOperator">The sub operator to add.</param>
     
    196169      OnSubOperatorAdded(subOperator, index);
    197170    }
    198     /// <inheritdoc cref="IOperator.TryAddSubOperator(HeuristicLab.Core.IOperator, int)"/>
    199     /// <param name="subOperator">The sub operator to add.</param>
    200     /// <remarks>Calls <see cref="OnSubOperatorAdded"/>.</remarks>
    201     public virtual bool TryAddSubOperator(IOperator subOperator, int index) {
    202       mySubOperators.Insert(index, subOperator);
    203       if (IsValid()) {
    204         OnSubOperatorAdded(subOperator, index);
    205         return true;
    206       } else {
    207         mySubOperators.RemoveAt(index);
    208         return false;
    209       }
    210     }
    211     /// <inheritdoc cref="IOperator.TryAddSubOperator(HeuristicLab.Core.IOperator, int, out
    212     /// System.Collections.Generic.ICollection&lt;HeuristicLab.Core.IConstraint&gt;)"/>
    213     /// <param name="subOperator">The sub operator to add.</param>
    214     /// <remarks>Calls <see cref="OnSubOperatorAdded"/>.</remarks>
    215     public virtual bool TryAddSubOperator(IOperator subOperator, int index, out ICollection<IConstraint> violatedConstraints) {
    216       mySubOperators.Insert(index, subOperator);
    217       if (IsValid(out violatedConstraints)) {
    218         OnSubOperatorAdded(subOperator, index);
    219         return true;
    220       } else {
    221         mySubOperators.RemoveAt(index);
    222         return false;
    223       }
    224     }
    225171    /// <inheritdoc/>
    226172    /// <remarks>Calls <see cref="OnSubOperatorRemoved"/>.</remarks>
     
    229175      mySubOperators.RemoveAt(index);
    230176      OnSubOperatorRemoved(op, index);
    231     }
    232     /// <inheritdoc/>
    233     /// <remarks>Calls <see cref="OnSubOperatorRemoved"/>.</remarks>
    234     public virtual bool TryRemoveSubOperator(int index) {
    235       IOperator op = mySubOperators[index];
    236       mySubOperators.RemoveAt(index);
    237       if (IsValid()) {
    238         OnSubOperatorRemoved(op, index);
    239         return true;
    240       } else {
    241         mySubOperators.Insert(index, op);
    242         return false;
    243       }
    244     }
    245     /// <inheritdoc/>
    246     /// <remarks>Calls <see cref="OnSubOperatorRemoved"/>.</remarks>
    247     public virtual bool TryRemoveSubOperator(int index, out ICollection<IConstraint> violatedConstraints) {
    248       IOperator op = mySubOperators[index];
    249       mySubOperators.RemoveAt(index);
    250       if (IsValid(out violatedConstraints)) {
    251         OnSubOperatorRemoved(op, index);
    252         return true;
    253       } else {
    254         mySubOperators.Insert(index, op);
    255         return false;
    256       }
    257177    }
    258178    #endregion
     
    274194    }
    275195    /// <inheritdoc/>
    276     /// <remarks>Calls <see cref="OnVariableInfoAdded"/>.</remarks>
    277     public virtual bool TryAddVariableInfo(IVariableInfo variableInfo) {
    278       myVariableInfos.Add(variableInfo.FormalName, variableInfo);
    279       if (IsValid()) {
    280         OnVariableInfoAdded(variableInfo);
    281         return true;
    282       } else {
    283         myVariableInfos.Remove(variableInfo.FormalName);
    284         return false;
    285       }
    286     }
    287     /// <inheritdoc/>
    288     /// <remarks>Calls <see cref="OnVariableInfoAdded"/>.</remarks>
    289     public virtual bool TryAddVariableInfo(IVariableInfo variableInfo, out ICollection<IConstraint> violatedConstraints) {
    290       myVariableInfos.Add(variableInfo.FormalName, variableInfo);
    291       if (IsValid(out violatedConstraints)) {
    292         OnVariableInfoAdded(variableInfo);
    293         return true;
    294       } else {
    295         myVariableInfos.Remove(variableInfo.FormalName);
    296         return false;
    297       }
    298     }
    299     /// <inheritdoc/>
    300196    /// <remarks>Calls <see cref="OnVariableInfoRemoved"/>.</remarks>
    301197    public virtual void RemoveVariableInfo(string formalName) {
     
    305201        OnVariableInfoRemoved(variableInfo);
    306202      }
    307     }
    308     /// <inheritdoc/>
    309     /// <remarks>Calls <see cref="OnVariableInfoRemoved"/>.</remarks>
    310     public virtual bool TryRemoveVariableInfo(string formalName) {
    311       IVariableInfo variableInfo;
    312       if (myVariableInfos.TryGetValue(formalName, out variableInfo)) {
    313         myVariableInfos.Remove(formalName);
    314         if (IsValid()) {
    315           OnVariableInfoRemoved(variableInfo);
    316           return true;
    317         } else {
    318           myVariableInfos.Add(formalName, variableInfo);
    319           return false;
    320         }
    321       }
    322       return true;
    323     }
    324     /// <inheritdoc/>
    325     /// <remarks>Calls <see cref="OnVariableInfoRemoved"/>.</remarks>
    326     public virtual bool TryRemoveVariableInfo(string formalName, out ICollection<IConstraint> violatedConstraints) {
    327       IVariableInfo variableInfo;
    328       if (myVariableInfos.TryGetValue(formalName, out variableInfo)) {
    329         myVariableInfos.Remove(formalName);
    330         if (IsValid(out violatedConstraints)) {
    331           OnVariableInfoRemoved(variableInfo);
    332           return true;
    333         } else {
    334           myVariableInfos.Add(formalName, variableInfo);
    335           return false;
    336         }
    337       }
    338       violatedConstraints = new List<IConstraint>();
    339       return true;
    340203    }
    341204    #endregion
     
    358221      variable.NameChanged += new EventHandler(Variable_NameChanged);
    359222      OnVariableAdded(variable);
    360     }
    361     /// <inheritdoc/>
    362     /// <remarks>Calls <see cref="OnVariableAdded"/> and adds <c>NameChanging</c> and <c>NameChanged</c>
    363     /// event handlers.</remarks>
    364     public virtual bool TryAddVariable(IVariable variable) {
    365       myVariables.Add(variable.Name, variable);
    366       if (IsValid()) {
    367         variable.NameChanging += new EventHandler<NameChangingEventArgs>(Variable_NameChanging);
    368         variable.NameChanged += new EventHandler(Variable_NameChanged);
    369         OnVariableAdded(variable);
    370         return true;
    371       } else {
    372         myVariableInfos.Remove(variable.Name);
    373         return false;
    374       }
    375     }
    376     /// <inheritdoc/>
    377     /// <remarks>Calls <see cref="OnVariableAdded"/> and adds <c>NameChanging</c> and <c>NameChanged</c>
    378     /// event handlers.</remarks>
    379     public virtual bool TryAddVariable(IVariable variable, out ICollection<IConstraint> violatedConstraints) {
    380       myVariables.Add(variable.Name, variable);
    381       if (IsValid(out violatedConstraints)) {
    382         variable.NameChanging += new EventHandler<NameChangingEventArgs>(Variable_NameChanging);
    383         variable.NameChanged += new EventHandler(Variable_NameChanged);
    384         OnVariableAdded(variable);
    385         return true;
    386       } else {
    387         myVariableInfos.Remove(variable.Name);
    388         return false;
    389       }
    390223    }
    391224    /// <inheritdoc/>
     
    400233        OnVariableRemoved(variable);
    401234      }
    402     }
    403     /// <inheritdoc/>
    404     /// <remarks>Calls <see cref="OnVariableRemoved"/> and removes <c>NameChanging</c> and <c>NameChanged</c>
    405     /// event handlers.</remarks>
    406     public virtual bool TryRemoveVariable(string name) {
    407       IVariable variable;
    408       if (myVariables.TryGetValue(name, out variable)) {
    409         myVariables.Remove(name);
    410         if (IsValid()) {
    411           variable.NameChanging -= new EventHandler<NameChangingEventArgs>(Variable_NameChanging);
    412           variable.NameChanged -= new EventHandler(Variable_NameChanged);
    413           OnVariableRemoved(variable);
    414           return true;
    415         } else {
    416           myVariables.Add(name, variable);
    417           return false;
    418         }
    419       }
    420       return true;
    421     }
    422     /// <inheritdoc/>
    423     /// <remarks>Calls <see cref="OnVariableRemoved"/> and removes <c>NameChanging</c> and <c>NameChanged</c>
    424     /// event handlers.</remarks>
    425     public virtual bool TryRemoveVariable(string name, out ICollection<IConstraint> violatedConstraints) {
    426       IVariable variable;
    427       if (myVariables.TryGetValue(name, out variable)) {
    428         myVariables.Remove(name);
    429         if (IsValid(out violatedConstraints)) {
    430           variable.NameChanging -= new EventHandler<NameChangingEventArgs>(Variable_NameChanging);
    431           variable.NameChanged -= new EventHandler(Variable_NameChanged);
    432           OnVariableRemoved(variable);
    433           return true;
    434         } else {
    435           myVariables.Add(name, variable);
    436           return false;
    437         }
    438       }
    439       violatedConstraints = new List<IConstraint>();
    440       return true;
    441235    }
    442236    private void Variable_NameChanging(object sender, NameChangingEventArgs e) {
     
    489283    }
    490284    #endregion
     285
    491286    /// <inheritdoc/>
    492287    public virtual IOperation Execute(IScope scope) {
Note: See TracChangeset for help on using the changeset viewer.