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

Last change on this file since 17349 was 17349, checked in by dpiringe, 12 months ago

#3026

  • added ConvertableAttribute, a new attribute for classes/structs (usage: convertable with JsonInterface)
  • changed JCGenerator -> is now a static class with one public static method Instantiate
  • changed JCInstantiator -> is now a static class with one public static method GenerateTemplate
  • refactored JsonItem
File size: 3.6 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
13    #region Private Fields
14    private string name;
15    private object value;
16    private IList<object> range;
17    #endregion
18   
19    public string Name {
20      get => name;
21      set {
22        name = value;
23        Path = Name;
24        UpdatePath();
25      }
26    }
27    public string Type { get; set; }
28    public string Path { get; set; }
29    public IList<JsonItem> Parameters { get; set; }
30    public IList<JsonItem> Operators { get; set; }
31    public object Value {
32      get => value;
33      set {
34        this.value = value;
35        CheckConstraints();
36      }
37    }
38    public IList<object> Range {
39      get => range;
40      set {
41        range = value;
42        CheckConstraints();
43      }
44    }
45    public string ActualName { get; set; }
46
47    #region JsonIgnore Properties
48    [JsonIgnore]
49    public JsonItem Reference { get; set; }
50
51    [JsonIgnore]
52    public bool IsConfigurable => (Value != null && Range != null);
53
54    [JsonIgnore]
55    public bool IsParameterizedItem => Parameters != null;
56    #endregion
57
58    #region Public Static Methods
59    public static void Merge(JsonItem target, JsonItem from) {
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;
64      target.Value = from.Value ?? target.Value;
65      target.Reference = from.Reference ?? target.Reference;
66      target.ActualName = from.ActualName ?? target.ActualName;
67      target.Parameters = from.Parameters ?? target.Parameters;
68      target.Operators = from.Operators ?? target.Operators;
69    }
70    #endregion
71
72    #region Public Methods
73    public void UpdatePath() {
74      if (Parameters != null)
75        UpdatePathHelper(Parameters);
76
77      if (Operators != null)
78        UpdatePathHelper(Operators);
79
80      if(Reference != null)
81        UpdatePathHelper(Reference);
82    }
83    #endregion
84
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      }
94    }
95
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;
107      return false;
108    }
109
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     
125    #endregion
126  }
127}
Note: See TracBrowser for help on using the repository browser.