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.Manufacture {
|
---|
11 | public class Component {
|
---|
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<Component> Parameters { get; set; }
|
---|
37 | public IList<Component> Operators { get; set; }
|
---|
38 |
|
---|
39 | public override bool Equals(object obj) =>
|
---|
40 | (obj is Component ? (obj.Cast<Component>().Name == this.Name) : false);
|
---|
41 |
|
---|
42 | public override int GetHashCode() => Name.GetHashCode();
|
---|
43 |
|
---|
44 | [JsonIgnore]
|
---|
45 | public IList<Component> ParameterizedItems { get; set; }
|
---|
46 |
|
---|
47 | [JsonIgnore]
|
---|
48 | public Component Reference { get; set; }
|
---|
49 |
|
---|
50 | public static void Merge(Component target, Component from) {
|
---|
51 | target.Name = from.Name ?? target.Name;
|
---|
52 | target.Type = from.Type ?? target.Type;
|
---|
53 | target.Range = from.Range ?? target.Range;
|
---|
54 | target.Path = from.Path ?? target.Path;
|
---|
55 | target.Default = from.Default ?? target.Default;
|
---|
56 | target.Reference = from.Reference ?? target.Reference;
|
---|
57 | target.Parameters = from.Parameters ?? target.Parameters;
|
---|
58 | target.ParameterizedItems = from.ParameterizedItems ?? target.ParameterizedItems;
|
---|
59 | target.Operators = from.Operators ?? target.Operators;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public bool FulfillConstraints() => FulfillConstraints(this);
|
---|
63 |
|
---|
64 | public static bool FulfillConstraints(Component data) =>
|
---|
65 | data.Range != null && data.Default != null && (
|
---|
66 | IsInRangeList(data.Range, data.Default) ||
|
---|
67 | IsInNumericRange<long>(data.Default, data.Range[0], data.Range[1]) ||
|
---|
68 | IsInNumericRange<int>(data.Default, data.Range[0], data.Range[1]) ||
|
---|
69 | IsInNumericRange<short>(data.Default, data.Range[0], data.Range[1]) ||
|
---|
70 | IsInNumericRange<byte>(data.Default, data.Range[0], data.Range[1]) ||
|
---|
71 | IsInNumericRange<float>(data.Default, data.Range[0], data.Range[1]) ||
|
---|
72 | IsInNumericRange<double>(data.Default, data.Range[0], data.Range[1]));
|
---|
73 |
|
---|
74 | #region Helper
|
---|
75 |
|
---|
76 | private static bool IsInRangeList(IEnumerable<object> list, object value) {
|
---|
77 | foreach (var x in list)
|
---|
78 | if (x.Equals(value)) return true;
|
---|
79 | return false;
|
---|
80 | }
|
---|
81 |
|
---|
82 | private static bool IsInNumericRange<T>(object value, object min, object max) where T : IComparable =>
|
---|
83 | (value != null && min != null && max != null && value is T && min is T && max is T &&
|
---|
84 | (((T)min).CompareTo(value) == -1 || ((T)min).CompareTo(value) == 0) &&
|
---|
85 | (((T)max).CompareTo(value) == 1 || ((T)max).CompareTo(value) == 0));
|
---|
86 | #endregion
|
---|
87 | }
|
---|
88 | }
|
---|