- Timestamp:
- 10/01/19 12:58:25 (5 years ago)
- Location:
- branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture
- Files:
-
- 1 added
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Component.cs
r17275 r17280 9 9 10 10 namespace HeuristicLab.Manufacture { 11 //JsonItem 11 12 public class Component { 12 13 private IList<object> range; … … 23 24 } 24 25 } 25 public string Path { get; set; } 26 public string Path { get; set; } = ""; 26 27 27 28 public IList<object> Range { … … 43 44 44 45 [JsonIgnore] 45 public IList<Component> ParameterizedItems { get; set; }46 47 [JsonIgnore]48 46 public Component Reference { get; set; } 49 47 … … 56 54 target.Reference = from.Reference ?? target.Reference; 57 55 target.Parameters = from.Parameters ?? target.Parameters; 58 target.ParameterizedItems = from.ParameterizedItems ?? target.ParameterizedItems;59 56 target.Operators = from.Operators ?? target.Operators; 60 57 } … … 72 69 IsInNumericRange<double>(data.Default, data.Range[0], data.Range[1])); 73 70 71 72 73 public void UpdatePaths() { 74 if (Parameters != null) { 75 foreach (var p in Parameters) { 76 p.Path = Path + "." + p.Name; 77 } 78 } 79 80 if (Operators != null) { 81 foreach (var p in Operators) { 82 p.Path = Path + "." + p.Name; 83 } 84 } 85 } 86 87 public void PrependPath(string str) { 88 Path = $"{str}.{Path}"; 89 PrependPathHelper(Parameters, str); 90 PrependPathHelper(Operators, str); 91 } 92 93 private void PrependPathHelper(IEnumerable<Component> components, string str) { 94 if (components != null) { 95 foreach (var p in components) 96 p.PrependPath(str); 97 } 98 } 99 74 100 #region Helper 75 76 101 private static bool IsInRangeList(IEnumerable<object> list, object value) { 77 102 foreach (var x in list) -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/HeuristicLab.Manufacture.csproj
r17275 r17280 51 51 </ItemGroup> 52 52 <ItemGroup> 53 <Compile Include="CustomJsonWriter.cs" /> 53 54 <Compile Include="Extensions\ObjectExtensions.cs" /> 54 55 <Compile Include="Extensions\TypeExtensions.cs" /> -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Interfaces/ITypeTransformer.cs
r17271 r17280 8 8 9 9 namespace HeuristicLab.Manufacture { 10 //IDataProcessor?11 10 public interface ITypeTransformer { 11 /// <summary> 12 /// Extracts all infos out of an IItem to create a Component. 13 /// (For template generation.) 14 /// </summary> 15 /// <param name="value">The IItem to extract infos.</param> 16 /// <returns>Component with infos to reinitialise the IItem.</returns> 12 17 Component Extract(IItem value); 18 19 /// <summary> 20 /// Injects the saved infos from the Component into the IItem. 21 /// (Sets the necessary values.) 22 /// </summary> 23 /// <param name="item">The IItem which get the data injected.</param> 24 /// <param name="data">The Component with the saved values.</param> 13 25 void Inject(IItem item, Component data); 14 26 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/JCGenerator.cs
r17275 r17280 1 1 using System; 2 2 using System.Collections.Generic; 3 using System.IO;4 3 using System.Linq; 5 4 using HeuristicLab.Algorithms.GeneticAlgorithm; … … 14 13 15 14 namespace HeuristicLab.Manufacture { 16 17 public class CustomWriter : JsonTextWriter {18 private const int ArrayFlatterLvl = 1;19 private Stack<Formatting> formattings = new Stack<Formatting>();20 private int lvl = 0;21 private bool isRangeArray = false;22 public override void WriteStartArray() {23 base.WriteStartArray();24 /*25 if(lvl > ArrayFlatterLvl) {26 formattings.Push(base.Formatting);27 base.Formatting = Formatting.None;28 }29 lvl++;30 */31 if(isRangeArray) base.Formatting = Formatting.None;32 }33 34 public override void WriteEndArray() {35 base.WriteEndArray();36 /*37 lvl--;38 if (lvl > ArrayFlatterLvl)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";50 }51 52 public CustomWriter(TextWriter writer) : base(writer) { }53 54 public static string Serialize(JToken token) {55 JsonSerializer serializer = new JsonSerializer();56 StringWriter sw = new StringWriter();57 CustomWriter writer = new CustomWriter(sw);58 writer.Formatting = Formatting.Indented;59 serializer.Serialize(writer, token);60 return sw.ToString();61 }62 }63 64 15 public class JCGenerator { 65 16 … … 74 25 75 26 private Dictionary<string, string> TypeList = new Dictionary<string, string>(); 76 27 28 JArray jsonItems = new JArray(); 29 private void PopulateJsonItems(Component component) { 30 if (component.Parameters != null) { 31 if(component.Range == null) 32 jsonItems.Add(Serialize(component)); 33 foreach (var p in component.Parameters) 34 if(p.Parameters != null) 35 PopulateJsonItems(p); 36 } 37 } 38 39 private JObject Serialize(Component component) { 40 JObject obj = JObject.FromObject(component, Settings()); 41 obj["StaticParameters"] = obj["Parameters"]; 42 obj["FreeParameters"] = obj["Parameters"]; 43 44 obj.Property("Parameters")?.Remove(); 45 RefactorFreeParameters(obj, null); 46 RefactorStaticParameters(obj); 47 48 obj.Property("Default")?.Remove(); 49 obj.Property("Type")?.Remove(); 50 51 TypeList.Add(component.Path, component.Type); 52 return obj; 53 } 54 77 55 public string GenerateTemplate(IAlgorithm algorithm, IProblem problem, params string[] freeParameters) { 78 56 algorithm.Problem = problem; 79 57 Component algorithmData = Transformer.Extract(algorithm); 80 58 Component problemData = Transformer.Extract(problem); 81 IList<Component> items = algorithmData.ParameterizedItems; 82 foreach (var pItem in problemData.ParameterizedItems) items.Add(pItem); 83 JArray jsonItems = new JArray(); 84 85 foreach(var item in items.Distinct()) { 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)) 113 jsonItems.Add(token); 114 115 if(!token["Parameters"].HasValues) { 116 token.Property("Parameters")?.Remove(); 117 } 118 } 59 PopulateJsonItems(algorithmData); 60 PopulateJsonItems(problemData); 119 61 120 62 template["Metadata"]["Algorithm"] = algorithm.Name; … … 123 65 template["Types"] = JObject.FromObject(TypeList); 124 66 125 return Custom Writer.Serialize(template);67 return CustomJsonWriter.Serialize(template); 126 68 } 127 69 … … 129 71 private void RefactorFreeParameters(JToken token, string[] freeParameters) { 130 72 131 //token["FreeParameters"] = token["StaticParameters"];132 73 133 74 IList<JObject> objToRemove = new List<JObject>(); … … 145 86 x.Property("Path")?.Remove(); 146 87 x.Property("Type")?.Remove(); 88 x.Property("Parameters")?.Remove(); 147 89 } 148 90 }, token["FreeParameters"]); … … 156 98 var p = x.ToObject<Component>(); 157 99 x.Property("Range")?.Remove(); 100 x.Property("Operators")?.Remove(); 101 x.Property("Parameters")?.Remove(); 102 x.Property("Type")?.Remove(); 158 103 if (p.Default == null) objToRemove.Add(x); 159 104 }, token["StaticParameters"]); … … 161 106 } 162 107 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 108 172 109 private JsonSerializer Settings() => new JsonSerializer() { 173 110 TypeNameHandling = TypeNameHandling.None, 174 NullValueHandling = NullValueHandling.Ignore 111 NullValueHandling = NullValueHandling.Ignore, 112 ReferenceLoopHandling = ReferenceLoopHandling.Serialize 175 113 }; 176 114 177 178 115 private void TransformNodes(Action<JObject> action, params JToken[] tokens) { 179 foreach(JObject obj in tokens.SelectMany(x => x.Children<JObject>())) {116 foreach(JObject obj in tokens.SelectMany(x => x.Children<JObject>())) 180 117 action(obj); 181 }182 118 } 183 119 #endregion -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/JCInstantiator.cs
r17275 r17280 47 47 foreach(JObject item in Config["Objects"]) 48 48 { 49 Component data = item.ToObject<Component>();// Component.Build(item);49 Component data = BuildComponent(item); 50 50 if (data.Name == key) return data; 51 51 } … … 80 80 Component tmp = BuildComponent(param); 81 81 Component comp = null; 82 foreach (var p in list) 82 foreach (var p in list) // TODO: nicht notwendig, da immer alle params im static block sind 83 83 if (p.Name == tmp.Name) comp = p; 84 84 if (comp != null) -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/ParameterTransformer/ConstrainedValueParameterTransformer.cs
r17271 r17280 17 17 } 18 18 19 public override Component ExtractData(IParameter value) { 20 21 return new Component() { 19 public override Component ExtractData(IParameter value) => 20 new Component() { 22 21 Name = value.Name, 23 22 Default = value.ActualValue?.GetType().Name, 24 23 Range = GetValidValues(value), 25 Parameter izedItems = GetParameterizedChilds(value)24 Parameters = GetParameterizedChilds(value) 26 25 }; 27 }28 26 29 27 #region Helper … … 34 32 return list.ToArray(); 35 33 } 36 34 // id = kombi aus path + default 37 35 private IList<Component> GetParameterizedChilds(IParameter value) { 38 36 List<Component> list = new List<Component>(); 39 37 var values = value.Cast<dynamic>().ValidValues; 40 38 foreach(var x in values) { 41 if (x is IParameterizedItem && 42 ((IParameterizedItem)x).Parameters.Any(p => !p.Hidden)) { 39 if (x is IParameterizedItem) { 43 40 Component tmp = Transformer.Extract(x); 44 if (tmp.ParameterizedItems != null) 45 list.AddRange(tmp.ParameterizedItems); 46 else 47 list.Add(tmp); 41 tmp.PrependPath(value.Name); 42 list.Add(tmp); 48 43 } 49 44 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/ParameterTransformer/ParameterBaseTransformer.cs
r17271 r17280 8 8 namespace HeuristicLab.Manufacture { 9 9 public abstract class ParameterBaseTransformer : BaseTransformer { 10 public override Component ExtractData(IItem value) => ExtractData(value.Cast<IParameter>()); 10 public override Component ExtractData(IItem value) { 11 IParameter param = value.Cast<IParameter>(); 12 Component comp = ExtractData(param); 13 //comp.Path = value.Cast<IParameter>().ActualValue?.ItemName; 14 /*comp.Path = value.Cast<IParameter>().Name + "." + comp.Path; 15 if(comp.ParameterizedItems != null) { 16 foreach (var item in comp.ParameterizedItems) { 17 item.Path = value.Cast<IParameter>().Name + "." + item.Path; 18 } 19 }*/ 20 return comp; 21 } 11 22 public abstract Component ExtractData(IParameter value); 12 23 -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/ParameterTransformer/ParameterizedItemTransformer.cs
r17275 r17280 9 9 { 10 10 public class ParameterizedItemTransformer : BaseTransformer { 11 12 11 public override void InjectData(IItem item, Component data) { 13 12 IParameterizedItem pItem = item.Cast<IParameterizedItem>(); … … 21 20 22 21 public override Component ExtractData(IItem value) { 23 List<Component> list = new List<Component>();24 25 22 Component obj = new Component(); 26 23 obj.Name = value.ItemName; 27 24 obj.Type = value.GetType().AssemblyQualifiedName; 28 obj.ParameterizedItems = list; 29 obj.Parameters = new List<Component>(); 30 list.Add(obj); 25 obj.Path = value.ItemName; 31 26 32 27 foreach (var param in value.Cast<IParameterizedItem>().Parameters) { 33 if (!param.Hidden) { 34 Component data = Transformer.Extract(param); 35 obj.Parameters.Add(data); 36 if(data.ParameterizedItems != null) 37 list.AddRange(data.ParameterizedItems); 38 } 28 Component data = Transformer.Extract(param); 29 data.Name = param.Name; 30 data.Path = param.Name; 31 data.PrependPath(obj.Path); 32 data.UpdatePaths(); 33 34 if (obj.Parameters == null) 35 obj.Parameters = new List<Component>(); 36 obj.Parameters.Add(data); 39 37 } 40 38 return obj; -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Program.cs
r17275 r17280 35 35 36 36 JCGenerator gen = new JCGenerator(); 37 File.WriteAllText(@"C:\Workspace\TemplateProto10.json", gen.GenerateTemplate(alg, tsp));37 //File.WriteAllText(@"C:\Workspace\TemplateProto11.json", gen.GenerateTemplate(alg, tsp)); 38 38 39 39 JCInstantiator configurator = new JCInstantiator(); 40 configurator.Instantiate(@"C:\Workspace\TemplateProto1 0.json");40 configurator.Instantiate(@"C:\Workspace\TemplateProto11.json"); 41 41 42 42 //Console.ReadLine(); -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Transformer.cs
r17271 r17280 11 11 private static IDictionary<Type, ITypeTransformer> transformers = new Dictionary<Type, ITypeTransformer>(); 12 12 13 public static void Register(Type type, ITypeTransformer transformer) { 14 if (!transformers.ContainsKey(type))13 public static void Register(Type type, ITypeTransformer transformer) { // TODO: explizit 14 if (!transformers.ContainsKey(type)) 15 15 transformers.Add(type, transformer); 16 16 } … … 19 19 public static void Register<T>(ITypeTransformer transformer) => Register(typeof(T), transformer); 20 20 21 public static IEnumerable<ITypeTransformer> Get(Type type) { 21 public static IEnumerable<ITypeTransformer> Get(Type type) { 22 22 IList<KeyValuePair<Type, ITypeTransformer>> possibleTransformers = new List<KeyValuePair<Type, ITypeTransformer>>(); 23 23 -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/TypeTransformer/EnumTypeTransformer.cs
r17271 r17280 9 9 namespace HeuristicLab.Manufacture { 10 10 public class EnumTypeTransformer : BaseTransformer { 11 12 11 public override void InjectData(IItem item, Component data) => 13 12 item.Cast<dynamic>().Value = Enum.Parse( -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/TypeTransformer/MultiCheckedOperatorTransformer.cs
r17275 r17280 10 10 public override Component ExtractData(IItem value) { 11 11 Component data = base.ExtractData(value); 12 12 13 data.Default = value.GetType().Name; 13 14 14 data.Operators = new List<Component>(); 15 /* 16 if (data.ParameterizedItems == null) 17 data.ParameterizedItems = new List<Component>(); 18 data.ParameterizedItems.Add(data); 19 */ 15 20 dynamic val = value.Cast<dynamic>(); 16 21 foreach (var op in val.Operators) { … … 18 23 Name = op.Name, 19 24 Default = val.Operators.ItemChecked(op), 20 Range = new object[] { false, true } 25 Range = new object[] { false, true }, 26 Path = data.Path + "." + op.Name 21 27 }); 22 28 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/TypeTransformer/ValueTypeValueTransformer.cs
r17271 r17280 18 18 new Component() { 19 19 Default = value.Cast<ValueType>().Value, 20 Range = new object[] { default(T), GetMaxValue() }20 Range = new object[] { GetMinValue(), GetMaxValue() } 21 21 }; 22 22 … … 43 43 } 44 44 } 45 46 private object GetMinValue() { 47 TypeCode typeCode = Type.GetTypeCode(typeof(T)); 48 49 if (typeof(ValueType).IsEqualTo(typeof(PercentValue))) 50 return 0.0d; 51 52 switch (typeCode) { 53 case TypeCode.Int16: return Int16.MinValue; 54 case TypeCode.Int32: return Int32.MinValue; 55 case TypeCode.Int64: return Int64.MinValue; 56 case TypeCode.UInt16: return UInt16.MinValue; 57 case TypeCode.UInt32: return UInt32.MinValue; 58 case TypeCode.UInt64: return UInt64.MinValue; 59 case TypeCode.Single: return Single.MinValue; 60 case TypeCode.Double: return Double.MinValue; 61 case TypeCode.Decimal: return Decimal.MinValue; 62 case TypeCode.Byte: return Byte.MinValue; 63 case TypeCode.Boolean: return false; 64 default: return default(T); 65 } 66 } 45 67 #endregion 46 68 }
Note: See TracChangeset
for help on using the changeset viewer.