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

Last change on this file since 17370 was 17370, checked in by chaider, 11 months ago

#2971 Removed the possibility of declaring open and closed intervals. All intervals are closed intervals now.

File size: 5.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 enabled;
101    public bool Enabled {
102      get => enabled;
103      set {
104        if (enabled == value) return;
105        enabled = value;
106        OnChanged();
107      }
108    }
109
110    [StorableConstructor]
111    private IntervalConstraint(StorableConstructorFlag _) : base(_) { }
112
113    public IntervalConstraint(string expression, string variable, string target, int numberOfDerivation, Interval interval, bool enabled) : base(){
114      this.expression = expression;
115      this.variable = variable;
116      this.target = target;
117      this.numberOfDerivation = numberOfDerivation;
118      this.interval = interval;
119      this.enabled = enabled;
120    }
121
122    public override IDeepCloneable Clone(Cloner cloner) {
123      return new IntervalConstraint(this, cloner);
124    }
125
126    private IntervalConstraint(IntervalConstraint original, Cloner cloner)
127      : base(original, cloner) {
128      this.Expression = original.Expression;
129      this.Variable = original.Variable;
130      this.Target = original.Target;
131      this.NumberOfDerivation = original.NumberOfDerivation;
132      this.Interval = original.Interval;
133      this.Enabled = original.Enabled;
134    }
135
136    public event EventHandler Changed;
137    private void OnChanged() {
138      EventHandler handlers = Changed;
139      if (handlers != null)
140        handlers(this, EventArgs.Empty);
141    }
142
143    private static string GetDerivationString(int derivation) {
144      switch (derivation) {
145        case 1:
146          return "";
147        case 2:
148          return "²";
149        case 3:
150          return "³";
151        default:
152          return "";
153      }
154    }
155
156    private void UpdateExpression() {
157      var expression = "";
158
159      if (!IsDerivation) {
160        expression = string.Format("Target:{0} in {1}{2} .. {3}{4}",
161          Variable,
162          "[",
163          Interval?.LowerBound,
164          Interval?.UpperBound,
165          "]");
166        Expression = expression;
167        return;
168      }
169      expression = string.Format("∂{6}{1}/∂{0}{6} in {2}{3} .. {4}{5}",
170        Variable,
171        Target,
172        "[",
173        Interval?.LowerBound,
174        Interval?.UpperBound,
175        "]",
176        GetDerivationString(numberOfDerivation));
177        Expression = expression;
178    }
179
180    private string GetDefinitionString() {
181      if (!IsDerivation) {
182        return "Target " + Variable;
183      }
184      var definition = $"∂{GetDerivationString(numberOfDerivation)}Target/∂{Variable}{GetDerivationString(numberOfDerivation)}";
185      return definition;
186    }
187
188    public override string ToString() {
189      return Expression;
190    }
191  }
192}
Note: See TracBrowser for help on using the repository browser.