Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2971 Added \n for end-of-line termination to regex

File size: 3.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.Text.RegularExpressions;
5
6namespace HeuristicLab.Problems.DataAnalysis {
7  public static class IntervalConstraintsParser {
8    public static IEnumerable<IntervalConstraint> Parse(string input) {
9      var options = RegexOptions.Multiline | RegexOptions.IgnoreCase;
10      var matches = Regex.Matches(input, @"^(.*)\bin\b\s*([\[\]])(.*[^\s])(\s*\.\.\s*)([^\s].*)([\[\]])\n*\r*\s*$", options);
11
12      for (var i = 0; i < matches.Count; ++i) {
13        if (matches[i].Groups.Count == 7) {
14          var intervalConstraint = new IntervalConstraint();
15          //pattern 1 = defintion
16          var definition = Regex.Replace(matches[i].Groups[1].Value, @"\s *", "");
17          if (Regex.IsMatch(definition, @"\/")) {
18            var splitted = Regex.Split(definition.Replace(" ", string.Empty), @"\/");
19            var match = Regex.Match(splitted[0], @"([d∂])([0-9]|[²³])?(.*[^\s*])");
20            if (match.Success) {
21              intervalConstraint.NumberOfDerivation = match.Groups[2].Success ? ParseDerivationCount(match.Groups[2].Value) : 1;
22              intervalConstraint.Definition = match.Groups[3].Value;
23              intervalConstraint.IsDerivation = true;
24              var formulation = Regex.Match(splitted[1], @"([d∂])(.*[^²³])([²³])?");
25              if (formulation.Success) {
26                intervalConstraint.Variable = formulation.Groups[2].Success ? formulation.Groups[2].Value : "";
27              }
28            } else {
29              throw new ArgumentException($"An error occured in the derivation part: {splitted[0]}");
30            }
31          } else {
32            intervalConstraint.Definition = Regex.Match(definition, @".*[^.\s]*").Value;
33            intervalConstraint.IsDerivation = false;
34          }
35          intervalConstraint.Expression = matches[i].Groups[0].Value.Trim(' ', '\t', '\n', '\r');
36          intervalConstraint.InclusiveLowerBound = (matches[i].Groups[2].Value == "[");
37          intervalConstraint.InclusiveUpperBound = (matches[i].Groups[6].Value == "]");
38          intervalConstraint.Interval = new Interval(ParseIntervalBounds(matches[i].Groups[3].Value.Replace(" ", string.Empty)),
39            ParseIntervalBounds(matches[i].Groups[5].Value.Replace(" ", string.Empty)));
40
41          yield return intervalConstraint;
42        } else {
43          throw new ArgumentException($"The given constraint: {matches[i].Value} is not valid.");
44        }
45      }
46    }
47
48    private static double ParseIntervalBounds(string input) {
49      input = input.ToLower();
50      switch (input) {
51        case "+inf.":
52        case "inf.":
53          return double.PositiveInfinity;
54        case "-inf.":
55          return double.NegativeInfinity;
56        default: {
57            if (double.TryParse(input, NumberStyles.Any, CultureInfo.InvariantCulture, out var value)) {
58              return value;
59            } else {
60              throw new ArgumentException("The given Interval in the constraint is not valid.");
61            }
62          }
63      }
64    }
65
66    private static int ParseDerivationCount(string input) {
67      switch (input) {
68        case "²":
69          return 2;
70        case "³":
71          return 3;
72        default:
73          int.TryParse(input, out var value);
74          return value;
75      }
76    }
77  }
78}
Note: See TracBrowser for help on using the repository browser.