source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/IntervalConstraint.cs @ 16964

Last change on this file since 16964 was 16964, checked in by chaider, 18 months ago

#2971 Changed setter conditions to precondiation checks

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21using System;
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HEAL.Attic;
25
26namespace HeuristicLab.Problems.DataAnalysis {
27  [StorableType("8109BE58-CCFB-4462-A2F4-EEE5DFADAFF7")]
28  [Item("Interval Constraint", "Constraint on intervals.")]
29  public sealed class IntervalConstraint : Item {
30    [Storable]
31    private string expression;
32    public string Expression {
33      get => expression;
34      private set {
35        if (expression == value) return;
36        expression = value;
37        OnChanged();
38        OnToStringChanged();
39      }
40    }
41
42    public string Definition {
43      get { return GetDefinitionString(); }
44    }
45
46    [Storable]
47    private string variable;
48    public string Variable {
49      get => variable;
50      private set {
51        if (variable == value) return;
52        variable = value;
53        UpdateExpression();
54        OnChanged();
55      }
56    }
57
58    [Storable]
59    private string target;
60    public string Target {
61      get => target;
62      set {
63        if (target == value) return;
64        target = value;
65        UpdateExpression();
66        OnChanged();
67      }
68    }
69
70    public bool IsDerivation {
71      get => numberOfDerivation > 0;
72    }
73
74    [Storable]
75    private int numberOfDerivation;
76    public int NumberOfDerivation {
77      get => numberOfDerivation;
78      set {
79        if (value < 0 || value > 3) throw new ArgumentException("Number of derivation has to be between 0 - 3.");
80        if (numberOfDerivation == value) return;
81        numberOfDerivation = value;
82        UpdateExpression();
83        OnChanged();
84      }
85    }
86
87    [Storable]
88    private Interval interval;
89    public Interval Interval {
90      get => interval;
91      set {
92        if (interval == value) return;
93        interval = value;
94        UpdateExpression();
95        OnChanged();
96      }
97    }
98
99    [Storable]
100    private bool inclusiveLowerBound;
101    public bool InclusiveLowerBound {
102      get => inclusiveLowerBound;
103      set {
104        if (inclusiveLowerBound == value) return;
105        inclusiveLowerBound = value;
106        UpdateExpression();
107        OnChanged();
108      }
109    }
110
111    [Storable]
112    private bool inclusiveUpperBound;
113    public bool InclusiveUpperBound {
114      get => inclusiveUpperBound;
115      set {
116        if (inclusiveUpperBound == value) return;
117        inclusiveUpperBound = value;
118        UpdateExpression();
119        OnChanged();
120      }
121    }
122
123    [Storable]
124    private bool enabled;
125    public bool Enabled {
126      get => enabled;
127      set {
128        if (enabled == value) return;
129        enabled = value;
130        OnChanged();
131      }
132    }
133
134    [StorableConstructor]
135    private IntervalConstraint(StorableConstructorFlag _) : base(_) { }
136
137    public IntervalConstraint(string expression, string variable, string target, int numberOfDerivation, Interval interval, bool inclusiveLowerBound,
138      bool inclusiveUpperBound, bool enabled) : base(){
139      this.expression = expression;
140      this.variable = variable;
141      this.target = target;
142      this.numberOfDerivation = numberOfDerivation;
143      this.interval = interval;
144      this.inclusiveLowerBound = inclusiveLowerBound;
145      this.inclusiveUpperBound = inclusiveUpperBound;
146      this.enabled = enabled;
147    }
148
149    public override IDeepCloneable Clone(Cloner cloner) {
150      return new IntervalConstraint(this, cloner);
151    }
152
153    private IntervalConstraint(IntervalConstraint original, Cloner cloner)
154      : base(original, cloner) {
155      this.Expression = original.Expression;
156      this.Variable = original.Variable;
157      this.Target = original.Target;
158      this.NumberOfDerivation = original.NumberOfDerivation;
159      this.Interval = original.Interval;
160      this.InclusiveLowerBound = original.InclusiveLowerBound;
161      this.InclusiveUpperBound = original.InclusiveUpperBound;
162      this.Enabled = original.Enabled;
163    }
164
165    public event EventHandler Changed;
166    private void OnChanged() {
167      EventHandler handlers = Changed;
168      if (handlers != null)
169        handlers(this, EventArgs.Empty);
170    }
171
172    private static string GetDerivationString(int derivation) {
173      switch (derivation) {
174        case 1:
175          return "";
176        case 2:
177          return "²";
178        case 3:
179          return "³";
180        default:
181          return "";
182      }
183    }
184
185    private void UpdateExpression() {
186      var expression = "";
187
188      if (!IsDerivation) {
189        expression = string.Format("Target:{0} in {1}{2} .. {3}{4}",
190          Variable,
191          (InclusiveLowerBound) ? "[" : "]",
192          Interval?.LowerBound,
193          Interval?.UpperBound,
194          (InclusiveUpperBound) ? "]" : "[");
195        Expression = expression;
196        return;
197      }
198      expression = string.Format("∂{6}{1}/∂{0}{6} in {2}{3} .. {4}{5}",
199        Variable,
200        Target,
201        (InclusiveLowerBound) ? "[" : "]",
202        Interval?.LowerBound,
203        Interval?.UpperBound,
204        (InclusiveUpperBound) ? "]" : "[",
205        GetDerivationString(numberOfDerivation));
206        Expression = expression;
207    }
208
209    private string GetDefinitionString() {
210      if (!IsDerivation) {
211        return "Target " + Variable;
212      }
213      var definition = $"∂{GetDerivationString(numberOfDerivation)}Target/∂{Variable}{GetDerivationString(numberOfDerivation)}";
214      return definition;
215    }
216
217    public override string ToString() {
218      return Expression;
219    }
220  }
221}
Note: See TracBrowser for help on using the repository browser.