Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2956_apriori_knowledge/HeuristicLab.Algorithms.DataAnalysis.KnowledgeIntegration/3.4/Util/FormulationParser.cs @ 16303

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

#2956: Added intermediate of a-priori knowledge

File size: 2.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Algorithms.DataAnalysis.KnowledgeIntegration.IntervalArithmetic;
7
8namespace HeuristicLab.Algorithms.DataAnalysis.KnowledgeIntegration.Util {
9  public class FormulationParser {
10
11    private static readonly char LEFT_SQUARE_BRACKET = '[';
12    private static readonly char RIGHT_SQUARE_BRACKET = ']';
13    private static readonly char POSITIVE = '+';
14    private static readonly char NEGATIVE = '-';
15    private static readonly string INFINITY = "Inf";
16    private static readonly string INTERVAL_SEPERATOR = "..";
17
18
19    public Interval Parse(string intervalString) {
20      var interval = new Interval();
21      //Remove all whitespaces from interval string
22      intervalString = intervalString.Replace(" ", String.Empty);
23
24      if (intervalString[0] == LEFT_SQUARE_BRACKET) {
25        //Closed interval start
26      } else if (intervalString[0] == RIGHT_SQUARE_BRACKET) {
27        //Opened interval start
28      } else {
29        throw new Exception("Interval has no valid starting symbol!");
30      }
31      if (intervalString[intervalString.Length-1] == LEFT_SQUARE_BRACKET) {
32        //Closed interval start
33      } else if (intervalString[intervalString.Length - 1] == RIGHT_SQUARE_BRACKET) {
34        //Opened interval start
35      } else {
36        throw new Exception("Interval has no valid ending symbol!");
37      }
38      //Get lower bound of interval
39      var splited = intervalString.Substring(1, intervalString.Length-2).Split(new string[] { INTERVAL_SEPERATOR }, StringSplitOptions.None);
40      if (splited.Length != 2)
41        throw new Exception("Interval has no lower/upper-bound seperator!");
42      else {
43        for (int i = 0; i < 2; ++i) {
44          if (int.TryParse(splited[i], out int lower)) {
45            if (i == 0)
46              interval.LowerBound = lower;
47            else
48              interval.UpperBound = lower;
49          } else if (splited[i][0] == POSITIVE || splited[i][0] == NEGATIVE) {
50            bool isPositive = true;
51            if (splited[i][0] == POSITIVE) {
52              isPositive = true;
53            } else if (splited[i][0] == NEGATIVE) {
54              isPositive = false;
55            }
56            if (splited[i].Substring(1, splited[i].Length-1) != INFINITY) {
57              throw new Exception("Lower bound is not a valid interval bound!");
58            } else {
59              if (isPositive) {
60                if (i == 0)
61                  interval.LowerBound = int.MaxValue;
62              } else {
63                if (i == 0)
64                  interval.LowerBound = int.MinValue;
65                }
66            }
67          } else {
68            throw new Exception("Lower bound is not a valid interval bound!");
69          }
70        }
71      }
72
73
74      return null;
75    }
76  }
77}
Note: See TracBrowser for help on using the repository browser.