Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2971

  • Added all fields to IntervalConstraintView
  • Rearanged fields
  • Updated NumberOfDerivation event
File size: 6.1 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    public bool IsDerivation {
61      get => numberOfDerivation > 0;
62    }
63
64    private 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 (numberOfDerivation != value) {
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    }
91    private bool inclusiveLowerBound;
92    [Storable]
93    public bool InclusiveLowerBound {
94      get => inclusiveLowerBound;
95      set {
96        if (inclusiveLowerBound != value) {
97          inclusiveLowerBound = value;
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();
114        }
115      }
116    }
117
118    private bool enabled;
119    [Storable]
120    public bool Enabled {
121      get => enabled;
122      set {
123        if (value != enabled) {
124          enabled = value;
125          OnChanged();
126        }
127      }
128    }
129
130    [StorableConstructor]
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;
142    }
143
144    public override IDeepCloneable Clone(Cloner cloner) {
145      return new IntervalConstraint(this, cloner);
146    }
147
148    private IntervalConstraint(IntervalConstraint original, Cloner cloner)
149      : base(original, cloner) {
150      this.Expression = original.Expression;
151      this.Interval = original.Interval;
152      this.InclusiveLowerBound = original.InclusiveLowerBound;
153      this.InclusiveUpperBound = original.InclusiveUpperBound;
154      this.Variable = original.Variable;
155      this.NumberOfDerivation = original.NumberOfDerivation;
156      this.enabled = original.enabled;
157    }
158
159    public event EventHandler Changed;
160    private void OnChanged() {
161      EventHandler handlers = Changed;
162      if (handlers != null)
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;
212    }
213  }
214}
Note: See TracBrowser for help on using the repository browser.