Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/JsonItem.cs @ 17530

Last change on this file since 17530 was 17519, checked in by dpiringe, 5 years ago

#3026:

  • added error output for failed runner initialization
  • reorganised some final view models
  • TargetedJsonItemType (in JsonItemVMBase) now automatically returns the type of the defined JsonItem
  • code cleanup
  • refactored RegressionProblemDataConverter
  • added lots of comments
  • added new view for StringArrayJsonItem
  • added new UI component for concrete restricted items and used it in JsonItemConcreteItemArrayControl and JsonItemValidValuesControl
File size: 4.2 KB
Line 
1using System.Collections;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Newtonsoft.Json;
6using Newtonsoft.Json.Linq;
7
8namespace HeuristicLab.JsonInterface {
9
10  public readonly struct ValidationResult {
11
12    public static ValidationResult Successful() => new ValidationResult(true, Enumerable.Empty<string>());
13    public static ValidationResult Faulty(IEnumerable<string> errors) => new ValidationResult(false, errors);
14    public static ValidationResult Faulty(string error) => new ValidationResult(false, error);
15
16    public ValidationResult(bool success, IEnumerable<string> errors) {
17      Success = success;
18      Errors = errors;
19    }
20
21    public ValidationResult(bool success, string error) {
22      Success = success;
23      Errors = Enumerable.Repeat(error, 1);
24    }
25
26    public bool Success { get; }
27    public IEnumerable<string> Errors { get; }
28  }
29
30  /// <summary>
31  /// Main data class for json interface.
32  /// </summary>
33  public abstract class JsonItem : IJsonItem {
34
35    public class JsonItemValidator : IJsonItemValidator {
36      private IDictionary<int, bool> Cache = new Dictionary<int, bool>();
37      private JsonItem Root { get; set; }
38      public JsonItemValidator(JsonItem root) {
39        Root = root;
40      }
41
42      public ValidationResult Validate() {
43        List<string> errors = new List<string>();
44        bool success = true;
45        foreach(var x in Root) {
46          JsonItem item = x as JsonItem;
47          if(item.Active) {
48            var res = ((JsonItem)x).Validate();
49            //if one success is false -> whole validation is false
50            success = success && res.Success;
51            errors.AddRange(res.Errors);
52          }
53        }
54        return new ValidationResult(success, errors);
55      }
56    }
57
58    public virtual string Name { get; set; }
59
60    public virtual string Description { get; set; }
61
62    private string fixedPath = "";
63    public virtual string Path {
64      get {
65        if (!string.IsNullOrWhiteSpace(fixedPath))
66          return fixedPath;
67
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    // TODO jsonIgnore dataType?
79    [JsonIgnore]
80    public virtual IEnumerable<IJsonItem> Children { get; protected set; }
81
82    [JsonIgnore]
83    public virtual IJsonItem Parent { get; set; }
84
85    [JsonIgnore]
86    public virtual bool Active { get; set; }
87
88    #region Constructors
89    public JsonItem() { }
90
91    public JsonItem(IEnumerable<IJsonItem> childs) {
92      AddChildren(childs);
93    }
94    #endregion
95   
96    #region Public Methods
97    public void AddChildren(params IJsonItem[] childs) =>
98      AddChildren(childs as IEnumerable<IJsonItem>);
99
100    public void AddChildren(IEnumerable<IJsonItem> childs) {
101      if (childs == null) return;
102      if (Children == null)
103        Children = new List<IJsonItem>();
104      if(Children is IList<IJsonItem> list) {
105        foreach (var child in childs) {
106          list.Add(child);
107          child.Parent = this;
108        }
109      }
110    }
111
112    public IJsonItemValidator GetValidator() => new JsonItemValidator(this);
113
114    public void FixatePath() => fixedPath = Path;
115    public void LoosenPath() => fixedPath = "";
116
117    public virtual JObject GenerateJObject() =>
118      JObject.FromObject(this, new JsonSerializer() {
119        TypeNameHandling = TypeNameHandling.None,
120        NullValueHandling = NullValueHandling.Ignore,
121        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
122      });
123
124    public virtual void SetJObject(JObject jObject) { }
125    #endregion
126
127    #region Abstract Methods
128    protected abstract ValidationResult Validate();
129    #endregion
130
131    #region IEnumerable Support
132    public virtual IEnumerator<IJsonItem> GetEnumerator() {
133      yield return this;
134     
135      if (Children != null) {
136        foreach (var x in Children) {
137          foreach (var c in x) {
138            yield return c;
139          }
140        }
141      }
142    }
143
144    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
145    #endregion
146  }
147}
Note: See TracBrowser for help on using the repository browser.