Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Component.cs @ 17275

Last change on this file since 17275 was 17275, checked in by dpiringe, 5 years ago

#3026

  • deleted Util.cs
  • BaseTransformer merges now the component with its reference (if a reference exists) -> easier handling for transformers with parameterizedItems
  • MultiCheckedOperatorTransformer now sets the type name as default value
  • added a type list at the end of the template file -> is needed to instantiate objects
  • changed JCGenerator and JCInstantiator to test a format where FreeParameters and StaticParameters are saved as single Parameter array
File size: 3.3 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.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}
Note: See TracBrowser for help on using the repository browser.