Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16917 was 16917, checked in by chaider, 5 years ago

#2971 Reformated code and added null-condition to Intervals

File size: 6.4 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    private string expression;
31    [Storable]
32    public string Expression {
33      get => expression;
34      private set {
35        if (expression != value) {
36          expression = value;
37          OnChanged();
38          OnToStringChanged();
39        }
40      }
41    }
42
43    public string Definition {
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    private string target;
61    [Storable]
62    public string Target {
63      get => target;
64      set {
65        if (target != value) {
66          target = value;
67          UpdateExpression();
68          OnChanged();
69        }
70      }
71    }
72
73    public bool IsDerivation {
74      get => numberOfDerivation > 0;
75    }
76
77    private int numberOfDerivation;
78    [Storable]
79    public int NumberOfDerivation {
80      get => numberOfDerivation;
81      set {
82        if (value < 0 || value > 3)
83          throw new ArgumentException("Number of derivation has to be between 0 - 3.");
84        if (numberOfDerivation != value) {
85          numberOfDerivation = value;
86          UpdateExpression();
87          OnChanged();
88        }
89      }
90    }
91
92    private Interval interval;
93    [Storable]
94    public Interval Interval {
95      get => interval;
96      set {
97        if (interval != value) {
98          interval = value;
99          UpdateExpression();
100          OnChanged();
101        }
102      }
103    }
104    private bool inclusiveLowerBound;
105    [Storable]
106    public bool InclusiveLowerBound {
107      get => inclusiveLowerBound;
108      set {
109        if (inclusiveLowerBound != value) {
110          inclusiveLowerBound = value;
111          UpdateExpression();
112          OnChanged();
113        }
114      }
115    }
116
117    private bool inclusiveUpperBound;
118    [Storable]
119    public bool InclusiveUpperBound {
120      get => inclusiveUpperBound;
121      set {
122        if (inclusiveUpperBound != value) {
123          inclusiveUpperBound = value;
124          UpdateExpression();
125          OnChanged();
126        }
127      }
128    }
129
130    private bool enabled;
131    [Storable]
132    public bool Enabled {
133      get => enabled;
134      set {
135        if (value != enabled) {
136          enabled = value;
137          OnChanged();
138        }
139      }
140    }
141
142    [StorableConstructor]
143    private IntervalConstraint(StorableConstructorFlag _) : base(_) { }
144
145    public IntervalConstraint(string expression, string variable, string target, int numberOfDerivation, Interval interval, bool inclusiveLowerBound,
146      bool inclusiveUpperBound, bool enabled) : base(){
147      this.expression = expression;
148      this.variable = variable;
149      this.target = target;
150      this.numberOfDerivation = numberOfDerivation;
151      this.interval = interval;
152      this.inclusiveLowerBound = inclusiveLowerBound;
153      this.inclusiveUpperBound = inclusiveUpperBound;
154      this.enabled = enabled;
155    }
156
157    public override IDeepCloneable Clone(Cloner cloner) {
158      return new IntervalConstraint(this, cloner);
159    }
160
161    private IntervalConstraint(IntervalConstraint original, Cloner cloner)
162      : base(original, cloner) {
163      this.Expression = original.Expression;
164      this.Variable = original.Variable;
165      this.Target = original.Target;
166      this.NumberOfDerivation = original.NumberOfDerivation;
167      this.Interval = original.Interval;
168      this.InclusiveLowerBound = original.InclusiveLowerBound;
169      this.InclusiveUpperBound = original.InclusiveUpperBound;
170      this.Enabled = original.Enabled;
171    }
172
173    public event EventHandler Changed;
174    private void OnChanged() {
175      EventHandler handlers = Changed;
176      if (handlers != null)
177        handlers(this, EventArgs.Empty);
178    }
179
180    private static string GetDerivationString(int derivation) {
181      switch (derivation) {
182        case 1:
183          return "";
184        case 2:
185          return "²";
186        case 3:
187          return "³";
188        default:
189          return "";
190      }
191    }
192
193    private void UpdateExpression() {
194      var expression = "";
195
196      if (!IsDerivation) {
197        expression = string.Format("Target:{0} in {1}{2} .. {3}{4}",
198          Variable,
199          (InclusiveLowerBound) ? "[" : "]",
200          Interval?.LowerBound,
201          Interval?.UpperBound,
202          (InclusiveUpperBound) ? "]" : "[");
203        Expression = expression;
204        return;
205      }
206      expression = string.Format("\u2202{6}{1}/\u2202{0}{6} in {2}{3} .. {4}{5}",
207        Variable,
208        Target,
209        (InclusiveLowerBound) ? "[" : "]",
210        Interval?.LowerBound,
211        Interval?.UpperBound,
212        (InclusiveUpperBound) ? "]" : "[",
213        GetDerivationString(numberOfDerivation));
214        Expression = expression;
215    }
216
217    private string GetDefinitionString() {
218      if (!IsDerivation) {
219        return "Target " + Variable;
220      }
221      var definition = $"\u2202{GetDerivationString(numberOfDerivation)}Target/\u2202{Variable}{GetDerivationString(numberOfDerivation)}";
222      return definition;
223    }
224
225    public override string ToString() {
226      return Expression;
227    }
228  }
229}
Note: See TracBrowser for help on using the repository browser.