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

Last change on this file since 17284 was 17284, checked in by dpiringe, 3 years ago

#3026: renamed namespace and project from HeuristicLab.Manufacture to HeuristicLab.JsonInterface

File size: 3.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Core;
7using Newtonsoft.Json;
8using Newtonsoft.Json.Linq;
9
10namespace HeuristicLab.JsonInterface {
11  public class JsonItem {
12    private IList<object> range;
13    private object defaultValue;
14
15    public string Name { get; set; }
16    public string Type { get; set; }
17    public object Default {
18      get => defaultValue;
19      set {
20        defaultValue = value;
21        if(Range != null && value != null && !FulfillConstraints())
22          throw new ArgumentOutOfRangeException("Default", "Default is not in range.");
23      }
24    }
25    public string Path { get; set; } = "";
26
27    public IList<object> Range {
28      get => range;
29      set {
30        range = value;
31        if (Default != null && value != null && !FulfillConstraints())
32          throw new ArgumentOutOfRangeException("Default", "Default is not in range.");
33      }
34    }
35
36    public IList<JsonItem> Parameters { get; set; }
37    public IList<JsonItem> Operators { get; set; }
38   
39    public override bool Equals(object obj) =>
40      (obj is JsonItem ? (obj.Cast<JsonItem>().Name == this.Name) : false);
41     
42    public override int GetHashCode() => Name.GetHashCode();
43
44    [JsonIgnore]
45    public JsonItem Reference { get; set; }
46   
47    public static void Merge(JsonItem target, JsonItem from) {
48      target.Name = from.Name ?? target.Name;
49      target.Type = from.Type ?? target.Type;
50      target.Range = from.Range ?? target.Range;
51      target.Path = from.Path ?? target.Path;
52      target.Default = from.Default ?? target.Default;
53      target.Reference = from.Reference ?? target.Reference;
54      target.Parameters = from.Parameters ?? target.Parameters;
55      target.Operators = from.Operators ?? target.Operators;
56    }
57
58    public bool FulfillConstraints() => FulfillConstraints(this);
59
60    public static bool FulfillConstraints(JsonItem data) =>
61      data.Range != null && data.Default != null && (
62      IsInRangeList(data.Range, data.Default) ||
63      IsInNumericRange<long>(data.Default, data.Range[0], data.Range[1]) ||
64      IsInNumericRange<int>(data.Default, data.Range[0], data.Range[1]) ||
65      IsInNumericRange<short>(data.Default, data.Range[0], data.Range[1]) ||
66      IsInNumericRange<byte>(data.Default, data.Range[0], data.Range[1]) ||
67      IsInNumericRange<float>(data.Default, data.Range[0], data.Range[1]) ||
68      IsInNumericRange<double>(data.Default, data.Range[0], data.Range[1]));
69
70
71
72    public void UpdatePaths() {
73      if (Parameters != null) {
74        foreach (var p in Parameters) {
75          p.Path = Path + "." + p.Name;
76        }
77      }
78
79      if (Operators != null) {
80        foreach (var p in Operators) {
81          p.Path = Path + "." + p.Name;
82        }
83      }
84    }
85
86    public void PrependPath(string str) {
87      Path = $"{str}.{Path}";
88      PrependPathHelper(Parameters, str);
89      PrependPathHelper(Operators, str);
90    }
91
92    private void PrependPathHelper(IEnumerable<JsonItem> items, string str) {
93      if (items != null) {
94        foreach (var p in items)
95          p.PrependPath(str);
96      }
97    }
98
99    #region Helper
100    private static bool IsInRangeList(IEnumerable<object> list, object value) {
101      foreach (var x in list)
102        if (x.Equals(value)) return true;
103      return false;
104    }
105
106    private static bool IsInNumericRange<T>(object value, object min, object max) where T : IComparable =>
107      (value != null && min != null && max != null && value is T && min is T && max is T &&
108        (((T)min).CompareTo(value) == -1 || ((T)min).CompareTo(value) == 0) &&
109        (((T)max).CompareTo(value) == 1 || ((T)max).CompareTo(value) == 0));
110    #endregion
111  }
112}
Note: See TracBrowser for help on using the repository browser.