using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using HeuristicLab.Algorithms.DataAnalysis.KnowledgeIntegration.IntervalArithmetic; namespace HeuristicLab.Algorithms.DataAnalysis.KnowledgeIntegration.Util { public class FormulationParser { private static readonly char LEFT_SQUARE_BRACKET = '['; private static readonly char RIGHT_SQUARE_BRACKET = ']'; private static readonly char POSITIVE = '+'; private static readonly char NEGATIVE = '-'; private static readonly string INFINITY = "Inf"; private static readonly string INTERVAL_SEPERATOR = ".."; public Interval Parse(string intervalString) { var interval = new Interval(); //Remove all whitespaces from interval string intervalString = intervalString.Replace(" ", String.Empty); if (intervalString[0] == LEFT_SQUARE_BRACKET) { //Closed interval start } else if (intervalString[0] == RIGHT_SQUARE_BRACKET) { //Opened interval start } else { throw new Exception("Interval has no valid starting symbol!"); } if (intervalString[intervalString.Length-1] == LEFT_SQUARE_BRACKET) { //Closed interval start } else if (intervalString[intervalString.Length - 1] == RIGHT_SQUARE_BRACKET) { //Opened interval start } else { throw new Exception("Interval has no valid ending symbol!"); } //Get lower bound of interval var splited = intervalString.Substring(1, intervalString.Length-2).Split(new string[] { INTERVAL_SEPERATOR }, StringSplitOptions.None); if (splited.Length != 2) throw new Exception("Interval has no lower/upper-bound seperator!"); else { for (int i = 0; i < 2; ++i) { if (int.TryParse(splited[i], out int lower)) { if (i == 0) interval.LowerBound = lower; else interval.UpperBound = lower; } else if (splited[i][0] == POSITIVE || splited[i][0] == NEGATIVE) { bool isPositive = true; if (splited[i][0] == POSITIVE) { isPositive = true; } else if (splited[i][0] == NEGATIVE) { isPositive = false; } if (splited[i].Substring(1, splited[i].Length-1) != INFINITY) { throw new Exception("Lower bound is not a valid interval bound!"); } else { if (isPositive) { if (i == 0) interval.LowerBound = int.MaxValue; } else { if (i == 0) interval.LowerBound = int.MinValue; } } } else { throw new Exception("Lower bound is not a valid interval bound!"); } } } return null; } } }