Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17471 was 17471, checked in by dpiringe, 4 years ago

#3026:

  • deleted INamedMatrixJsonItem and all corresponding classes/views, because of bad design
  • added ILookupJsonItem and IValueLookupJsonItem (incl. all corresponding implementations, VMs, Views)
  • added IResultJsonItem
  • changed type of property Control from JsonItemBaseControl to UserControl in IJsonItemVM (because the details control now builds up with linked user controls -> allows better construction of dynamic controls)
  • added all properties of INamedMatrixJsonItem in IMatrixJsonItem
  • refactored a lot of views for better usage (TableLayoutPanel is used a lot now -> for better item positioning)
  • property ActualName is now located in ILookupJsonItem instead of IJsonItem
File size: 4.8 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  /// <summary>
11  /// Main data class for json interface.
12  /// </summary>
13  public class JsonItem : IJsonItem {
14
15    public class JsonItemValidator : IJsonItemValidator {
16      private IDictionary<int, bool> Cache = new Dictionary<int, bool>();
17      private JsonItem Root { get; set; }
18      public JsonItemValidator(JsonItem root) {
19        Root = root;
20      }
21
22      public bool Validate(ref IList<IJsonItem> faultyItems) {
23        faultyItems = new List<IJsonItem>();
24        return ValidateHelper(Root, ref faultyItems);
25      }
26
27      private bool ValidateHelper(JsonItem item, ref IList<IJsonItem> faultyItems) {
28        int hash = item.GetHashCode();
29        if (Cache.TryGetValue(hash, out bool r))
30          return r;
31
32        bool res = true;
33        if (item.Value != null && item.Range != null)
34          res = item.IsInRange();
35        if (!res) faultyItems.Add(item);
36        Cache.Add(hash, res);
37        if(item.Children != null)
38          foreach (var child in item.Children)
39            res = res && ValidateHelper(child as JsonItem, ref faultyItems);
40        return res;
41      }
42    }
43
44    public virtual string Name { get; set; }
45
46    public virtual string Description { get; set; }
47
48    private string fixedPath = "";
49    public virtual string Path {
50      get {
51        if (!string.IsNullOrWhiteSpace(fixedPath))
52          return fixedPath;
53
54        IJsonItem tmp = Parent;
55        StringBuilder builder = new StringBuilder(this.Name);
56        while(tmp != null) {
57          builder.Insert(0, tmp.Name + ".");
58          tmp = tmp.Parent;
59        }
60        return builder.ToString();
61      }
62    }
63
64    public virtual object Value { get; set; }
65
66    public virtual IEnumerable<object> Range { get; set; }
67   
68    // TODO jsonIgnore dataType?
69
70    [JsonIgnore]
71    public virtual IList<IJsonItem> Children { get; protected set; }
72
73    [JsonIgnore]
74    public virtual IJsonItem Parent { get; set; }
75
76    [JsonIgnore]
77    public virtual bool Active { get; set; }
78
79    #region Constructors
80    public JsonItem() { }
81
82    public JsonItem(IEnumerable<IJsonItem> childs) {
83      AddChildren(childs);
84    }
85    #endregion
86   
87    #region Public Methods
88    public void AddChildren(params IJsonItem[] childs) =>
89      AddChildren(childs as IEnumerable<IJsonItem>);
90
91    public void AddChildren(IEnumerable<IJsonItem> childs) {
92      if (childs == null) return;
93      if (Children == null)
94        Children = new List<IJsonItem>();
95      foreach (var child in childs) {
96        Children.Add(child);
97        child.Parent = this;
98      }
99    }
100
101    public virtual IJsonItemValidator GetValidator() => new JsonItemValidator(this);
102
103    public void FixatePath() => fixedPath = Path;
104    public void LoosenPath() => fixedPath = "";
105
106    public virtual void SetFromJObject(JObject jObject) {
107      Value = jObject[nameof(IJsonItem.Value)]?.ToObject<object>();
108      Range = jObject[nameof(IJsonItem.Range)]?.ToObject<object[]>();
109    }
110    #endregion
111
112    #region Helper
113    /*
114     * TODO protected abstract bool Validate();
115     */
116     
117    protected virtual bool IsInRange() {
118      bool b1 = true, b2 = true;
119      if (Value is IEnumerable && !(Value is string)) {
120        foreach (var x in (IEnumerable)Value) {
121          b1 = b1 ? IsInRangeList(x) : b1;
122          b2 = b2 ? IsInNumericRange(x) : b2;
123        }
124      }
125      else {
126        b1 = IsInRangeList(Value);
127        b2 = IsInNumericRange(Value);
128      }
129      return b1 || b2;
130    }
131
132    protected bool IsInRangeList(object value) {
133      foreach (var x in Range)
134        if (x.Equals(value)) return true;
135      return false;
136    }
137
138    protected bool IsInNumericRange(object value) =>
139      IsInNumericRange<ulong>(value)
140      || IsInNumericRange<uint>(value)
141      || IsInNumericRange<ushort>(value)
142      || IsInNumericRange<long>(value)
143      || IsInNumericRange<int>(value)
144      || IsInNumericRange<short>(value)
145      || IsInNumericRange<byte>(value)
146      || IsInNumericRange<float>(value)
147      || IsInNumericRange<double>(value)
148      || (value is float && float.IsNaN((float)value))
149      || (value is double && double.IsNaN((double)value));
150
151    protected bool IsInNumericRange<T>(object value) where T : IComparable {
152      object min = Range.First(), max = Range.Last();
153      return
154        value != null && min != null && max != null && value is T && min is T && max is T &&
155        (((T)min).CompareTo(value) == -1 || ((T)min).CompareTo(value) == 0) &&
156        (((T)max).CompareTo(value) == 1 || ((T)max).CompareTo(value) == 0);
157    }
158    #endregion
159  }
160}
Note: See TracBrowser for help on using the repository browser.