- Timestamp:
- 11/08/19 11:36:09 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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);
Note: See TracChangeset
for help on using the changeset viewer.