Changeset 17275 for branches/3026_IntegrationIntoSymSpace
- Timestamp:
- 09/26/19 17:43:44 (5 years ago)
- Location:
- branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture
- Files:
-
- 1 deleted
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Component.cs
r17271 r17275 6 6 using HeuristicLab.Core; 7 7 using Newtonsoft.Json; 8 using Newtonsoft.Json.Linq; 8 9 9 namespace HeuristicLab.Manufacture { 10 /* 11 public class ParameterData { //Blueprint, Component, ? 10 namespace HeuristicLab.Manufacture { 11 public class Component { 12 private IList<object> range; 13 private object defaultValue; 14 12 15 public string Name { get; set; } 13 16 public string Type { get; set; } 14 public IList<ParameterData> FreeParameters { get; set; } 15 public IList<ParameterData> StaticParameters { 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; } 16 26 17 }*/18 19 public class Component {20 public string Name { get; set; }21 public string Type { get; set; }22 public object Default { get; set; }23 public string Path { get; set; }24 public IList<object> Range { get; set;}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 } 25 35 26 36 public IList<Component> Parameters { get; set; } 27 37 public IList<Component> Operators { get; set; } 28 29 [JsonIgnore] 30 public Component this[string index] { 31 get { 32 if (Parameters == null) return null; 33 foreach (var p in Parameters) 34 if (p.Name == index) return p; 35 return null; 36 } 37 set { 38 if (Parameters == null) 39 Parameters = new List<Component>(); 40 Component data = this[index]; 41 if (data != null && CheckConstraints(value)) 42 Merge(data, value); 43 else 44 Parameters.Add(value); 45 } 46 } 47 48 public override bool Equals(object obj) { 49 if (!(obj is Component)) 50 return false; 51 else 52 return obj.Cast<Component>().Name == this.Name; 53 } 54 55 public override int GetHashCode() { 56 return Name.GetHashCode(); 57 } 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(); 58 43 59 44 [JsonIgnore] … … 62 47 [JsonIgnore] 63 48 public Component Reference { get; set; } 64 65 #region Helper 49 66 50 public static void Merge(Component target, Component from) { 67 51 target.Name = from.Name ?? target.Name; … … 73 57 target.Parameters = from.Parameters ?? target.Parameters; 74 58 target.ParameterizedItems = from.ParameterizedItems ?? target.ParameterizedItems; 59 target.Operators = from.Operators ?? target.Operators; 75 60 } 76 61 77 private bool CheckConstraints(Component data) => 62 public bool FulfillConstraints() => FulfillConstraints(this); 63 64 public static bool FulfillConstraints(Component data) => 78 65 data.Range != null && data.Default != null && ( 79 66 IsInRangeList(data.Range, data.Default) || 80 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]) || 81 72 IsInNumericRange<double>(data.Default, data.Range[0], data.Range[1])); 82 73 83 private bool IsInRangeList(IEnumerable<object> list, object value) { 74 #region Helper 75 76 private static bool IsInRangeList(IEnumerable<object> list, object value) { 84 77 foreach (var x in list) 85 78 if (x.Equals(value)) return true; … … 87 80 } 88 81 89 private bool IsInNumericRange<T>(object value, object min, object max) where T : IComparable =>82 private static bool IsInNumericRange<T>(object value, object min, object max) where T : IComparable => 90 83 (value != null && min != null && max != null && value is T && min is T && max is T && 91 84 (((T)min).CompareTo(value) == -1 || ((T)min).CompareTo(value) == 0) && -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/HeuristicLab.Manufacture.csproj
r17271 r17275 73 73 <Compile Include="Interfaces\ITypeTransformer.cs" /> 74 74 <Compile Include="Transformer.cs" /> 75 <Compile Include="Util.cs" />76 75 </ItemGroup> 77 76 <ItemGroup> -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/JCGenerator.cs
r17271 r17275 19 19 private Stack<Formatting> formattings = new Stack<Formatting>(); 20 20 private int lvl = 0; 21 private bool isRangeArray = false; 21 22 public override void WriteStartArray() { 22 23 base.WriteStartArray(); 24 /* 23 25 if(lvl > ArrayFlatterLvl) { 24 26 formattings.Push(base.Formatting); … … 26 28 } 27 29 lvl++; 30 */ 31 if(isRangeArray) base.Formatting = Formatting.None; 28 32 } 29 33 30 34 public override void WriteEndArray() { 31 35 base.WriteEndArray(); 36 /* 32 37 lvl--; 33 38 if (lvl > ArrayFlatterLvl) 34 39 base.Formatting = formattings.Pop(); 40 */ 41 if(isRangeArray) { 42 base.Formatting = Formatting.Indented; 43 isRangeArray = false; 44 } 45 } 46 47 public override void WritePropertyName(string name) { 48 base.WritePropertyName(name); 49 isRangeArray = name == "Range"; 35 50 } 36 51 … … 54 69 'Problem':'' 55 70 }, 56 'Objects': [] 71 'Objects': [], 72 'Types': {} 57 73 }"); 58 74 59 60 75 private Dictionary<string, string> TypeList = new Dictionary<string, string>(); 61 76 … … 69 84 70 85 foreach(var item in items.Distinct()) { 71 JToken token = JObject.FromObject(item, Settings()); 72 token["StaticParameters"] = token["Parameters"]; 73 token["FreeParameters"] = token["Parameters"]; 74 token.Cast<JObject>().Property("Parameters")?.Remove(); 75 RefactorFreeParameters(token, freeParameters); 76 RefactorStaticParameters(token); 77 if(token["StaticParameters"].HasValues || token["FreeParameters"].HasValues) 86 JObject token = JObject.FromObject(item, Settings()); 87 //token["StaticParameters"] = token["Parameters"]; 88 //token["FreeParameters"] = token["Parameters"]; 89 90 foreach (JObject x in token["Parameters"]) { 91 x.Property("Parameters")?.Remove(); 92 x.Property("Type")?.Remove(); 93 } 94 TypeList.Add(item.Name, item.Type); 95 RefactorParameters(token); 96 /* 97 foreach (JObject x in token["StaticParameters"]) { 98 x.Property("Parameters")?.Remove(); 99 x.Property("Type")?.Remove(); 100 } 101 foreach (JObject x in token["FreeParameters"]) { 102 x.Property("Parameters")?.Remove(); 103 x.Property("Type")?.Remove(); 104 } 105 */ 106 //token.Cast<JObject>().Property("Parameters")?.Remove(); 107 token.Property("Default")?.Remove(); 108 token.Property("Type")?.Remove(); 109 110 //RefactorFreeParameters(token, freeParameters); 111 //RefactorStaticParameters(token); 112 if (/*token["StaticParameters"].HasValues || token["FreeParameters"].HasValues ||*/ token["Parameters"].HasValues || (token["Operators"]?.HasValues ?? false)) 78 113 jsonItems.Add(token); 114 115 if(!token["Parameters"].HasValues) { 116 token.Property("Parameters")?.Remove(); 117 } 79 118 } 80 119 … … 82 121 template["Metadata"]["Problem"] = problem.Name; 83 122 template["Objects"] = jsonItems; 123 template["Types"] = JObject.FromObject(TypeList); 84 124 85 125 return CustomWriter.Serialize(template); … … 121 161 } 122 162 163 private void RefactorParameters(JToken token) { 164 IList<JObject> objToRemove = new List<JObject>(); 165 TransformNodes(x => { 166 var p = x.ToObject<Component>(); 167 if (p.Default == null) objToRemove.Add(x); 168 }, token["Parameters"]); 169 foreach (var x in objToRemove) x.Remove(); 170 } 171 123 172 private JsonSerializer Settings() => new JsonSerializer() { 124 173 TypeNameHandling = TypeNameHandling.None, -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/JCInstantiator.cs
r17271 r17275 13 13 14 14 namespace HeuristicLab.Manufacture { 15 16 17 15 public class JCInstantiator { 18 16 19 17 private JToken Config { get; set; } 18 private Dictionary<string, string> TypeList = new Dictionary<string, string>(); 20 19 21 20 public IAlgorithm Instantiate(string configFile) { 22 21 Config = JToken.Parse(File.ReadAllText(configFile)); 22 TypeList = Config["Types"].ToObject<Dictionary<string, string>>(); 23 23 24 24 Component algorithmData = GetData(Config["Metadata"]["Algorithm"].ToString()); … … 37 37 } 38 38 39 /*40 * resolve references41 */42 43 39 private void ResolveReferences(Component data) { 44 foreach (var p in data.Parameters) {45 if (p.Default is string && p.Reference == null) {40 foreach (var p in data.Parameters) 41 if (p.Default is string && p.Reference == null) 46 42 p.Reference = GetData(p.Default.Cast<string>()); 47 }48 }49 43 } 50 44 … … 53 47 foreach(JObject item in Config["Objects"]) 54 48 { 55 Component data = BuildDataFromJObject(item);49 Component data = item.ToObject<Component>();// Component.Build(item); 56 50 if (data.Name == key) return data; 57 51 } … … 59 53 } 60 54 61 private Component BuildDataFromJObject(JObject obj) { 62 Component data = new Component() { 63 Name = obj["Name"]?.ToString(), 64 Default = obj["Default"]?.ToObject<object>(), 65 Range = obj["Range"]?.ToObject<object[]>(), 66 Type = obj["Type"]?.ToObject<string>() 55 private T CreateObject<T>(Component data) { 56 if (TypeList.TryGetValue(data.Name, out string typeName)) { 57 Type type = Type.GetType(typeName); 58 return (T)Activator.CreateInstance(type); 59 } else throw new TypeLoadException($"Cannot find AssemblyQualifiedName for {data.Name}."); 60 } 61 62 private Component BuildComponent(JObject obj) => 63 new Component() { 64 Name = obj[nameof(Component.Name)]?.ToString(), 65 Default = obj[nameof(Component.Default)]?.ToObject<object>(), 66 Range = obj[nameof(Component.Range)]?.ToObject<object[]>(), 67 Type = obj[nameof(Component.Type)]?.ToObject<string>(), 68 Parameters = PopulateParameters(obj), 69 Operators = PopulateOperators(obj) 67 70 }; 68 71 69 if(obj["StaticParameters"] != null) 70 foreach (JObject sp in obj["StaticParameters"]) 71 data[sp["Name"].ToString()] = BuildDataFromJObject(sp); 72 private IList<Component> PopulateParameters(JObject obj) { 73 IList<Component> list = new List<Component>(); 74 if (obj["StaticParameters"] != null) 75 foreach (JObject param in obj["StaticParameters"]) 76 list.Add(BuildComponent(param)); 72 77 73 if (obj["FreeParameters"] != null) 74 foreach (JObject sp in obj["FreeParameters"]) 75 data[sp["Name"].ToString()] = BuildDataFromJObject(sp); 76 77 if (obj["Operators"] != null) { 78 data.Operators = new List<Component>(); 79 foreach (JObject sp in obj["Operators"]) 80 data.Operators.Add(BuildDataFromJObject(sp)); 78 if (obj["FreeParameters"] != null) { 79 foreach (JObject param in obj["FreeParameters"]) { 80 Component tmp = BuildComponent(param); 81 Component comp = null; 82 foreach (var p in list) 83 if (p.Name == tmp.Name) comp = p; 84 if (comp != null) 85 Component.Merge(comp, tmp); 86 else list.Add(tmp); 87 } 81 88 } 82 83 return data; 89 return list; 84 90 } 85 91 86 private T CreateObject<T>(Component data) { 87 Type type = Type.GetType(data.Type); 88 return (T)Activator.CreateInstance(type); 92 private IList<Component> PopulateOperators(JObject obj) { 93 IList<Component> list = new List<Component>(); 94 if (obj[nameof(Operators)] != null) 95 foreach (JObject sp in obj[nameof(Operators)]) { 96 Component tmp = BuildComponent(sp); 97 list.Add(tmp); 98 } 99 return list; 89 100 } 90 91 101 } 92 102 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/ParameterTransformer/ParameterizedItemTransformer.cs
r17271 r17275 13 13 IParameterizedItem pItem = item.Cast<IParameterizedItem>(); 14 14 15 foreach (var sp in data.Parameters) 16 if (pItem.Parameters.TryGetValue(sp.Name, out IParameter param)) 17 Transformer.Inject(param, sp); 15 if(data.Parameters != null) { 16 foreach (var sp in data.Parameters) 17 if (pItem.Parameters.TryGetValue(sp.Name, out IParameter param)) 18 Transformer.Inject(param, sp); 19 } 18 20 } 19 21 … … 25 27 obj.Type = value.GetType().AssemblyQualifiedName; 26 28 obj.ParameterizedItems = list; 29 obj.Parameters = new List<Component>(); 27 30 list.Add(obj); 28 31 … … 30 33 if (!param.Hidden) { 31 34 Component data = Transformer.Extract(param); 32 obj [data.Name] = data;35 obj.Parameters.Add(data); 33 36 if(data.ParameterizedItems != null) 34 37 list.AddRange(data.ParameterizedItems); -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Program.cs
r17269 r17275 35 35 36 36 JCGenerator gen = new JCGenerator(); 37 //File.WriteAllText(@"C:\Workspace\TemplateProto10.json", gen.GenerateTemplate(alg, tsp));37 File.WriteAllText(@"C:\Workspace\TemplateProto10.json", gen.GenerateTemplate(alg, tsp)); 38 38 39 39 JCInstantiator configurator = new JCInstantiator(); -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/TypeTransformer/BaseTransformer.cs
r17271 r17275 16 16 } 17 17 18 public void Inject(IItem item, Component data) => InjectData(item, data); 18 public void Inject(IItem item, Component data) { 19 if(data.Reference != null) { 20 Component.Merge(data, data.Reference); 21 } 22 InjectData(item, data); 23 } 19 24 20 25 public abstract void InjectData(IItem item, Component data); -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/TypeTransformer/MultiCheckedOperatorTransformer.cs
r17271 r17275 10 10 public override Component ExtractData(IItem value) { 11 11 Component data = base.ExtractData(value); 12 data.Default = value.GetType().Name; 12 13 13 14 data.Operators = new List<Component>();
Note: See TracChangeset
for help on using the changeset viewer.