Free cookie consent management tool by TermsFeed Policy Generator

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

Operator architecture refactoring (#95)

  • worked on parameters and operators
Location:
trunk/sources/HeuristicLab.Parameters/3.3
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Parameters/3.3/LookupParameter.cs

    r2756 r2757  
    3232  /// A parameter whose value is retrieved from the scope.
    3333  /// </summary>
    34   [Item("LookupParameter<T>", "A parameter whose value is retrieved from the scope.")]
     34  [Item("LookupParameter<T>", "A parameter whose value is retrieved from or written to a scope.")]
    3535  public class LookupParameter<T> : Parameter, ILookupParameter<T> where T : class, IItem {
    3636    [Storable]
     
    4646      }
    4747    }
    48     public T ActualValue {
    49       get { return GetActualValue(); }
     48    public new T ActualValue {
     49      get { return (T)GetActualValue(); }
    5050      set { SetActualValue(value); }
    5151    }
     
    8080      return scope != null ? scope.Variables[actualName] : null;
    8181    }
    82     protected virtual T GetActualValue() {
     82    protected override IItem GetActualValue() {
    8383      string name = TranslateName(Name, ExecutionContext);
    8484      IVariable var = LookupVariable(name);
     
    9595      return null;
    9696    }
    97     protected virtual void SetActualValue(T value) {
     97    protected override void SetActualValue(IItem value) {
     98      T val = value as T;
     99      if (val == null)
     100        throw new InvalidOperationException(
     101          string.Format("Type mismatch. Value is not a \"{0}\".",
     102                        typeof(T).GetPrettyName())
     103        );
    98104      string name = TranslateName(Name, ExecutionContext);
    99105      IVariable var = LookupVariable(name);
    100       if (var != null) var.Value = value;
    101       else ExecutionContext.Scope.Variables.Add(new Variable(name, value));
     106      if (var != null) var.Value = val;
     107      else ExecutionContext.Scope.Variables.Add(new Variable(name, val));
    102108    }
    103109
  • trunk/sources/HeuristicLab.Parameters/3.3/Parameter.cs

    r2756 r2757  
    4545      get { return dataType; }
    4646    }
     47    public IItem ActualValue {
     48      get { return GetActualValue(); }
     49      set { SetActualValue(value); }
     50    }
    4751    [Storable]
    4852    private ExecutionContext executionContext;
    4953    public ExecutionContext ExecutionContext {
    5054      get { return executionContext; }
    51       set { executionContext = value; }
     55      set {
     56        if (value != executionContext) {
     57          executionContext = value;
     58          OnExecutionContextChanged();
     59        }
     60      }
    5261    }
    5362
     
    7786      return string.Format("{0} ({1})", Name, DataType.Name);
    7887    }
     88
     89    protected abstract IItem GetActualValue();
     90    protected abstract void SetActualValue(IItem value);
     91
     92    protected virtual void OnExecutionContextChanged() { }
    7993  }
    8094}
  • trunk/sources/HeuristicLab.Parameters/3.3/ScopeParameter.cs

    r2756 r2757  
    3535  [Creatable("Test")]
    3636  public class ScopeParameter : Parameter {
    37     public IScope Value {
     37    public new IScope ActualValue {
    3838      get { return ExecutionContext.Scope; }
    3939    }
     
    5252      return string.Format("{0} ({1})", Name, DataType.Name);
    5353    }
     54
     55    protected override IItem GetActualValue() {
     56      return ExecutionContext.Scope;
     57    }
     58    protected override void SetActualValue(IItem value) {
     59      throw new NotSupportedException("The actual value of a ScopeParameter cannot be set. It is always the current scope.");
     60    }
    5461  }
    5562}
  • trunk/sources/HeuristicLab.Parameters/3.3/SubScopesLookupParameter.cs

    r2756 r2757  
    3232  /// A generic parameter representing instances of type T which are collected from the sub-scopes of the current scope.
    3333  /// </summary>
    34   [Item("SubScopesLookupParameter<T>", "A generic parameter representing instances of type T which are collected from the sub-scopes of the current scope.")]
     34  [Item("SubScopesLookupParameter<T>", "A generic parameter representing instances of type T which are collected from or written to the sub-scopes of the current scope.")]
    3535  public class SubScopesLookupParameter<T> : Parameter, ILookupParameter<T> where T : class, IItem {
    3636    [Storable]
     
    4747    }
    4848
    49     public T[] ActualValues {
    50       get { return GetActualValues(); }
    51       set { SetActualValues(value); }
     49    public new ItemArray<T> ActualValue {
     50      get { return (ItemArray<T>)GetActualValue(); }
     51      set { SetActualValue(value); }
    5252    }
    5353
    5454    public SubScopesLookupParameter()
    55       : base("Anonymous", typeof(T)) {
     55      : base("Anonymous", typeof(ItemArray<T>)) {
    5656      actualName = Name;
    5757    }
    5858    public SubScopesLookupParameter(string name)
    59       : base(name, typeof(T)) {
     59      : base(name, typeof(ItemArray<T>)) {
    6060      actualName = Name;
    6161    }
    6262    public SubScopesLookupParameter(string name, string description)
    63       : base(name, description, typeof(T)) {
     63      : base(name, description, typeof(ItemArray<T>)) {
    6464      actualName = Name;
    6565    }
     
    7575    }
    7676
    77     protected virtual T[] GetActualValues() {
     77    protected override IItem GetActualValue() {
    7878      string name = LookupParameter<T>.TranslateName(Name, ExecutionContext);
    7979      IScope scope = ExecutionContext.Scope;
    80       T[] values = new T[scope.SubScopes.Count];
     80      ItemArray<T> values = new ItemArray<T>(scope.SubScopes.Count);
    8181      IVariable var;
    8282      T value;
     
    9797      return values;
    9898    }
    99     protected virtual void SetActualValues(T[] values) {
     99    protected override void SetActualValue(IItem value) {
     100      ItemArray<T> values = value as ItemArray<T>;
     101      if (values == null)
     102        throw new InvalidOperationException(
     103          string.Format("Type mismatch. Value is not a \"{0}\".",
     104                        typeof(ItemArray<T>).GetPrettyName())
     105        );
     106
    100107      string name = LookupParameter<T>.TranslateName(Name, ExecutionContext);
    101108      IScope scope = ExecutionContext.Scope;
  • trunk/sources/HeuristicLab.Parameters/3.3/ValueLookupParameter.cs

    r2756 r2757  
    3232  /// A parameter whose value is either defined it the parameter itself or is retrieved from the scope.
    3333  /// </summary>
    34   [Item("ValueLookupParameter<T>", "A parameter whose value is either defined it the parameter itself or is retrieved from the scope.")]
     34  [Item("ValueLookupParameter<T>", "A parameter whose value is either defined it the parameter itself or is retrieved from or written to a scope.")]
    3535  public class ValueLookupParameter<T> : Parameter, IValueLookupParameter<T> where T : class, IItem {
    3636    [Storable]
     
    6161    }
    6262
    63     public T ActualValue {
    64       get { return GetActualValue(); }
     63    public new T ActualValue {
     64      get { return (T)GetActualValue(); }
    6565      set { SetActualValue(value); }
    6666    }
     
    137137      return scope != null ? scope.Variables[actualName] : null;
    138138    }
    139     protected virtual T GetActualValue() {
     139    protected override IItem GetActualValue() {
    140140      string name;
    141141      // try to get local value from context stack
     
    157157      return null;
    158158    }
    159     protected virtual void SetActualValue(T value) {
     159    protected override void SetActualValue(IItem value) {
     160      T val = value as T;
     161      if (val == null)
     162        throw new InvalidOperationException(
     163          string.Format("Type mismatch. Value is not a \"{0}\".",
     164                        typeof(T).GetPrettyName())
     165        );
     166      // try to get local value from context stack
    160167      string name;
    161       // try to get local value from context stack
    162168      IValueParameter<T> param = GetParameter(out name);
    163       if (param != null) param.Value = value;
     169      if (param != null) param.Value = val;
    164170      else {  // try to get variable from scope
    165171        IVariable var = LookupVariable(name);
    166         if (var != null) var.Value = value;
     172        if (var != null) var.Value = val;
    167173        else ExecutionContext.Scope.Variables.Add(new Variable(name, value));
    168174      }
  • trunk/sources/HeuristicLab.Parameters/3.3/ValueParameter.cs

    r2756 r2757  
    2424using System.Text;
    2525using System.Xml;
     26using HeuristicLab.Common;
    2627using HeuristicLab.Core;
    2728using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    4546        }
    4647      }
     48    }
     49    public new T ActualValue {
     50      get { return Value; }
     51      set { Value = value; }
    4752    }
    4853
     
    7580    }
    7681
     82    protected override IItem GetActualValue() {
     83      return Value;
     84    }
     85    protected override void SetActualValue(IItem value) {
     86      T val = value as T;
     87      if (val == null)
     88        throw new InvalidOperationException(
     89          string.Format("Type mismatch. Value is not a \"{0}\".",
     90                        typeof(T).GetPrettyName())
     91        );
     92      Value = val;
     93    }
     94
    7795    public event EventHandler ValueChanged;
    7896    private void OnValueChanged() {
Note: See TracChangeset for help on using the changeset viewer.