Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/06/19 13:01:29 (5 years ago)
Author:
chaider
Message:

#2971 Changed IntervalConstraint

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Parser/IntervalConstraint.cs

    r16886 r16895  
    2727  [StorableType("8109BE58-CCFB-4462-A2F4-EEE5DFADAFF7")]
    2828  [Item("Interval Constraint", "Constraint on intervals.")]
    29   public class IntervalConstraint : NamedItem {
     29  public sealed class IntervalConstraint : Item {
    3030    private string expression;
    3131    [Storable]
    3232    public string Expression {
    3333      get => expression;
    34       set {
    35         if (value != expression) {
     34      private set {
     35        if (expression != value) {
    3636          expression = value;
    37           OnChanged(EventArgs.Empty);
    38         }
    39       }
    40     }
    41     private string definition;
    42     [Storable]
     37          OnChanged();
     38          OnToStringChanged();
     39        }
     40      }
     41    }
     42
    4343    public string Definition {
    44       get => definition;
    45       set {
    46         if (value != definition) {
    47           definition = value;
    48           OnChanged(EventArgs.Empty);
    49         }
    50       }
    51     }
    52     [Storable]
    53     public Interval Interval { get; set; }
     44      get { return GetDefinitionString(); }
     45    }
     46
     47    private string variable;
     48    [Storable]
     49    public string Variable {
     50      get => variable;
     51      private set {
     52        if (variable != value) {
     53          variable = value;
     54          UpdateExpression();
     55          OnChanged();
     56        }
     57      }
     58    }
     59
     60    public bool IsDerivation {
     61      get => numberOfDerivation > 0;
     62    }
     63
     64    public int numberOfDerivation;
     65    [Storable]
     66    public int NumberOfDerivation {
     67      get => numberOfDerivation;
     68      set {
     69        if (value < 0 || value > 3)
     70          throw new ArgumentException("Number of derivation has to be between 0 - 3.");
     71        if (value != numberOfDerivation) {
     72          numberOfDerivation = value;
     73          UpdateExpression();
     74          OnChanged();
     75        }
     76      }
     77    }
     78
     79    private Interval interval;
     80    [Storable]
     81    public Interval Interval {
     82      get => interval;
     83      set {
     84        if (interval != value) {
     85          interval = value;
     86          UpdateExpression();
     87          OnChanged();
     88        }
     89      }
     90    }
    5491    private bool inclusiveLowerBound;
    5592    [Storable]
     
    5794      get => inclusiveLowerBound;
    5895      set {
    59         if (value != inclusiveLowerBound) {
     96        if (inclusiveLowerBound != value) {
    6097          inclusiveLowerBound = value;
    61           OnChanged(EventArgs.Empty);
    62         }
    63       }
    64     }
    65     [Storable]
    66     public bool InclusiveUpperBound { get; set; }
    67     [Storable]
    68     public bool IsDerivation { get; set; }
    69 
    70     private string variable;
    71     [Storable]
    72     public string Variable {
    73       get => variable;
    74       set {
    75         if (value != variable) {
    76           variable = value;
    77           OnChanged(EventArgs.Empty);
    78         }
    79       }
    80     }
    81 
    82     public int numberOfDerivation;
    83     [Storable]
    84     public int NumberOfDerivation {
    85       get => numberOfDerivation;
    86       set {
    87         if (value != numberOfDerivation) {
    88           numberOfDerivation = value;
    89           OnChanged(EventArgs.Empty);
     98          UpdateExpression();
     99          OnChanged();
     100        }
     101      }
     102    }
     103
     104    private bool inclusiveUpperBound;
     105
     106    [Storable]
     107    public bool InclusiveUpperBound {
     108      get => inclusiveUpperBound;
     109      set {
     110        if (inclusiveUpperBound != value) {
     111          inclusiveUpperBound = value;
     112          UpdateExpression();
     113          OnChanged();
    90114        }
    91115      }
     
    99123        if (value != enabled) {
    100124          enabled = value;
    101           OnChanged(EventArgs.Empty);
     125          OnChanged();
    102126        }
    103127      }
     
    105129
    106130    [StorableConstructor]
    107     protected IntervalConstraint(StorableConstructorFlag _) : base(_) { }
    108 
    109     public IntervalConstraint(string expression, string definition, Interval interval, bool inclusiveLowerBound,
    110       bool inclusiveUpperBound, bool isDerivation, string variable, int numberOfDerivation, bool isChecked) {
    111       base.name = expression;
    112       Expression = expression;
    113       Definition = definition;
    114       Interval = interval;
    115       InclusiveLowerBound = inclusiveLowerBound;
    116       InclusiveUpperBound = inclusiveUpperBound;
    117       IsDerivation = isDerivation;
    118       Variable = variable;
    119       NumberOfDerivation = numberOfDerivation;
    120       enabled = isChecked;
     131    private IntervalConstraint(StorableConstructorFlag _) : base(_) { }
     132
     133    public IntervalConstraint(string expression, string variable, int numberOfDerivation, Interval interval, bool inclusiveLowerBound,
     134      bool inclusiveUpperBound, bool enabled) : base(){
     135      this.expression = expression;
     136      this.variable = variable;
     137      this.numberOfDerivation = numberOfDerivation;
     138      this.interval = interval;
     139      this.inclusiveLowerBound = inclusiveLowerBound;
     140      this.inclusiveUpperBound = inclusiveUpperBound;
     141      this.enabled = enabled;
    121142    }
    122143
     
    125146    }
    126147
    127     protected IntervalConstraint(IntervalConstraint original, Cloner cloner)
     148    private IntervalConstraint(IntervalConstraint original, Cloner cloner)
    128149      : base(original, cloner) {
    129150      this.Expression = original.Expression;
    130       this.Definition = original.Definition;
    131151      this.Interval = original.Interval;
    132152      this.InclusiveLowerBound = original.InclusiveLowerBound;
    133153      this.InclusiveUpperBound = original.InclusiveUpperBound;
    134       this.IsDerivation = original.IsDerivation;
    135154      this.Variable = original.Variable;
    136155      this.NumberOfDerivation = original.NumberOfDerivation;
     
    139158
    140159    public event EventHandler Changed;
    141     protected virtual void OnChanged(EventArgs e) {
     160    private void OnChanged() {
    142161      EventHandler handlers = Changed;
    143162      if (handlers != null)
    144         handlers(this, e);
     163        handlers(this, EventArgs.Empty);
     164    }
     165
     166    private static string GetDerivationString(int derivation) {
     167      switch (derivation) {
     168        case 1:
     169          return "";
     170        case 2:
     171          return "²";
     172        case 3:
     173          return "³";
     174        default:
     175          return "";
     176      }
     177    }
     178
     179    private void UpdateExpression() {
     180      var expression = "";
     181
     182      if (!IsDerivation) {
     183        expression = string.Format("{0} in {1}{2} .. {3}{4}",
     184          Variable,
     185          (InclusiveLowerBound) ? "[" : "]",
     186          Interval.LowerBound,
     187          Interval.UpperBound,
     188          (InclusiveUpperBound) ? "]" : "[");
     189        Expression = expression;
     190        return;
     191      }
     192      expression = string.Format("\u2202{5}Target/\u2202{0}{5} in {1}{2} .. {3}{4}",
     193        Variable,
     194        (InclusiveLowerBound) ? "[" : "]",
     195        Interval.LowerBound,
     196        Interval.UpperBound,
     197        (InclusiveUpperBound) ? "]" : "[",
     198        GetDerivationString(numberOfDerivation));
     199        Expression = expression;
     200    }
     201
     202    private string GetDefinitionString() {
     203      if (!IsDerivation) {
     204        return "Target " + Variable;
     205      }
     206      var definition = $"\u2202{GetDerivationString(numberOfDerivation)}Target/\u2202{Variable}{GetDerivationString(numberOfDerivation)}";
     207      return definition;
     208    }
     209
     210    public override string ToString() {
     211      return Expression;
    145212    }
    146213  }
Note: See TracChangeset for help on using the changeset viewer.