Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/17/20 17:03:24 (4 years ago)
Author:
dpiringe
Message:

#3026:

  • refactored json item validation process -> every method now returns a ValidationResult containing bool Success (true if validation is successful) and IEnumerable<string> Errors (error messages)
    • this design allows to specify error messages directly in json items
Location:
branches/3026_IntegrationIntoSymSpace
Files:
18 edited

Legend:

Unmodified
Added
Removed
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/ExportJsonDialog.Designer.cs

    r17451 r17481  
    273273    #endregion
    274274
    275     private System.Windows.Forms.GroupBox groupBox1;
    276275    private System.Windows.Forms.BindingSource jsonItemBindingSource;
    277276    private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/ExportJsonDialog.cs

    r17477 r17481  
    6969      // clear all runs
    7070      Optimizer.Runs.Clear();
    71      
    72       IList<IJsonItem> faultyItems = new List<IJsonItem>();
    73      
    74       if (!Root.GetValidator().Validate(ref faultyItems)) {
     71
     72      var validationResult = Root.GetValidator().Validate();
     73      if (!validationResult.Success) {
    7574        IList<Exception> list = new List<Exception>();
    7675        //print faultyItems
    77         foreach (var x in faultyItems) {
    78           list.Add(new Exception($"Combination of value and range is not valid for {x.Name}"));
     76        foreach (var x in validationResult.Errors) {
     77          list.Add(new Exception(x));
    7978        }
    8079        ErrorHandling.ShowErrorDialog(this, new AggregateException(list));
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Interfaces/IJsonItemValidator.cs

    r17406 r17481  
    77namespace HeuristicLab.JsonInterface {
    88  public interface IJsonItemValidator {
    9     bool Validate(ref IList<IJsonItem> faultyItems);
     9    ValidationResult Validate();
    1010  }
    1111}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonTemplateInstantiator.cs

    r17477 r17481  
    1414
    1515namespace HeuristicLab.JsonInterface {
    16   public struct InstantiatorResult {
    17     public IOptimizer Optimizer { get; set; }
    18     public IEnumerable<IResultJsonItem> ConfiguredResultItems { get; set; }
     16  public readonly struct InstantiatorResult {
     17    public InstantiatorResult(IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItems) {
     18      Optimizer = optimizer;
     19      ConfiguredResultItems = configuredResultItems;
     20    }
     21
     22    public IOptimizer Optimizer { get; }
     23    public IEnumerable<IResultJsonItem> ConfiguredResultItems { get; }
    1924  }
    2025
     
    4449    #region Helper
    4550    private InstantiatorResult ExecuteInstantiaton(string templateFile, string configFile = null) {
    46       InstantiatorResult result = new InstantiatorResult();
    4751
    4852      #region Parse Files
     
    5862      ProtoBufSerializer serializer = new ProtoBufSerializer();
    5963      IOptimizer optimizer = (IOptimizer)serializer.Deserialize(hLFileLocation);
    60       result.Optimizer = optimizer;
    6164      #endregion
    6265
     
    7376      JsonItemConverter.Inject(optimizer, rootItem);
    7477
    75       result.ConfiguredResultItems = CollectResults();
    76 
    77       return result;
     78      return new InstantiatorResult(optimizer, CollectResults());
    7879    }
    7980
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/BoolJsonItems.cs

    r17473 r17481  
    77namespace HeuristicLab.JsonInterface {
    88  public class BoolJsonItem : ValueJsonItem<bool> {
    9     protected override bool Validate() => true;
     9    protected override ValidationResult Validate() => ValidationResult.Successful();
    1010  }
    1111
    1212  public class BoolArrayJsonItem : ArrayJsonItem<bool> {
    13     protected override bool Validate() => true;
     13    protected override ValidationResult Validate() => ValidationResult.Successful();
    1414  }
    1515
    1616  public class BoolMatrixJsonItem : MatrixJsonItem<bool> {
    17     protected override bool Validate() => true;
     17    protected override ValidationResult Validate() => ValidationResult.Successful();
    1818  }
    1919}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/ConcreteRestrictedArrayJsonItem.cs

    r17473 r17481  
    99    public IEnumerable<T> ConcreteRestrictedItems { get; set; }
    1010
    11     protected override bool Validate() {
     11    protected override ValidationResult Validate() {
    1212      bool res = true;
     13      IList<string> errors = new List<string>();
    1314      foreach(var x in Value) {
    1415        bool tmp = false;
     
    1617          tmp = tmp || x.Equals(restrictedItem); //if one tmp is true, it stays true (match found)
    1718        }
     19        if (!tmp)
     20          errors.Add($"[{Path}]: Value '{x}' is not one of the allowed values: " +
     21                     $"'{ string.Join(",", ConcreteRestrictedItems.Select(s => s.ToString()).ToArray()) }'.");
    1822        res = res && tmp; //if one tmp is false, res will set false
    1923      }
    20       return res;
     24      if (res)
     25        return ValidationResult.Successful();
     26      else
     27        return ValidationResult.Faulty(errors);
    2128    }
    2229  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/ConcreteRestrictedValueJsonItem.cs

    r17473 r17481  
    99    public IEnumerable<T> ConcreteRestrictedItems { get; set; }
    1010
    11     protected override bool Validate() {
    12       if (ConcreteRestrictedItems == null) return true;
     11    protected override ValidationResult Validate() {
     12      if (ConcreteRestrictedItems == null) return ValidationResult.Successful();
    1313      foreach (var x in ConcreteRestrictedItems)
    14         if (Value.Equals(x)) return true;
    15       return false;
     14        if (Value.Equals(x)) return ValidationResult.Successful();
     15      return ValidationResult.Faulty(
     16        $"[{Path}]: Value {Value} is not one of the allowed values: " +
     17        $"'{ string.Join(",", ConcreteRestrictedItems.Select(s => s.ToString()).ToArray()) }'.");
    1618    }
    1719  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/DateTimeJsonItem.cs

    r17477 r17481  
    88namespace HeuristicLab.JsonInterface {
    99  public class DateTimeJsonItem : IntervalRestrictedValueJsonItem<DateTime> {
    10     protected override bool Validate() => Minimum.CompareTo(Value) >= 0 && Maximum.CompareTo(Value) <= 0;
    1110   
    1211    public override JObject GenerateJObject() {
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/EmptyJsonItem.cs

    r17473 r17481  
    77namespace HeuristicLab.JsonInterface {
    88  public class EmptyJsonItem : JsonItem {
    9     protected override bool Validate() => true;
     9    protected override ValidationResult Validate() => ValidationResult.Successful();
    1010  }
    1111}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/IntervalRestrictedArrayJsonItem.cs

    r17477 r17481  
    1212    public T Maximum { get; set; }
    1313
    14     protected override bool Validate() {
    15       foreach(var x in Value) {
    16         if (Minimum.CompareTo(x) > 0 || Maximum.CompareTo(x) < 0)
    17           return false;
     14    protected override ValidationResult Validate() {
     15      IList<string> errors = new List<string>();
     16      bool success = true;
     17      foreach (var x in Value) {
     18        if (Minimum.CompareTo(x) > 0 && Maximum.CompareTo(x) < 0) {
     19          success = false;
     20          errors.Add($"[{Path}]: Value {x} is not between {Minimum} and {Maximum}.");
     21        }
    1822      }
    19       return true;
     23      return new ValidationResult(success, errors);
    2024    }
    2125
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/IntervalRestrictedMatrixJsonItem.cs

    r17477 r17481  
    1111    public T Minimum { get; set; }
    1212    public T Maximum { get; set; }
    13     protected override bool Validate() {
     13
     14    protected override ValidationResult Validate() {
     15      IList<string> errors = new List<string>();
     16      bool success = true;
    1417      foreach (var x in Value) {
    15         foreach(var y in x) {
    16           if (Minimum.CompareTo(y) > 0 || Maximum.CompareTo(y) < 0)
    17             return false;
     18        foreach (var y in x) {
     19          if (Minimum.CompareTo(y) > 0 || Maximum.CompareTo(y) < 0) {
     20            success = false;
     21            errors.Add($"[{Path}]: Value {y} is not between {Minimum} and {Maximum}.");
     22          }
    1823        }
    1924      }
    20       return true;
     25      return new ValidationResult(success, errors);
    2126    }
     27
    2228    public override void SetJObject(JObject jObject) {
    2329      base.SetJObject(jObject);
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/IntervalRestrictedValueJsonItem.cs

    r17477 r17481  
    1212    public T Maximum { get; set; }
    1313
    14     protected override bool Validate() => Minimum.CompareTo(Value) <= 0 && Maximum.CompareTo(Value) >= 0;
     14    protected override ValidationResult Validate() {
     15      bool res = Minimum.CompareTo(Value) <= 0 && Maximum.CompareTo(Value) >= 0;
     16      if (res) return ValidationResult.Successful();
     17      else return ValidationResult.Faulty($"[{Path}]: Value {Value} is not between {Minimum} and {Maximum}.");
     18    }
    1519
    1620    public override void SetJObject(JObject jObject) {
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/JsonItem.cs

    r17477 r17481  
    88
    99namespace HeuristicLab.JsonInterface {
     10
     11  public readonly struct ValidationResult {
     12
     13    public static ValidationResult Successful() => new ValidationResult(true, Enumerable.Empty<string>());
     14    public static ValidationResult Faulty(IEnumerable<string> errors) => new ValidationResult(false, errors);
     15    public static ValidationResult Faulty(string error) => new ValidationResult(false, error);
     16
     17    public ValidationResult(bool success, IEnumerable<string> errors) {
     18      Success = success;
     19      Errors = errors;
     20    }
     21
     22    public ValidationResult(bool success, string error) {
     23      Success = success;
     24      Errors = Enumerable.Repeat(error, 1);
     25    }
     26
     27    public bool Success { get; }
     28    public IEnumerable<string> Errors { get; }
     29  }
     30
    1031  /// <summary>
    1132  /// Main data class for json interface.
     
    2041      }
    2142
    22       public bool Validate(ref IList<IJsonItem> faultyItems) {
    23         faultyItems = new List<IJsonItem>();
    24         return ValidateHelper(Root, ref faultyItems);
    25       }
    26 
    27       // TODO: return ValidationResult ?
    28       private bool ValidateHelper(JsonItem item, ref IList<IJsonItem> faultyItems) {
    29         int hash = item.GetHashCode();
    30         if (Cache.TryGetValue(hash, out bool r))
    31           return r;
    32 
    33         bool res = item.Validate();
    34         if (!res) faultyItems.Add(item);
    35         Cache.Add(hash, res);
    36         if(item.Children != null) {
    37           foreach (var child in item.Children)
    38             if (!ValidateHelper(child as JsonItem, ref faultyItems))
    39               res = false && res;
     43      public ValidationResult Validate() {
     44        IEnumerable<string> errors = Enumerable.Empty<string>();
     45        bool success = true;
     46        foreach(var x in Root) {
     47          var res = ((JsonItem)x).Validate();
     48          //if one success is false -> whole validation is false
     49          success = success && res.Success;
     50          errors.Concat(res.Errors);
    4051        }
    41         return res;
     52        return new ValidationResult(success, errors);
    4253      }
    4354    }
     
    96107    }
    97108
    98     public virtual IJsonItemValidator GetValidator() => new JsonItemValidator(this);
     109    public IJsonItemValidator GetValidator() => new JsonItemValidator(this);
    99110
    100111    public void FixatePath() => fixedPath = Path;
     
    112123
    113124    #region Abstract Methods
    114     protected abstract bool Validate();
     125    protected abstract ValidationResult Validate();
    115126    #endregion
    116127
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/LookupJsonItem.cs

    r17477 r17481  
    1515    }
    1616
    17     protected override bool Validate() => true;
     17    protected override ValidationResult Validate() => ValidationResult.Successful();
    1818  }
    1919}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/RangedJsonItem.cs

    r17473 r17481  
    1313    public T Maximum { get; set; }
    1414
    15     protected override bool Validate() =>
    16       (Minimum.CompareTo(MinValue) <= 0 && Maximum.CompareTo(MinValue) >= 0) &&
    17       (Minimum.CompareTo(MaxValue) <= 0 && Maximum.CompareTo(MaxValue) >= 0);
     15    protected override ValidationResult Validate() {
     16      IList<string> errors = new List<string>();
     17      bool successMin = (Minimum.CompareTo(MinValue) <= 0 && Maximum.CompareTo(MinValue) >= 0);
     18      bool successMax = (Minimum.CompareTo(MaxValue) <= 0 && Maximum.CompareTo(MaxValue) >= 0);
     19      if (!successMin) errors.Add($"[{Path}]: Value {MinValue} is not between {Minimum} and {Maximum}.");
     20      if (!successMax) errors.Add($"[{Path}]: Value {MaxValue} is not between {Minimum} and {Maximum}.");
     21      return new ValidationResult((successMin && successMax), errors);
     22
     23    }
     24     
    1825  }
    1926}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/ResultJsonItem.cs

    r17473 r17481  
    77namespace HeuristicLab.JsonInterface {
    88  public class ResultJsonItem : JsonItem, IResultJsonItem {
    9     protected override bool Validate() => true;
     9    protected override ValidationResult Validate() => ValidationResult.Successful();
    1010  }
    1111}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/UnsupportedJsonItem.cs

    r17473 r17481  
    3434    }
    3535
    36     protected override bool Validate() => true;
     36    protected override ValidationResult Validate() => ValidationResult.Successful();
    3737  }
    3838}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/ValueLookupJsonItem.cs

    r17477 r17481  
    1212    public IJsonItem ActualValue { get; set; }
    1313
    14     protected override bool Validate() {
    15       if (ActualValue == null) return true;
    16       IList<IJsonItem> faultyItems = new List<IJsonItem>();
    17       return ActualValue.GetValidator().Validate(ref faultyItems);
     14    protected override ValidationResult Validate() {
     15      if (ActualValue == null) return ValidationResult.Successful();
     16      return ActualValue.GetValidator().Validate();
    1817    }
    1918
Note: See TracChangeset for help on using the changeset viewer.