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

Last change on this file since 17843 was 17843, checked in by dpiringe, 7 weeks ago

#3026

  • removed property ConvertableType from all converters
  • removed the option to fixate or loosen the path of JsonItems (obsolete)
  • added a abstract formatter SymbolicRegressionSolutionFormatterBase as base formatter for ISymbolicRegressionSolution
  • unified the construction of exporter controls
  • code cleanup
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    public virtual string Path {
67      get {
68        IJsonItem tmp = Parent;
69        StringBuilder builder = new StringBuilder(this.Name);
70        while(tmp != null) {
71          builder.Insert(0, tmp.Name + ".");
72          tmp = tmp.Parent;
73        }
74        return builder.ToString();
75      }
76    }
77
78    [JsonIgnore]
79    public virtual IEnumerable<IJsonItem> Children { get; protected set; }
80
81    [JsonIgnore]
82    public virtual IJsonItem Parent { get; set; }
83
84    [JsonIgnore]
85    public virtual bool Active { get; set; }
86
87    #region Constructors
88    public JsonItem() { }
89
90    public JsonItem(IEnumerable<IJsonItem> childs) {
91      AddChildren(childs);
92    }
93    #endregion
94   
95    #region Public Methods
96    public void AddChildren(params IJsonItem[] childs) =>
97      AddChildren(childs as IEnumerable<IJsonItem>);
98
99    public void AddChildren(IEnumerable<IJsonItem> childs) {
100      if (childs == null) return;
101      if (Children == null)
102        Children = new List<IJsonItem>();
103      if(Children is IList<IJsonItem> list) {
104        foreach (var child in childs) {
105          list.Add(child);
106          child.Parent = this;
107        }
108      }
109    }
110
111    public IJsonItemValidator GetValidator() => new JsonItemValidator(this);
112
113    public virtual JObject GenerateJObject() =>
114      JObject.FromObject(this, new JsonSerializer() {
115        TypeNameHandling = TypeNameHandling.None,
116        NullValueHandling = NullValueHandling.Ignore,
117        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
118      });
119
120    public virtual void SetJObject(JObject jObject) {
121      Name = (jObject[nameof(IJsonItem.Name)]?.ToObject<string>());
122      Description = (jObject[nameof(IJsonItem.Description)]?.ToObject<string>());
123    }
124    #endregion
125
126    #region Abstract Methods
127    protected abstract ValidationResult Validate();
128    #endregion
129
130    #region IEnumerable Support
131    public virtual IEnumerator<IJsonItem> GetEnumerator() {
132      yield return this;
133     
134      if (Children != null) {
135        foreach (var x in Children) {
136          foreach (var c in x) {
137            yield return c;
138          }
139        }
140      }
141    }
142
143    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
144    #endregion
145  }
146}
Note: See TracBrowser for help on using the repository browser.