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

Last change on this file since 17834 was 17834, checked in by dpiringe, 2 months ago

#3026

  • added ResultFormatter to add an extra layer of result transformation logic (converting a result value to a string with a defined logic, e.g. MatlabResultFormatter for ISymbolicRegressionSolution)
  • extended the IResultJsonItem with two properties for result formatting
  • added a new control to selected a result formatter for a result value
  • refactored the Runner for the new result formatting process
File size: 4.5 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      Name = (jObject[nameof(IJsonItem.Name)]?.ToObject<string>());
129      Description = (jObject[nameof(IJsonItem.Description)]?.ToObject<string>());
130    }
131    #endregion
132
133    #region Abstract Methods
134    protected abstract ValidationResult Validate();
135    #endregion
136
137    #region IEnumerable Support
138    public virtual IEnumerator<IJsonItem> GetEnumerator() {
139      yield return this;
140     
141      if (Children != null) {
142        foreach (var x in Children) {
143          foreach (var c in x) {
144            yield return c;
145          }
146        }
147      }
148    }
149
150    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
151    #endregion
152  }
153}
Note: See TracBrowser for help on using the repository browser.