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