- Timestamp:
- 11/08/19 11:36:09 (5 years ago)
- Location:
- branches/3026_IntegrationIntoSymSpace
- Files:
-
- 2 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.App/Runner.cs
r17339 r17349 11 11 internal static class Runner { 12 12 internal static void Run(string template, string config, string outputFile = @"C:\Workspace\test.txt") { 13 JCInstantiator instantiator = new JCInstantiator(); 14 IAlgorithm alg = instantiator.Instantiate(template, config); 13 IAlgorithm alg = JCInstantiator.Instantiate(template, config); 15 14 16 15 Task task = alg.StartAsync(); -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/FileManager.cs
r17339 r17349 43 43 44 44 if (saveFileDialog.ShowDialog() == DialogResult.OK) { 45 JCGenerator gen = new JCGenerator();46 45 IAlgorithm alg = namedItem as IAlgorithm; 47 File.WriteAllText(saveFileDialog.FileName, gen.GenerateTemplate(alg));46 File.WriteAllText(saveFileDialog.FileName, JCGenerator.GenerateTemplate(alg)); 48 47 } 49 48 } … … 62 61 63 62 if (openFileDialog.ShowDialog() == DialogResult.OK) { 64 JCInstantiator instantiator = new JCInstantiator();65 63 try { 66 var content = instantiator.Instantiate(openFileDialog.FileName);64 var content = JCInstantiator.Instantiate(openFileDialog.FileName); 67 65 IView view = MainFormManager.MainForm.ShowContent(content); 68 66 if (view == null) -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/HeuristicLab.JsonInterface.csproj
r17324 r17349 61 61 </ItemGroup> 62 62 <ItemGroup> 63 <Compile Include="Attributes\ConvertableAttribute.cs" /> 63 64 <Compile Include="Constants.cs" /> 64 65 <Compile Include="Converters\ValueLookupParameterConverter.cs" /> -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JCGenerator.cs
r17342 r17349 9 9 10 10 namespace HeuristicLab.JsonInterface { 11 public class JCGenerator { 12 private JObject template = JObject.Parse(Constants.Template); 13 private Dictionary<string, string> TypeList = new Dictionary<string, string>(); 14 private JArray JsonItems { get; set; } = new JArray(); 11 public static class JCGenerator { 15 12 16 public string GenerateTemplate(IAlgorithm algorithm) { 17 JsonItems.Clear(); 18 TypeList.Clear(); 13 private struct GenData { 14 public JObject Template { get; set; } 15 public IDictionary<string, string> TypeList { get; set; } 16 public JArray JsonItems { get; set; } 17 } 18 19 20 public static string GenerateTemplate(IAlgorithm algorithm) { 21 // data container 22 GenData genData = new GenData() { 23 Template = JObject.Parse(Constants.Template), 24 TypeList = new Dictionary<string, string>(), 25 JsonItems = new JArray() 26 }; 19 27 20 28 // 1.1. extract JsonItem, save the name in the metadata section of the 21 29 // template and save it an JArray incl. all parameters of the JsonItem, 22 30 // which have parameters aswell 23 AddInstantiableIItem(Constants.Algorithm, algorithm); 24 if (algorithm.Problem != null) // 1.2. only when an problem exists 25 AddInstantiableIItem(Constants.Problem, algorithm.Problem); 31 AddInstantiableIItem(Constants.Algorithm, algorithm, genData); 32 IsConvertable(algorithm, true); 33 if (algorithm.Problem != null && IsConvertable(algorithm.Problem, true)) // 1.2. only when an problem exists 34 AddInstantiableIItem(Constants.Problem, algorithm.Problem, genData); 26 35 27 36 // 2. save the JArray with JsonItems (= IParameterizedItems) 28 template[Constants.Objects] =JsonItems;37 genData.Template[Constants.Objects] = genData.JsonItems; 29 38 // 3. save the types of the JsonItems (for instatiation) 30 template[Constants.Types] = JObject.FromObject(TypeList);39 genData.Template[Constants.Types] = JObject.FromObject(genData.TypeList); 31 40 // 4. serialize template and return string 32 return CustomJsonWriter.Serialize(template); 41 return CustomJsonWriter.Serialize(genData.Template); 42 } 43 44 #region Helper 45 private static bool IsConvertable(object obj, bool throwException) { 46 bool tmp = ConvertableAttribute.IsConvertable(obj); 47 if (throwException && tmp) 48 throw new NotSupportedException($"Type {obj.GetType().Name} is not convertable!"); 49 return tmp; 50 } 51 private static void AddInstantiableIItem(string metaDataTagName, IItem item, GenData genData) { 52 JsonItem jsonItem = JsonItemConverter.Extract(item); 53 genData.Template[Constants.Metadata][metaDataTagName] = item.ItemName; 54 PopulateJsonItems(jsonItem, genData); 33 55 } 34 56 35 #region Helper 36 private void AddInstantiableIItem(string metaDataTagName, IItem item) { 37 JsonItem jsonItem = JsonItemConverter.Extract(item); 38 template[Constants.Metadata][metaDataTagName] = item.ItemName; 39 PopulateJsonItems(jsonItem); 40 } 41 42 private void PopulateJsonItems(JsonItem item) { 57 // serializes ParameterizedItems and saves them in list "JsonItems". 58 private static void PopulateJsonItems(JsonItem item, GenData genData) { 43 59 if (item.Parameters != null) { 44 60 if (item.Range == null) 45 JsonItems.Add(Serialize(item));61 genData.JsonItems.Add(Serialize(item, genData)); 46 62 foreach (var p in item.Parameters) 47 if (p. Parameters != null)48 PopulateJsonItems(p );63 if (p.IsParameterizedItem) 64 PopulateJsonItems(p, genData); 49 65 } 50 66 } 51 67 52 private JObject Serialize(JsonItem item) {68 private static JObject Serialize(JsonItem item, GenData genData) { 53 69 JObject obj = JObject.FromObject(item, Settings()); 54 70 obj[Constants.StaticParameters] = obj[nameof(JsonItem.Parameters)]; … … 62 78 obj.Property(nameof(JsonItem.Type))?.Remove(); 63 79 64 TypeList.Add(item.Path, item.Type);80 genData.TypeList.Add(item.Path, item.Type); 65 81 return obj; 66 82 } 67 83 68 private void RefactorFreeParameters(JToken token) { 84 // deletes unnecessary properties for free parameters. 85 private static void RefactorFreeParameters(JToken token) { 69 86 IList<JObject> objToRemove = new List<JObject>(); 70 87 TransformNodes(x => { … … 80 97 } 81 98 82 private void RefactorStaticParameters(JToken token) { 99 // deletes unnecessary properties for static parameters. 100 private static void RefactorStaticParameters(JToken token) { 83 101 IList<JObject> objToRemove = new List<JObject>(); 84 102 TransformNodes(x => { … … 88 106 x.Property(nameof(JsonItem.Parameters))?.Remove(); 89 107 x.Property(nameof(JsonItem.Type))?.Remove(); 90 if (p.Value == null) objToRemove.Add(x); 108 //TODO: maybe allow JsonItems with Value==null in static parameters too? 109 if (p.Value == null) objToRemove.Add(x); 91 110 }, token[Constants.StaticParameters]); 92 111 foreach (var x in objToRemove) x.Remove(); 93 112 } 94 113 95 private JsonSerializer Settings() => new JsonSerializer() { 114 /// <summary> 115 /// Settings for the json serialization. 116 /// </summary> 117 /// <returns>A configured JsonSerializer.</returns> 118 private static JsonSerializer Settings() => new JsonSerializer() { 96 119 TypeNameHandling = TypeNameHandling.None, 97 120 NullValueHandling = NullValueHandling.Ignore, … … 99 122 }; 100 123 101 private void TransformNodes(Action<JObject> action, params JToken[] tokens) {124 private static void TransformNodes(Action<JObject> action, params JToken[] tokens) { 102 125 foreach(JObject obj in tokens.SelectMany(x => x.Children<JObject>())) 103 126 action(obj); -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JCInstantiator.cs
r17342 r17349 14 14 15 15 namespace HeuristicLab.JsonInterface { 16 public class JCInstantiator { 16 public static class JCInstantiator { 17 private struct InstData { 18 public JToken Template { get; set; } 19 public JArray Config { get; set; } 20 public Dictionary<string, string> TypeList { get; set; } 21 public IDictionary<string, JsonItem> ParameterizedItems { get; set; } 22 public IDictionary<string, JsonItem> ConfigurableItems { get; set; } 23 } 17 24 18 private JToken Template { get; set; } 19 private JArray Config { get; set; } 20 21 private Dictionary<string, string> TypeList = new Dictionary<string, string>(); 22 private IDictionary<string, JsonItem> ParameterizedItems { get; set; } = new Dictionary<string, JsonItem>(); 23 private IDictionary<string, JsonItem> ConfigurableItems { get; set; } = new Dictionary<string, JsonItem>(); 24 25 public IAlgorithm Instantiate(string templateFile, string configFile = "") { 25 public static IAlgorithm Instantiate(string templateFile, string configFile = "") { 26 InstData instData = new InstData() { 27 TypeList = new Dictionary<string, string>(), 28 ParameterizedItems = new Dictionary<string, JsonItem>(), 29 ConfigurableItems = new Dictionary<string, JsonItem>() 30 }; 26 31 27 32 //1. Parse Template and Config files 28 Template = JToken.Parse(File.ReadAllText(templateFile));33 instData.Template = JToken.Parse(File.ReadAllText(templateFile)); 29 34 if(!string.IsNullOrEmpty(configFile)) 30 Config = JArray.Parse(File.ReadAllText(configFile));31 TypeList =Template[Constants.Types].ToObject<Dictionary<string, string>>();32 string algorithmName = Template[Constants.Metadata][Constants.Algorithm].ToString();33 string problemName = Template[Constants.Metadata][Constants.Problem].ToString();35 instData.Config = JArray.Parse(File.ReadAllText(configFile)); 36 instData.TypeList = instData.Template[Constants.Types].ToObject<Dictionary<string, string>>(); 37 string algorithmName = instData.Template[Constants.Metadata][Constants.Algorithm].ToString(); 38 string problemName = instData.Template[Constants.Metadata][Constants.Problem].ToString(); 34 39 35 40 //2. Collect all parameterizedItems from template 36 CollectParameterizedItems( );41 CollectParameterizedItems(instData); 37 42 38 43 //3. select all ConfigurableItems 39 SelectConfigurableItems( );44 SelectConfigurableItems(instData); 40 45 41 46 //4. if config != null -> merge Template and Config 42 if ( Config != null)43 MergeTemplateWithConfig( );47 if (instData.Config != null) 48 MergeTemplateWithConfig(instData); 44 49 45 50 //5. resolve the references between parameterizedItems 46 ResolveReferences( );51 ResolveReferences(instData); 47 52 48 53 //6. get algorthm data and object 49 JsonItem algorithmData = GetData(algorithmName );50 IAlgorithm algorithm = CreateObject<IAlgorithm>(algorithmData );54 JsonItem algorithmData = GetData(algorithmName, instData); 55 IAlgorithm algorithm = CreateObject<IAlgorithm>(algorithmData, instData); 51 56 52 57 //7. get problem data and object 53 JsonItem problemData = GetData(problemName );54 IProblem problem = CreateObject<IProblem>(problemData );58 JsonItem problemData = GetData(problemName, instData); 59 IProblem problem = CreateObject<IProblem>(problemData, instData); 55 60 algorithm.Problem = problem; 56 61 … … 59 64 JsonItemConverter.Inject(problem, problemData); 60 65 66 // TODO: let the engine be configurable 61 67 if (algorithm is EngineAlgorithm) 62 68 algorithm.Cast<EngineAlgorithm>().Engine = new SequentialEngine.SequentialEngine(); … … 65 71 } 66 72 67 private void CollectParameterizedItems() { 68 foreach (JObject item in Template[Constants.Objects]) { 69 JsonItem data = BuildJsonItem(item); 70 ParameterizedItems.Add(data.Path, data); 73 #region Helper 74 private static void CollectParameterizedItems(InstData instData) { 75 foreach (JObject item in instData.Template[Constants.Objects]) { 76 JsonItem data = BuildJsonItem(item, instData); 77 instData.ParameterizedItems.Add(data.Path, data); 71 78 } 72 79 } 73 80 74 private void SelectConfigurableItems() {75 foreach (var item in ParameterizedItems.Values) {81 private static void SelectConfigurableItems(InstData instData) { 82 foreach (var item in instData.ParameterizedItems.Values) { 76 83 if (item.Parameters != null) 77 AddConfigurableItems(item.Parameters );84 AddConfigurableItems(item.Parameters, instData); 78 85 79 86 if (item.Operators != null) 80 AddConfigurableItems(item.Operators );87 AddConfigurableItems(item.Operators, instData); 81 88 } 82 89 } 83 90 84 private void AddConfigurableItems(IEnumerable<JsonItem> items) {91 private static void AddConfigurableItems(IEnumerable<JsonItem> items, InstData instData) { 85 92 foreach (var item in items) 86 93 if (item.IsConfigurable) 87 ConfigurableItems.Add(item.Path, item);94 instData.ConfigurableItems.Add(item.Path, item); 88 95 } 89 96 90 private void ResolveReferences() {91 foreach(var x in ParameterizedItems.Values)97 private static void ResolveReferences(InstData instData) { 98 foreach(var x in instData.ParameterizedItems.Values) 92 99 foreach (var p in x.Parameters) 93 100 if (p.Value is string) { … … 96 103 key = $"{p.Path}.{p.Value.Cast<string>()}"; 97 104 98 if ( ParameterizedItems.TryGetValue(key, out JsonItem value))105 if (instData.ParameterizedItems.TryGetValue(key, out JsonItem value)) 99 106 p.Reference = value; 100 107 } 101 108 } 102 109 103 private void MergeTemplateWithConfig() {104 foreach (JObject obj in Config) {110 private static void MergeTemplateWithConfig(InstData instData) { 111 foreach (JObject obj in instData.Config) { 105 112 // build item from config object 106 JsonItem item = BuildJsonItem(obj );113 JsonItem item = BuildJsonItem(obj, instData); 107 114 // override default value 108 if ( ConfigurableItems.TryGetValue(item.Path, out JsonItem param)) {115 if (instData.ConfigurableItems.TryGetValue(item.Path, out JsonItem param)) { 109 116 param.Value = item.Value; 110 117 // override ActualName (for LookupParameters) … … 115 122 } 116 123 117 private JsonItem GetData(string key)124 private static JsonItem GetData(string key, InstData instData) 118 125 { 119 if ( ParameterizedItems.TryGetValue(key, out JsonItem value))126 if (instData.ParameterizedItems.TryGetValue(key, out JsonItem value)) 120 127 return value; 121 128 else … … 123 130 } 124 131 125 private T CreateObject<T>(JsonItem data) {126 if ( TypeList.TryGetValue(data.Name, out string typeName)) {132 private static T CreateObject<T>(JsonItem data, InstData instData) { 133 if (instData.TypeList.TryGetValue(data.Name, out string typeName)) { 127 134 Type type = Type.GetType(typeName); 128 135 return (T)Activator.CreateInstance(type); … … 131 138 132 139 #region BuildJsonItemMethods 133 private JsonItem BuildJsonItem(JObject obj) =>140 private static JsonItem BuildJsonItem(JObject obj, InstData instData) => 134 141 new JsonItem() { 135 142 Name = obj[nameof(JsonItem.Name)]?.ToString(), … … 137 144 Value = obj[nameof(JsonItem.Value)]?.ToObject<object>(), 138 145 Range = obj[nameof(JsonItem.Range)]?.ToObject<object[]>(), 139 Type = GetType(obj[nameof(JsonItem.Path)]?.ToObject<string>() ),146 Type = GetType(obj[nameof(JsonItem.Path)]?.ToObject<string>(), instData), 140 147 ActualName = obj[nameof(JsonItem.ActualName)]?.ToString(), 141 Parameters = PopulateParameters(obj ),142 Operators = PopulateOperators(obj )148 Parameters = PopulateParameters(obj, instData), 149 Operators = PopulateOperators(obj, instData) 143 150 }; 144 151 145 private st ring GetType(string path) {152 private static string GetType(string path, InstData instData) { 146 153 if(!string.IsNullOrEmpty(path)) 147 if ( TypeList.TryGetValue(path, out string value))154 if (instData.TypeList.TryGetValue(path, out string value)) 148 155 return value; 149 156 return null; 150 157 } 151 158 152 private IList<JsonItem> PopulateParameters(JObject obj) {159 private static IList<JsonItem> PopulateParameters(JObject obj, InstData instData) { 153 160 IList<JsonItem> list = new List<JsonItem>(); 154 161 … … 156 163 if (obj[Constants.StaticParameters] != null) 157 164 foreach (JObject param in obj[Constants.StaticParameters]) 158 list.Add(BuildJsonItem(param ));165 list.Add(BuildJsonItem(param, instData)); 159 166 160 167 // merge staticParameter with freeParameter 161 168 if (obj[Constants.FreeParameters] != null) { 162 169 foreach (JObject param in obj[Constants.FreeParameters]) { 163 JsonItem tmp = BuildJsonItem(param );170 JsonItem tmp = BuildJsonItem(param, instData); 164 171 165 172 // search staticParameter from list … … 176 183 } 177 184 178 private IList<JsonItem> PopulateOperators(JObject obj) {185 private static IList<JsonItem> PopulateOperators(JObject obj, InstData instData) { 179 186 IList<JsonItem> list = new List<JsonItem>(); 180 187 JToken operators = obj[nameof(JsonItem.Operators)]; 181 188 if (operators != null) 182 189 foreach (JObject sp in operators) 183 list.Add(BuildJsonItem(sp ));190 list.Add(BuildJsonItem(sp, instData)); 184 191 return list; 185 192 } 186 193 #endregion 194 #endregion 187 195 } 188 196 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonItem.cs
r17342 r17349 10 10 namespace HeuristicLab.JsonInterface { 11 11 public class JsonItem { 12 13 #region Private Fields 14 private string name; 15 private object value; 16 private IList<object> range; 17 #endregion 12 18 13 private string name;14 19 public string Name { 15 20 get => name; … … 24 29 public IList<JsonItem> Parameters { get; set; } 25 30 public IList<JsonItem> Operators { get; set; } 26 27 private object value;28 31 public object Value { 29 32 get => value; 30 33 set { 31 34 this.value = value; 32 if(Range != null && value != null && !FulfillConstraints()) 33 throw new ArgumentOutOfRangeException("Default", "Default is not in range."); 35 CheckConstraints(); 34 36 } 35 37 } 36 37 private IList<object> range;38 38 public IList<object> Range { 39 39 get => range; 40 40 set { 41 41 range = value; 42 if (Value != null && value != null && !FulfillConstraints()) 43 throw new ArgumentOutOfRangeException("Default", "Default is not in range."); 42 CheckConstraints(); 44 43 } 45 44 } 46 47 45 public string ActualName { get; set; } 48 46 47 #region JsonIgnore Properties 49 48 [JsonIgnore] 50 49 public JsonItem Reference { get; set; } … … 53 52 public bool IsConfigurable => (Value != null && Range != null); 54 53 54 [JsonIgnore] 55 public bool IsParameterizedItem => Parameters != null; 56 #endregion 57 58 #region Public Static Methods 55 59 public static void Merge(JsonItem target, JsonItem from) { 56 60 target.Name = from.Name ?? target.Name; … … 64 68 target.Operators = from.Operators ?? target.Operators; 65 69 } 70 #endregion 66 71 67 public bool FulfillConstraints() => FulfillConstraints(this); 68 69 public static bool FulfillConstraints(JsonItem data) => 70 data.Range != null && data.Value != null && ( 71 IsInRangeList(data.Range, data.Value) || 72 IsInNumericRange<long>(data.Value, data.Range[0], data.Range[1]) || 73 IsInNumericRange<int>(data.Value, data.Range[0], data.Range[1]) || 74 IsInNumericRange<short>(data.Value, data.Range[0], data.Range[1]) || 75 IsInNumericRange<byte>(data.Value, data.Range[0], data.Range[1]) || 76 IsInNumericRange<float>(data.Value, data.Range[0], data.Range[1]) || 77 IsInNumericRange<double>(data.Value, data.Range[0], data.Range[1])); 78 72 #region Public Methods 79 73 public void UpdatePath() { 80 74 if (Parameters != null) … … 87 81 UpdatePathHelper(Reference); 88 82 } 83 #endregion 89 84 90 85 #region Helper … … 99 94 } 100 95 101 private static bool IsInRangeList(IEnumerable<object> list, object value) { 102 foreach (var x in list) 103 if (x.Equals(value)) return true; 96 private void CheckConstraints() { 97 if (Range != null && Value != null && !IsInRange()) 98 throw new ArgumentOutOfRangeException("Default", "Default is not in range."); 99 } 100 101 102 private bool IsInRange() => IsInRangeList() || IsInNumericRange(); 103 104 private bool IsInRangeList() { 105 foreach (var x in Range) 106 if (x.Equals(Value)) return true; 104 107 return false; 105 108 } 106 109 107 private static bool IsInNumericRange<T>(object value, object min, object max) where T : IComparable => 108 (value != null && min != null && max != null && value is T && min is T && max is T && 109 (((T)min).CompareTo(value) == -1 || ((T)min).CompareTo(value) == 0) && 110 (((T)max).CompareTo(value) == 1 || ((T)max).CompareTo(value) == 0)); 110 private bool IsInNumericRange() => 111 IsInNumericRange<long>() 112 || IsInNumericRange<int>() 113 || IsInNumericRange<short>() 114 || IsInNumericRange<byte>() 115 || IsInNumericRange<float>() 116 || IsInNumericRange<double>(); 117 118 private bool IsInNumericRange<T>() where T : IComparable { 119 object value = Value, min = Range[0], max = Range[1]; 120 return value != null && min != null && max != null && value is T && min is T && max is T && 121 (((T)min).CompareTo(value) == -1 || ((T)min).CompareTo(value) == 0) && 122 (((T)max).CompareTo(value) == 1 || ((T)max).CompareTo(value) == 0); 123 } 124 111 125 #endregion 112 126 }
Note: See TracChangeset
for help on using the changeset viewer.