Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/06/09 02:09:35 (15 years ago)
Author:
swagner
Message:

Refactoring of the operator architecture (#95)

Location:
branches/Operator Architecture Refactoring/HeuristicLab.Operators/3.2
Files:
1 added
17 edited

Legend:

Unmodified
Added
Removed
  • branches/Operator Architecture Refactoring/HeuristicLab.Operators/3.2/ComparatorBase.cs

    r1530 r2027  
    3636    /// </summary>
    3737    protected ComparatorBase() {
    38       AddVariableInfo(new VariableInfo("LeftSide", "Variable on the left side of the comparison", typeof(IItem), VariableKind.In));
    39       AddVariableInfo(new VariableInfo("RightSide", "Variable on the right side of the comparison", typeof(IItem), VariableKind.In));
    40       AddVariableInfo(new VariableInfo("Result", "Result of the comparison", typeof(BoolData), VariableKind.Out | VariableKind.New));
     38      AddParameter(new Parameter("LeftSide", "Variable on the left side of the comparison", typeof(IItem), ParameterType.In));
     39      AddParameter(new Parameter("RightSide", "Variable on the right side of the comparison", typeof(IItem), ParameterType.In));
     40      AddParameter(new Parameter("Result", "Result of the comparison", typeof(BoolData), ParameterType.Out));
    4141    }
    4242
     
    5050    /// <param name="scope">The scope where to apply the compare operation.</param>
    5151    /// <returns><c>null</c>.</returns>
    52     public override IOperation Apply(IScope scope) {
    53       BoolData result = GetVariableValue<BoolData>("Result", scope, false, false);
     52    public override IOperation Apply(IEnvironment env, IScope scope) {
     53      string resultName = env.TranslateName("Result");
     54      BoolData result = scope.GetVariableValue<BoolData>(resultName, false, false);
    5455      if (result == null) {
    5556        result = new BoolData(true);
    56         IVariableInfo info = GetVariableInfo("Result");
    57         if (info.Local)
    58           AddVariable(new Variable(info.ActualName, result));
    59         else
    60           scope.AddVariable(new Variable(scope.TranslateName(info.FormalName), result));
     57        scope.AddVariable(new Variable(resultName, result));
    6158      }
    62       IItem leftSide = GetVariableValue<IItem>("LeftSide", scope, true);
     59      IItem leftSide = scope.GetVariableValue<IItem>(env.TranslateName("LeftSide"), true);
    6360      if (!(leftSide is IComparable)) throw new InvalidOperationException("Comparator: Left side needs to be of type IComparable");
    6461      IComparable left = (IComparable)leftSide;
    65       IItem rightSide = GetVariableValue<IItem>("RightSide", scope, true);
     62      IItem rightSide = scope.GetVariableValue<IItem>(env.TranslateName("RightSide"), true);
    6663      result.Data = Compare(left, rightSide);
    6764      return null;
  • branches/Operator Architecture Refactoring/HeuristicLab.Operators/3.2/ConditionalBranch.cs

    r1530 r2027  
    4545    public ConditionalBranch()
    4646      : base() {
    47       AddVariableInfo(new VariableInfo("Condition", "A boolean variable that decides the branch", typeof(BoolData), VariableKind.In));
     47      AddParameter(new Parameter("Condition", "A boolean variable that decides the branch", typeof(BoolData), ParameterType.In));
    4848    }
    4949
     
    5656    /// <returns>A new <see cref="AtomicOperation"/> with either operator 1 or operator 2 applied
    5757    /// 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;
     58    public override IOperation Apply(IEnvironment env, IScope scope) {
     59      bool result = scope.GetVariableValue<BoolData>(env.TranslateName("Condition"), true).Data;
    6160
    6261      if ((result) && (SubOperators.Count > 0) && (SubOperators[0] != null))
    63         return new AtomicOperation(SubOperators[0], scope);
     62        return new AtomicOperation(SubOperators[0], env, scope);
    6463      else if ((!result) && (SubOperators.Count > 1) && (SubOperators[1] != null))
    65         return new AtomicOperation(SubOperators[1], scope);
     64        return new AtomicOperation(SubOperators[1], env, scope);
    6665      return null;
    6766    }
  • branches/Operator Architecture Refactoring/HeuristicLab.Operators/3.2/Counter.cs

    r1530 r2027  
    4343    public Counter()
    4444      : base() {
    45       AddVariableInfo(new VariableInfo("Value", "Counter value", typeof(IntData), VariableKind.In | VariableKind.Out));
     45      AddParameter(new Parameter("Value", "Counter value", typeof(IntData), ParameterType.InOut));
    4646    }
    4747
     
    5151    /// <param name="scope">The scope whose variable should be incremented.</param>
    5252    /// <returns><c>null</c>.</returns>
    53     public override IOperation Apply(IScope scope) {
    54       IntData value = GetVariableValue<IntData>("Value", scope, true);
     53    public override IOperation Apply(IEnvironment env, IScope scope) {
     54      IntData value = scope.GetVariableValue<IntData>(env.TranslateName("Value"), true);
    5555      value.Data++;
    5656      return null;
  • branches/Operator Architecture Refactoring/HeuristicLab.Operators/3.2/DoubleCounter.cs

    r1530 r2027  
    4444    public DoubleCounter()
    4545      : 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));
     46      AddParameter(new Parameter("Value", "Counter value", typeof(DoubleData), ParameterType.InOut));
     47      AddParameter(new Parameter("Interval", "Interval value", typeof(DoubleData), ParameterType.In));
    4848    }
    4949
     
    5353    /// <param name="scope">The scope whose variable should be incremented.</param>
    5454    /// <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;
     55    public override IOperation Apply(IEnvironment env, IScope scope) {
     56      DoubleData value = scope.GetVariableValue<DoubleData>(env.TranslateName("Value"), true);
     57      double interval = scope.GetVariableValue<DoubleData>(env.TranslateName("Interval"), true).Data;
    5858      value.Data += interval;
    5959      return null;
  • branches/Operator Architecture Refactoring/HeuristicLab.Operators/3.2/EmptyOperator.cs

    r1530 r2027  
    4747    /// <param name="scope">The scope to apply the operator on.</param>
    4848    /// <returns><c>null</c>.</returns>
    49     public override IOperation Apply(IScope scope) {
     49    public override IOperation Apply(IEnvironment environment, IScope scope) {
    5050      return null;
    5151    }
  • branches/Operator Architecture Refactoring/HeuristicLab.Operators/3.2/HeuristicLab.Operators-3.2.csproj

    r1534 r2027  
    8181  </ItemGroup>
    8282  <ItemGroup>
    83     <Compile Include="AddVariableInfoDialog.cs">
    84       <SubType>Form</SubType>
     83    <Compile Include="ComparatorBase.cs">
     84      <SubType>Code</SubType>
    8585    </Compile>
    86     <Compile Include="AddVariableInfoDialog.Designer.cs">
    87       <DependentUpon>AddVariableInfoDialog.cs</DependentUpon>
    88     </Compile>
    89     <Compile Include="CombinedOperator.cs" />
    90     <Compile Include="CombinedOperatorView.cs">
    91       <SubType>UserControl</SubType>
    92     </Compile>
    93     <Compile Include="CombinedOperatorView.Designer.cs">
    94       <DependentUpon>CombinedOperatorView.cs</DependentUpon>
    95     </Compile>
    96     <Compile Include="ComparatorBase.cs" />
    9786    <Compile Include="ConditionalBranch.cs">
    9887      <SubType>Code</SubType>
    9988    </Compile>
    100     <Compile Include="DelegatingOperator.cs" />
    101     <Compile Include="DoubleCounter.cs" />
    102     <Compile Include="ScopeCleaner.cs" />
    103     <Compile Include="StochasticMultiBranch.cs" />
    104     <Compile Include="SubScopesMixer.cs" />
    105     <Compile Include="DataCollector.cs" />
    106     <Compile Include="EqualToComparator.cs" />
    107     <Compile Include="GreaterOrEqualThanComparator.cs" />
    108     <Compile Include="GreaterThanComparator.cs" />
    109     <Compile Include="LessOrEqualThanComparator.cs" />
    110     <Compile Include="LessThanComparator.cs" />
    111     <Compile Include="OperatorExtractor.cs" />
    112     <Compile Include="ParallelProcessor.cs" />
    113     <Compile Include="EmptyOperator.cs" />
    114     <Compile Include="SequentialProcessor.cs" />
    115     <Compile Include="SingleObjectiveEvaluatorBase.cs" />
    116     <Compile Include="Sorter.cs" />
    117     <Compile Include="StochasticBranch.cs" />
    118     <Compile Include="SubScopesRemover.cs" />
    119     <Compile Include="UnequalToComparator.cs" />
    120     <Compile Include="UniformSequentialSubScopesProcessor.cs" />
    121     <Compile Include="UniformParallelSubScopesProcessor.cs" />
    122     <Compile Include="ParallelSubScopesProcessor.cs" />
    123     <Compile Include="SequentialSubScopesProcessor.cs" />
    124     <Compile Include="SubScopesCreater.cs" />
    125     <Compile Include="Counter.cs" />
     89    <Compile Include="Counter.cs">
     90      <SubType>Code</SubType>
     91    </Compile>
     92    <Compile Include="DoubleCounter.cs">
     93      <SubType>Code</SubType>
     94    </Compile>
     95    <Compile Include="EmptyOperator.cs">
     96      <SubType>Code</SubType>
     97    </Compile>
     98    <Compile Include="EqualToComparator.cs">
     99      <SubType>Code</SubType>
     100    </Compile>
     101    <Compile Include="GreaterOrEqualThanComparator.cs">
     102      <SubType>Code</SubType>
     103    </Compile>
     104    <Compile Include="GreaterThanComparator.cs">
     105      <SubType>Code</SubType>
     106    </Compile>
    126107    <Compile Include="HeuristicLabOperatorsPlugin.cs" />
     108    <Compile Include="LessOrEqualThanComparator.cs">
     109      <SubType>Code</SubType>
     110    </Compile>
     111    <Compile Include="LessThanComparator.cs">
     112      <SubType>Code</SubType>
     113    </Compile>
     114    <Compile Include="ParallelProcessor.cs">
     115      <SubType>Code</SubType>
     116    </Compile>
     117    <Compile Include="ParallelSubScopesProcessor.cs">
     118      <SubType>Code</SubType>
     119    </Compile>
    127120    <Compile Include="Properties\AssemblyInfo.cs" />
    128     <Compile Include="VariableInjector.cs" />
     121    <Compile Include="SequentialProcessor.cs">
     122      <SubType>Code</SubType>
     123    </Compile>
     124    <Compile Include="SequentialSubScopesProcessor.cs">
     125      <SubType>Code</SubType>
     126    </Compile>
     127    <Compile Include="SubScopesCreater.cs">
     128      <SubType>Code</SubType>
     129    </Compile>
     130    <Compile Include="UnequalToComparator.cs">
     131      <SubType>Code</SubType>
     132    </Compile>
     133    <Compile Include="UniformParallelSubScopesProcessor.cs">
     134      <SubType>Code</SubType>
     135    </Compile>
     136    <Compile Include="UniformSequentialSubScopesProcessor.cs">
     137      <SubType>Code</SubType>
     138    </Compile>
     139    <Compile Include="VariableInjector.cs">
     140      <SubType>Code</SubType>
     141    </Compile>
    129142    <Compile Include="VariableInjectorView.cs">
    130143      <SubType>UserControl</SubType>
     
    135148  </ItemGroup>
    136149  <ItemGroup>
    137     <ProjectReference Include="..\..\HeuristicLab.Constraints\3.2\HeuristicLab.Constraints-3.2.csproj">
    138       <Project>{FCD62C6F-4793-4593-AE9A-0BDCA256EE99}</Project>
    139       <Name>HeuristicLab.Constraints-3.2</Name>
    140     </ProjectReference>
    141150    <ProjectReference Include="..\..\HeuristicLab.Core\3.2\HeuristicLab.Core-3.2.csproj">
    142151      <Project>{F43B59AB-2B8C-4570-BC1E-15592086517C}</Project>
     
    157166  </ItemGroup>
    158167  <ItemGroup>
    159     <EmbeddedResource Include="AddVariableInfoDialog.resx">
    160       <DependentUpon>AddVariableInfoDialog.cs</DependentUpon>
    161       <SubType>Designer</SubType>
    162     </EmbeddedResource>
    163     <EmbeddedResource Include="CombinedOperatorView.resx">
    164       <DependentUpon>CombinedOperatorView.cs</DependentUpon>
    165       <SubType>Designer</SubType>
     168    <EmbeddedResource Include="VariableInjectorView.resx">
     169      <DependentUpon>VariableInjectorView.cs</DependentUpon>
    166170    </EmbeddedResource>
    167171  </ItemGroup>
  • branches/Operator Architecture Refactoring/HeuristicLab.Operators/3.2/HeuristicLabOperatorsPlugin.cs

    r1927 r2027  
    3131  [ClassInfo(Name = "HeuristicLab.Operators-3.2")]
    3232  [PluginFile(Filename = "HeuristicLab.Operators-3.2.dll", Filetype = PluginFileType.Assembly)]
    33   [Dependency(Dependency = "HeuristicLab.Constraints-3.2")]
    3433  [Dependency(Dependency = "HeuristicLab.Core-3.2")]
    3534  [Dependency(Dependency = "HeuristicLab.Data-3.2")]
  • branches/Operator Architecture Refactoring/HeuristicLab.Operators/3.2/ParallelProcessor.cs

    r1530 r2027  
    4242    /// <returns>A new <see cref="CompositeOperation"/> with the <c>n</c> operators applied
    4343    /// to the given <paramref name="scope"/> with the <c>ExecuteInParallel</c> set to <c>true</c>.</returns>
    44     public override IOperation Apply(IScope scope) {
     44    public override IOperation Apply(IEnvironment env, IScope scope) {
    4545      CompositeOperation next = new CompositeOperation();
    4646      next.ExecuteInParallel = true;
    4747      for (int i = 0; i < SubOperators.Count; i++)
    48         next.AddOperation(new AtomicOperation(SubOperators[i], scope));
     48        next.AddOperation(new AtomicOperation(SubOperators[i], env, scope));
    4949      return next;
    5050    }
  • branches/Operator Architecture Refactoring/HeuristicLab.Operators/3.2/ParallelSubScopesProcessor.cs

    r1530 r2027  
    4242    /// <returns>A new <see cref="CompositeOperation"/> with the <c>i</c>th operator applied
    4343    /// on the <c>i</c>th sub scope, the <c>ExecuteInParallel</c> flag set to <c>true</c>.</returns>
    44     public override IOperation Apply(IScope scope) {
     44    public override IOperation Apply(IEnvironment env, IScope scope) {
    4545      CompositeOperation next = new CompositeOperation();
    4646      next.ExecuteInParallel = true;
    4747      for (int i = 0; i < scope.SubScopes.Count; i++)
    48         next.AddOperation(new AtomicOperation(SubOperators[i], scope.SubScopes[i]));
     48        next.AddOperation(new AtomicOperation(SubOperators[i], env, scope.SubScopes[i]));
    4949      return next;
    5050    }
  • branches/Operator Architecture Refactoring/HeuristicLab.Operators/3.2/SequentialProcessor.cs

    r1530 r2027  
    4242    /// <returns>A new <see cref="CompositeOperation"/> with the <c>n</c> operators applied
    4343    /// to the given <paramref name="scope"/>.</returns>
    44     public override IOperation Apply(IScope scope) {
     44    public override IOperation Apply(IEnvironment env, IScope scope) {
    4545      CompositeOperation next = new CompositeOperation();
    4646      for (int i = 0; i < SubOperators.Count; i++)
    47         next.AddOperation(new AtomicOperation(SubOperators[i], scope));
     47        next.AddOperation(new AtomicOperation(SubOperators[i], env, scope));
    4848      return next;
    4949    }
  • branches/Operator Architecture Refactoring/HeuristicLab.Operators/3.2/SequentialSubScopesProcessor.cs

    r1530 r2027  
    4242    /// <returns>A new <see cref="CompositeOperation"/> with the <c>i</c>th operator applied
    4343    /// on the <c>i</c>th sub scope.</returns>
    44     public override IOperation Apply(IScope scope) {
     44    public override IOperation Apply(IEnvironment env, IScope scope) {
    4545      CompositeOperation next = new CompositeOperation();
    4646      for (int i = 0; i < scope.SubScopes.Count; i++)
    47         next.AddOperation(new AtomicOperation(SubOperators[i], scope.SubScopes[i]));
     47        next.AddOperation(new AtomicOperation(SubOperators[i], env, scope.SubScopes[i]));
    4848      return next;
    4949    }
  • branches/Operator Architecture Refactoring/HeuristicLab.Operators/3.2/SubScopesCreater.cs

    r1530 r2027  
    4141    public SubScopesCreater()
    4242      : base() {
    43       AddVariableInfo(new VariableInfo("SubScopes", "Number of sub-scopes", typeof(IntData), VariableKind.In));
     43      AddParameter(new Parameter("SubScopes", "Number of sub-scopes", typeof(IntData), ParameterType.In));
    4444    }
    4545
     
    4949    /// <param name="scope">The scope where to create the sub scopes.</param>
    5050    /// <returns><c>null</c>.</returns>
    51     public override IOperation Apply(IScope scope) {
    52       IntData count = GetVariableValue<IntData>("SubScopes", scope, true);
     51    public override IOperation Apply(IEnvironment env, IScope scope) {
     52      IntData count = scope.GetVariableValue<IntData>(env.TranslateName("SubScopes"), true);
    5353
    5454      for (int i = 0; i < count.Data; i++)
  • branches/Operator Architecture Refactoring/HeuristicLab.Operators/3.2/UniformParallelSubScopesProcessor.cs

    r1530 r2027  
    4343    /// <returns>A new <see cref="CompositeOperation"/> with one operator and all sub scopes and
    4444    /// the <c>ExecuteInParallel</c> flag set to <c>true</c>.</returns>
    45     public override IOperation Apply(IScope scope) {
     45    public override IOperation Apply(IEnvironment env, IScope scope) {
    4646      CompositeOperation next = new CompositeOperation();
    4747      next.ExecuteInParallel = true;
    4848      for (int i = 0; i < scope.SubScopes.Count; i++)
    49         next.AddOperation(new AtomicOperation(SubOperators[0], scope.SubScopes[i]));
     49        next.AddOperation(new AtomicOperation(SubOperators[0], env, scope.SubScopes[i]));
    5050      return next;
    5151    }
  • branches/Operator Architecture Refactoring/HeuristicLab.Operators/3.2/UniformSequentialSubScopesProcessor.cs

    r1530 r2027  
    4242    /// <param name="scope">The scope on whose sub scopes the operator is applied.</param>
    4343    /// <returns>A new <see cref="CompositeOperation"/> with one operator and all sub scopes.</returns>
    44     public override IOperation Apply(IScope scope) {
     44    public override IOperation Apply(IEnvironment env, IScope scope) {
    4545      CompositeOperation next = new CompositeOperation();
    4646      for (int i = 0; i < scope.SubScopes.Count; i++)
    47         next.AddOperation(new AtomicOperation(SubOperators[0], scope.SubScopes[i]));
     47        next.AddOperation(new AtomicOperation(SubOperators[0], env, scope.SubScopes[i]));
    4848      return next;
    4949    }
  • branches/Operator Architecture Refactoring/HeuristicLab.Operators/3.2/VariableInjector.cs

    r1530 r2027  
    3030  /// Class to inject local variables into the scope.
    3131  /// </summary>
    32   public class VariableInjector : OperatorBase {
    33     private Dictionary<IVariable, IVariableInfo> variableVariableInfoTable;
    34     private Dictionary<IVariableInfo, IVariable> variableInfoVariableTable;
     32  public class VariableInjector : OperatorBase, IVariablesContainer {
     33    private Dictionary<string, IVariable> myVariables;
     34    /// <summary>
     35    /// Gets a collection of all variables of the current operator.
     36    /// </summary>
     37    public virtual ICollection<IVariable> Variables {
     38      get { return myVariables.Values; }
     39    }
    3540
    3641    /// <inheritdoc select="summary"/>
    3742    public override string Description {
    38       get { return @"TODO\r\nOperator description still missing ..."; }
     43      get { return @"Injects new variable into a scope."; }
     44    }
     45
     46    /// <summary>
     47    /// Overrides the Parameters property so that a parameter is returned for each injected variable.
     48    /// </summary>
     49    public override ICollection<IParameter> Parameters {
     50      get {
     51        List<IParameter> parameters = new List<IParameter>();
     52        foreach (IVariable variable in Variables)
     53          parameters.Add(new Parameter(variable.Name, "Injected variable.", variable.Value.GetType(), ParameterType.Out));
     54        return parameters;
     55      }
    3956    }
    4057
     
    4461    public VariableInjector()
    4562      : base() {
    46       variableVariableInfoTable = new Dictionary<IVariable, IVariableInfo>();
    47       variableInfoVariableTable = new Dictionary<IVariableInfo, IVariable>();
     63      myVariables = new Dictionary<string, IVariable>();
    4864    }
    4965
     
    5672    /// <returns>The cloned object as <see cref="VariableInjector"/>.</returns>
    5773    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    58       VariableInjector clone = new VariableInjector();
    59       clonedObjects.Add(Guid, clone);
    60       clone.Name = Name;
     74      VariableInjector clone = (VariableInjector)base.Clone(clonedObjects);
    6175      foreach (IVariable variable in Variables)
    6276        clone.AddVariable((IVariable)Auxiliary.Clone(variable, clonedObjects));
     
    6579
    6680    /// <summary>
    67     /// Adds the specified <paramref name="variable"/> to the current instance to get injected.
    68     /// </summary>
    69     /// <param name="variable">The variable to add.</param>
    70     /// <remarks>Calls private method <see cref="CreateVariableInfo"/> and <see cref="OperatorBase.AddVariable"/>
    71     /// of base class <see cref="OperatorBase"/>.</remarks>
    72     public override void AddVariable(IVariable variable) {
    73       base.AddVariable(variable);
    74       CreateVariableInfo(variable);
    75     }
    76 
    77     /// <summary>
    78     /// Removes a variable with the specified <paramref name="name"/> from the current injector.
    79     /// </summary>
    80     /// <remarks>Calls private method <see cref="DeleteVariableInfo"/> and <see cref="OperatorBase.RemoveVariable"/>
    81     /// of base class <see cref="OperatorBase"/>.</remarks>
    82     /// <param name="name">The name of the </param>
    83     public override void RemoveVariable(string name) {
    84       DeleteVariableInfo(name);
    85       base.RemoveVariable(name);
    86     }
    87 
    88     /// <summary>
    8981    /// Adds the specified variables to the given <paramref name="scope"/> (and removes them first,
    9082    /// if they already exist in the current scope).
     
    9284    /// <param name="scope">The scope where to inject the variables.</param>
    9385    /// <returns><c>null</c>.</returns>
    94     public override IOperation Apply(IScope scope) {
     86    public override IOperation Apply(IEnvironment env, IScope scope) {
    9587      foreach (IVariable variable in Variables) {
    9688        if (scope.GetVariable(variable.Name) != null)
     
    109101    }
    110102
    111     private void CreateVariableInfo(IVariable variable) {
    112       IVariableInfo info = new VariableInfo(Guid.NewGuid().ToString(), "Injected variable", variable.Value.GetType(), VariableKind.New);
    113       info.ActualName = variable.Name;
    114       AddVariableInfo(info);
    115       variableVariableInfoTable.Add(variable, info);
    116       variableInfoVariableTable.Add(info, variable);
    117       info.ActualNameChanged += new EventHandler(VariableInfo_ActualNameChanged);
     103    /// <summary>
     104    /// Gets a variable with the given <paramref name="name"/>.
     105    /// </summary>
     106    /// <param name="name">The name of the variable.</param>
     107    /// <returns>The variable with the specified name.</returns>
     108    public virtual IVariable GetVariable(string name) {
     109      IVariable variable;
     110      if (myVariables.TryGetValue(name, out variable))
     111        return variable;
     112      else
     113        return null;
     114    }
     115    /// <summary>
     116    /// Adds the specified <paramref name="variable"/> to the current instance.
     117    /// </summary>
     118    /// <param name="variable">The variable to add.</param>
     119    public virtual void AddVariable(IVariable variable) {
     120      myVariables.Add(variable.Name, variable);
     121      variable.NameChanging += new EventHandler<NameChangingEventArgs>(Variable_NameChanging);
    118122      variable.NameChanged += new EventHandler(Variable_NameChanged);
    119     }
    120     private void DeleteVariableInfo(string name) {
    121       IVariable variable = GetVariable(name);
    122       if (variable != null) {
    123         IVariableInfo info = variableVariableInfoTable[variable];
    124         RemoveVariableInfo(info.FormalName);
    125         variableVariableInfoTable.Remove(variable);
    126         variableInfoVariableTable.Remove(info);
    127         info.ActualNameChanged -= new EventHandler(VariableInfo_ActualNameChanged);
     123      OnVariableAdded(variable);
     124    }
     125    /// <summary>
     126    /// Deletes the variable with the specified <paramref name="name"/>.
     127    /// </summary>
     128    /// <param name="name">The name of the variable to delete.</param>
     129    public virtual void RemoveVariable(string name) {
     130      IVariable variable;
     131      if (myVariables.TryGetValue(name, out variable)) {
     132        variable.NameChanging -= new EventHandler<NameChangingEventArgs>(Variable_NameChanging);
    128133        variable.NameChanged -= new EventHandler(Variable_NameChanged);
    129       }
    130     }
    131 
    132     #region VariableInfo and Variable Events
    133     private void VariableInfo_ActualNameChanged(object sender, EventArgs e) {
    134       IVariableInfo info = (IVariableInfo)sender;
    135       IVariable variable = variableInfoVariableTable[info];
    136       variable.Name = info.ActualName;
     134        myVariables.Remove(name);
     135        OnVariableRemoved(variable);
     136      }
     137    }
     138    private void Variable_NameChanging(object sender, NameChangingEventArgs e) {
     139      e.Cancel = myVariables.ContainsKey(e.Name);
    137140    }
    138141    private void Variable_NameChanged(object sender, EventArgs e) {
    139142      IVariable variable = (IVariable)sender;
    140       IVariableInfo info = variableVariableInfoTable[variable];
    141       info.ActualName = variable.Name;
    142     }
    143     #endregion
     143      string oldName = null;
     144      foreach (KeyValuePair<string, IVariable> element in myVariables) {
     145        if (element.Value == variable)
     146          oldName = element.Key;
     147      }
     148      myVariables.Remove(oldName);
     149      myVariables.Add(variable.Name, variable);
     150    }
     151
     152    /// <summary>
     153    /// Occurs when a variable has been added.
     154    /// </summary>
     155    public event EventHandler<VariableEventArgs> VariableAdded;
     156    /// <summary>
     157    /// Fires a new <c>VariableAdded</c> event.
     158    /// </summary>
     159    /// <param name="variable">The variable that has been added.</param>
     160    protected virtual void OnVariableAdded(IVariable variable) {
     161      if (VariableAdded != null)
     162        VariableAdded(this, new VariableEventArgs(variable));
     163    }
     164    /// <summary>
     165    /// Occurs when a variable has been deleted.
     166    /// </summary>
     167    public event EventHandler<VariableEventArgs> VariableRemoved;
     168    /// <summary>
     169    /// Fires a new <c>VariableRemoved</c> event.
     170    /// </summary>
     171    /// <param name="variable">The variable that has been removed</param>
     172    protected virtual void OnVariableRemoved(IVariable variable) {
     173      if (VariableRemoved != null)
     174        VariableRemoved(this, new VariableEventArgs(variable));
     175    }
    144176
    145177    #region Persistence Methods
    146178    /// <summary>
    147179    /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
    148     /// <note type="caution"> Variable infos are not persisted!</note>
    149     /// </summary>
    150     /// <remarks>Calls <see cref="OperatorBase.GetXmlNode"/> of base class <see cref="OperatorBase"/>.</remarks>
     180    /// </summary>
     181    /// <remarks>
     182    /// Calls <see cref="OperatorBase.GetXmlNode"/> of base class <see cref="OperatorBase"/>.
     183    /// <br/>A quick overview how the single elements of the current instance are saved:
     184    /// <list type="bullet">
     185    /// <item>
     186    /// <term>Variables: </term>
     187    /// <description>Saved as child node with tag name <c>Variables</c>. All variables are themselves
     188    /// saved as child nodes.</description>
     189    /// </item>
     190    /// </list>
     191    /// </remarks>
    151192    /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
    152193    /// <param name="document">The <see cref="XmlDocument"/> where to save the data.</param>
    153194    /// <param name="persistedObjects">The dictionary of all already persisted objects. (Needed to avoid cycles.)</param>
    154195    /// <returns>The saved <see cref="XmlNode"/>.</returns>
    155     public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
     196    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    156197      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
    157       // variable infos should not be persisted
    158       XmlNode infosNode = node.SelectSingleNode("VariableInfos");
    159       infosNode.RemoveAll();
     198      XmlNode variablesNode = document.CreateNode(XmlNodeType.Element, "Variables", null);
     199      foreach (IVariable variable in myVariables.Values)
     200        variablesNode.AppendChild(PersistenceManager.Persist(variable, document, persistedObjects));
     201      node.AppendChild(variablesNode);
    160202      return node;
     203    }
     204    /// <summary>
     205    /// Loads the persisted variable injector from the specified <paramref name="node"/>.
     206    /// </summary>
     207    /// <remarks>Calls <see cref="OperatorBase.Populate"/> of base class
     208    /// <see cref="OperatorBase"/>.
     209    /// For informations how the different elements must be saved please see <see cref="GetXmlNode"/>.</remarks>
     210    /// <param name="node">The <see cref="XmlNode"/> where the operation is saved.</param>
     211    /// <param name="restoredObjects">A dictionary of all already restored objects. (Needed to avoid cycles.)</param>
     212    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
     213      base.Populate(node, restoredObjects);
     214      XmlNode variablesNode = node.SelectSingleNode("Variables");
     215      foreach (XmlNode variableNode in variablesNode.ChildNodes)
     216        AddVariable((IVariable)PersistenceManager.Restore(variableNode, restoredObjects));
    161217    }
    162218    #endregion
  • branches/Operator Architecture Refactoring/HeuristicLab.Operators/3.2/VariableInjectorView.Designer.cs

    r1530 r2027  
    4545    /// </summary>
    4646    private void InitializeComponent() {
    47       components = new System.ComponentModel.Container();
     47      this.variablesTabPage = new System.Windows.Forms.TabPage();
     48      this.variablesContainerView = new HeuristicLab.Core.VariablesContainerView();
     49      this.tabControl.SuspendLayout();
     50      this.parametersTabPage.SuspendLayout();
     51      this.variablesTabPage.SuspendLayout();
     52      this.SuspendLayout();
     53      //
     54      // tabControl
     55      //
     56      this.tabControl.Controls.Add(this.variablesTabPage);
     57      this.tabControl.Controls.SetChildIndex(this.parametersTabPage, 0);
     58      this.tabControl.Controls.SetChildIndex(this.variablesTabPage, 0);
     59      //
     60      // variablesTabPage
     61      //
     62      this.variablesTabPage.Controls.Add(this.variablesContainerView);
     63      this.variablesTabPage.Location = new System.Drawing.Point(4, 22);
     64      this.variablesTabPage.Name = "variablesTabPage";
     65      this.variablesTabPage.Padding = new System.Windows.Forms.Padding(3);
     66      this.variablesTabPage.Size = new System.Drawing.Size(415, 307);
     67      this.variablesTabPage.TabIndex = 4;
     68      this.variablesTabPage.Text = "Variables";
     69      this.variablesTabPage.UseVisualStyleBackColor = true;
     70      //
     71      // variablesContainerView
     72      //
     73      this.variablesContainerView.Caption = "Variables Container";
     74      this.variablesContainerView.Dock = System.Windows.Forms.DockStyle.Fill;
     75      this.variablesContainerView.Location = new System.Drawing.Point(3, 3);
     76      this.variablesContainerView.Name = "variablesContainerView";
     77      this.variablesContainerView.Size = new System.Drawing.Size(409, 301);
     78      this.variablesContainerView.TabIndex = 0;
     79      this.variablesContainerView.VariablesContainer = null;
     80      //
     81      // VariableInjectorView
     82      //
     83      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    4884      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
     85      this.Name = "VariableInjectorView";
     86      this.tabControl.ResumeLayout(false);
     87      this.parametersTabPage.ResumeLayout(false);
     88      this.variablesTabPage.ResumeLayout(false);
     89      this.ResumeLayout(false);
     90
    4991    }
    5092
    5193    #endregion
     94
     95    private System.Windows.Forms.TabPage variablesTabPage;
     96    private HeuristicLab.Core.VariablesContainerView variablesContainerView;
    5297  }
    5398}
  • branches/Operator Architecture Refactoring/HeuristicLab.Operators/3.2/VariableInjectorView.cs

    r1530 r2027  
    6060      VariableInjector = variableInjector;
    6161    }
     62
     63    /// <summary>
     64    /// Removes event handlers in all children.
     65    /// </summary>
     66    protected override void RemoveItemEvents() {
     67      variablesContainerView.VariablesContainer = null;
     68      base.RemoveItemEvents();
     69    }
     70    /// <summary>
     71    /// Adds event handlers in all children.
     72    /// </summary>
     73    protected override void AddItemEvents() {
     74      base.AddItemEvents();
     75      variablesContainerView.VariablesContainer = VariableInjector;
     76    }
     77
    6278  }
    6379}
Note: See TracChangeset for help on using the changeset viewer.