Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/05/10 05:23:56 (15 years ago)
Author:
swagner
Message:

Operator architecture refactoring (#95)

  • worked on parameters and operators
Location:
trunk/sources/HeuristicLab.Parameters/3.3
Files:
2 added
5 edited
2 moved

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Parameters/3.3/HeuristicLab.Parameters-3.3.csproj

    r2754 r2756  
    5050  <ItemGroup>
    5151    <None Include="HeuristicLabParametersPlugin.cs.frame" />
    52     <Compile Include="SubScopesItemParameter.cs" />
     52    <Compile Include="LookupParameter.cs" />
     53    <Compile Include="SubScopesLookupParameter.cs" />
     54    <Compile Include="ValueLookupParameter.cs" />
     55    <Compile Include="ValueParameter.cs" />
    5356    <Compile Include="ScopeParameter.cs" />
    5457    <Compile Include="HeuristicLabParametersPlugin.cs" />
    55     <Compile Include="ItemParameter.cs" />
    5658    <Compile Include="OperatorParameter.cs" />
    5759    <Compile Include="Parameter.cs" />
     
    6668      <Project>{958B43BC-CC5C-4FA2-8628-2B3B01D890B6}</Project>
    6769      <Name>HeuristicLab.Collections-3.3</Name>
     70    </ProjectReference>
     71    <ProjectReference Include="..\..\HeuristicLab.Common\3.2\HeuristicLab.Common-3.2.csproj">
     72      <Project>{1FC004FC-59AF-4249-B1B6-FF25873A20E4}</Project>
     73      <Name>HeuristicLab.Common-3.2</Name>
    6874    </ProjectReference>
    6975    <ProjectReference Include="..\..\HeuristicLab.Core\3.3\HeuristicLab.Core-3.3.csproj">
  • trunk/sources/HeuristicLab.Parameters/3.3/HeuristicLabParametersPlugin.cs.frame

    r2754 r2756  
    3232  [PluginFile("HeuristicLab.Parameters-3.3.dll", PluginFileType.Assembly)]
    3333  [PluginDependency("HeuristicLab.Collections", "3.3")]
     34  [PluginDependency("HeuristicLab.Common", "3.2")]
    3435  [PluginDependency("HeuristicLab.Core", "3.3")]
    3536  [PluginDependency("HeuristicLab.Persistence", "3.3")]
  • trunk/sources/HeuristicLab.Parameters/3.3/OperatorParameter.cs

    r2740 r2756  
    3232  /// </summary>
    3333  [Item("OperatorParameter", "A parameter which represents an operator.")]
     34  [EmptyStorableClass]
    3435  [Creatable("Test")]
    35   public class OperatorParameter : Parameter, IOperatorParameter {
    36     private IOperator value;
    37     [Storable]
    38     public IOperator Value {
    39       get { return this.value; }
    40       set {
    41         if (value != this.value) {
    42           if (this.value != null) this.value.Changed -= new ChangedEventHandler(Value_Changed);
    43           this.value = value;
    44           if (this.value != null) this.value.Changed += new ChangedEventHandler(Value_Changed);
    45           OnValueChanged();
    46         }
    47       }
     36  public class OperatorParameter : ValueParameter<IOperator> {
     37    public OperatorParameter()
     38      : base("Anonymous") {
    4839    }
    49 
    50     public OperatorParameter()
    51       : base("Anonymous", null, typeof(IOperator)) {
     40    public OperatorParameter(string name)
     41      : base(name) {
     42    }
     43    public OperatorParameter(string name, IOperator value)
     44      : base(name, value) {
     45      Value = value;
    5246    }
    5347    public OperatorParameter(string name, string description)
    54       : base(name, description, typeof(IOperator)) {
     48      : base(name, description) {
    5549    }
    5650    public OperatorParameter(string name, string description, IOperator value)
    57       : base(name, description, typeof(IOperator)) {
     51      : base(name, description, value) {
    5852      Value = value;
    59     }
    60 
    61     public override IDeepCloneable Clone(Cloner cloner) {
    62       OperatorParameter clone = (OperatorParameter)base.Clone(cloner);
    63       clone.Value = (IOperator)cloner.Clone(value);
    64       return clone;
    65     }
    66 
    67     public override string ToString() {
    68       return string.Format("{0}: {1} ({2})", Name, Value != null ? Value.ToString() : "null", DataType.Name);
    69     }
    70 
    71     public event EventHandler ValueChanged;
    72     private void OnValueChanged() {
    73       if (ValueChanged != null)
    74         ValueChanged(this, new EventArgs());
    75       OnChanged();
    76     }
    77     private void Value_Changed(object sender, ChangedEventArgs e) {
    78       OnChanged(e);
    7953    }
    8054  }
  • trunk/sources/HeuristicLab.Parameters/3.3/Parameter.cs

    r2740 r2756  
    5656      dataType = typeof(IItem);
    5757    }
     58    protected Parameter(string name, Type dataType)
     59      : base(name) {
     60      if (dataType == null) throw new ArgumentNullException();
     61      this.dataType = dataType;
     62    }
    5863    protected Parameter(string name, string description, Type dataType)
    5964      : base(name, description) {
  • trunk/sources/HeuristicLab.Parameters/3.3/ScopeParameter.cs

    r2740 r2756  
    4040
    4141    public ScopeParameter()
    42       : base("Anonymous", null, typeof(IScope)) {
     42      : base("Anonymous", typeof(IScope)) {
     43    }
     44    public ScopeParameter(string name)
     45      : base(name, typeof(IScope)) {
    4346    }
    4447    public ScopeParameter(string name, string description)
  • trunk/sources/HeuristicLab.Parameters/3.3/SubScopesLookupParameter.cs

    r2754 r2756  
    2424using System.Text;
    2525using System.Xml;
     26using HeuristicLab.Common;
    2627using HeuristicLab.Core;
    2728using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    3132  /// A generic parameter representing instances of type T which are collected from the sub-scopes of the current scope.
    3233  /// </summary>
    33   [Item("SubScopesItemParameter<T>", "A generic parameter representing instances of type T which are collected from the sub-scopes of the current scope.")]
    34   public class SubScopesItemParameter<T> : Parameter where T : class, IItem {
     34  [Item("SubScopesLookupParameter<T>", "A generic parameter representing instances of type T which are collected from the sub-scopes of the current scope.")]
     35  public class SubScopesLookupParameter<T> : Parameter, ILookupParameter<T> where T : class, IItem {
    3536    [Storable]
    3637    private string actualName;
     
    4647    }
    4748
    48     public T[] Values {
    49       get { return GetValues(); }
    50       set { SetValues(value); }
     49    public T[] ActualValues {
     50      get { return GetActualValues(); }
     51      set { SetActualValues(value); }
    5152    }
    5253
    53     public SubScopesItemParameter()
    54       : base("Anonymous", null, typeof(T)) {
     54    public SubScopesLookupParameter()
     55      : base("Anonymous", typeof(T)) {
    5556      actualName = Name;
    5657    }
    57     public SubScopesItemParameter(string name, string description)
     58    public SubScopesLookupParameter(string name)
     59      : base(name, typeof(T)) {
     60      actualName = Name;
     61    }
     62    public SubScopesLookupParameter(string name, string description)
    5863      : base(name, description, typeof(T)) {
    5964      actualName = Name;
     
    6166
    6267    public override IDeepCloneable Clone(Cloner cloner) {
    63       SubScopesItemParameter<T> clone = (SubScopesItemParameter<T>)base.Clone(cloner);
     68      SubScopesLookupParameter<T> clone = (SubScopesLookupParameter<T>)base.Clone(cloner);
    6469      clone.actualName = actualName;
    6570      return clone;
     
    7075    }
    7176
    72     protected string GetActualName() {
    73       string name = Name;
    74       ExecutionContext current = ExecutionContext;
    75       while (current != null) {
    76         if (current.Operator.Parameters.ContainsKey(name))
    77           name = ((SubScopesItemParameter<T>)current.Operator.Parameters[name]).ActualName;
    78         current = current.Parent;
     77    protected virtual T[] GetActualValues() {
     78      string name = LookupParameter<T>.TranslateName(Name, ExecutionContext);
     79      IScope scope = ExecutionContext.Scope;
     80      T[] values = new T[scope.SubScopes.Count];
     81      IVariable var;
     82      T value;
     83
     84      for (int i = 0; i < values.Length; i++) {
     85        scope.SubScopes[i].Variables.TryGetValue(name, out var);
     86        if (var != null) {
     87          value = var.Value as T;
     88          if (value == null)
     89            throw new InvalidOperationException(
     90              string.Format("Type mismatch. Variable \"{0}\" does not contain a \"{1}\".",
     91                            name,
     92                            typeof(T).GetPrettyName())
     93            );
     94          values[i] = value;
     95        }
    7996      }
    80       return name;
     97      return values;
    8198    }
    82     protected virtual T[] GetValues() {
    83       string name = GetActualName();
    84       IScope scope = ExecutionContext.Scope;
    85       T[] value = new T[scope.SubScopes.Count];
    86       IVariable var;
    87 
    88       for (int i = 0; i < value.Length; i++) {
    89         scope.SubScopes[i].Variables.TryGetValue(name, out var);
    90         if (var != null) value[i] = (T)var.Value;
    91       }
    92       return value;
    93     }
    94     protected virtual void SetValues(T[] values) {
    95       string name = GetActualName();
     99    protected virtual void SetActualValues(T[] values) {
     100      string name = LookupParameter<T>.TranslateName(Name, ExecutionContext);
    96101      IScope scope = ExecutionContext.Scope;
    97102      IVariable var;
  • trunk/sources/HeuristicLab.Parameters/3.3/ValueLookupParameter.cs

    r2754 r2756  
    2424using System.Text;
    2525using System.Xml;
     26using HeuristicLab.Common;
    2627using HeuristicLab.Core;
    2728using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    2930namespace HeuristicLab.Parameters {
    3031  /// <summary>
    31   /// A generic parameter which represents an instance of type T.
     32  /// A parameter whose value is either defined it the parameter itself or is retrieved from the scope.
    3233  /// </summary>
    33   [Item("ItemParameter<T>", "A generic parameter which represents an instance of type T.")]
    34   public class ItemParameter<T> : Parameter where T : class, IItem {
     34  [Item("ValueLookupParameter<T>", "A parameter whose value is either defined it the parameter itself or is retrieved from the scope.")]
     35  public class ValueLookupParameter<T> : Parameter, IValueLookupParameter<T> where T : class, IItem {
    3536    [Storable]
    3637    private string actualName;
     
    4647    }
    4748
    48     private T localValue;
     49    private T value;
    4950    [Storable]
    50     public T LocalValue {
    51       get { return this.localValue; }
     51    public T Value {
     52      get { return this.value; }
    5253      set {
    53         if (value != this.localValue) {
    54           if ((value != null) && (!DataType.IsInstanceOfType(value))) throw new ArgumentException("Static value does not match data type of parameter");
    55           if (this.localValue != null) this.localValue.Changed -= new ChangedEventHandler(LocalValue_Changed);
    56           this.localValue = value;
    57           if (this.localValue != null) this.localValue.Changed += new ChangedEventHandler(LocalValue_Changed);
    58           OnLocalValueChanged();
     54        if (value != this.value) {
     55          if (this.value != null) this.value.Changed -= new ChangedEventHandler(Value_Changed);
     56          this.value = value;
     57          if (this.value != null) this.value.Changed += new ChangedEventHandler(Value_Changed);
     58          OnValueChanged();
    5959        }
    6060      }
    6161    }
    6262
    63     public T Value {
    64       get { return GetValue(); }
    65       set { SetValue(value); }
     63    public T ActualValue {
     64      get { return GetActualValue(); }
     65      set { SetActualValue(value); }
    6666    }
    6767
    68     public ItemParameter()
    69       : base("Anonymous", null, typeof(T)) {
     68    public ValueLookupParameter()
     69      : base("Anonymous", typeof(T)) {
    7070      actualName = Name;
    71       LocalValue = null;
    7271    }
    73     public ItemParameter(string name, string description)
     72    public ValueLookupParameter(string name)
     73      : base(name, typeof(T)) {
     74      actualName = Name;
     75    }
     76    public ValueLookupParameter(string name, T value)
     77      : base(name, typeof(T)) {
     78      actualName = Name;
     79      Value = value;
     80    }
     81    public ValueLookupParameter(string name, string description)
    7482      : base(name, description, typeof(T)) {
    7583      actualName = Name;
    76       LocalValue = null;
    7784    }
    78     public ItemParameter(string name, string description, T localValue)
     85    public ValueLookupParameter(string name, string description, T value)
    7986      : base(name, description, typeof(T)) {
    8087      actualName = Name;
    81       LocalValue = localValue;
     88      Value = value;
    8289    }
    8390
    8491    public override IDeepCloneable Clone(Cloner cloner) {
    85       ItemParameter<T> clone = (ItemParameter<T>)base.Clone(cloner);
     92      ValueLookupParameter<T> clone = (ValueLookupParameter<T>)base.Clone(cloner);
    8693      clone.actualName = actualName;
    87       clone.LocalValue = (T)cloner.Clone(localValue);
     94      clone.Value = (T)cloner.Clone(value);
    8895      return clone;
    8996    }
    9097
    9198    public override string ToString() {
    92       return string.Format("{0}: {1} ({2})", Name, LocalValue != null ? LocalValue.ToString() : ActualName, DataType.Name);
     99      return string.Format("{0}: {1} ({2})", Name, Value != null ? Value.ToString() : ActualName, DataType.Name);
    93100    }
    94101
    95     protected ItemParameter<T> GetParameter(out string name) {
    96       ItemParameter<T> param = this;
     102    private IValueParameter<T> GetParameter(out string name) {
     103      IValueParameter<T> valueParam = this;
     104      ILookupParameter<T> lookupParam = this;
    97105      ExecutionContext current = ExecutionContext;
    98       name = param.Name;
    99       while (param != null) {
    100         if (param.LocalValue != null) return param;
    101         name = param.ActualName;
     106
     107      name = Name;
     108      while ((valueParam != null) && (lookupParam != null)) {
     109        if ((valueParam != null) && (valueParam.Value != null)) return valueParam;
     110        if (lookupParam != null) name = lookupParam.ActualName;
     111
    102112        current = current.Parent;
    103113        while ((current != null) && !current.Operator.Parameters.ContainsKey(name))
    104114          current = current.Parent;
    105         if (current != null)
    106           param = (ItemParameter<T>)current.Operator.Parameters[actualName];
    107         else
    108           param = null;
     115
     116        if (current != null) {
     117          valueParam = current.Operator.Parameters[name] as IValueParameter<T>;
     118          lookupParam = current.Operator.Parameters[name] as ILookupParameter<T>;
     119          if ((valueParam == null) && (lookupParam == null))
     120            throw new InvalidOperationException(
     121              string.Format("Parameter look-up chain broken. Parameter \"{0}\" is not an \"{1}\" or an \"{2}\".",
     122                            name,
     123                            typeof(IValueParameter<T>).GetPrettyName(),
     124                            typeof(ILookupParameter<T>).GetPrettyName())
     125            );
     126        } else {
     127          valueParam = null;
     128          lookupParam = null;
     129        }
    109130      }
    110131      return null;
    111132    }
    112     protected IVariable GetVariable(string name) {
     133    private IVariable LookupVariable(string name) {
    113134      IScope scope = ExecutionContext.Scope;
    114135      while ((scope != null) && !scope.Variables.ContainsKey(name))
     
    116137      return scope != null ? scope.Variables[actualName] : null;
    117138    }
    118     protected virtual T GetValue() {
     139    protected virtual T GetActualValue() {
    119140      string name;
    120141      // try to get local value from context stack
    121       ItemParameter<T> param = GetParameter(out name);
     142      IValueParameter<T> param = GetParameter(out name);
    122143      if (param != null) return param.Value;
    123       else {
    124         // try to get variable from scope
    125         IVariable var = GetVariable(name);
    126         if (var != null) return (T)var.Value;
     144      else {  // try to get variable from scope
     145        IVariable var = LookupVariable(name);
     146        if (var != null) {
     147          T value = var.Value as T;
     148          if (value == null)
     149            throw new InvalidOperationException(
     150              string.Format("Type mismatch. Variable \"{0}\" does not contain a \"{1}\".",
     151                            name,
     152                            typeof(T).GetPrettyName())
     153            );
     154          return value;
     155        }
    127156      }
    128157      return null;
    129158    }
    130     protected virtual void SetValue(T value) {
     159    protected virtual void SetActualValue(T value) {
    131160      string name;
    132161      // try to get local value from context stack
    133       ItemParameter<T> param = GetParameter(out name);
     162      IValueParameter<T> param = GetParameter(out name);
    134163      if (param != null) param.Value = value;
    135       else {
    136         // try to get variable from scope
    137         IVariable var = GetVariable(name);
     164      else {  // try to get variable from scope
     165        IVariable var = LookupVariable(name);
    138166        if (var != null) var.Value = value;
    139167        else ExecutionContext.Scope.Variables.Add(new Variable(name, value));
     
    147175      OnChanged();
    148176    }
    149     public event EventHandler LocalValueChanged;
    150     private void OnLocalValueChanged() {
    151       if (LocalValueChanged != null)
    152         LocalValueChanged(this, new EventArgs());
     177    public event EventHandler ValueChanged;
     178    private void OnValueChanged() {
     179      if (ValueChanged != null)
     180        ValueChanged(this, new EventArgs());
    153181      OnChanged();
    154182    }
    155     private void LocalValue_Changed(object sender, ChangedEventArgs e) {
     183    private void Value_Changed(object sender, ChangedEventArgs e) {
    156184      OnChanged(e);
    157185    }
Note: See TracChangeset for help on using the changeset viewer.