Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026:

  • renamed ResultItem to ResultJsonItem
  • implemented RegressionProblemDataConverter
  • added support for "named matrices" (named = rows and columns have names) -> INamedMatrixJsonItem incl. base classes and views
  • added a bool property Active in IJsonItem -> marks items, which should be written into the template
File size: 5.0 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 eigene items für LookUp?
69    public virtual string ActualName { get; set; }
70
71    // TODO jsonIgnore dataType?
72
73    [JsonIgnore]
74    public virtual IList<IJsonItem> Children { get; protected set; }
75
76    [JsonIgnore]
77    public virtual IJsonItem Parent { get; set; }
78
79    [JsonIgnore]
80    public virtual bool Active { get; set; }
81
82    #region Constructors
83    public JsonItem() { }
84
85    public JsonItem(IEnumerable<IJsonItem> childs) {
86      AddChildren(childs);
87    }
88    #endregion
89   
90    #region Public Methods
91    public void AddChildren(params IJsonItem[] childs) =>
92      AddChildren(childs as IEnumerable<IJsonItem>);
93
94    public void AddChildren(IEnumerable<IJsonItem> childs) {
95      if (childs == null) return;
96      if (Children == null)
97        Children = new List<IJsonItem>();
98      foreach (var child in childs) {
99        Children.Add(child);
100        child.Parent = this;
101      }
102    }
103
104    public virtual IJsonItemValidator GetValidator() => new JsonItemValidator(this);
105
106    public void FixatePath() => fixedPath = Path;
107    public void LoosenPath() => fixedPath = "";
108
109    public virtual void SetFromJObject(JObject jObject) {
110      Value = jObject[nameof(IJsonItem.Value)]?.ToObject<object>();
111      Range = jObject[nameof(IJsonItem.Range)]?.ToObject<object[]>();
112      ActualName = jObject[nameof(IJsonItem.ActualName)]?.ToString();
113    }
114    #endregion
115
116    #region Helper
117    /*
118     * TODO protected abstract bool Validate();
119     */
120
121    protected virtual bool IsInRange() {
122      bool b1 = true, b2 = true;
123      if (Value is IEnumerable && !(Value is string)) {
124        foreach (var x in (IEnumerable)Value) {
125          b1 = b1 ? IsInRangeList(x) : b1;
126          b2 = b2 ? IsInNumericRange(x) : b2;
127        }
128      }
129      else {
130        b1 = IsInRangeList(Value);
131        b2 = IsInNumericRange(Value);
132      }
133      return b1 || b2;
134    }
135
136    protected bool IsInRangeList(object value) {
137      foreach (var x in Range)
138        if (x.Equals(value)) return true;
139      return false;
140    }
141
142    protected bool IsInNumericRange(object value) =>
143      IsInNumericRange<ulong>(value)
144      || IsInNumericRange<uint>(value)
145      || IsInNumericRange<ushort>(value)
146      || IsInNumericRange<long>(value)
147      || IsInNumericRange<int>(value)
148      || IsInNumericRange<short>(value)
149      || IsInNumericRange<byte>(value)
150      || IsInNumericRange<float>(value)
151      || IsInNumericRange<double>(value)
152      || (value is float && float.IsNaN((float)value))
153      || (value is double && double.IsNaN((double)value));
154
155    protected bool IsInNumericRange<T>(object value) where T : IComparable {
156      object min = Range.First(), max = Range.Last();
157      return
158        value != null && min != null && max != null && value is T && min is T && max is T &&
159        (((T)min).CompareTo(value) == -1 || ((T)min).CompareTo(value) == 0) &&
160        (((T)max).CompareTo(value) == 1 || ((T)max).CompareTo(value) == 0);
161    }
162    #endregion
163  }
164}
Note: See TracBrowser for help on using the repository browser.