[17263] | 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;
|
---|
[17275] | 8 | using Newtonsoft.Json.Linq;
|
---|
[17263] | 9 |
|
---|
[17284] | 10 | namespace HeuristicLab.JsonInterface {
|
---|
[17283] | 11 | public class JsonItem {
|
---|
[17349] | 12 |
|
---|
| 13 | #region Private Fields
|
---|
| 14 | private string name;
|
---|
| 15 | private object value;
|
---|
| 16 | private IList<object> range;
|
---|
| 17 | #endregion
|
---|
[17339] | 18 |
|
---|
| 19 | public string Name {
|
---|
| 20 | get => name;
|
---|
| 21 | set {
|
---|
| 22 | name = value;
|
---|
| 23 | Path = Name;
|
---|
| 24 | UpdatePath();
|
---|
| 25 | }
|
---|
| 26 | }
|
---|
[17263] | 27 | public string Type { get; set; }
|
---|
[17324] | 28 | public string Path { get; set; }
|
---|
[17322] | 29 | public IList<JsonItem> Parameters { get; set; }
|
---|
| 30 | public IList<JsonItem> Operators { get; set; }
|
---|
[17342] | 31 | public object Value {
|
---|
| 32 | get => value;
|
---|
[17275] | 33 | set {
|
---|
[17342] | 34 | this.value = value;
|
---|
[17349] | 35 | CheckConstraints();
|
---|
[17275] | 36 | }
|
---|
| 37 | }
|
---|
| 38 | public IList<object> Range {
|
---|
| 39 | get => range;
|
---|
[17269] | 40 | set {
|
---|
[17275] | 41 | range = value;
|
---|
[17349] | 42 | CheckConstraints();
|
---|
[17275] | 43 | }
|
---|
[17263] | 44 | }
|
---|
[17342] | 45 | public string ActualName { get; set; }
|
---|
| 46 |
|
---|
[17349] | 47 | #region JsonIgnore Properties
|
---|
[17342] | 48 | [JsonIgnore]
|
---|
[17322] | 49 | public JsonItem Reference { get; set; }
|
---|
[17263] | 50 |
|
---|
[17322] | 51 | [JsonIgnore]
|
---|
[17342] | 52 | public bool IsConfigurable => (Value != null && Range != null);
|
---|
[17263] | 53 |
|
---|
[17349] | 54 | [JsonIgnore]
|
---|
| 55 | public bool IsParameterizedItem => Parameters != null;
|
---|
| 56 | #endregion
|
---|
| 57 |
|
---|
| 58 | #region Public Static Methods
|
---|
[17283] | 59 | public static void Merge(JsonItem target, JsonItem from) {
|
---|
[17269] | 60 | target.Name = from.Name ?? target.Name;
|
---|
| 61 | target.Type = from.Type ?? target.Type;
|
---|
| 62 | target.Range = from.Range ?? target.Range;
|
---|
| 63 | target.Path = from.Path ?? target.Path;
|
---|
[17342] | 64 | target.Value = from.Value ?? target.Value;
|
---|
[17269] | 65 | target.Reference = from.Reference ?? target.Reference;
|
---|
[17342] | 66 | target.ActualName = from.ActualName ?? target.ActualName;
|
---|
[17269] | 67 | target.Parameters = from.Parameters ?? target.Parameters;
|
---|
[17275] | 68 | target.Operators = from.Operators ?? target.Operators;
|
---|
[17269] | 69 | }
|
---|
[17349] | 70 | #endregion
|
---|
[17269] | 71 |
|
---|
[17349] | 72 | #region Public Methods
|
---|
[17339] | 73 | public void UpdatePath() {
|
---|
[17287] | 74 | if (Parameters != null)
|
---|
[17339] | 75 | UpdatePathHelper(Parameters);
|
---|
[17280] | 76 |
|
---|
[17339] | 77 | if (Operators != null)
|
---|
| 78 | UpdatePathHelper(Operators);
|
---|
| 79 |
|
---|
| 80 | if(Reference != null)
|
---|
| 81 | UpdatePathHelper(Reference);
|
---|
[17280] | 82 | }
|
---|
[17349] | 83 | #endregion
|
---|
[17280] | 84 |
|
---|
[17339] | 85 | #region Helper
|
---|
| 86 | private void UpdatePathHelper(params JsonItem[] items) =>
|
---|
| 87 | UpdatePathHelper((IEnumerable<JsonItem>)items);
|
---|
| 88 |
|
---|
| 89 | private void UpdatePathHelper(IEnumerable<JsonItem> items) {
|
---|
| 90 | foreach (var item in items) {
|
---|
| 91 | item.Path = $"{Path}.{item.Name}";
|
---|
| 92 | item.UpdatePath();
|
---|
| 93 | }
|
---|
[17280] | 94 | }
|
---|
| 95 |
|
---|
[17349] | 96 | private void CheckConstraints() {
|
---|
| 97 | if (Range != null && Value != null && !IsInRange())
|
---|
| 98 | throw new ArgumentOutOfRangeException("Default", "Default is not in range.");
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 | private bool IsInRange() => IsInRangeList() || IsInNumericRange();
|
---|
| 103 |
|
---|
| 104 | private bool IsInRangeList() {
|
---|
| 105 | foreach (var x in Range)
|
---|
| 106 | if (x.Equals(Value)) return true;
|
---|
[17269] | 107 | return false;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[17349] | 110 | private bool IsInNumericRange() =>
|
---|
| 111 | IsInNumericRange<long>()
|
---|
| 112 | || IsInNumericRange<int>()
|
---|
| 113 | || IsInNumericRange<short>()
|
---|
| 114 | || IsInNumericRange<byte>()
|
---|
| 115 | || IsInNumericRange<float>()
|
---|
| 116 | || IsInNumericRange<double>();
|
---|
| 117 |
|
---|
| 118 | private bool IsInNumericRange<T>() where T : IComparable {
|
---|
| 119 | object value = Value, min = Range[0], max = Range[1];
|
---|
| 120 | return value != null && min != null && max != null && value is T && min is T && max is T &&
|
---|
| 121 | (((T)min).CompareTo(value) == -1 || ((T)min).CompareTo(value) == 0) &&
|
---|
| 122 | (((T)max).CompareTo(value) == 1 || ((T)max).CompareTo(value) == 0);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[17269] | 125 | #endregion
|
---|
| 126 | }
|
---|
[17339] | 127 | } |
---|