Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Parser/IntervalConstraintsParser.cs @ 16879

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

#2971 Removed unused parser method

File size: 7.0 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 System.Collections.Generic;
23using System.Globalization;
24using System.Linq;
25using System.Text.RegularExpressions;
26
27namespace HeuristicLab.Problems.DataAnalysis {
28  public static class IntervalConstraintsParser {
29
30    public static IEnumerable<IntervalConstraint> ParseInput(string input, string target = "", IEnumerable<string> variables = null) {
31      var lines = input.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
32      foreach (var line in lines) {
33        var trimmedLine = line.TrimStart();
34        //Check for target-variable constraint
35        if (trimmedLine.StartsWith("Target:")) {
36          var start = "Target:".Length;
37          var end = trimmedLine.Length;
38          var targetConstraint = trimmedLine.Substring(start, end-start);
39          var match = Regex.Match(targetConstraint,
40            @"['](.*)[']\s*(\bin\b)\s*([\[\]])\s*(\S*)\s*(..)\s*(\S*)\s*([\[\]])");
41          if (match.Success) {
42            if (match.Groups.Count != 8) {
43              throw new ArgumentException("The given target-constraint is not complete!");
44            } else {
45              if (target != "") {
46                if (match.Groups[1].Value.Trim() != target) {
47                  throw new ArgumentException("The given target variable is not in the given dataset!");
48                }
49              }
50              var lowerBound = ParseIntervalBounds(match.Groups[4].Value);
51              var upperBound = ParseIntervalBounds(match.Groups[6].Value);
52              var constraint = new IntervalConstraint("Target:" + match.Groups[0].Value);
53              constraint.Expression = "Target:" + match.Groups[0].Value;
54              constraint.Definition = "Target " + match.Groups[1].Value.Trim();
55              constraint.Variable = match.Groups[1].Value.Trim();
56              constraint.InclusiveLowerBound = match.Groups[3].Value.Trim() == "[";
57              constraint.InclusiveUpperBound = match.Groups[7].Value.Trim() == "]";
58              constraint.IsDerivation = false;
59              constraint.NumberOfDerivation = 0;
60              constraint.Interval = new Interval(lowerBound, upperBound);
61
62              yield return constraint;
63            }
64          } else {
65            throw new ArgumentException("The inserted target constraint is not valid!");
66          }
67          //Check for derivation
68        } else if (trimmedLine.StartsWith("d") || trimmedLine.StartsWith("\u2202")) {
69          var match = Regex.Match(trimmedLine,
70            @"([d∂])([²³]?)\s*['](.*)[']\s*(\/)\s*([d∂])\s*['](.*)[']\s*([²³]?)\s*\bin\b\s*([\[\]])\s*(\S*)\s*(..)\s*(\S*)\s*([\[\]])");
71
72          if (match.Success) {
73            if (match.Groups.Count != 13) {
74              throw new ArgumentException("The given derivation-constraint is not complete");
75            } else {
76              if (target != "") {
77                if (match.Groups[3].Value != target)
78                  throw new ArgumentException("The given target variable is not given in the dataset!");
79              }
80
81              if (variables != null && variables.Any()) {
82                if (variables.All(v => v != match.Groups[6].Value.Trim())) {
83                  throw new ArgumentException("The given variable does not exist in the dataset!");
84                }
85              }
86
87              if (match.Groups[2].Value.Trim() != "" || match.Groups[7].Value.Trim() != "") {
88                if (match.Groups[2].Value.Trim() == "" || match.Groups[7].Value.Trim() == "")
89                  throw new ArgumentException("Number of derivation has to be written on both sides!");
90                if (match.Groups[2].Value.Trim() != match.Groups[7].Value.Trim())
91                  throw new ArgumentException("Derivation number is not equal on both sides!");
92              }
93
94              var lowerBound = ParseIntervalBounds(match.Groups[9].Value);
95              var upperBound = ParseIntervalBounds(match.Groups[11].Value);
96              var constraint = new IntervalConstraint(match.Groups[0].Value);
97              constraint.Expression = match.Groups[0].Value;
98              constraint.Definition = match.Groups[1].Value + match.Groups[2].Value + match.Groups[3].Value +
99                                      match.Groups[4].Value + match.Groups[5].Value + match.Groups[6].Value + match.Groups[7].Value;
100              constraint.IsDerivation = true;
101              constraint.InclusiveLowerBound = match.Groups[8].Value.Trim() == "[";
102              constraint.InclusiveUpperBound = match.Groups[12].Value.Trim() == "]";
103              constraint.Variable = match.Groups[6].Value.Trim();
104              constraint.NumberOfDerivation = ParseDerivationCount(match.Groups[2].Value.Trim());
105              constraint.Interval = new Interval(lowerBound, upperBound);
106
107              yield return constraint;
108            }
109          } else {
110            throw new ArgumentException("The inserted derivation constraint is not valid!");
111          }
112          //Check for comment
113        } else if (trimmedLine.StartsWith("#") || trimmedLine == "") {
114          //If it is a comment just continue without saving anything
115          continue;
116        } else {
117          throw new ArgumentException("Error at your constraints definition constraints have to start with (Target: | d | \u2202 | #)");
118        }
119      }
120    }
121
122    private static double ParseIntervalBounds(string input) {
123      input = input.ToLower();
124      switch (input) {
125        case "+inf.":
126        case "inf.":
127          return double.PositiveInfinity;
128        case "-inf.":
129          return double.NegativeInfinity;
130        default: {
131            if (double.TryParse(input, NumberStyles.Any, CultureInfo.InvariantCulture, out var value)) {
132              return value;
133            } else {
134              throw new ArgumentException("The given boundary is not a double value!");
135            }
136          }
137      }
138    }
139
140    private static int ParseDerivationCount(string input) {
141      switch (input) {
142        case "":
143          return 1;
144        case "²":
145          return 2;
146        case "³":
147          return 3;
148        default:
149          int.TryParse(input, out var value);
150          return value;
151      }
152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.