Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/09/19 14:58:24 (6 years ago)
Author:
chaider
Message:

#2971

  • Fixes and updates in IntervalConstraintView
  • Changed Parser
Location:
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Parser
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Parser/IntervalConstraint.cs

    r16772 r16773  
    1111  [StorableType("8109BE58-CCFB-4462-A2F4-EEE5DFADAFF7")]
    1212  [Item("Interval Constraint", "Constraint on intervals.")]
    13   public class IntervalConstraint : Item {
     13  public class IntervalConstraint : NamedItem {
    1414    public string Expression { get; set; }
    1515    public string Definition { get; set; }
     
    2323    public IntervalConstraint() {}
    2424
     25    public IntervalConstraint(string name) {
     26      base.name = name;
     27    }
     28
    2529    public IntervalConstraint(string expression, string definition, Interval interval, bool inclusiveLowerBound,
    2630      bool inclusiveUpperBound, bool isDerivation, string variable, int numberOfDerivation) {
     31      base.name = expression;
    2732      Expression = expression;
    2833      Definition = definition;
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Parser/IntervalConstraintsParser.cs

    r16732 r16773  
    22using System.Collections.Generic;
    33using System.Globalization;
     4using System.Linq;
    45using System.Text.RegularExpressions;
    56
     
    4647    }
    4748
     49    public static IEnumerable<IntervalConstraint> ParseInput(string input, string target = "", IEnumerable<string> variables = null) {
     50      var lines = input.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
     51      foreach (var line in lines) {
     52        var trimmedLine = line.TrimStart();
     53        //Check for target-variable constraint
     54        if (trimmedLine.StartsWith("Target:")) {
     55          var start = "Target:".Length;
     56          var end = trimmedLine.Length;
     57          var targetConstraint = trimmedLine.Substring(start, end-start);
     58          var match = Regex.Match(targetConstraint,
     59            @"([^\s]*)\s*(\bin\b)\s*([\[\]])\s*(\S*)\s*(..)\s*(\S*)\s*([\[\]])");
     60          if (match.Success) {
     61            if (match.Groups.Count != 8) {
     62              throw new ArgumentException("The given target-constraint is not complete!");
     63            } else {
     64              if (target != "") {
     65                if (match.Groups[1].Value.Trim() != target) {
     66                  throw new ArgumentException("The given target variable is not in the given dataset!");
     67                }
     68              }
     69              var lowerBound = ParseIntervalBounds(match.Groups[4].Value);
     70              var upperBound = ParseIntervalBounds(match.Groups[6].Value);
     71              var constraint = new IntervalConstraint(match.Groups[0].Value);
     72              constraint.Expression = match.Groups[0].Value;
     73              constraint.Definition = "";
     74              constraint.Variable = match.Groups[1].Value.Trim();
     75              constraint.InclusiveLowerBound = match.Groups[3].Value.Trim() == "[";
     76              constraint.InclusiveUpperBound = match.Groups[7].Value.Trim() == "]";
     77              constraint.IsDerivation = false;
     78              constraint.NumberOfDerivation = 0;
     79              constraint.Interval = new Interval(lowerBound, upperBound);
     80
     81              yield return constraint;
     82            }
     83          } else {
     84            throw new ArgumentException("The inserted target constraint is not valid!");
     85          }
     86          //Check for derivation
     87        } else if (trimmedLine.StartsWith("d") || trimmedLine.StartsWith("\u2202")) {
     88          var match = Regex.Match(trimmedLine,
     89            @"([d∂])([²³])?\s*(\S*)\s*(\/)([d∂])\s*([^²³]*)([²³])?\s*\bin\b\s*([\[\]])\s*(\S*)\s*(..)\s*(\S*)\s*([\[\]])");
     90
     91          if (match.Success) {
     92            if (match.Groups.Count != 13) {
     93              throw new ArgumentException("The given derivation-constraint is not complete");
     94            } else {
     95              if (target != "") {
     96                if (match.Groups[3].Value != target)
     97                  throw new ArgumentException("The given target variable is not given in the dataset!");
     98              }
     99
     100              if (variables != null && variables.Any()) {
     101                if (variables.All(v => v != match.Groups[6].Value.Trim())) {
     102                  throw new ArgumentException("The given variable does not exist in the dataset!");
     103                }
     104              }
     105
     106              if (match.Groups[2].Value.Trim() != "" || match.Groups[7].Value.Trim() != "") {
     107                if (match.Groups[2].Value.Trim() == "" || match.Groups[7].Value.Trim() == "")
     108                  throw new ArgumentException("Number of derivation has to be written on both sides!");
     109                if (match.Groups[2].Value.Trim() != match.Groups[7].Value.Trim())
     110                  throw new ArgumentException("Derivation number is not equal on both sides!");
     111              }
     112
     113              var lowerBound = ParseIntervalBounds(match.Groups[9].Value);
     114              var upperBound = ParseIntervalBounds(match.Groups[11].Value);
     115              var constraint = new IntervalConstraint(match.Groups[0].Value);
     116              constraint.Expression = match.Groups[0].Value;
     117              constraint.Definition = match.Groups[1].Value + match.Groups[2].Value + match.Groups[3].Value +
     118                                      match.Groups[4].Value + match.Groups[5].Value + match.Groups[6].Value + match.Groups[7].Value;
     119              constraint.IsDerivation = true;
     120              constraint.InclusiveLowerBound = match.Groups[8].Value.Trim() == "[";
     121              constraint.InclusiveUpperBound = match.Groups[12].Value.Trim() == "]";
     122              constraint.Variable = match.Groups[6].Value.Trim();
     123              constraint.NumberOfDerivation = ParseDerivationCount(match.Groups[2].Value);
     124              constraint.Interval = new Interval(lowerBound, upperBound);
     125
     126              yield return constraint;
     127            }
     128          } else {
     129            throw new ArgumentException("The inserted derivation constraint is not valid!");
     130          }
     131          //Check for comment
     132        } else if (trimmedLine.StartsWith("#") || trimmedLine == "") {
     133          //If it is a comment just continue without saving anything
     134          //TODO maybe save comment too.
     135          continue;
     136        } else {
     137          throw new ArgumentException("Error at your constraints definition constraints have to start with Target: | d | \u2202 | #");
     138        }
     139      }
     140    }
     141
    48142    private static double ParseIntervalBounds(string input) {
    49143      input = input.ToLower();
     
    58152              return value;
    59153            } else {
    60               throw new ArgumentException("The given Interval in the constraint is not valid.");
     154              throw new ArgumentException("The given boundary is not a double value!");
    61155            }
    62156          }
Note: See TracChangeset for help on using the changeset viewer.