Changeset 17481
- Timestamp:
- 03/17/20 17:03:24 (5 years ago)
- Location:
- branches/3026_IntegrationIntoSymSpace
- Files:
-
- 18 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/ExportJsonDialog.Designer.cs
r17451 r17481 273 273 #endregion 274 274 275 private System.Windows.Forms.GroupBox groupBox1;276 275 private System.Windows.Forms.BindingSource jsonItemBindingSource; 277 276 private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn1; -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/ExportJsonDialog.cs
r17477 r17481 69 69 // clear all runs 70 70 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) { 75 74 IList<Exception> list = new List<Exception>(); 76 75 //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)); 79 78 } 80 79 ErrorHandling.ShowErrorDialog(this, new AggregateException(list)); -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Interfaces/IJsonItemValidator.cs
r17406 r17481 7 7 namespace HeuristicLab.JsonInterface { 8 8 public interface IJsonItemValidator { 9 bool Validate(ref IList<IJsonItem> faultyItems);9 ValidationResult Validate(); 10 10 } 11 11 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonTemplateInstantiator.cs
r17477 r17481 14 14 15 15 namespace 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; } 19 24 } 20 25 … … 44 49 #region Helper 45 50 private InstantiatorResult ExecuteInstantiaton(string templateFile, string configFile = null) { 46 InstantiatorResult result = new InstantiatorResult();47 51 48 52 #region Parse Files … … 58 62 ProtoBufSerializer serializer = new ProtoBufSerializer(); 59 63 IOptimizer optimizer = (IOptimizer)serializer.Deserialize(hLFileLocation); 60 result.Optimizer = optimizer;61 64 #endregion 62 65 … … 73 76 JsonItemConverter.Inject(optimizer, rootItem); 74 77 75 result.ConfiguredResultItems = CollectResults(); 76 77 return result; 78 return new InstantiatorResult(optimizer, CollectResults()); 78 79 } 79 80 -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/BoolJsonItems.cs
r17473 r17481 7 7 namespace HeuristicLab.JsonInterface { 8 8 public class BoolJsonItem : ValueJsonItem<bool> { 9 protected override bool Validate() => true;9 protected override ValidationResult Validate() => ValidationResult.Successful(); 10 10 } 11 11 12 12 public class BoolArrayJsonItem : ArrayJsonItem<bool> { 13 protected override bool Validate() => true;13 protected override ValidationResult Validate() => ValidationResult.Successful(); 14 14 } 15 15 16 16 public class BoolMatrixJsonItem : MatrixJsonItem<bool> { 17 protected override bool Validate() => true;17 protected override ValidationResult Validate() => ValidationResult.Successful(); 18 18 } 19 19 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/ConcreteRestrictedArrayJsonItem.cs
r17473 r17481 9 9 public IEnumerable<T> ConcreteRestrictedItems { get; set; } 10 10 11 protected override boolValidate() {11 protected override ValidationResult Validate() { 12 12 bool res = true; 13 IList<string> errors = new List<string>(); 13 14 foreach(var x in Value) { 14 15 bool tmp = false; … … 16 17 tmp = tmp || x.Equals(restrictedItem); //if one tmp is true, it stays true (match found) 17 18 } 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()) }'."); 18 22 res = res && tmp; //if one tmp is false, res will set false 19 23 } 20 return res; 24 if (res) 25 return ValidationResult.Successful(); 26 else 27 return ValidationResult.Faulty(errors); 21 28 } 22 29 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/ConcreteRestrictedValueJsonItem.cs
r17473 r17481 9 9 public IEnumerable<T> ConcreteRestrictedItems { get; set; } 10 10 11 protected override boolValidate() {12 if (ConcreteRestrictedItems == null) return true;11 protected override ValidationResult Validate() { 12 if (ConcreteRestrictedItems == null) return ValidationResult.Successful(); 13 13 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()) }'."); 16 18 } 17 19 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/DateTimeJsonItem.cs
r17477 r17481 8 8 namespace HeuristicLab.JsonInterface { 9 9 public class DateTimeJsonItem : IntervalRestrictedValueJsonItem<DateTime> { 10 protected override bool Validate() => Minimum.CompareTo(Value) >= 0 && Maximum.CompareTo(Value) <= 0;11 10 12 11 public override JObject GenerateJObject() { -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/EmptyJsonItem.cs
r17473 r17481 7 7 namespace HeuristicLab.JsonInterface { 8 8 public class EmptyJsonItem : JsonItem { 9 protected override bool Validate() => true;9 protected override ValidationResult Validate() => ValidationResult.Successful(); 10 10 } 11 11 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/IntervalRestrictedArrayJsonItem.cs
r17477 r17481 12 12 public T Maximum { get; set; } 13 13 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 } 18 22 } 19 return true;23 return new ValidationResult(success, errors); 20 24 } 21 25 -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/IntervalRestrictedMatrixJsonItem.cs
r17477 r17481 11 11 public T Minimum { get; set; } 12 12 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; 14 17 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 } 18 23 } 19 24 } 20 return true;25 return new ValidationResult(success, errors); 21 26 } 27 22 28 public override void SetJObject(JObject jObject) { 23 29 base.SetJObject(jObject); -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/IntervalRestrictedValueJsonItem.cs
r17477 r17481 12 12 public T Maximum { get; set; } 13 13 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 } 15 19 16 20 public override void SetJObject(JObject jObject) { -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/JsonItem.cs
r17477 r17481 8 8 9 9 namespace 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 10 31 /// <summary> 11 32 /// Main data class for json interface. … … 20 41 } 21 42 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); 40 51 } 41 return res;52 return new ValidationResult(success, errors); 42 53 } 43 54 } … … 96 107 } 97 108 98 public virtualIJsonItemValidator GetValidator() => new JsonItemValidator(this);109 public IJsonItemValidator GetValidator() => new JsonItemValidator(this); 99 110 100 111 public void FixatePath() => fixedPath = Path; … … 112 123 113 124 #region Abstract Methods 114 protected abstract boolValidate();125 protected abstract ValidationResult Validate(); 115 126 #endregion 116 127 -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/LookupJsonItem.cs
r17477 r17481 15 15 } 16 16 17 protected override bool Validate() => true;17 protected override ValidationResult Validate() => ValidationResult.Successful(); 18 18 } 19 19 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/RangedJsonItem.cs
r17473 r17481 13 13 public T Maximum { get; set; } 14 14 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 18 25 } 19 26 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/ResultJsonItem.cs
r17473 r17481 7 7 namespace HeuristicLab.JsonInterface { 8 8 public class ResultJsonItem : JsonItem, IResultJsonItem { 9 protected override bool Validate() => true;9 protected override ValidationResult Validate() => ValidationResult.Successful(); 10 10 } 11 11 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/UnsupportedJsonItem.cs
r17473 r17481 34 34 } 35 35 36 protected override bool Validate() => true;36 protected override ValidationResult Validate() => ValidationResult.Successful(); 37 37 } 38 38 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/ValueLookupJsonItem.cs
r17477 r17481 12 12 public IJsonItem ActualValue { get; set; } 13 13 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(); 18 17 } 19 18
Note: See TracChangeset
for help on using the changeset viewer.