Changeset 17266 for branches/3026_IntegrationIntoSymSpace
- Timestamp:
- 09/20/19 16:23:42 (5 years ago)
- Location:
- branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture
- Files:
-
- 2 added
- 20 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Extensions/IItemExtensions.cs
r17263 r17266 6 6 using HeuristicLab.Core; 7 7 8 namespace ParameterTest{8 namespace HeuristicLab.Manufacture { 9 9 public static class IItemExtensions { 10 10 public static void SetWithParameterData(this IItem item, ParameterData data) { -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Extensions/ObjectExtensions.cs
r17263 r17266 5 5 using System.Threading.Tasks; 6 6 7 namespace ParameterTest{7 namespace HeuristicLab.Manufacture { 8 8 public static class ObjectExtensions { 9 9 public static T Cast<T>(this object obj) => (T)obj; -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Extensions/TypeExtensions.cs
r17263 r17266 5 5 using System.Threading.Tasks; 6 6 7 namespace ParameterTest{7 namespace HeuristicLab.Manufacture { 8 8 public static class TypeExtensions { 9 public static int GetInterfaceDistance(this Type type, Type interfaceType) { 10 if (!interfaceType.IsInterface) return -1; 11 int distance = 0; 12 Type baseType = type; 13 while (baseType != typeof(object)) { 14 var interfaces = baseType.GetInterfaces(); 15 var minimalInterfaces = interfaces.Except(interfaces.SelectMany(i => i.GetInterfaces())); 16 if (baseType == interfaceType && minimalInterfaces.Any(i => i == interfaceType)) 17 ++distance; 18 baseType = baseType.BaseType; 19 } 20 return distance; 21 } 9 22 public static bool IsEqualTo(this Type type, Type other) { 10 23 if (other == null) throw new ArgumentNullException("other"); -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/HeuristicLab.Manufacture.csproj
r17263 r17266 7 7 <ProjectGuid>{0E3AAB5E-F152-44E0-A054-4D9A83ECEE08}</ProjectGuid> 8 8 <OutputType>Exe</OutputType> 9 <RootNamespace> ParameterTest</RootNamespace>10 <AssemblyName> ParameterTest</AssemblyName>9 <RootNamespace>HeuristicLab.Manufacture</RootNamespace> 10 <AssemblyName>HeuristicLab.Manufacture</AssemblyName> 11 11 <TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion> 12 12 <FileAlignment>512</FileAlignment> … … 60 60 <Compile Include="ParameterTransformer\ValueParameterTransformer.cs" /> 61 61 <Compile Include="TypeTransformer\EnumTypeTransformer.cs" /> 62 <Compile Include="TypeTransformer\DummyTransformer.cs" /> 63 <Compile Include="ParameterTransformer\LookupParameterTransformer.cs" /> 62 64 <Compile Include="TypeTransformer\StringValueTransformer.cs" /> 63 65 <Compile Include="TypeTransformer\ValueTypeArrayTransformer.cs" /> -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Interfaces/ITypeTransformer.cs
r17263 r17266 7 7 using Newtonsoft.Json.Linq; 8 8 9 namespace ParameterTest { 9 namespace HeuristicLab.Manufacture { 10 //IDataProcessor? 10 11 public interface ITypeTransformer { 11 ParameterData ToData(IItem value); 12 //IItem FromData(ParameterData obj, Type targetType); 13 14 void SetValue(IItem item, ParameterData obj); 15 //void SetParameter(ParameterData data, IParameter parameter, Type ) 12 ParameterData Extract(IItem value); 13 void Inject(IItem item, ParameterData data); 16 14 } 17 15 } 16 -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/JCGenerator.cs
r17263 r17266 11 11 using Newtonsoft.Json; 12 12 using Newtonsoft.Json.Linq; 13 using Newtonsoft.Json.Serialization; 13 14 14 namespace ParameterTest { 15 namespace HeuristicLab.Manufacture { 16 17 public class CustomWriter : JsonTextWriter { 18 private Stack<Formatting> formattings = new Stack<Formatting>(); 19 private int lvl = 0; 20 public override void WriteStartArray() { 21 base.WriteStartArray(); 22 if(lvl > 1) { 23 formattings.Push(base.Formatting); 24 base.Formatting = Formatting.None; 25 } 26 lvl++; 27 } 28 29 public override void WriteEndArray() { 30 base.WriteEndArray(); 31 lvl--; 32 if (lvl > 1) 33 base.Formatting = formattings.Pop(); 34 } 35 36 public CustomWriter(TextWriter writer) : base(writer) { } 37 38 public static string Serialize(JToken token) { 39 JsonSerializer serializer = new JsonSerializer(); 40 StringWriter sw = new StringWriter(); 41 CustomWriter writer = new CustomWriter(sw); 42 writer.Formatting = Formatting.Indented; 43 serializer.Serialize(writer, token); 44 return sw.ToString(); 45 } 46 } 47 15 48 public class JCGenerator { 16 49 … … 28 61 private Dictionary<string, string> TypeList = new Dictionary<string, string>(); 29 62 30 public string GenerateTemplate(IAlgorithm algorithm, IProblem problem ) {63 public string GenerateTemplate(IAlgorithm algorithm, IProblem problem, params string[] freeParameters) { 31 64 algorithm.Problem = problem; 32 foreach(var x in BJCO(algorithm)) { 33 Console.WriteLine(x.ToString()); 34 } 35 /* 36 JCObject algorithmData = BuildJCObject(algorithm); 37 JCObject problemData = BuildJCObject(problem); 65 IList<JCObject> items = BJCO(algorithm); 66 JArray jsonItems = new JArray(); 67 68 foreach(var item in items) { 69 JToken token = JObject.FromObject(item, Settings()); 38 70 39 ProcessStaticParameters(algorithmData, problemData); 40 ProcessFreeParameters(algorithmData, problemData); 41 ProcessTypeList(); 42 43 return template.ToString(Formatting.Indented); 44 */ 45 return ""; 71 RefactorFreeParameters(token, freeParameters); 72 RefactorStaticParameters(token); 73 if(token["StaticParameters"].HasValues || token["FreeParameters"].HasValues) 74 jsonItems.Add(token); 75 } 76 return CustomWriter.Serialize(jsonItems); 46 77 } 47 78 48 #region Helper 49 private JCObject BuildJCObject(IItem item) { 50 JCObject obj = new JCObject(); 51 obj.Name = item.ItemName; 52 obj.Parameters = BuildJCParameter(item).Parameters; 53 return obj; 79 private void RefactorFreeParameters(JToken token, string[] freeParameters) { 80 81 token["FreeParameters"] = token["StaticParameters"]; 82 83 IList<JObject> objToRemove = new List<JObject>(); 84 TransformNodes(x => { 85 var p = x.ToObject<ParameterData>(); 86 87 /*bool isSelected = false; 88 string name = x["Name"].ToObject<string>(); 89 foreach (var selected in freeParameters) 90 isSelected = (name == selected || isSelected); 91 */ 92 if (/*!isSelected ||*/ p.Default == null || (p.Default != null && p.Default.GetType() == typeof(string) && p.Range == null)) { 93 objToRemove.Add(x); 94 } else { 95 x.Property("Path")?.Remove(); 96 x.Property("Type")?.Remove(); 97 } 98 }, token["FreeParameters"]); 99 foreach (var x in objToRemove) x.Remove(); 100 54 101 } 55 102 103 private void RefactorStaticParameters(JToken token) { 104 IList<JObject> objToRemove = new List<JObject>(); 105 TransformNodes(x => { 106 var p = x.ToObject<ParameterData>(); 107 x.Property("Range")?.Remove(); 108 if (p.Default == null) objToRemove.Add(x); 109 }, token["StaticParameters"]); 110 foreach (var x in objToRemove) x.Remove(); 111 } 112 113 114 115 #region Helper 56 116 private IList<JCObject> BJCO(IParameterizedItem item) { 57 117 List<JCObject> list = new List<JCObject>(); 58 118 JCObject obj = new JCObject(); 59 119 obj.Name = item.ItemName; 120 obj.Type = item.GetType().AssemblyQualifiedName; 60 121 obj.StaticParameters = new List<ParameterData>(); 61 obj.FreeParameters = new List<ParameterData>();62 63 122 list.Add(obj); 64 123 65 124 foreach (var param in item.Parameters) { 66 125 if(!param.Hidden) { 67 ParameterData newParam = new ParameterData(); 68 newParam.Name = param.Name; 69 if (!Util.IsTypeOf(param, typeof(ILookupParameter<>))) { 70 if (param.ActualValue is IntValue) { 71 SetParameterValueStats(newParam, ((IntValue)param.ActualValue).Value, 0, int.MaxValue); 72 } else if (param.ActualValue is BoolValue) { 73 SetParameterValueStats(newParam, ((BoolValue)param.ActualValue).Value, false, true); 74 } else if (param.ActualValue is PercentValue) { 75 SetParameterValueStats(newParam, ((PercentValue)param.ActualValue).Value, 0d, 1d); 76 } else if (param.ActualValue is DoubleValue) { 77 SetParameterValueStats(newParam, ((DoubleValue)param.ActualValue).Value, 0d, double.MaxValue); 78 } else if (Util.IsTypeOf(param.ActualValue, typeof(EnumValue<>))) { 79 var enumType = ((dynamic)param.ActualValue).Value.GetType(); 80 SetParameterValueStats(newParam, 81 Enum.GetName(enumType, ((dynamic)param.ActualValue).Value), 82 Enum.GetNames(enumType)); 83 } else if (param.ActualValue is StringValue) { 84 SetParameterValueStats(newParam, ((StringValue)param.ActualValue).Value); 85 } else if (Util.IsTypeOf(param.ActualValue, typeof(ValueTypeMatrix<>))) { 86 SetParameterValueStats(newParam, ((dynamic)param.ActualValue).CloneAsMatrix()); 87 } else if (Util.IsTypeOf(param.ActualValue, typeof(ValueTypeArray<>))) { 88 SetParameterValueStats(newParam, ((dynamic)param.ActualValue).CloneAsArray()); 89 } else { 90 AddType(param.ActualValue); 91 //newParam.Path = newParam.Path.Substring(0, newParam.Path.Length - 6); 92 SetParameterValueStats(newParam, param.ActualValue?.GetType().Name, ExtractPossibleValues(item, param.DataType)); 93 } 94 } 126 obj.StaticParameters.Add(Transformer.Extract(param)); 95 127 96 97 obj.StaticParameters.Add(newParam); 98 99 if (param is IParameterizedItem) { 100 list.AddRange(BJCO((IParameterizedItem)param)); 101 } 102 if (Util.IsTypeOf(param, typeof(IConstrainedValueParameter<>))) { 103 foreach (var validValue in ((dynamic)param).ValidValues) { 104 if (newParam.Range == null) newParam.Range = new List<object>(); 105 object v = (object)validValue; 106 try { 107 if (!newParam.Range.Contains(v)) 108 newParam.Range.Add(v); 109 } catch { } 110 if (validValue is IParameterizedItem) { 128 if (param is IParameterizedItem) 129 list.AddRange(BJCO(param.Cast<IParameterizedItem>())); 130 131 if (param.GetType().IsEqualTo(typeof(IConstrainedValueParameter<>))) 132 foreach (var validValue in param.Cast<dynamic>().ValidValues) 133 if (validValue is IParameterizedItem && 134 ((IParameterizedItem)validValue).Parameters.Any(p => !p.Hidden)) 111 135 list.AddRange(BJCO((IParameterizedItem)validValue)); 112 }113 }114 }115 136 } 116 137 } … … 119 140 120 141 121 122 private IEnumerable<IParameterizedItem> GetIParameterItems(IParameterizedItem item) {123 List<IParameterizedItem> items = new List<IParameterizedItem>();124 items.Add(item);125 foreach(var param in item.Parameters) {126 if(param is IParameterizedItem) {127 items.AddRange(GetIParameterItems((IParameterizedItem)param));128 }129 if(Util.IsTypeOf(param, typeof(IConstrainedValueParameter<>))){130 foreach(var validValue in ((dynamic)param).ValidValues) {131 if(validValue is IParameterizedItem) {132 items.AddRange(GetIParameterItems((IParameterizedItem)validValue));133 }134 }135 }136 }137 return items;138 }139 140 142 private JsonSerializer Settings() => new JsonSerializer() { 141 143 TypeNameHandling = TypeNameHandling.None, … … 143 145 }; 144 146 145 private void ProcessFreeParameters(JCObject algorithmData, JCObject problemData) {146 JObject freeParams = new JObject();147 JToken alg = JToken.FromObject(algorithmData.Parameters, Settings());148 JToken prob = JToken.FromObject(problemData.Parameters, Settings());149 150 IList<JObject> objToRemove = new List<JObject>();151 TransformNodes(x => {152 var p = x.ToObject<ParameterData>();153 if (p.Default == null || (p.Default != null && p.Default.GetType() == typeof(string) && p.Range == null)) {154 objToRemove.Add(x);155 } else {156 x.Property("Path")?.Remove();157 x.Property("Type")?.Remove();158 }159 },alg, prob);160 foreach (var x in objToRemove) x.Remove();161 162 freeParams.Add(algorithmData.Name, alg);163 freeParams.Add(problemData.Name, prob);164 template["FreeParameters"] = freeParams;165 }166 167 private void ProcessStaticParameters(JCObject algorithmData, JCObject problemData) {168 template["StaticParameters"]["Algorithm"] = JObject.FromObject(algorithmData, Settings());169 template["StaticParameters"]["Problem"] = JObject.FromObject(problemData, Settings());170 TransformNodes(x => x.Property("Range")?.Remove(),171 template["StaticParameters"]["Algorithm"]["Parameters"],172 template["StaticParameters"]["Problem"]["Parameters"]);173 }174 175 private void ProcessTypeList() {176 template["StaticParameters"]["TypeList"] = JObject.FromObject(TypeList);177 }178 147 179 148 private void TransformNodes(Action<JObject> action, params JToken[] tokens) { … … 184 153 #endregion 185 154 186 187 private ParameterData BuildJCParameter(IItem item, string startPath = "") { 188 ParameterData obj = new ParameterData(); 189 obj.Name = item.ItemName; 190 obj.Path = startPath; 191 192 AddType(item, obj.Name); 193 194 try { 195 ExtractParameterInfo(item, obj); 196 } catch { } 197 ExtractOperatorInfo(item, obj); 198 return obj; 199 } 200 201 private bool ProcessValueTypeParameters(ParameterData jCParameter, IParameter parameter) { 202 if (parameter.ActualValue is IntValue) 203 SetParameterValueStats(jCParameter, 204 ((IntValue)parameter.ActualValue).Value, 205 0, int.MaxValue); 206 else if (parameter.ActualValue is BoolValue) 207 SetParameterValueStats(jCParameter, 208 ((BoolValue)parameter.ActualValue).Value, 209 false, true); 210 else if (parameter.ActualValue is PercentValue) 211 SetParameterValueStats(jCParameter, 212 ((PercentValue)parameter.ActualValue).Value, 213 0d, 1d); 214 else if (parameter.ActualValue is DoubleValue) 215 SetParameterValueStats(jCParameter, 216 ((DoubleValue)parameter.ActualValue).Value, 217 0d, double.MaxValue); 218 else return false; 219 return true; 220 } 221 222 private bool ProcessEnumTypeParameters(ParameterData jCParameter, IParameter parameter) { 223 if (Util.IsTypeOf(parameter.ActualValue, typeof(EnumValue<>))) { 224 var enumType = ((dynamic)parameter.ActualValue).Value.GetType(); 225 SetParameterValueStats(jCParameter, 226 Enum.GetName(enumType, ((dynamic)parameter.ActualValue).Value), 227 Enum.GetNames(enumType)); 228 } else return false; 229 return true; 230 } 231 232 private bool ProcessMatrixTypeParameters(ParameterData jCParameter, IParameter parameter) { 233 if (Util.IsTypeOf(parameter.ActualValue, typeof(ValueTypeMatrix<>))) { 234 SetParameterValueStats(jCParameter, ((dynamic)parameter.ActualValue).CloneAsMatrix()); 235 } else return false; 236 return true; 237 } 238 239 private bool ProcessArrayTypeParameters(ParameterData jCParameter, IParameter parameter) { 240 if (Util.IsTypeOf(parameter.ActualValue, typeof(ValueTypeArray<>))) { 241 SetParameterValueStats(jCParameter, ((dynamic)parameter.ActualValue).CloneAsArray()); 242 } else return false; 243 return true; 244 } 245 246 private void SetParameterValueStats(ParameterData parameter, object value, params object[] range) { 247 parameter.Default = value; 248 parameter.Range = range.Length == 0 ? null : range; 249 } 250 251 252 private void ExtractParameterInfo(IItem item, ParameterData obj) { 253 if (item is IParameterizedItem) { 254 foreach (var param in ((IParameterizedItem)item).Parameters) { 255 if (!param.Hidden) { 256 257 ParameterData newParam = BuildJCParameter(param); 258 newParam.Name = param.Name; 259 var tmpType = param.GetType(); 260 newParam.Path = (obj.Path != "" ? obj.Path + "." : "") + param.Name + ".Value.Value"; 261 262 if (obj.Parameters == null) 263 obj.Parameters = new List<ParameterData>(); 264 obj.Parameters.Add(newParam); 265 266 267 if (param.ActualValue is IntValue) { 268 SetParameterValueStats(newParam, ((IntValue)param.ActualValue).Value, 0, int.MaxValue); 269 } else if (param.ActualValue is BoolValue) { 270 SetParameterValueStats(newParam, ((BoolValue)param.ActualValue).Value, false, true); 271 } else if (param.ActualValue is PercentValue) { 272 SetParameterValueStats(newParam, ((PercentValue)param.ActualValue).Value, 0d, 1d); 273 } else if (param.ActualValue is DoubleValue) { 274 SetParameterValueStats(newParam, ((DoubleValue)param.ActualValue).Value, 0d, double.MaxValue); 275 } else if (Util.IsTypeOf(param.ActualValue, typeof(EnumValue<>))) { 276 var enumType = ((dynamic)param.ActualValue).Value.GetType(); 277 SetParameterValueStats(newParam, 278 Enum.GetName(enumType, ((dynamic)param.ActualValue).Value), 279 Enum.GetNames(enumType)); 280 } else if (param.ActualValue is StringValue) { 281 SetParameterValueStats(newParam, ((StringValue)param.ActualValue).Value); 282 } else if (Util.IsTypeOf(param.ActualValue, typeof(ValueTypeMatrix<>))) { 283 SetParameterValueStats(newParam, ((dynamic)param.ActualValue).CloneAsMatrix()); 284 } else if (Util.IsTypeOf(param.ActualValue, typeof(ValueTypeArray<>))) { 285 SetParameterValueStats(newParam, ((dynamic)param.ActualValue).CloneAsArray()); 286 } else { 287 AddType(param.ActualValue); 288 newParam.Path = newParam.Path.Substring(0, newParam.Path.Length - 6); 289 SetParameterValueStats(newParam, param.ActualValue?.GetType().Name, ExtractPossibleValues(item, param.DataType)); 290 } 291 } 292 } 293 } 294 } 295 296 private object[] ExtractPossibleValues(IItem item, Type paramType) { 297 IList<object> list = new List<object>(); 298 if (item is Algorithm) { 299 IEnumerable<IItem> items = ((Algorithm)item).Problem.Operators.Where(x => paramType.IsAssignableFrom(x.GetType())); 300 foreach (var op in items) { 301 string name = op.GetType().Name; 302 list.Add(name); 303 AddType(op, name); 304 } 305 } 306 return list.ToArray(); 307 } 308 309 private void AddType(object obj, string key = null) { 310 if (obj != null) { 311 if (key == null) key = obj.GetType().Name; 312 try { 313 if (!TypeList.ContainsKey(key)) 314 TypeList.Add(key, obj.GetType().AssemblyQualifiedName); 315 } catch { } 316 } 317 } 318 155 /* 319 156 private void ExtractOperatorInfo(IItem item, ParameterData obj) { 320 157 if (item is IMultiOperator) { … … 330 167 } 331 168 } 332 } 169 }*/ 333 170 } 334 171 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/JCInstantiator.cs
r17263 r17266 12 12 using Newtonsoft.Json.Linq; 13 13 14 namespace ParameterTest{14 namespace HeuristicLab.Manufacture { 15 15 16 16 … … 28 28 29 29 public IAlgorithm Instantiate(string configFile) { 30 JObject config = JObject.Parse(File.ReadAllText(configFile)); 30 JArray config = JArray.Parse(File.ReadAllText(configFile)); 31 JCObject obj = config[0].ToObject<JCObject>(); 32 IAlgorithm algorithm = CreateObject<IAlgorithm>(obj); 31 33 34 foreach(var sp in obj.StaticParameters) { 35 if(algorithm.Parameters.TryGetValue(sp.Name, out IParameter param)) { 36 Transformer.Inject(param, sp); 37 } 38 } 39 40 foreach (var sp in obj.FreeParameters) { 41 if (algorithm.Parameters.TryGetValue(sp.Name, out IParameter param)) { 42 if(IsInRangeList(sp.Range, sp.Default) || 43 IsInNumericRange<long>(sp.Default, sp.Range[0], sp.Range[1]) || 44 IsInNumericRange<double>(sp.Default, sp.Range[0], sp.Range[1])) 45 Transformer.Inject(param, sp); 46 } 47 } 48 49 50 return algorithm; 51 52 53 /* 32 54 TypeList = config[SParametersID][TListID].ToObject<Dictionary<string, string>>(); 33 55 … … 46 68 UseFreeParams(problemData.Name, freeProblemParameters, algorithm.Problem); 47 69 return algorithm; 70 */ 71 } 72 73 private T CreateObject<T>(JCObject obj) { 74 Type type = Type.GetType(obj.Type); 75 return (T)Activator.CreateInstance(type); 48 76 } 49 77 … … 56 84 57 85 private void ProcessParameters(JCObject obj, IParameterizedItem instance) { 58 foreach (var param in obj. Parameters)86 foreach (var param in obj.StaticParameters) 59 87 if (param.Default != null) 60 88 SetParameterValue(FindPropertyByKey(instance, param.Name), param.Default); -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/ParameterTransformer/ConstrainedValueParameterTransformer.cs
r17263 r17266 6 6 using HeuristicLab.Core; 7 7 8 namespace ParameterTest.ParameterTransformer{8 namespace HeuristicLab.Manufacture { 9 9 public class ConstrainedValueParameterTransformer : ParameterBaseTransformer { 10 public override void SetValue(IParameter parameter, ParameterData data) {10 public override void InjectData(IParameter parameter, ParameterData data) { 11 11 foreach (var x in parameter.Cast<dynamic>().ValidValues) 12 12 if (x.GetType().Name == CastValue<string>(data.Default)) 13 13 parameter.ActualValue = x; 14 14 } 15 16 public override ParameterData ExtractData(IParameter value) { 17 return new ParameterData() { 18 Name = value.Name, 19 Default = value.ActualValue?.GetType().Name, 20 Range = GetValidValues(value) 21 }; 22 } 23 24 #region Helper 25 private object[] GetValidValues(IParameter value) { 26 List<object> list = new List<object>(); 27 var values = value.Cast<dynamic>().ValidValues; 28 foreach (var x in values) list.Add(x); 29 return list.Select(x => x.GetType().Name).ToArray(); 30 } 31 #endregion 15 32 } 16 33 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/ParameterTransformer/ParameterBaseTransformer.cs
r17263 r17266 6 6 using HeuristicLab.Core; 7 7 8 namespace ParameterTest{8 namespace HeuristicLab.Manufacture { 9 9 public abstract class ParameterBaseTransformer : BaseTransformer { 10 public override IItem FromData(ParameterData obj, Type targetType) { 11 throw new NotImplementedException(); 12 } 10 public override ParameterData ExtractData(IItem value) => ExtractData(value.Cast<IParameter>()); 11 public abstract ParameterData ExtractData(IParameter value); 13 12 13 public override void InjectData(IItem item, ParameterData data) => InjectData(item.Cast<IParameter>(), data); 14 14 15 16 public override void SetValue(IItem item, ParameterData data) => SetValue(item.Cast<IParameter>(), data); 17 18 public abstract void SetValue(IParameter parameter, ParameterData data); 15 public abstract void InjectData(IParameter parameter, ParameterData data); 19 16 } 20 17 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/ParameterTransformer/ValueParameterTransformer.cs
r17263 r17266 6 6 using HeuristicLab.Core; 7 7 8 namespace ParameterTest{8 namespace HeuristicLab.Manufacture { 9 9 public class ValueParameterTransformer : ParameterBaseTransformer { 10 10 11 public override void SetValue(IParameter parameter, ParameterData data) =>12 Transformer. SetValue(parameter.ActualValue, data);11 public override void InjectData(IParameter parameter, ParameterData data) => 12 Transformer.Inject(parameter.ActualValue, data); 13 13 14 public override ParameterData ExtractData(IParameter value) { 15 ParameterData data = Transformer.Extract(value.ActualValue); 16 data.Name = value.Name; 17 return data; 18 } 14 19 } 15 20 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Program.cs
r17263 r17266 27 27 28 28 29 namespace ParameterTest{29 namespace HeuristicLab.Manufacture { 30 30 31 31 public class Program { … … 36 36 37 37 JCGenerator gen = new JCGenerator(); 38 File.WriteAllText(@"C:\Workspace\TemplateProto 9.json", gen.GenerateTemplate(alg, tsp));38 File.WriteAllText(@"C:\Workspace\TemplateProto10.json", gen.GenerateTemplate(alg, tsp)); 39 39 40 40 JCInstantiator configurator = new JCInstantiator(); 41 //configurator.Instantiate(@"C:\Workspace\TemplateProto9.json");41 configurator.Instantiate(@"C:\Workspace\TemplateProto10.json"); 42 42 //Configure(@"C:\Workspace\TemplateProto9.json"); 43 43 //Console.WriteLine(Optimizer); 44 44 45 Console.ReadLine();45 //Console.ReadLine(); 46 46 } 47 47 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Template.cs
r17263 r17266 7 7 using Newtonsoft.Json; 8 8 9 namespace ParameterTest{10 public class JCObject { 9 namespace HeuristicLab.Manufacture { 10 public class JCObject { //Blueprint, Component, ? 11 11 public string Name { get; set; } 12 public IList<ParameterData> Parameters{ get; set; }12 public string Type { get; set; } 13 13 public IList<ParameterData> FreeParameters { get; set; } 14 14 public IList<ParameterData> StaticParameters { get; set; } … … 18 18 sb.Append($"{Name}: ["); 19 19 foreach(var x in StaticParameters) { 20 sb.Append(x .ToString());20 sb.Append(x?.ToString()); 21 21 } 22 22 sb.Append($"\n]"); … … 36 36 public IList<ParameterData> Operators { get; set; } 37 37 38 public ParameterData() { } 39 /* 40 public ParameterData(ParameterData original) { 41 this.Name = original.Name; 42 this.Type = original.Type; 43 this.Default = original.Default; 44 this.Path = original.Path; 45 this.Name = original.Name; 46 this.Name = original.Name; 47 }*/ 38 48 public override string ToString() { 39 49 StringBuilder sb = new StringBuilder(); -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Transformer.cs
r17263 r17266 7 7 using HeuristicLab.Data; 8 8 9 namespace ParameterTest{9 namespace HeuristicLab.Manufacture { 10 10 public static class Transformer { 11 11 private static IDictionary<Type, ITypeTransformer> transformers = new Dictionary<Type, ITypeTransformer>(); … … 20 20 21 21 public static ITypeTransformer Get(Type type) { 22 IList<KeyValuePair<Type, ITypeTransformer>> possibleTransformers = new List<KeyValuePair<Type, ITypeTransformer>>(); 22 23 foreach (var x in transformers) { 23 if (type.IsEqualTo(x.Key)) return x.Value; 24 if (type.IsEqualTo(x.Key)) 25 possibleTransformers.Add(x); 24 26 } 25 return null; 27 28 if(possibleTransformers.Count > 0) { 29 ITypeTransformer nearest = possibleTransformers.First().Value; 30 int nearestDistance = -1; 31 foreach (var x in possibleTransformers) { 32 int d = type.GetInterfaceDistance(x.Key); 33 if (d != -1 && (nearestDistance == -1 || d < nearestDistance)) { 34 nearestDistance = d; 35 nearest = x.Value; 36 } 37 } 38 return nearest; 39 } 40 return new DummyTransformer(); 26 41 } 27 42 28 internal static void SetValue(IItem item, ParameterData data) { 29 Get(item.GetType()).SetValue(item, data); 43 internal static void Inject(IItem item, ParameterData data) { 44 Get(item.GetType()).Inject(item, data); 45 } 46 47 internal static ParameterData Extract(IItem item) { 48 return Get(item.GetType()).Extract(item); 30 49 } 31 50 … … 48 67 Register<PercentMatrix>(new ValueTypeMatrixTransformer<PercentMatrix, double>()); 49 68 Register<BoolMatrix>(new ValueTypeMatrixTransformer<BoolMatrix, bool>()); 69 70 Register(typeof(IConstrainedValueParameter<>), new ConstrainedValueParameterTransformer()); 71 Register(typeof(ILookupParameter), new LookupParameterTransformer()); 72 Register(typeof(IValueParameter), new ValueParameterTransformer()); 50 73 } 51 74 -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/TypeTransformer/BaseTransformer.cs
r17263 r17266 7 7 using Newtonsoft.Json.Linq; 8 8 9 namespace ParameterTest{9 namespace HeuristicLab.Manufacture { 10 10 public abstract class BaseTransformer : ITypeTransformer 11 11 { 12 public abstract void SetValue(IItem item, ParameterData data); 13 public abstract IItem FromData(ParameterData obj, Type targetType); 14 public virtual ParameterData ToData(IItem value) => 15 new ParameterData() { Name = value.ItemName }; 12 public ParameterData Extract(IItem value) { 13 ParameterData data = ExtractData(value); 14 data.Name = String.IsNullOrEmpty(data.Name) ? value.ItemName : data.Name; 15 return data; 16 } 16 17 18 public void Inject(IItem item, ParameterData data) => InjectData(item, data); 19 20 public abstract void InjectData(IItem item, ParameterData data); 21 public abstract ParameterData ExtractData(IItem value); 22 23 #region Helper 17 24 protected ValueType CastValue<ValueType>(object obj) { 18 25 if (obj is JToken) 19 return ((JToken)obj).ToObject<ValueType>(); 20 else 21 return (ValueType)obj; 26 return (obj.Cast<JToken>()).ToObject<ValueType>(); 27 else if (obj is IConvertible) 28 return Convert.ChangeType(obj, typeof(ValueType)).Cast<ValueType>(); 29 else return (ValueType)obj; 22 30 } 23 31 … … 26 34 27 35 protected IItem Instantiate<T>(params object[] args) => Instantiate(typeof(T), args); 36 #endregion 28 37 } 29 38 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/TypeTransformer/EnumTypeTransformer.cs
r17263 r17266 7 7 using HeuristicLab.Data; 8 8 9 namespace ParameterTest{9 namespace HeuristicLab.Manufacture { 10 10 public class EnumTypeTransformer : BaseTransformer { 11 public override IItem FromData(ParameterData obj, Type targetType) {12 Type enumType = targetType.GenericTypeArguments.First();13 var data = Enum.Parse(enumType, CastValue<string>(obj.Default));14 //item.Cast<dynamic>().Value = data;15 return Instantiate(targetType, data);16 }17 11 18 public override void SetValue(IItem item, ParameterData data) {19 Type enumType = item.GetType().GenericTypeArguments.First();20 item.Cast<dynamic>().Value = Enum.Parse(enumType, CastValue<string>(data.Default));21 }12 public override void InjectData(IItem item, ParameterData data) => 13 item.Cast<dynamic>().Value = Enum.Parse( 14 item.GetType().GenericTypeArguments.First(), 15 CastValue<string>(data.Default)); 22 16 23 public override ParameterData ToData(IItem value) {24 ParameterData data = base.ToData(value);17 public override ParameterData ExtractData(IItem value) { 18 ParameterData data = new ParameterData(); 25 19 object val = ((dynamic)value).Value; 26 20 Type enumType = val.GetType(); -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/TypeTransformer/StringValueTransformer.cs
r17263 r17266 7 7 using HeuristicLab.Data; 8 8 9 namespace ParameterTest{9 namespace HeuristicLab.Manufacture { 10 10 public class StringValueTransformer : BaseTransformer { 11 public override IItem FromData(ParameterData obj, Type targetType) => 12 //item.Cast<StringValue>().Value = CastValue<string>(obj.Default); 13 new StringValue() { Value = CastValue<string>(obj.Default) }; 14 15 public override void SetValue(IItem item, ParameterData data) => 11 public override void InjectData(IItem item, ParameterData data) => 16 12 item.Cast<StringValue>().Value = CastValue<string>(data.Default); 17 13 18 public override ParameterData ToData(IItem value) { 19 ParameterData data = base.ToData(value); 20 data.Default = value.Cast<StringValue>().Value; 21 return data; 22 } 23 14 public override ParameterData ExtractData(IItem value) => 15 new ParameterData() { 16 Default = value.Cast<StringValue>().Value 17 }; 24 18 } 25 19 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/TypeTransformer/ValueTypeArrayTransformer.cs
r17263 r17266 8 8 using Newtonsoft.Json.Linq; 9 9 10 namespace ParameterTest{10 namespace HeuristicLab.Manufacture { 11 11 public class ValueTypeArrayTransformer<ArrayType, T> : BaseTransformer 12 12 where ArrayType : ValueTypeArray<T> 13 13 where T : struct 14 14 { 15 public override IItem FromData(ParameterData obj, Type targetType) =>16 Instantiate<ArrayType>(CastValue<T[]>(obj.Default));15 public override void InjectData(IItem item, ParameterData data) => 16 CopyArrayData(item.Cast<ArrayType>(), CastValue<T[]>(data.Default)); 17 17 18 public override void SetValue(IItem item, ParameterData data) { 19 T[] arr = CastValue<T[]>(data.Default); 20 ArrayType valueArray = item.Cast<ArrayType>(); 21 for (int i = 0; i < arr.Length; ++i) { 22 valueArray[i] = arr[i]; 18 public override ParameterData ExtractData(IItem value) => 19 new ParameterData() { 20 Default = value.Cast<ArrayType>().CloneAsArray() 21 }; 22 23 #region Helper 24 private void CopyArrayData(ArrayType array, T[] data) { 25 for (int i = 0; i < data.Length; ++i) { 26 array[i] = data[i]; 23 27 } 24 28 } 25 //item.Cast<StringValue>().Value = CastValue<string>(data.Default); 26 27 public override ParameterData ToData(IItem value) { 28 ParameterData data = base.ToData(value); 29 data.Default = value.Cast<ArrayType>().CloneAsArray(); 30 return data; 31 } 29 #endregion 32 30 } 33 31 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/TypeTransformer/ValueTypeMatrixTransformer.cs
r17263 r17266 8 8 using HeuristicLab.Core; 9 9 10 namespace ParameterTest{10 namespace HeuristicLab.Manufacture { 11 11 public class ValueTypeMatrixTransformer<MatrixType, T> : BaseTransformer 12 12 where MatrixType : ValueTypeMatrix<T> 13 13 where T : struct 14 14 { 15 public override IItem FromData(ParameterData obj, Type targetType) { 16 /*T[,] arr = CastValue<T[,]>(obj.Default); 17 MatrixType matrix = item.Cast<MatrixType>(); 18 for (int x = 0; x < arr.GetLength(0); ++x){ 19 for (int y = 0; y < arr.GetLength(1); ++y) { 20 matrix[x, y] = arr[x, y]; 21 } 22 }*/ 23 return Instantiate<MatrixType>(CastValue<T[,]>(obj.Default)); 24 } 15 public override void InjectData(IItem item, ParameterData data) => 16 CopyMatrixData(item.Cast<MatrixType>(), CastValue<T[,]>(data.Default)); 25 17 26 public override void SetValue(IItem item, ParameterData data) { 27 T[,] arr = CastValue<T[,]>(data.Default); 28 MatrixType matrix = item.Cast<MatrixType>(); 29 for (int x = 0; x < arr.GetLength(0); ++x) { 30 for (int y = 0; y < arr.GetLength(1); ++y) { 31 matrix[x, y] = arr[x, y]; 18 public override ParameterData ExtractData(IItem value) => 19 new ParameterData() { 20 Default = value.Cast<MatrixType>().CloneAsMatrix() 21 }; 22 23 #region Helper 24 private void CopyMatrixData(MatrixType matrix, T[,] data) { 25 for (int x = 0; x < data.GetLength(0); ++x) { 26 for (int y = 0; y < data.GetLength(1); ++y) { 27 matrix[x, y] = data[x, y]; 32 28 } 33 29 } 34 30 } 35 36 public override ParameterData ToData(IItem value) { 37 ParameterData data = base.ToData(value); 38 data.Default = value.Cast<MatrixType>().CloneAsMatrix(); 39 return data; 40 } 31 #endregion 41 32 } 42 33 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/TypeTransformer/ValueTypeValueTransformer.cs
r17263 r17266 7 7 using HeuristicLab.Data; 8 8 9 namespace ParameterTest{9 namespace HeuristicLab.Manufacture { 10 10 public class ValueTypeValueTransformer<ValueType, T> : BaseTransformer 11 11 where ValueType : ValueTypeValue<T> 12 12 where T : struct { 13 public override IItem FromData(ParameterData obj, Type targetType) => 14 //item.Cast<ValueType>().Value = CastValue<T>(obj.Default); 15 Instantiate<ValueType>(CastValue<T>(obj.Default)); 16 public override void SetValue(IItem item, ParameterData data) => 13 14 public override void InjectData(IItem item, ParameterData data) => 17 15 item.Cast<ValueType>().Value = CastValue<T>(data.Default); 18 16 19 public override ParameterData ToData(IItem value) { 20 ParameterData data = base.ToData(value); 21 data.Default = value.Cast<ValueType>().Value; 22 data.Range = new object[] { default(T), GetMaxValue() }; 23 return data; 24 } 17 public override ParameterData ExtractData(IItem value) => 18 new ParameterData() { 19 Default = value.Cast<ValueType>().Value, 20 Range = new object[] { default(T), GetMaxValue() } 21 }; 25 22 23 #region Helper 26 24 private object GetMaxValue() { 27 25 TypeCode typeCode = Type.GetTypeCode(typeof(T)); 26 27 if (typeof(ValueType).IsEqualTo(typeof(PercentValue))) 28 return 1.0d; 29 28 30 switch (typeCode) { 29 31 case TypeCode.Int16: return Int16.MaxValue; … … 41 43 } 42 44 } 45 #endregion 43 46 } 44 47 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Util.cs
r17263 r17266 6 6 using HeuristicLab.Core; 7 7 8 namespace ParameterTest{8 namespace HeuristicLab.Manufacture { 9 9 public class Util { 10 10 public static bool IsTypeOf(object obj, Type t) {
Note: See TracChangeset
for help on using the changeset viewer.