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

Last change on this file since 18055 was 18055, checked in by dpiringe, 11 months ago

#3026

  • added StorableTypeAttribute and StorableConstructorAttribute to all JsonItems
  • added a new JsonItem ListJsonItem + Interfaces IListJsonItem
  • renamed SymRegPythonProcessor to RunCollectionSRSolutionPythonFormatter
  • removed Interface IResultCollectionProcessor -> using the interface IRunCollectionModifier now (has aleady implementations)
  • renamed all related variables/fields/properties with a connection to ResultCollectionProcessor
  • added new implementations for IRunCollectionModifier
File size: 4.4 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Linq;
5using System.Text;
6using Newtonsoft.Json;
7using Newtonsoft.Json.Linq;
8using HEAL.Attic;
9
10namespace HeuristicLab.JsonInterface {
11
12  public readonly struct ValidationResult {
13
14    public static ValidationResult Successful() => new ValidationResult(true, Enumerable.Empty<string>());
15    public static ValidationResult Faulty(IEnumerable<string> errors) => new ValidationResult(false, errors);
16    public static ValidationResult Faulty(string error) => new ValidationResult(false, error);
17
18    public ValidationResult(bool success, IEnumerable<string> errors) {
19      Success = success;
20      Errors = errors;
21    }
22
23    public ValidationResult(bool success, string error) {
24      Success = success;
25      Errors = Enumerable.Repeat(error, 1);
26    }
27
28    public bool Success { get; }
29    public IEnumerable<string> Errors { get; }
30
31    public Exception GenerateException() =>
32      new AggregateException(Errors.Select(x => new ArgumentException(x)));
33  }
34
35  [StorableType("B1270D98-B0D9-40FB-B089-F6E70C65CD65")]
36  /// <summary>
37  /// Main data class for json interface.
38  /// </summary>
39  public abstract class JsonItem : IJsonItem {
40
41    public class JsonItemValidator : IJsonItemValidator {
42      //private IDictionary<int, bool> Cache = new Dictionary<int, bool>();
43      private JsonItem Root { get; set; }
44      public JsonItemValidator(JsonItem root) {
45        Root = root;
46      }
47
48      public ValidationResult Validate() {
49        List<string> errors = new List<string>();
50        bool success = true;
51        foreach (var x in Root) {
52          JsonItem item = x as JsonItem;
53          if (item.Active) {
54            var res = ((JsonItem)x).Validate();
55            //if one success is false -> whole validation is false
56            success = success && res.Success;
57            errors.AddRange(res.Errors);
58          }
59        }
60        return new ValidationResult(success, errors);
61      }
62    }
63
64    public virtual string Name { get; set; }
65
66    public virtual string Description { get; set; }
67
68    public virtual string Path {
69      get {
70        IJsonItem tmp = Parent;
71        StringBuilder builder = new StringBuilder(this.Name);
72        while (tmp != null) {
73          builder.Insert(0, tmp.Name + ".");
74          tmp = tmp.Parent;
75        }
76        return builder.ToString();
77      }
78    }
79
80    [JsonIgnore]
81    public virtual IEnumerable<IJsonItem> Children { get; protected set; }
82
83    [JsonIgnore]
84    public virtual IJsonItem Parent { get; set; }
85
86    [JsonIgnore]
87    public virtual bool Active { get; set; }
88
89    #region Constructors
90    [StorableConstructor]
91    protected JsonItem(StorableConstructorFlag _) { }
92
93    public JsonItem() { }
94
95    public JsonItem(IEnumerable<IJsonItem> childs) {
96      AddChildren(childs);
97    }
98    #endregion
99
100    #region Public Methods
101    public void AddChildren(params IJsonItem[] childs) =>
102      AddChildren(childs as IEnumerable<IJsonItem>);
103
104    public void AddChildren(IEnumerable<IJsonItem> childs) {
105      if (childs == null) return;
106      if (Children == null)
107        Children = new List<IJsonItem>();
108      if (Children is IList<IJsonItem> list) {
109        foreach (var child in childs) {
110          list.Add(child);
111          child.Parent = this;
112        }
113      }
114    }
115
116    public IJsonItemValidator GetValidator() => new JsonItemValidator(this);
117
118    public virtual JObject GenerateJObject() =>
119      JObject.FromObject(this, new JsonSerializer() {
120        TypeNameHandling = TypeNameHandling.None,
121        NullValueHandling = NullValueHandling.Ignore,
122        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
123      });
124
125    public virtual void SetJObject(JObject jObject) {
126      Name = (jObject[nameof(IJsonItem.Name)]?.ToObject<string>());
127      Description = (jObject[nameof(IJsonItem.Description)]?.ToObject<string>());
128    }
129    #endregion
130
131    #region Abstract Methods
132    protected abstract ValidationResult Validate();
133    #endregion
134
135    #region IEnumerable Support
136    public virtual IEnumerator<IJsonItem> GetEnumerator() {
137      yield return this;
138
139      if (Children != null) {
140        foreach (var x in Children) {
141          foreach (var c in x) {
142            yield return c;
143          }
144        }
145      }
146    }
147
148    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
149    #endregion
150  }
151}
Note: See TracBrowser for help on using the repository browser.