Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/08/10 03:43:36 (15 years ago)
Author:
swagner
Message:

Operator architecture refactoring (#95)

  • worked on parameters and operators
Location:
trunk/sources/HeuristicLab.Operators/3.3
Files:
3 added
15 deleted
15 edited

Legend:

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

    r2740 r2757  
    6868    }
    6969
    70     public override ExecutionContextCollection Apply() {
    71       ExecutionContextCollection next = base.Apply();
     70    public override IExecutionContext Apply() {
     71      ExecutionContextCollection next = new ExecutionContextCollection(base.Apply());
    7272      if (operatorGraph.InitialOperator != null)
    7373        next.Insert(0, new ExecutionContext(ExecutionContext, operatorGraph.InitialOperator, ExecutionContext.Scope));
  • trunk/sources/HeuristicLab.Operators/3.3/ConditionalBranch.cs

    r1530 r2757  
    2323using System.Collections.Generic;
    2424using System.Text;
     25using System.Xml;
    2526using HeuristicLab.Core;
    2627using HeuristicLab.Data;
     28using HeuristicLab.Parameters;
     29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2730
    2831namespace HeuristicLab.Operators {
    2932  /// <summary>
    30   /// Branch of (one or)two operators whose executions depend on a specific condition.
     33  /// A branch of two operators whose executions depend on a condition.
    3134  /// </summary>
    32   public class ConditionalBranch : OperatorBase {
    33     /// <inheritdoc select="summary"/>
    34     public override string Description {
    35       get { return @"ConditionalBranch expects to have 1 or 2 sub-operators.
    36 It will return the 1st sub-operator if ""Condition"" is true and the 2nd sub-operator if ""Condition"" equals to false.
    37 
    38 In case a 2nd sub-operator does not exist and ""Condition"" would equal to false, Conditional Branch will not return a new operation."; }
     35  [Item("ConditionalBranch", "A branch of two operators whose executions depend on a boolean condition.")]
     36  [Creatable("Test")]
     37  [EmptyStorableClass]
     38  public class ConditionalBranch : SingleSuccessorOperator {
     39    public LookupParameter<BoolData> ConditionParameter {
     40      get { return (LookupParameter<BoolData>)Parameters["Condition"]; }
     41    }
     42    protected OperatorParameter TrueBranchParameter {
     43      get { return (OperatorParameter)Parameters["TrueBranch"]; }
     44    }
     45    protected OperatorParameter FalseBranchParameter {
     46      get { return (OperatorParameter)Parameters["FalseBranch"]; }
     47    }
     48    public IOperator TrueBranch {
     49      get { return TrueBranchParameter.Value; }
     50      set { TrueBranchParameter.Value = value; }
     51    }
     52    public IOperator FalseBranch {
     53      get { return FalseBranchParameter.Value; }
     54      set { FalseBranchParameter.Value = value; }
    3955    }
    4056
    41     /// <summary>
    42     /// Initializes a new instance of <see cref="ConditionalBranch"/> with one variable info
    43     /// (<c>Condition</c>).
    44     /// </summary>
    4557    public ConditionalBranch()
    4658      : base() {
    47       AddVariableInfo(new VariableInfo("Condition", "A boolean variable that decides the branch", typeof(BoolData), VariableKind.In));
     59      Parameters.Add(new LookupParameter<BoolData>("Condition", "A boolean variable which defines which branch is executed."));
     60      Parameters.Add(new OperatorParameter("TrueBranch", "The operator which is executed if the condition is true."));
     61      Parameters.Add(new OperatorParameter("FalseBranch", "The operator which is executed if the condition is false."));
    4862    }
    4963
    50     /// <summary>
    51     /// Applies the operator of branch one under a specific condition on the given
    52     /// <paramref name="scope"/>, or - if existent - operator of branch two if the condition could not
    53     /// be fulfilled.
    54     /// </summary>
    55     /// <param name="scope">The scope to apply the operators on.</param>
    56     /// <returns>A new <see cref="AtomicOperation"/> with either operator 1 or operator 2 applied
    57     /// to the given <paramref name="scope"/> or <c>null</c>.</returns>
    58     public override IOperation Apply(IScope scope) {
    59       BoolData resultData = GetVariableValue<BoolData>("Condition", scope, true);
    60       bool result = resultData.Data;
    61 
    62       if ((result) && (SubOperators.Count > 0) && (SubOperators[0] != null))
    63         return new AtomicOperation(SubOperators[0], scope);
    64       else if ((!result) && (SubOperators.Count > 1) && (SubOperators[1] != null))
    65         return new AtomicOperation(SubOperators[1], scope);
    66       return null;
     64    public override IExecutionContext Apply() {
     65      ExecutionContextCollection next = new ExecutionContextCollection(base.Apply());
     66      if (ConditionParameter.ActualValue.Value) {
     67        if (TrueBranch != null) next.Insert(0, new ExecutionContext(ExecutionContext.Parent, TrueBranch, ExecutionContext.Scope));
     68      } else {
     69        if (FalseBranch != null) next.Insert(0, new ExecutionContext(ExecutionContext.Parent, FalseBranch, ExecutionContext.Scope));
     70      }
     71      return next;
    6772    }
    6873  }
  • trunk/sources/HeuristicLab.Operators/3.3/Counter.cs

    r2756 r2757  
    3939      get { return (LookupParameter<IntData>)Parameters["Value"]; }
    4040    }
    41     public ValueLookupParameter<IntData> IncrementParaneter {
     41    public ValueLookupParameter<IntData> IncrementParameter {
    4242      get { return (ValueLookupParameter<IntData>)Parameters["Increment"]; }
    4343    }
    4444    public IntData Increment {
    45       get { return IncrementParaneter.Value; }
    46       set { IncrementParaneter.Value = value; }
     45      get { return IncrementParameter.Value; }
     46      set { IncrementParameter.Value = value; }
    4747    }
    4848
     
    5353    }
    5454
    55     public override ExecutionContextCollection Apply() {
     55    public override IExecutionContext Apply() {
    5656      if (ValueParameter.ActualValue == null) ValueParameter.ActualValue = new IntData();
    57       ValueParameter.ActualValue.Value += IncrementParaneter.ActualValue.Value;
     57      ValueParameter.ActualValue.Value += IncrementParameter.ActualValue.Value;
    5858      return base.Apply();
    5959    }
  • trunk/sources/HeuristicLab.Operators/3.3/DoubleCounter.cs

    r1530 r2757  
    2525using HeuristicLab.Core;
    2626using HeuristicLab.Data;
     27using HeuristicLab.Parameters;
     28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2729
    2830namespace HeuristicLab.Operators {
    2931  /// <summary>
    30   /// Class to add a specified interval to a double value.
     32  /// Operator which increments a double variable.
    3133  /// </summary>
    32   public class DoubleCounter : OperatorBase {
    33     /// <summary>
    34     /// Gets the description of the current instance.
    35     /// </summary>
    36     public override string Description {
    37       get { return @"Adds a given interval to a double value"; }
     34  [Item("DoubleCounter", "An operator which increments a double variable.")]
     35  [EmptyStorableClass]
     36  [Creatable("Test")]
     37  public sealed class DoubleCounter : SingleSuccessorOperator {
     38    public LookupParameter<DoubleData> ValueParameter {
     39      get { return (LookupParameter<DoubleData>)Parameters["Value"]; }
     40    }
     41    public ValueLookupParameter<DoubleData> IncrementParameter {
     42      get { return (ValueLookupParameter<DoubleData>)Parameters["Increment"]; }
     43    }
     44    public DoubleData Increment {
     45      get { return IncrementParameter.Value; }
     46      set { IncrementParameter.Value = value; }
    3847    }
    3948
    40     /// <summary>
    41     /// Initializes a new instance of <see cref="DoubleCounter"/>, including two variable infos
    42     /// (<c>Value</c> and <c>Interval</c>).
    43     /// </summary>
    4449    public DoubleCounter()
    4550      : base() {
    46       AddVariableInfo(new VariableInfo("Value", "Counter value", typeof(DoubleData), VariableKind.In | VariableKind.Out));
    47       AddVariableInfo(new VariableInfo("Interval", "Interval value", typeof(DoubleData), VariableKind.In));
     51      Parameters.Add(new LookupParameter<DoubleData>("Value", "The value which should be incremented."));
     52      Parameters.Add(new ValueLookupParameter<DoubleData>("Increment", "The increment which is added to the value.", new DoubleData(1)));
    4853    }
    4954
    50     /// <summary>
    51     /// Adds to a double value of the given <paramref name="scope"/> a specified interval.
    52     /// </summary>
    53     /// <param name="scope">The scope whose variable should be incremented.</param>
    54     /// <returns><c>null</c>.</returns>
    55     public override IOperation Apply(IScope scope) {
    56       DoubleData value = GetVariableValue<DoubleData>("Value", scope, true);
    57       double interval = GetVariableValue<DoubleData>("Interval", scope, true).Data;
    58       value.Data += interval;
    59       return null;
     55    public override IExecutionContext Apply() {
     56      if (ValueParameter.ActualValue == null) ValueParameter.ActualValue = new DoubleData();
     57      ValueParameter.ActualValue.Value += IncrementParameter.ActualValue.Value;
     58      return base.Apply();
    6059    }
    6160  }
  • trunk/sources/HeuristicLab.Operators/3.3/HeuristicLab.Operators-3.3.csproj

    r2754 r2757  
    8787    <Compile Include="CombinedOperator.cs" />
    8888    <None Include="HeuristicLabOperatorsPlugin.cs.frame" />
     89    <Compile Include="ConditionalBranch.cs" />
     90    <Compile Include="Comparator.cs" />
     91    <Compile Include="ScopeCleaner.cs" />
     92    <Compile Include="SubScopesRemover.cs" />
     93    <Compile Include="SubScopesSorter.cs" />
     94    <Compile Include="DoubleCounter.cs" />
     95    <Compile Include="ParallelProcessor.cs" />
     96    <Compile Include="UniformParallelSubScopesProcessor.cs" />
     97    <Compile Include="UniformSequentialSubScopesProcessor.cs" />
     98    <Compile Include="ValueCollector.cs" />
    8999    <Compile Include="MultipleSuccessorsOperator.cs" />
    90100    <Compile Include="Counter.cs" />
     
    97107    <Compile Include="SequentialProcessor.cs" />
    98108    <Compile Include="SingleSuccessorOperator.cs" />
     109    <Compile Include="SubScopesCreater.cs" />
     110    <Compile Include="VariableInjector.cs">
     111      <SubType>Code</SubType>
     112    </Compile>
    99113  </ItemGroup>
    100114  <ItemGroup>
  • trunk/sources/HeuristicLab.Operators/3.3/Operator.cs

    r2740 r2757  
    6161    }
    6262
     63    [Storable]
     64    private ExecutionContext executionContext;
     65    protected ExecutionContext ExecutionContext {
     66      get { return executionContext; }
     67      private set {
     68        if (value != executionContext) {
     69          executionContext = value;
     70          OnExecutionContextChanged();
     71        }
     72      }
     73    }
     74
    6375    /// <summary>
    6476    /// Flag whether the current instance has been canceled.
     
    6880    protected bool Canceled {
    6981      get { return canceled; }
     82      private set {
     83        if (value != canceled) {
     84          canceled = value;
     85          OnCanceledChanged();
     86        }
     87      }
    7088    }
    7189
     
    82100        }
    83101      }
    84     }
    85 
    86     [Storable]
    87     private ExecutionContext executionContext;
    88     protected ExecutionContext ExecutionContext {
    89       get { return executionContext; }
    90102    }
    91103
     
    118130
    119131    /// <inheritdoc/>
    120     public virtual ExecutionContextCollection Execute(ExecutionContext context) {
     132    public virtual IExecutionContext Execute(ExecutionContext context) {
    121133      try {
    122         canceled = false;
    123         executionContext = context;
     134        Canceled = false;
     135        ExecutionContext = context;
    124136        foreach (IParameter param in Parameters)
    125137          param.ExecutionContext = context;
    126         ExecutionContextCollection next = Apply();
     138        IExecutionContext next = Apply();
    127139        OnExecuted();
    128140        return next;
     
    131143        foreach (IParameter param in Parameters)
    132144          param.ExecutionContext = null;
    133         context = null;
     145        ExecutionContext = null;
    134146      }
    135147    }
    136148    /// <inheritdoc/>
    137149    /// <remarks>Sets property <see cref="Canceled"/> to <c>true</c>.</remarks>
    138     public virtual void Abort() {
    139       canceled = true;
     150    public void Abort() {
     151      Canceled = true;
    140152    }
    141153    /// <summary>
     
    144156    /// <param name="scope">The scope where to execute the operator</param>
    145157    /// <returns><c>null</c>.</returns>
    146     public virtual ExecutionContextCollection Apply() {
    147       return new ExecutionContextCollection();
    148     }
     158    public abstract IExecutionContext Apply();
    149159
     160    protected virtual void OnExecutionContextChanged() { }
     161    protected virtual void OnCanceledChanged() { }
    150162    /// <inheritdoc/>
    151163    public event EventHandler BreakpointChanged;
  • trunk/sources/HeuristicLab.Operators/3.3/ParallelProcessor.cs

    r1530 r2757  
    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; operations can be executed in parallel.
     33  /// Operator which executes multiple operators in parallel.
    3134  /// </summary>
    32   public class ParallelProcessor : OperatorBase {
    33     /// <inheritdoc select="summary"/>
    34     public override string Description {
    35       get { return @"TODO\r\nOperator description still missing ..."; }
     35  [Item("ParallelProcessor", "An operator which executes multiple operators in parallel.")]
     36  [Creatable("Test")]
     37  [EmptyStorableClass]
     38  public sealed class ParallelProcessor : MultipleSuccessorsOperator {
     39    public ParallelProcessor()
     40      : base() {
    3641    }
    3742
    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"/> with the <c>ExecuteInParallel</c> set to <c>true</c>.</returns>
    44     public override IOperation Apply(IScope scope) {
    45       CompositeOperation next = new CompositeOperation();
    46       next.ExecuteInParallel = true;
    47       for (int i = 0; i < SubOperators.Count; i++)
    48         next.AddOperation(new AtomicOperation(SubOperators[i], scope));
     43    public override IExecutionContext Apply() {
     44      ExecutionContextCollection next = new ExecutionContextCollection();
     45      for (int i = 0; i < Successors.Count; i++) {
     46        if (Successors[i] != null)
     47          next.Add(new ExecutionContext(ExecutionContext.Parent, Successors[i], ExecutionContext.Scope));
     48      }
     49      next.Parallel = true;
    4950      return next;
    5051    }
  • trunk/sources/HeuristicLab.Operators/3.3/ScopeCleaner.cs

    r1530 r2757  
    2424using System.Text;
    2525using HeuristicLab.Core;
    26 using HeuristicLab.Data;
     26using HeuristicLab.Parameters;
     27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2728
    2829namespace HeuristicLab.Operators {
    2930  /// <summary>
    30   /// Operator that removes all variables in the given scope and deletes also all subscopes.
     31  /// An operator which removes all variables and sub-scopes from the current scope.
    3132  /// </summary>
    32   public class ScopeCleaner : OperatorBase {
    33     /// <inheritdoc select="summary"/>
    34     public override string Description {
    35       get { return @"Removes all variables in the current scope and deletes all subscopes"; }
     33  [Item("ScopeCleaner", "An operator which removes all variables and sub-scopes from the current scope.")]
     34  [EmptyStorableClass]
     35  [Creatable("Test")]
     36  public sealed class ScopeCleaner : SingleSuccessorOperator {
     37    private ScopeParameter CurrentScopeParameter {
     38      get { return (ScopeParameter)Parameters["CurrentScope"]; }
     39    }
     40    public IScope CurrentScope {
     41      get { return CurrentScopeParameter.ActualValue; }
    3642    }
    3743
    38     /// <summary>
    39     /// Deletes all variable and sub scopes from the given <paramref name="scope"/>.
    40     /// </summary>
    41     /// <remarks>Calls <see cref="IScope.Clear"/> of interface <see cref="IScope"/>.</remarks>
    42     /// <param name="scope">The scope to clear.</param>
    43     /// <returns><c>null</c>.</returns>
    44     public override IOperation Apply(IScope scope) {
    45       scope.Clear();
    46       return null;
     44    public ScopeCleaner()
     45      : base() {
     46      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope whose variables and sub-scopes should be removed."));
     47    }
     48
     49    public override IExecutionContext Apply() {
     50      CurrentScope.Variables.Clear();
     51      CurrentScope.SubScopes.Clear();
     52      return base.Apply();
    4753    }
    4854  }
  • trunk/sources/HeuristicLab.Operators/3.3/SequentialProcessor.cs

    r2740 r2757  
    4141    }
    4242
    43     public override ExecutionContextCollection Apply() {
     43    public override IExecutionContext Apply() {
    4444      ExecutionContextCollection next = new ExecutionContextCollection();
    45       for (int i = 0; i < Successors.Count; i++)
    46         next.Add(new ExecutionContext(ExecutionContext.Parent, Successors[i], ExecutionContext.Scope));
     45      for (int i = 0; i < Successors.Count; i++) {
     46        if (Successors[i] != null)
     47          next.Add(new ExecutionContext(ExecutionContext.Parent, Successors[i], ExecutionContext.Scope));
     48      }
    4749      return next;
    4850    }
  • trunk/sources/HeuristicLab.Operators/3.3/SingleSuccessorOperator.cs

    r2740 r2757  
    4949    }
    5050
    51     public override ExecutionContextCollection Apply() {
     51    public override IExecutionContext Apply() {
    5252      if (Successor != null)
    53         return new ExecutionContextCollection(new ExecutionContext(ExecutionContext.Parent, Successor, ExecutionContext.Scope));
     53        return new ExecutionContext(ExecutionContext.Parent, Successor, ExecutionContext.Scope);
    5454      else
    55         return new ExecutionContextCollection();
     55        return null;
    5656    }
    5757  }
  • trunk/sources/HeuristicLab.Operators/3.3/SubScopesCreater.cs

    r1530 r2757  
    2525using HeuristicLab.Core;
    2626using HeuristicLab.Data;
     27using HeuristicLab.Parameters;
     28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2729
    2830namespace HeuristicLab.Operators {
    2931  /// <summary>
    30   /// Operator to create sub scopes in a given scope.
     32  /// An operator which adds new and empty sub-scopes to the current scope.
    3133  /// </summary>
    32   public class SubScopesCreater : OperatorBase {
    33     /// <inheritdoc select="summary"/>
    34     public override string Description {
    35       get { return @"TODO\r\nOperator description still missing ..."; }
     34  [Item("SubScopesCreater", "An operator which adds new and empty sub-scopes to the current scope.")]
     35  [EmptyStorableClass]
     36  [Creatable("Test")]
     37  public class SubScopesCreater : SingleSuccessorOperator {
     38    public ValueLookupParameter<IntData> NumberOfSubScopesParameter {
     39      get { return (ValueLookupParameter<IntData>)Parameters["NumberOfSubScopes"]; }
     40    }
     41    protected ScopeParameter CurrentScopeParameter {
     42      get { return (ScopeParameter)Parameters["CurrentScope"]; }
     43    }
     44    public IScope CurrentScope {
     45      get { return CurrentScopeParameter.ActualValue; }
    3646    }
    3747
    38     /// <summary>
    39     /// Initializes a new instance of <see cref="SubScopesCreater"/> with one variable info (<c>SubScopes</c>).
    40     /// </summary>
    4148    public SubScopesCreater()
    4249      : base() {
    43       AddVariableInfo(new VariableInfo("SubScopes", "Number of sub-scopes", typeof(IntData), VariableKind.In));
     50      Parameters.Add(new ValueLookupParameter<IntData>("NumberOfSubScopes", "The number of new and empty sub-scopes which should be added to the current scope."));
     51      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope to which the new and empty sub-scopes are added."));
    4452    }
    4553
    46     /// <summary>
    47     /// Creates a specified number of sub scopes in the given <paramref name="scope"/>.
    48     /// </summary>
    49     /// <param name="scope">The scope where to create the sub scopes.</param>
    50     /// <returns><c>null</c>.</returns>
    51     public override IOperation Apply(IScope scope) {
    52       IntData count = GetVariableValue<IntData>("SubScopes", scope, true);
    53 
    54       for (int i = 0; i < count.Data; i++)
    55         scope.AddSubScope(new Scope(i.ToString()));
    56 
    57       return null;
     54    public override IExecutionContext Apply() {
     55      int n = NumberOfSubScopesParameter.ActualValue.Value;
     56      for (int i = 0; i < n; i++)
     57        CurrentScope.SubScopes.Add(new Scope(i.ToString()));
     58      return base.Apply();
    5859    }
    5960  }
  • trunk/sources/HeuristicLab.Operators/3.3/SubScopesRemover.cs

    r1530 r2757  
    2525using HeuristicLab.Core;
    2626using HeuristicLab.Data;
     27using HeuristicLab.Parameters;
     28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2729
    2830namespace HeuristicLab.Operators {
    2931  /// <summary>
    30   /// Removes one specified or - if nothing specified - all operators from the given scope.
     32  /// An operator which removes one specified or (if not specified) all sub-scopes from the current scope.
    3133  /// </summary>
    32   public class SubScopesRemover : OperatorBase {
    33     /// <inheritdoc select="summary"/>
    34     public override string Description {
    35       get {
    36         return @"This operator either removes the subscope specified in the variable SubScopeIndex or if this variable is absent, removes all subscopes.";
    37       }
     34  [Item("SubScopesRemover", "An operator which removes one specified or (if not specified) all sub-scopes from the current scope.")]
     35  [EmptyStorableClass]
     36  [Creatable("Test")]
     37  public class SubScopesRemover : SingleSuccessorOperator {
     38    public ValueLookupParameter<IntData> SubScopeIndexParameter {
     39      get { return (ValueLookupParameter<IntData>)Parameters["SubScopeIndex"]; }
     40    }
     41    protected ScopeParameter CurrentScopeParameter {
     42      get { return (ScopeParameter)Parameters["CurrentScope"]; }
     43    }
     44    public IScope CurrentScope {
     45      get { return CurrentScopeParameter.ActualValue; }
    3846    }
    3947
    40     /// <summary>
    41     /// Initializes a new instance of <see cref="SubScopesRemover"/> with one variable info
    42     /// (<c>SubScopeIndex</c>).
    43     /// </summary>
    4448    public SubScopesRemover()
    4549      : base() {
    46       AddVariableInfo(new VariableInfo("SubScopeIndex", "(Optional) the index of the subscope to remove", typeof(IntData), VariableKind.In));
     50      Parameters.Add(new ValueLookupParameter<IntData>("SubScopeIndex", "The index of the sub-scope which should be removed. If this parameter has no value, all sub-scopes of the current scope are removed."));
     51      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope from which one or all sub-scopes should be removed."));
    4752    }
    4853
    49     /// <summary>
    50     /// Removes one sub scope with a specified index from the given <paramref name="scope"/>, or if no
    51     /// index has been specified, all sub scopes are removed.
    52     /// </summary>
    53     /// <exception cref="InvalidOperationException">Thrown when no sub scope with the specified index
    54     /// exists.</exception>
    55     /// <param name="scope">The scope whose sub scope(s) to remove.</param>
    56     /// <returns><c>null</c>.</returns>
    57     public override IOperation Apply(IScope scope) {
    58       IntData index = GetVariableValue<IntData>("SubScopeIndex", scope, true, false);
    59       if (index == null) { // remove all scopes
    60         while (scope.SubScopes.Count > 0) {
    61           scope.RemoveSubScope(scope.SubScopes[0]);
    62         }
    63       } else {
    64         if (index.Data < 0 && index.Data >= scope.SubScopes.Count) throw new InvalidOperationException("ERROR: no scope with index " + index.Data + " exists");
    65         scope.RemoveSubScope(scope.SubScopes[index.Data]);
    66       }
    67       return null;
     54    public override IExecutionContext Apply() {
     55      IntData index = SubScopeIndexParameter.ActualValue;
     56      if (index != null)
     57        CurrentScope.SubScopes.RemoveAt(index.Value);
     58      else
     59        CurrentScope.SubScopes.Clear();
     60      return base.Apply();
    6861    }
    6962  }
  • trunk/sources/HeuristicLab.Operators/3.3/UniformParallelSubScopesProcessor.cs

    r1530 r2757  
    2323using System.Collections.Generic;
    2424using System.Text;
     25using System.Xml;
    2526using HeuristicLab.Core;
    26 using HeuristicLab.Data;
     27using HeuristicLab.Parameters;
     28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2729
    2830namespace HeuristicLab.Operators {
    2931  /// <summary>
    30   /// Performs the same operator on all <c>n</c> existing sub scopes of a given scope;
    31   /// operations can be executed in parallel.
     32  /// An operator which applies a specified operator on all sub-scopes of the current scope in parallel.
    3233  /// </summary>
    33   public class UniformParallelSubScopesProcessor : OperatorBase {
    34     /// <inheritdoc select="summary"/>
    35     public override string Description {
    36       get { return @"TODO\r\nOperator description still missing ..."; }
     34  [Item("UniformParallelSubScopesProcessor", "An operator which applies a specified operator on all sub-scopes of the current scope in parallel.")]
     35  [Creatable("Test")]
     36  [EmptyStorableClass]
     37  public sealed class UniformParallelSubScopesProcessor : SingleSuccessorOperator {
     38    private OperatorParameter OperatorParameter {
     39      get { return (OperatorParameter)Parameters["Operator"]; }
     40    }
     41    public IOperator Operator {
     42      get { return OperatorParameter.Value; }
     43      set { OperatorParameter.Value = value; }
    3744    }
    3845
    39     /// <summary>
    40     /// Applies one operator on all the sub scopes of the given <paramref name="scope"/>.
    41     /// </summary>
    42     /// <param name="scope">The scope on whose sub scopes the operator is applied.</param>
    43     /// <returns>A new <see cref="CompositeOperation"/> with one operator and all sub scopes and
    44     /// the <c>ExecuteInParallel</c> flag set to <c>true</c>.</returns>
    45     public override IOperation Apply(IScope scope) {
    46       CompositeOperation next = new CompositeOperation();
    47       next.ExecuteInParallel = true;
    48       for (int i = 0; i < scope.SubScopes.Count; i++)
    49         next.AddOperation(new AtomicOperation(SubOperators[0], scope.SubScopes[i]));
     46    public UniformParallelSubScopesProcessor()
     47      : base() {
     48      Parameters.Add(new OperatorParameter("Operator", "The operator which should be applied on all sub-scopes of the current scope in parallel."));
     49    }
     50
     51    public override IExecutionContext Apply() {
     52      ExecutionContextCollection next = new ExecutionContextCollection(base.Apply());
     53      if (Operator != null) {
     54        ExecutionContextCollection inner = new ExecutionContextCollection();
     55        inner.Parallel = true;
     56        for (int i = 0; i < ExecutionContext.Scope.SubScopes.Count; i++)
     57          inner.Add(new ExecutionContext(ExecutionContext.Parent, Operator, ExecutionContext.Scope.SubScopes[i]));
     58        next.Insert(0, inner);
     59      }
    5060      return next;
    5161    }
  • trunk/sources/HeuristicLab.Operators/3.3/UniformSequentialSubScopesProcessor.cs

    r1530 r2757  
    2323using System.Collections.Generic;
    2424using System.Text;
     25using System.Xml;
    2526using HeuristicLab.Core;
    26 using HeuristicLab.Data;
     27using HeuristicLab.Parameters;
     28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2729
    2830namespace HeuristicLab.Operators {
    2931  /// <summary>
    30   /// Performs the same operator on all existing sub scopes of a given scope,
    31   /// must be executed sequentially.
     32  /// An operator which applies a specified operator sequentially on all sub-scopes of the current scope.
    3233  /// </summary>
    33   public class UniformSequentialSubScopesProcessor : OperatorBase {
    34     /// <inheritdoc select="summary"/>
    35     public override string Description {
    36       get { return @"TODO\r\nOperator description still missing ..."; }
     34  [Item("UniformSequentialSubScopesProcessor", "An operator which applies a specified operator sequentially on all sub-scopes of the current scope.")]
     35  [Creatable("Test")]
     36  [EmptyStorableClass]
     37  public sealed class UniformSequentialSubScopesProcessor : SingleSuccessorOperator {
     38    private OperatorParameter OperatorParameter {
     39      get { return (OperatorParameter)Parameters["Operator"]; }
     40    }
     41    public IOperator Operator {
     42      get { return OperatorParameter.Value; }
     43      set { OperatorParameter.Value = value; }
    3744    }
    3845
    39     /// <summary>
    40     /// Applies one operator on all the sub scopes of the given <paramref name="scope"/>.
    41     /// </summary>
    42     /// <param name="scope">The scope on whose sub scopes the operator is applied.</param>
    43     /// <returns>A new <see cref="CompositeOperation"/> with one operator and all sub scopes.</returns>
    44     public override IOperation Apply(IScope scope) {
    45       CompositeOperation next = new CompositeOperation();
    46       for (int i = 0; i < scope.SubScopes.Count; i++)
    47         next.AddOperation(new AtomicOperation(SubOperators[0], scope.SubScopes[i]));
     46    public UniformSequentialSubScopesProcessor()
     47      : base() {
     48      Parameters.Add(new OperatorParameter("Operator", "The operator which should be applied sequentially on all sub-scopes of the current scope."));
     49    }
     50
     51    public override IExecutionContext Apply() {
     52      ExecutionContextCollection next = new ExecutionContextCollection(base.Apply());
     53      if (Operator != null) {
     54        ExecutionContextCollection inner = new ExecutionContextCollection();
     55        for (int i = 0; i < ExecutionContext.Scope.SubScopes.Count; i++)
     56          inner.Add(new ExecutionContext(ExecutionContext.Parent, Operator, ExecutionContext.Scope.SubScopes[i]));
     57        next.Insert(0, inner);
     58      }
    4859      return next;
    4960    }
  • trunk/sources/HeuristicLab.Operators/3.3/VariableInjector.cs

    r2526 r2757  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Linq;
    2425using System.Text;
    2526using System.Xml;
     27using HeuristicLab.Collections;
    2628using HeuristicLab.Core;
     29using HeuristicLab.Parameters;
    2730using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2831
    2932namespace HeuristicLab.Operators {
    3033  /// <summary>
    31   /// Class to inject local variables into the scope.
     34  /// An operator which collects the actual values of parameters and clones them into the current scope.
    3235  /// </summary>
    33   public class VariableInjector : OperatorBase {
    34 
    35     private Dictionary<IVariable, IVariableInfo> variableVariableInfoTable;   
    36     private Dictionary<IVariableInfo, IVariable> variableInfoVariableTable;
    37 
    38     /// <inheritdoc select="summary"/>
    39     public override string Description {
    40       get { return @"TODO\r\nOperator description still missing ..."; }
     36  [Item("VariableInjector", "An operator which collects the actual values of parameters and clones them into the current scope.")]
     37  [Creatable("Test")]
     38  [EmptyStorableClass]
     39  public class VariableInjector : ValueCollector {
     40    protected ScopeParameter CurrentScopeParameter {
     41      get { return (ScopeParameter)Parameters["CurrentScope"]; }
     42    }
     43    public IScope CurrentScope {
     44      get { return CurrentScopeParameter.ActualValue; }
    4145    }
    4246
    43     /// <summary>
    44     /// Initializes a new instance of <see cref="VariableInjector"/>.
    45     /// </summary>
    4647    public VariableInjector()
    4748      : base() {
    48       variableVariableInfoTable = new Dictionary<IVariable, IVariableInfo>();
    49       variableInfoVariableTable = new Dictionary<IVariableInfo, IVariable>();
     49      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope into which the parameter values are cloned."));
    5050    }
    5151
    52     /// <summary>
    53     /// Clones the current instance (deep clone).
    54     /// </summary>
    55     /// <remarks>Deep clone performed with <see cref="cloner.Clone"/> of helper class
    56     /// <see cref="Auxiliary"/>.</remarks>
    57     /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
    58     /// <returns>The cloned object as <see cref="VariableInjector"/>.</returns>
    59     public override IItem Clone(ICloner cloner) {
    60       VariableInjector clone = new VariableInjector();
    61       cloner.RegisterClonedObject(this, clone);
    62       clone.Name = Name;
    63       foreach (IVariable variable in Variables)
    64         clone.AddVariable((IVariable)cloner.Clone(variable));
    65       return clone;
     52    public override IExecutionContext Apply() {
     53      IVariable var;
     54      foreach (IParameter param in CollectedValues) {
     55        CurrentScope.Variables.TryGetValue(param.Name, out var);
     56        if (var != null)
     57          var.Value = (IItem)param.ActualValue.Clone();
     58        else
     59          CurrentScope.Variables.Add(new Variable(param.Name, (IItem)param.ActualValue.Clone()));
     60      }
     61      return base.Apply();
    6662    }
    67 
    68     /// <summary>
    69     /// Adds the specified <paramref name="variable"/> to the current instance to get injected.
    70     /// </summary>
    71     /// <param name="variable">The variable to add.</param>
    72     /// <remarks>Calls private method <see cref="CreateVariableInfo"/> and <see cref="OperatorBase.AddVariable"/>
    73     /// of base class <see cref="OperatorBase"/>.</remarks>
    74     public override void AddVariable(IVariable variable) {
    75       base.AddVariable(variable);
    76       CreateVariableInfo(variable);
    77     }
    78 
    79     /// <summary>
    80     /// Removes a variable with the specified <paramref name="name"/> from the current injector.
    81     /// </summary>
    82     /// <remarks>Calls private method <see cref="DeleteVariableInfo"/> and <see cref="OperatorBase.RemoveVariable"/>
    83     /// of base class <see cref="OperatorBase"/>.</remarks>
    84     /// <param name="name">The name of the </param>
    85     public override void RemoveVariable(string name) {
    86       DeleteVariableInfo(name);
    87       base.RemoveVariable(name);
    88     }
    89 
    90     /// <summary>
    91     /// Adds the specified variables to the given <paramref name="scope"/> (and removes them first,
    92     /// if they already exist in the current scope).
    93     /// </summary>
    94     /// <param name="scope">The scope where to inject the variables.</param>
    95     /// <returns><c>null</c>.</returns>
    96     public override IOperation Apply(IScope scope) {
    97       foreach (IVariable variable in Variables) {
    98         if (scope.GetVariable(variable.Name) != null)
    99           scope.RemoveVariable(variable.Name);
    100         scope.AddVariable((IVariable)variable.Clone());
    101       }
    102       return null;
    103     }
    104 
    105     [Storable]
    106     private KeyValuePair<Dictionary<IVariableInfo, IVariable>, Dictionary<IVariable, IVariableInfo>> VariableMappingPersistence {
    107       get {
    108         return new KeyValuePair<Dictionary<IVariableInfo, IVariable>, Dictionary<IVariable, IVariableInfo>>(
    109           variableInfoVariableTable, variableVariableInfoTable);
    110       }
    111       set {
    112         variableInfoVariableTable = value.Key;
    113         variableVariableInfoTable = value.Value;
    114         foreach (var pair in variableInfoVariableTable) {
    115           pair.Key.ActualNameChanged += new EventHandler(VariableInfo_ActualNameChanged);
    116           pair.Value.NameChanged += new EventHandler(Variable_NameChanged);         
    117         }
    118       }
    119     }
    120 
    121     private void CreateVariableInfo(IVariable variable) {
    122       IVariableInfo info = new VariableInfo(Guid.NewGuid().ToString(), "Injected variable", variable.Value.GetType(), VariableKind.New);
    123       info.ActualName = variable.Name;
    124       AddVariableInfo(info);
    125       variableVariableInfoTable.Add(variable, info);
    126       variableInfoVariableTable.Add(info, variable);
    127       info.ActualNameChanged += new EventHandler(VariableInfo_ActualNameChanged);
    128       variable.NameChanged += new EventHandler(Variable_NameChanged);
    129     }
    130     private void DeleteVariableInfo(string name) {
    131       IVariable variable = GetVariable(name);
    132       if (variable != null) {
    133         IVariableInfo info = variableVariableInfoTable[variable];
    134         RemoveVariableInfo(info.FormalName);
    135         variableVariableInfoTable.Remove(variable);
    136         variableInfoVariableTable.Remove(info);
    137         info.ActualNameChanged -= new EventHandler(VariableInfo_ActualNameChanged);
    138         variable.NameChanged -= new EventHandler(Variable_NameChanged);
    139       }
    140     }
    141 
    142     #region VariableInfo and Variable Events
    143     private void VariableInfo_ActualNameChanged(object sender, EventArgs e) {
    144       IVariableInfo info = (IVariableInfo)sender;
    145       IVariable variable = variableInfoVariableTable[info];
    146       variable.Name = info.ActualName;
    147     }
    148     private void Variable_NameChanged(object sender, EventArgs e) {
    149       IVariable variable = (IVariable)sender;
    150       IVariableInfo info = variableVariableInfoTable[variable];
    151       info.ActualName = variable.Name;
    152     }
    153     #endregion
    15463  }
    15564}
Note: See TracChangeset for help on using the changeset viewer.