Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/26/10 05:14:51 (15 years ago)
Author:
swagner
Message:

Operator architecture refactoring (#95)

  • continued work on adapting and refactoring HeuristicLab.Data according to the changes in HeuristicLab.Core
  • started work on adapting and refactoring HeuristicLab.Operators according to changes in HeuristicLab.Core
Location:
trunk/sources/HeuristicLab.Operators/3.3
Files:
3 deleted
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Operators/3.3/CombinedOperator.cs

    r2526 r2684  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Drawing;
    2425using System.Text;
    2526using System.Xml;
     27using HeuristicLab.Collections;
    2628using HeuristicLab.Core;
    2729using HeuristicLab.Data;
     
    3032namespace HeuristicLab.Operators {
    3133  /// <summary>
    32   /// Contains an operator graph and automatically injects its sub-operators into the scope it is
    33   /// applied on (useful for modularization to assemble complex operators out of simpler ones).
     34  /// Operator which contains an operator graph.
    3435  /// </summary>
    35   public class CombinedOperator : DelegatingOperator {
    36 
     36  [Item("CombinedOperator", "An operator which contains an operator graph.")]
     37  [Creatable("Test")]
     38  public sealed class CombinedOperator : StandardOperator, IOperator {
     39    public override Image ItemImage {
     40      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Module; }
     41    }
    3742    [Storable]
    38     private string myDescription;
    39     /// <summary>
    40     /// Gets the description of the current instance.
    41     /// </summary>
    42     public override string Description {
    43       get { return myDescription; }
     43    private OperatorGraph operatorGraph;
     44    public OperatorGraph OperatorGraph {
     45      get { return operatorGraph; }
     46    }
     47    public new ParameterCollection Parameters {
     48      get {
     49        return base.Parameters;
     50      }
     51    }
     52    IObservableKeyedCollection<string, IParameter> IOperator.Parameters {
     53      get { return Parameters; }
     54    }
     55    public override bool CanChangeDescription {
     56      get { return true; }
    4457    }
    4558
    46     [Storable]
    47     private IOperatorGraph myOperatorGraph;
    48     /// <summary>
    49     /// Gets the operator graph of the current instance.
    50     /// </summary>
    51     public IOperatorGraph OperatorGraph {
    52       get { return myOperatorGraph; }
     59    public CombinedOperator()
     60      : base() {
     61      operatorGraph = new OperatorGraph();
    5362    }
    5463
    55     /// <summary>
    56     /// Initializes a new instance of <see cref="CombinedOperator"/>.
    57     /// </summary>
    58     public CombinedOperator()
    59       : base() {
    60       myDescription =
    61         @"A combined operator contains a whole operator graph. It is useful for modularization to assemble complex operators out of simpler ones.
    62 
    63 A combined operator automatically inject its sub-operators into the scope it is applied on. Thereby the names of the sub-operators are used as variable names. Those operators can be extracted again in the contained operator graph by using an OperatorExtractor. So it is possible to parameterize a combined operator with custom operators.";
    64       myOperatorGraph = new OperatorGraph();
     64    public override IDeepCloneable Clone(Cloner cloner) {
     65      CombinedOperator clone = (CombinedOperator)base.Clone(cloner);
     66      clone.operatorGraph = (OperatorGraph)cloner.Clone(operatorGraph);
     67      return base.Clone(cloner);
    6568    }
    6669
    67     /// <summary>
    68     /// Sets the description of the current instance.
    69     /// </summary>
    70     /// <remarks>Calls <see cref="OnDescriptionChanged"/>.</remarks>
    71     /// <exception cref="NullReferenceException">Thrown when <paramref name="description"/> is <c>null</c>.</exception>
    72     /// <param name="description">The description to set.</param>
    73     public void SetDescription(string description) {
    74       if (description == null)
    75         throw new NullReferenceException("description must not be null");
    76 
    77       if (description != myDescription) {
    78         myDescription = description;
    79         OnDescriptionChanged();
    80       }
    81     }
    82 
    83     /// <summary>
    84     /// Clones the current instance (deep clone).
    85     /// </summary>
    86     /// <remarks>Calls <see cref="OperatorBase.Clone
    87     /// (System.Collections.Generic.IDictionary&lt;System.Guid, object&gt;)"/>
    88     /// of base class <see cref="DelegatingOperator"/>.<br/>
    89     /// Deep clone through <see cref="cloner.Clone"/> method of helper class
    90     /// <see cref="Auxiliary"/>.</remarks>
    91     /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
    92     /// <returns>The cloned object as <see cref="CombinedOperator"/>.</returns>
    93     public override IItem Clone(ICloner cloner) {
    94       CombinedOperator clone = (CombinedOperator)base.Clone(cloner);
    95       clone.myDescription = Description;
    96       clone.myOperatorGraph = (IOperatorGraph)cloner.Clone(OperatorGraph);
    97       return clone;
    98     }
    99 
    100     /// <summary>
    101     /// Adds all sub operators to the specified <paramref name="scope"/>.
    102     /// </summary>
    103     /// <param name="scope">The scope where to inject the sub operators.</param>
    104     /// <returns><c>null</c> if the initial operator is <c>nulll</c>, else a new
    105     /// <see cref="AtomicOperation"/> with the initial operator and the given <paramref name="scope"/>.</returns>
    106     public override IOperation Apply(IScope scope) {
    107       if (OperatorGraph.InitialOperator != null) {
    108         for (int i = 0; i < SubOperators.Count; i++) {
    109           if (scope.GetVariable(SubOperators[i].Name) != null)
    110             scope.RemoveVariable(SubOperators[i].Name);
    111           scope.AddVariable(new Variable(SubOperators[i].Name, SubOperators[i]));
    112         }
    113         return new AtomicOperation(OperatorGraph.InitialOperator, scope);
    114       } else {
    115         return null;
    116       }
    117     }
    118 
    119     /// <summary>
    120     /// Occurs when the description of the current instance has been changed.
    121     /// </summary>
    122     public event EventHandler DescriptionChanged;
    123     /// <summary>
    124     /// Fires a new <c>DescriptionChanged</c> event.
    125     /// </summary>
    126     protected virtual void OnDescriptionChanged() {
    127       if (DescriptionChanged != null)
    128         DescriptionChanged(this, new EventArgs());
     70    public override ExecutionContextCollection Apply(ExecutionContext context) {
     71      ExecutionContextCollection next = base.Apply(context);
     72      if (operatorGraph.InitialOperator != null)
     73        next.Insert(0, new ExecutionContext(context, operatorGraph.InitialOperator, context.Scope));
     74      return next;
    12975    }
    13076  }
  • trunk/sources/HeuristicLab.Operators/3.3/Counter.cs

    r1530 r2684  
    2525using HeuristicLab.Core;
    2626using HeuristicLab.Data;
     27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2728
    2829namespace HeuristicLab.Operators {
    2930  /// <summary>
    30   /// Class to increment an int variable by 1.
     31  /// Operator which increments an integer variable.
    3132  /// </summary>
    32   public class Counter : OperatorBase {
    33     /// <summary>
    34     /// Gets the description of the current instance.
    35     /// </summary>
    36     public override string Description {
    37       get { return @"TODO\r\nOperator description still missing ..."; }
     33  [Item("Counter", "An operator which increments an integer variable.")]
     34  [EmptyStorableClass]
     35  [Creatable("Test")]
     36  public sealed class Counter : StandardOperator {
     37    public ItemParameter<IntData> Value {
     38      get { return (ItemParameter<IntData>)Parameters["Value"]; }
     39    }
     40    public ItemParameter<IntData> Increment {
     41      get { return (ItemParameter<IntData>)Parameters["Increment"]; }
    3842    }
    3943
    40     /// <summary>
    41     /// Initializes a new instance of <see cref="Counter"/> with one variable info (<c>Value</c>).
    42     /// </summary>
    4344    public Counter()
    4445      : base() {
    45       AddVariableInfo(new VariableInfo("Value", "Counter value", typeof(IntData), VariableKind.In | VariableKind.Out));
     46      Parameters.Add(new ItemParameter<IntData>("Value", "The value which should be incremented."));
     47      Parameters.Add(new ItemParameter<IntData>("Increment", "The increment which is added to the value.", new IntData(1)));
    4648    }
    4749
    48     /// <summary>
    49     /// Increments an int value of the specified <paramref name="scope"/> by one.
    50     /// </summary>
    51     /// <param name="scope">The scope whose variable should be incremented.</param>
    52     /// <returns><c>null</c>.</returns>
    53     public override IOperation Apply(IScope scope) {
    54       IntData value = GetVariableValue<IntData>("Value", scope, true);
    55       value.Data++;
    56       return null;
     50    public override ExecutionContextCollection Apply(ExecutionContext context) {
     51      IntData value = Value.GetValue(context);
     52      IntData increment = Increment.GetValue(context);
     53      value.Value += increment.Value;
     54      return base.Apply(context);
    5755    }
    5856  }
  • trunk/sources/HeuristicLab.Operators/3.3/EmptyOperator.cs

    r2664 r2684  
    3131  /// An operator which represents an empty statement.
    3232  /// </summary>
    33   [Item("Empty Operator", "An operator which represents an empty statement.")]
     33  [Item("EmptyOperator", "An operator which represents an empty statement.")]
    3434  [Creatable("Test")]
    3535  [EmptyStorableClass]
  • trunk/sources/HeuristicLab.Operators/3.3/HeuristicLab.Operators-3.3.csproj

    r2664 r2684  
    8585  </ItemGroup>
    8686  <ItemGroup>
     87    <Compile Include="CombinedOperator.cs" />
     88    <Compile Include="Counter.cs" />
    8789    <Compile Include="EmptyOperator.cs">
    8890      <SubType>Code</SubType>
     
    9193    <Compile Include="Operator.cs" />
    9294    <Compile Include="Properties\AssemblyInfo.cs" />
     95    <Compile Include="SequentialProcessor.cs" />
    9396    <Compile Include="StandardOperator.cs" />
    9497  </ItemGroup>
  • trunk/sources/HeuristicLab.Operators/3.3/Operator.cs

    r2664 r2684  
    3737    public override Image ItemImage {
    3838      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Method; }
     39    }
     40    public override bool CanChangeDescription {
     41      get { return false; }
    3942    }
    4043
  • trunk/sources/HeuristicLab.Operators/3.3/SequentialProcessor.cs

    r1530 r2684  
    2323using System.Collections.Generic;
    2424using System.Text;
     25using System.Xml;
     26using HeuristicLab.Collections;
    2527using HeuristicLab.Core;
    2628using HeuristicLab.Data;
     29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2730
    2831namespace HeuristicLab.Operators {
    2932  /// <summary>
    30   /// Performs <c>n</c> operators on the given scope sequentially.
     33  /// Operator which executes multiple operators sequentially.
    3134  /// </summary>
    32   public class SequentialProcessor : OperatorBase {
    33     /// <inheritdoc select="summary"/>
    34     public override string Description {
    35       get { return @"TODO\r\nOperator description still missing ..."; }
     35  [Item("SequentialProcessor", "An operator which executes multiple operators sequentially.")]
     36  [Creatable("Test")]
     37  [EmptyStorableClass]
     38  public sealed class SequentialProcessor : Operator, IOperator {
     39    public new ParameterCollection Parameters {
     40      get {
     41        return base.Parameters;
     42      }
     43    }
     44    IObservableKeyedCollection<string, IParameter> IOperator.Parameters {
     45      get { return Parameters; }
    3646    }
    3747
    38     /// <summary>
    39     /// Applies <c>n</c> operators on the given <paramref name="scope"/>.
    40     /// </summary>
    41     /// <param name="scope">The scope to apply the operators on.</param>
    42     /// <returns>A new <see cref="CompositeOperation"/> with the <c>n</c> operators applied
    43     /// to the given <paramref name="scope"/>.</returns>
    44     public override IOperation Apply(IScope scope) {
    45       CompositeOperation next = new CompositeOperation();
    46       for (int i = 0; i < SubOperators.Count; i++)
    47         next.AddOperation(new AtomicOperation(SubOperators[i], scope));
     48    public SequentialProcessor()
     49      : base() {
     50    }
     51
     52    public override ExecutionContextCollection Apply(ExecutionContext context) {
     53      ExecutionContextCollection next = new ExecutionContextCollection();
     54      foreach (IParameter param in Parameters) {
     55        IOperatorParameter opParam = param as IOperatorParameter;
     56        if (opParam != null) {
     57          IOperator op = (IOperator)opParam.GetValue(context);
     58          if (op != null) next.Add(new ExecutionContext(context.Parent, op, context.Scope));
     59        }
     60      }
    4861      return next;
    4962    }
  • trunk/sources/HeuristicLab.Operators/3.3/StandardOperator.cs

    r2664 r2684  
    3131  /// A base class for operators which have only one successor.
    3232  /// </summary>
    33   [Item("Standard Operator", "A base class for operators which have only one successor.")]
     33  [Item("StandardOperator", "A base class for operators which have only one successor.")]
    3434  [Creatable("Test")]
    3535  [EmptyStorableClass]
     
    4545
    4646    public override ExecutionContextCollection Apply(ExecutionContext context) {
    47       IOperator successor = Successor.GetValue(context);
     47      IOperator successor = Successor.GetValue(context, false);
    4848      if (successor != null)
    4949        return new ExecutionContextCollection(new ExecutionContext(context.Parent, successor, context.Scope));
Note: See TracChangeset for help on using the changeset viewer.