Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2971 fixed persisting of problems

  • Attached Storable propperty to backing fields
  • Changed the lazy enumerable of the Constraints field
File size: 6.6 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(Name = "Expression")]
31    private string expression;
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    [Storable(Name = "Variable")]
48    private string variable;
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    [Storable(Name = "Target")]
61    private string target;
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    [Storable(Name = "NumberOfDerivation")]
78    private int numberOfDerivation;
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    [Storable(Name = "Interval")]
93    private Interval interval;
94    public Interval Interval {
95      get => interval;
96      set {
97        if (interval != value) {
98          interval = value;
99          UpdateExpression();
100          OnChanged();
101        }
102      }
103    }
104
105    [Storable(Name = "InclusiveLowerBound")]
106    private bool inclusiveLowerBound;
107    public bool InclusiveLowerBound {
108      get => inclusiveLowerBound;
109      set {
110        if (inclusiveLowerBound != value) {
111          inclusiveLowerBound = value;
112          UpdateExpression();
113          OnChanged();
114        }
115      }
116    }
117
118    [Storable(Name = "InclusiveUpperBound")]
119    private bool inclusiveUpperBound;
120    public bool InclusiveUpperBound {
121      get => inclusiveUpperBound;
122      set {
123        if (inclusiveUpperBound != value) {
124          inclusiveUpperBound = value;
125          UpdateExpression();
126          OnChanged();
127        }
128      }
129    }
130
131    [Storable(Name = "Enabled")]
132    private bool enabled;
133    public bool Enabled {
134      get => enabled;
135      set {
136        if (value != enabled) {
137          enabled = value;
138          OnChanged();
139        }
140      }
141    }
142
143    [StorableConstructor]
144    private IntervalConstraint(StorableConstructorFlag _) : base(_) { }
145
146    public IntervalConstraint(string expression, string variable, string target, int numberOfDerivation, Interval interval, bool inclusiveLowerBound,
147      bool inclusiveUpperBound, bool enabled) : base(){
148      this.expression = expression;
149      this.variable = variable;
150      this.target = target;
151      this.numberOfDerivation = numberOfDerivation;
152      this.interval = interval;
153      this.inclusiveLowerBound = inclusiveLowerBound;
154      this.inclusiveUpperBound = inclusiveUpperBound;
155      this.enabled = enabled;
156    }
157
158    public override IDeepCloneable Clone(Cloner cloner) {
159      return new IntervalConstraint(this, cloner);
160    }
161
162    private IntervalConstraint(IntervalConstraint original, Cloner cloner)
163      : base(original, cloner) {
164      this.Expression = original.Expression;
165      this.Variable = original.Variable;
166      this.Target = original.Target;
167      this.NumberOfDerivation = original.NumberOfDerivation;
168      this.Interval = original.Interval;
169      this.InclusiveLowerBound = original.InclusiveLowerBound;
170      this.InclusiveUpperBound = original.InclusiveUpperBound;
171      this.Enabled = original.Enabled;
172    }
173
174    public event EventHandler Changed;
175    private void OnChanged() {
176      EventHandler handlers = Changed;
177      if (handlers != null)
178        handlers(this, EventArgs.Empty);
179    }
180
181    private static string GetDerivationString(int derivation) {
182      switch (derivation) {
183        case 1:
184          return "";
185        case 2:
186          return "²";
187        case 3:
188          return "³";
189        default:
190          return "";
191      }
192    }
193
194    private void UpdateExpression() {
195      var expression = "";
196
197      if (!IsDerivation) {
198        expression = string.Format("Target:{0} in {1}{2} .. {3}{4}",
199          Variable,
200          (InclusiveLowerBound) ? "[" : "]",
201          Interval?.LowerBound,
202          Interval?.UpperBound,
203          (InclusiveUpperBound) ? "]" : "[");
204        Expression = expression;
205        return;
206      }
207      expression = string.Format("∂{6}{1}/∂{0}{6} in {2}{3} .. {4}{5}",
208        Variable,
209        Target,
210        (InclusiveLowerBound) ? "[" : "]",
211        Interval?.LowerBound,
212        Interval?.UpperBound,
213        (InclusiveUpperBound) ? "]" : "[",
214        GetDerivationString(numberOfDerivation));
215        Expression = expression;
216    }
217
218    private string GetDefinitionString() {
219      if (!IsDerivation) {
220        return "Target " + Variable;
221      }
222      var definition = $"∂{GetDerivationString(numberOfDerivation)}Target/∂{Variable}{GetDerivationString(numberOfDerivation)}";
223      return definition;
224    }
225
226    public override string ToString() {
227      return Expression;
228    }
229  }
230}
Note: See TracBrowser for help on using the repository browser.