Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonItems/JsonItem.cs @ 17829

Last change on this file since 17829 was 17828, checked in by dpiringe, 3 years ago

#3026

  • removed the option to set the value for JsonItems via exporter
    • reworked some base controls
    • added new controls for JsonItem specific properties (e.g. ArrayResizable)
    • deleted a lot of obsolet controls
  • removed the Enable checkbox in the detail view of JsonItems
  • exporter now clones the IOptimizer object
  • added a check + message for unsupported exports
  • list of JsonItems now includes unsupported JsonItems (disabled and marked with 'unsupported')
  • refactored the converter type check
    • now every converter has to specify its supported type(s)
File size: 4.3 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Linq;
5using System.Text;
6using Newtonsoft.Json;
7using Newtonsoft.Json.Linq;
8
9namespace 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    public Exception GenerateException() =>
31      new AggregateException(Errors.Select(x => new ArgumentException(x)));
32  }
33
34  /// <summary>
35  /// Main data class for json interface.
36  /// </summary>
37  public abstract class JsonItem : IJsonItem {
38
39    public class JsonItemValidator : IJsonItemValidator {
40      private IDictionary<int, bool> Cache = new Dictionary<int, bool>();
41      private JsonItem Root { get; set; }
42      public JsonItemValidator(JsonItem root) {
43        Root = root;
44      }
45
46      public ValidationResult Validate() {
47        List<string> errors = new List<string>();
48        bool success = true;
49        foreach(var x in Root) {
50          JsonItem item = x as JsonItem;
51          if(item.Active) {
52            var res = ((JsonItem)x).Validate();
53            //if one success is false -> whole validation is false
54            success = success && res.Success;
55            errors.AddRange(res.Errors);
56          }
57        }
58        return new ValidationResult(success, errors);
59      }
60    }
61
62    public virtual string Name { get; set; }
63
64    public virtual string Description { get; set; }
65
66    private string fixedPath = "";
67    public virtual string Path {
68      get {
69        if (!string.IsNullOrWhiteSpace(fixedPath))
70          return fixedPath;
71
72        IJsonItem tmp = Parent;
73        StringBuilder builder = new StringBuilder(this.Name);
74        while(tmp != null) {
75          builder.Insert(0, tmp.Name + ".");
76          tmp = tmp.Parent;
77        }
78        return builder.ToString();
79      }
80    }
81
82    [JsonIgnore]
83    public virtual IEnumerable<IJsonItem> Children { get; protected set; }
84
85    [JsonIgnore]
86    public virtual IJsonItem Parent { get; set; }
87
88    [JsonIgnore]
89    public virtual bool Active { get; set; }
90
91    #region Constructors
92    public JsonItem() { }
93
94    public JsonItem(IEnumerable<IJsonItem> childs) {
95      AddChildren(childs);
96    }
97    #endregion
98   
99    #region Public Methods
100    public void AddChildren(params IJsonItem[] childs) =>
101      AddChildren(childs as IEnumerable<IJsonItem>);
102
103    public void AddChildren(IEnumerable<IJsonItem> childs) {
104      if (childs == null) return;
105      if (Children == null)
106        Children = new List<IJsonItem>();
107      if(Children is IList<IJsonItem> list) {
108        foreach (var child in childs) {
109          list.Add(child);
110          child.Parent = this;
111        }
112      }
113    }
114
115    public IJsonItemValidator GetValidator() => new JsonItemValidator(this);
116
117    public void FixatePath() => fixedPath = Path;
118    public void LoosenPath() => fixedPath = "";
119
120    public virtual JObject GenerateJObject() =>
121      JObject.FromObject(this, new JsonSerializer() {
122        TypeNameHandling = TypeNameHandling.None,
123        NullValueHandling = NullValueHandling.Ignore,
124        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
125      });
126
127    public virtual void SetJObject(JObject jObject) { }
128    #endregion
129
130    #region Abstract Methods
131    protected abstract ValidationResult Validate();
132    #endregion
133
134    #region IEnumerable Support
135    public virtual IEnumerator<IJsonItem> GetEnumerator() {
136      yield return this;
137     
138      if (Children != null) {
139        foreach (var x in Children) {
140          foreach (var c in x) {
141            yield return c;
142          }
143        }
144      }
145    }
146
147    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
148    #endregion
149  }
150}
Note: See TracBrowser for help on using the repository browser.