Changeset 17477
- Timestamp:
- 03/17/20 12:33:35 (5 years ago)
- Location:
- branches/3026_IntegrationIntoSymSpace
- Files:
-
- 30 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.App/Runner.cs
r17464 r17477 16 16 internal static class Runner { 17 17 internal static void Run(string template, string config, string outputFile) { 18 IOptimizer optimizer = JsonTemplateInstantiator.Instantiate(template, config, out IEnumerable<string> allowedResultNames); 18 InstantiatorResult instantiatorResult = JsonTemplateInstantiator.Instantiate(template, config); 19 IOptimizer optimizer = instantiatorResult.Optimizer; 20 IEnumerable<IResultJsonItem> configuredResultItem = instantiatorResult.ConfiguredResultItems; 21 19 22 optimizer.Runs.Clear(); 20 23 if(optimizer is EngineAlgorithm e) … … 23 26 Task task = optimizer.StartAsync(); 24 27 while(!task.IsCompleted) { 25 WriteResultsToFile(outputFile, optimizer, allowedResultNames);28 WriteResultsToFile(outputFile, optimizer, configuredResultItem); 26 29 Thread.Sleep(100); 27 30 } 28 31 29 WriteResultsToFile(outputFile, optimizer, allowedResultNames);32 WriteResultsToFile(outputFile, optimizer, configuredResultItem); 30 33 } 31 34 32 private static void WriteResultsToFile(string file, IOptimizer optimizer, IEnumerable< string> allowedResultNames) =>33 File.WriteAllText(file, FetchResults(optimizer, allowedResultNames));35 private static void WriteResultsToFile(string file, IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem) => 36 File.WriteAllText(file, FetchResults(optimizer, configuredResultItem)); 34 37 35 private static string FetchResults(IOptimizer optimizer, IEnumerable< string> allowedResultNames) {38 private static string FetchResults(IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem) { 36 39 JArray arr = new JArray(); 37 40 IEnumerable<string> configuredResults = configuredResultItem.Select(x => x.Name); 41 38 42 foreach (var run in optimizer.Runs) { 39 43 JObject obj = new JObject(); … … 41 45 obj.Add("Run", JToken.FromObject(run.ToString())); 42 46 foreach (var res in run.Results) { 43 if ( allowedResultNames.Contains(res.Key)) {47 if (configuredResults.Contains(res.Key)) { 44 48 if (res.Value is ISymbolicRegressionSolution solution) { 45 49 var formatter = new SymbolicDataAnalysisExpressionMATLABFormatter(); -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/FileManager.cs
r17436 r17477 24 24 } 25 25 public static void ExportJsonTemplate(IContentView view) { 26 // TODO: view to select free params, warning if no results are generated27 26 IStorableContent content = view.Content as IStorableContent; 28 27 if (!view.Locked && content != null) { … … 46 45 try { 47 46 var content = JsonTemplateInstantiator.Instantiate(openFileDialog.FileName); 48 IView view = MainFormManager.MainForm.ShowContent(content );47 IView view = MainFormManager.MainForm.ShowContent(content.Optimizer); 49 48 if (view == null) 50 49 ErrorHandling.ShowErrorDialog("There is no view for the loaded item. It cannot be displayed.", new InvalidOperationException("No View Available")); -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/ViewModels/JsonItemVMBase.cs
r17473 r17477 13 13 { 14 14 IJsonItem IJsonItemVM.Item { 15 get => item;16 set => item = (JsonItemType)value;15 get => Item; 16 set => Item = (JsonItemType)value; 17 17 } 18 18 … … 39 39 if (TreeView != null) 40 40 TreeView.Refresh(); 41 SelectedChanged?.Invoke(); 41 42 OnPropertyChange(this, nameof(Selected)); 42 43 } … … 62 63 public event PropertyChangedEventHandler PropertyChanged; 63 64 public event Action ItemChanged; 65 public event Action SelectedChanged; 64 66 65 67 -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/ViewModels/ValueLookupJsonItemVM.cs
r17473 r17477 10 10 public override Type TargetedJsonItemType => typeof(ValueLookupJsonItem); 11 11 public override UserControl Control => new ValueLookupJsonItemControl(this); 12 public IJsonItem JsonItemReference => ((IValueLookupJsonItem)Item).JsonItemReference; 12 public IJsonItem JsonItemReference => ((IValueLookupJsonItem)Item).ActualValue; 13 14 public ValueLookupJsonItemVM() { 15 base.SelectedChanged += () => { 16 if (JsonItemReference != null) 17 JsonItemReference.Active = base.Selected; 18 }; 19 } 13 20 } 14 21 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/ExportJsonDialog.cs
r17473 r17477 16 16 namespace HeuristicLab.JsonInterface.OptimizerIntegration { 17 17 public partial class ExportJsonDialog : Form { 18 19 #region Private Properties 18 20 private static FolderBrowserDialog FolderBrowserDialog { get; set; } 19 21 private IDictionary<TreeNode, UserControl> Node2Control { get; set; } = new Dictionary<TreeNode, UserControl>(); … … 23 25 private IOptimizer Optimizer { get; set; } 24 26 private IList<IJsonItemVM> VMs { get; set; } 25 private JCGenerator Generator { get; set; } = new JCGenerator();27 #endregion 26 28 27 29 private IContent content; … … 31 33 content = value; 32 34 35 #region Clear 33 36 VMs = new List<IJsonItemVM>(); 34 37 treeView.Nodes.Clear(); 35 38 treeViewResults.Nodes.Clear(); 39 #endregion 36 40 37 41 Optimizer = content as IOptimizer; … … 45 49 } 46 50 } 47 48 49 50 51 51 52 private void InitCache() { … … 85 86 86 87 if (FolderBrowserDialog.ShowDialog() == DialogResult.OK) { 87 Generator.GenerateTemplate(FolderBrowserDialog.SelectedPath, textBoxTemplateName.Text, Optimizer, Root);88 JCGenerator.GenerateTemplate(FolderBrowserDialog.SelectedPath, textBoxTemplateName.Text, Optimizer, Root); 88 89 Close(); 89 90 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/JsonItemBoolControl.Designer.cs
r17471 r17477 37 37 this.checkBoxValue.Margin = new System.Windows.Forms.Padding(0); 38 38 this.checkBoxValue.Name = "checkBoxValue"; 39 this.checkBoxValue.Padding = new System.Windows.Forms.Padding(0, 0, 0, 1); 39 40 this.checkBoxValue.Size = new System.Drawing.Size(400, 22); 40 41 this.checkBoxValue.TabIndex = 19; -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/LookupJsonItemControl.Designer.cs
r17471 r17477 64 64 this.tableLayoutPanel1.RowCount = 1; 65 65 this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); 66 this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 2 0F));66 this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 22F)); 67 67 this.tableLayoutPanel1.Size = new System.Drawing.Size(416, 22); 68 68 this.tableLayoutPanel1.TabIndex = 13; -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/BaseConverter.cs
r17433 r17477 18 18 19 19 #region Helper 20 protected ValueType CastValue<ValueType>(object obj) {21 if (obj is JToken)22 return ((JToken)obj).ToObject<ValueType>();23 else if (obj is IConvertible)24 return (ValueType)Convert.ChangeType(obj, typeof(ValueType));25 else return (ValueType)obj;26 }27 20 28 21 protected IItem Instantiate(Type type, params object[] args) => 29 22 (IItem)Activator.CreateInstance(type,args); 30 31 32 protected object GetMaxValue(Type t) {33 TypeCode typeCode = Type.GetTypeCode(t);34 35 if (t.IsEqualTo(typeof(PercentValue)))36 return 1.0d;37 38 if(t == typeof(IntValue)) {39 return int.MaxValue;40 }41 42 switch (typeCode) {43 case TypeCode.Int16: return short.MaxValue;44 case TypeCode.Int32: return int.MaxValue;45 case TypeCode.Int64: return long.MaxValue;46 case TypeCode.UInt16: return ushort.MaxValue;47 case TypeCode.UInt32: return uint.MaxValue;48 case TypeCode.UInt64: return ulong.MaxValue;49 case TypeCode.Single: return float.MaxValue;50 case TypeCode.Double: return double.MaxValue;51 case TypeCode.Decimal: return decimal.MaxValue;52 case TypeCode.Byte: return byte.MaxValue;53 case TypeCode.Boolean: return true;54 default: return GetDefaultValue(t);55 }56 }57 58 protected object GetMinValue(Type t) {59 TypeCode typeCode = Type.GetTypeCode(t);60 61 if (t.IsEqualTo(typeof(PercentValue)))62 return 0.0d;63 64 if (t == typeof(IntValue)) {65 return int.MinValue;66 }67 68 switch (typeCode) {69 case TypeCode.Int16: return short.MinValue;70 case TypeCode.Int32: return int.MinValue;71 case TypeCode.Int64: return long.MinValue;72 case TypeCode.UInt16: return ushort.MinValue;73 case TypeCode.UInt32: return uint.MinValue;74 case TypeCode.UInt64: return ulong.MinValue;75 case TypeCode.Single: return float.MinValue;76 case TypeCode.Double: return double.MinValue;77 case TypeCode.Decimal: return decimal.MinValue;78 case TypeCode.Byte: return byte.MinValue;79 case TypeCode.Boolean: return false;80 default: return GetDefaultValue(t);81 }82 }83 84 protected object GetDefaultValue(Type t) => t.IsValueType ? Activator.CreateInstance(t) : null;85 23 #endregion 86 24 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ConstrainedValueParameterConverter.cs
r17473 r17477 16 16 IParameter parameter = item as IParameter; 17 17 foreach (var x in GetValidValues(parameter)) 18 if(x.ToString() == CastValue<string>(cdata.Value))18 if(x.ToString() == cdata.Value) 19 19 parameter.ActualValue = x; 20 20 21 if (parameter.ActualValue != null && /*parameter.ActualValue is IParameterizedItem &&*/cdata.Children != null) {22 foreach(var paramin cdata.Children) {23 if( param.Name == parameter.ActualValue.ItemName)24 root.Inject(parameter.ActualValue, param, root);21 if (parameter.ActualValue != null && parameter.ActualValue is IParameterizedItem && cdata.Children != null) { 22 foreach(var child in cdata.Children) { 23 if(child.Name == parameter.ActualValue.ItemName) // name kann verändert werden? egal da bei inject der original name vorhanden ist 24 root.Inject(parameter.ActualValue, child, root); 25 25 } 26 26 } … … 36 36 ConcreteRestrictedItems = GetValidValues(parameter).Select(x => x.ToString()) 37 37 }; 38 item.AddChildren(GetParameterizedChilds(parameter ));38 item.AddChildren(GetParameterizedChilds(parameter, root)); 39 39 40 40 return item; … … 49 49 } 50 50 51 private IList<IJsonItem> GetParameterizedChilds(IParameter value ) {51 private IList<IJsonItem> GetParameterizedChilds(IParameter value, IJsonItemConverter root) { 52 52 List<IJsonItem> list = new List<IJsonItem>(); 53 53 var values = ((dynamic)value).ValidValues; 54 54 foreach(var x in values) { 55 if (x is IParameterizedItem) { 56 IJsonItem tmp = JsonItemConverter.Extract(x);55 if (x is IParameterizedItem) { // only makes sense for IParameterizedItems to go deeper 56 IJsonItem tmp = root.Extract(x, root); 57 57 if(!(tmp is UnsupportedJsonItem)) 58 58 list.Add(tmp); -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueLookupParameterConverter.cs
r17473 r17477 15 15 IValueLookupJsonItem lookupItem = data as IValueLookupJsonItem; 16 16 param.ActualName = lookupItem.ActualName; 17 if (param.Value != null )18 root.Inject(param.Value, lookupItem. JsonItemReference, root);17 if (param.Value != null && lookupItem.ActualValue != null) 18 root.Inject(param.Value, lookupItem.ActualValue, root); 19 19 } 20 20 … … 26 26 if (param.Value != null) { 27 27 IJsonItem tmp = root.Extract(param.Value, root); 28 item.AddChildren(tmp.Children); 29 item.JsonItemReference = tmp; 28 tmp.Parent = item; 29 //item.AddChildren(tmp.Children); 30 item.ActualValue = tmp; 30 31 } 31 32 item.Name = param.Name; -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeArrayConverter.cs
r17473 r17477 16 16 public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) { 17 17 IntArray arr = item as IntArray; 18 int[] d = CastValue<int[]>(data);18 IntArrayJsonItem intArrayItem = data as IntArrayJsonItem; 19 19 bool resizeTmp = arr.Resizable; 20 20 arr.Resizable = true; 21 arr.Length = d.Length;22 for (int i = 0; i < d.Length; ++i)23 arr[i] = d[i];21 arr.Length = intArrayItem.Value.Length; 22 for (int i = 0; i < intArrayItem.Value.Length; ++i) 23 arr[i] = intArrayItem.Value[i]; 24 24 arr.Resizable = resizeTmp; 25 25 } … … 27 27 public override IJsonItem Extract(IItem value, IJsonItemConverter root) => 28 28 new IntArrayJsonItem() { 29 Name = "[OverridableParamName]",29 Name = value.ItemName, 30 30 Description = value.ItemDescription, 31 31 Value = ((IntArray)value).CloneAsArray(), … … 41 41 public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) { 42 42 DoubleArray arr = item as DoubleArray; 43 DoubleArrayJsonItem doubleItem = data as DoubleArrayJsonItem; 44 //double[] d = CastValue<double[]>(data); 43 DoubleArrayJsonItem doubleArrayItem = data as DoubleArrayJsonItem; 45 44 bool resizeTmp = arr.Resizable; 46 45 arr.Resizable = true; 47 //arr.Length = d.Length; 48 arr.Length = doubleItem.Value.Length; 49 for (int i = 0; i < doubleItem.Value.Length; ++i) 50 arr[i] = doubleItem.Value[i]; 46 arr.Length = doubleArrayItem.Value.Length; 47 for (int i = 0; i < doubleArrayItem.Value.Length; ++i) 48 arr[i] = doubleArrayItem.Value[i]; 51 49 arr.Resizable = resizeTmp; 52 50 } … … 54 52 public override IJsonItem Extract(IItem value, IJsonItemConverter root) => 55 53 new DoubleArrayJsonItem() { 56 Name = "[OverridableParamName]",54 Name = value.ItemName, 57 55 Description = value.ItemDescription, 58 56 Value = ((DoubleArray)value).CloneAsArray(), … … 68 66 public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) { 69 67 PercentArray arr = item as PercentArray; 70 double[] d = CastValue<double[]>(data);68 DoubleArrayJsonItem doubleArrayItem = data as DoubleArrayJsonItem; 71 69 bool resizeTmp = arr.Resizable; 72 70 arr.Resizable = true; 73 arr.Length = d .Length;74 for (int i = 0; i < d .Length; ++i)75 arr[i] = d [i];71 arr.Length = doubleArrayItem.Value.Length; 72 for (int i = 0; i < doubleArrayItem.Value.Length; ++i) 73 arr[i] = doubleArrayItem.Value[i]; 76 74 arr.Resizable = resizeTmp; 77 75 } … … 79 77 public override IJsonItem Extract(IItem value, IJsonItemConverter root) => 80 78 new DoubleArrayJsonItem() { 81 Name = "[OverridableParamName]",79 Name = value.ItemName, 82 80 Description = value.ItemDescription, 83 81 Value = ((PercentArray)value).CloneAsArray(), … … 93 91 public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) { 94 92 BoolArray arr = item as BoolArray; 95 bool[] d = CastValue<bool[]>(data);93 BoolArrayJsonItem boolArrayItem = data as BoolArrayJsonItem; 96 94 bool resizeTmp = arr.Resizable; 97 95 arr.Resizable = true; 98 arr.Length = d.Length;99 for(int i = 0; i < d.Length; ++i)100 arr[i] = d[i];96 arr.Length = boolArrayItem.Value.Length; 97 for(int i = 0; i < boolArrayItem.Value.Length; ++i) 98 arr[i] = boolArrayItem.Value[i]; 101 99 arr.Resizable = resizeTmp; 102 100 } … … 104 102 public override IJsonItem Extract(IItem value, IJsonItemConverter root) => 105 103 new BoolArrayJsonItem() { 106 //Name = "[OverridableParamName]",107 104 Name = value.ItemName, 108 105 Description = value.ItemDescription, -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Interfaces/IIntervalRestrictedJsonItem.cs
r17473 r17477 7 7 namespace HeuristicLab.JsonInterface { 8 8 public interface IIntervalRestrictedJsonItem<T> : IJsonItem 9 where T : IComparable 10 { 9 where T : IComparable { 11 10 T Minimum { get; set; } 12 11 T Maximum { get; set; } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Interfaces/IJsonItem.cs
r17473 r17477 1 1 using System; 2 using System.Collections; 2 3 using System.Collections.Generic; 3 4 using System.Linq; … … 8 9 9 10 namespace HeuristicLab.JsonInterface { 10 public interface IJsonItem { 11 [JsonObject] 12 public interface IJsonItem : IEnumerable<IJsonItem> { 11 13 bool Active { get; set; } 12 14 string Name { get; set; } … … 23 25 [JsonIgnore] 24 26 IJsonItem Parent { get; set; } 25 26 //object Value { get; set; } 27 28 //IEnumerable<object> Range { get; set; } 29 27 30 28 IJsonItemValidator GetValidator(); 31 29 … … 46 44 void LoosenPath(); 47 45 48 void SetFromJObject(JObject jObject); 46 JObject GenerateJObject(); 47 void SetJObject(JObject jObject); 49 48 } 50 49 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Interfaces/IValueJsonItem.cs
r17473 r17477 8 8 public interface IValueJsonItem : IJsonItem { 9 9 object Value { get; set; } 10 //IEnumerable<object> Range { get; set; }11 10 } 12 11 13 12 public interface IValueJsonItem<T> : IValueJsonItem { 14 13 new T Value { get; set; } 15 //new IEnumerable<T> Range { get; set; }16 14 } 17 15 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Interfaces/IValueLookupJsonItem.cs
r17471 r17477 9 9 public interface IValueLookupJsonItem : ILookupJsonItem { 10 10 [JsonIgnore] 11 IJsonItem JsonItemReference { get; set; }11 IJsonItem ActualValue { get; set; } 12 12 } 13 13 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JCGenerator.cs
r17473 r17477 17 17 public class JCGenerator { 18 18 19 public void GenerateTemplate(string path, IOptimizer optimizer) =>19 public static void GenerateTemplate(string path, IOptimizer optimizer) => 20 20 GenerateTemplate(path, optimizer.Name, optimizer); 21 21 22 public void GenerateTemplate(string path, string templateName, IOptimizer optimizer) =>22 public static void GenerateTemplate(string path, string templateName, IOptimizer optimizer) => 23 23 GenerateTemplate(path, templateName, optimizer, JsonItemConverter.Extract(optimizer)); 24 24 25 public void GenerateTemplate(string path, string templateName, IOptimizer optimizer, IJsonItem rootItem) {26 // init25 public static void GenerateTemplate(string path, string templateName, IOptimizer optimizer, IJsonItem rootItem) { 26 #region Init 27 27 JObject template = JObject.Parse(Constants.Template); 28 28 JArray parameterItems = new JArray(); … … 30 30 IList<IJsonItem> jsonItems = new List<IJsonItem>(); 31 31 string fullPath = Path.GetFullPath(path); 32 32 #endregion 33 33 34 // recursively filter items with values/ranges/actualNames 34 35 PopulateJsonItems(rootItem, jsonItems); 35 36 36 // serialize hl file37 #region Serialize HL File 37 38 ProtoBufSerializer serializer = new ProtoBufSerializer(); 38 39 string hlFilePath = fullPath + @"\" + templateName + ".hl"; 39 40 serializer.Serialize(optimizer, hlFilePath); 41 #endregion 40 42 41 // filter result items43 #region Filter Items 42 44 foreach (var item in jsonItems) { 43 45 if (item is IResultJsonItem) 44 resultItems.Add( Serialize(item));46 resultItems.Add(item.GenerateJObject()); 45 47 else 46 parameterItems.Add( Serialize(item));48 parameterItems.Add(item.GenerateJObject()); 47 49 } 48 49 // set template data 50 #endregion 51 52 #region Set Template Data 50 53 template[Constants.Metadata][Constants.TemplateName] = templateName; 51 54 template[Constants.Metadata][Constants.HLFileLocation] = hlFilePath; 52 55 template[Constants.Parameters] = parameterItems; 53 56 template[Constants.Results] = resultItems; 57 #endregion 54 58 55 // serialize template and writefile59 #region Serialize and write to file 56 60 File.WriteAllText(fullPath + @"\" + templateName + ".json", SingleLineArrayJsonWriter.Serialize(template)); 61 #endregion 57 62 } 58 63 59 64 #region Helper 60 // serializes ParameterizedItems and saves them in list "JsonItems". 61 private void PopulateJsonItems(IJsonItem item, IList<IJsonItem> jsonItems) { 62 IEnumerable<IJsonItem> children = item.Children; 63 64 if (item.Active && !(item is EmptyJsonItem)) { 65 jsonItems.Add(item); 66 } 67 68 if (children != null) { 69 foreach (var p in children) { 70 PopulateJsonItems(p, jsonItems); 65 private static void PopulateJsonItems(IJsonItem item, IList<IJsonItem> jsonItems) { 66 foreach(var x in item) { 67 if (x.Active && !(x is EmptyJsonItem)) { 68 jsonItems.Add(x); 71 69 } 72 70 } 73 71 } 74 75 private static JObject Serialize(IJsonItem item) =>76 JObject.FromObject(item, Settings());77 78 /// <summary>79 /// Settings for the json serialization.80 /// </summary>81 /// <returns>A configured JsonSerializer.</returns>82 private static JsonSerializer Settings() => new JsonSerializer() {83 TypeNameHandling = TypeNameHandling.None,84 NullValueHandling = NullValueHandling.Ignore,85 ReferenceLoopHandling = ReferenceLoopHandling.Serialize86 };87 72 #endregion 88 73 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonTemplateInstantiator.cs
r17473 r17477 14 14 15 15 namespace HeuristicLab.JsonInterface { 16 /// <summary> 17 /// Static class to instantiate an IAlgorithm object with a json interface template and config. 18 /// </summary> 19 public static class JsonTemplateInstantiator { 20 private struct InstData { 21 public JToken Template { get; set; } 22 public JArray Config { get; set; } 23 public IDictionary<string, IJsonItem> Objects { get; set; } 24 public IOptimizer Optimizer { get; set; } 25 } 26 27 public static IOptimizer Instantiate(string templateFile) => 28 Instantiate(templateFile, null, out IEnumerable<string> allowedResultNames); 16 public struct InstantiatorResult { 17 public IOptimizer Optimizer { get; set; } 18 public IEnumerable<IResultJsonItem> ConfiguredResultItems { get; set; } 19 } 29 20 30 21 31 public static IOptimizer Instantiate(string templateFile, out IEnumerable<string> allowedResultNames) => 32 Instantiate(templateFile, null, out allowedResultNames); 22 /// <summary> 23 /// Class to instantiate an IAlgorithm object with a json interface template and config. 24 /// </summary> 25 public class JsonTemplateInstantiator { 33 26 34 27 #region Private Properties 28 private JToken Template { get; set; } 29 private JArray Config { get; set; } 30 private IDictionary<string, IJsonItem> Objects { get; set; } = new Dictionary<string, IJsonItem>(); 31 #endregion 35 32 36 33 /// <summary> … … 40 37 /// <param name="configFile">Config file (json) for the template.</param> 41 38 /// <returns>confugrated IOptimizer object</returns> 42 public static IOptimizer Instantiate(string templateFile, string configFile, out IEnumerable<string> allowedResultNames) { 43 InstData instData = new InstData() { 44 Objects = new Dictionary<string, IJsonItem>() 45 }; 46 47 // parse template and config files 48 instData.Template = JToken.Parse(File.ReadAllText(templateFile)); 49 if(!string.IsNullOrEmpty(configFile)) 50 instData.Config = JArray.Parse(File.ReadAllText(configFile)); 51 52 // extract metadata information 53 string hLFileLocation = Path.GetFullPath(instData.Template[Constants.Metadata][Constants.HLFileLocation].ToString()); 54 55 // deserialize hl file 56 ProtoBufSerializer serializer = new ProtoBufSerializer(); 57 IOptimizer optimizer = (IOptimizer)serializer.Deserialize(hLFileLocation); 58 instData.Optimizer = optimizer; 59 60 // collect all parameterizedItems from template 61 CollectParameterizedItems(instData); 62 63 // if config != null -> merge Template and Config 64 if (instData.Config != null) 65 MergeTemplateWithConfig(instData); 66 67 // get algorthm data and object 68 IJsonItem optimizerData = instData.Objects.First().Value; 69 70 // inject configuration 71 JsonItemConverter.Inject(optimizer, optimizerData); 72 73 allowedResultNames = CollectResults(instData); 74 75 return optimizer; 39 public static InstantiatorResult Instantiate(string templateFile, string configFile = null) { 40 JsonTemplateInstantiator instantiator = new JsonTemplateInstantiator(); 41 return instantiator.ExecuteInstantiaton(templateFile, configFile); 76 42 } 77 43 78 44 #region Helper 79 private static IEnumerable<string> CollectResults(InstData instData) { 80 IList<string> res = new List<string>(); 81 foreach(JObject obj in instData.Template[Constants.Results]) { 82 res.Add(obj.Property("Name").Value.ToString()); 45 private InstantiatorResult ExecuteInstantiaton(string templateFile, string configFile = null) { 46 InstantiatorResult result = new InstantiatorResult(); 47 48 #region Parse Files 49 Template = JToken.Parse(File.ReadAllText(templateFile)); 50 if(!string.IsNullOrEmpty(configFile)) 51 Config = JArray.Parse(File.ReadAllText(configFile)); 52 #endregion 53 54 // extract metadata information 55 string hLFileLocation = Path.GetFullPath(Template[Constants.Metadata][Constants.HLFileLocation].ToString()); 56 57 #region Deserialize HL File 58 ProtoBufSerializer serializer = new ProtoBufSerializer(); 59 IOptimizer optimizer = (IOptimizer)serializer.Deserialize(hLFileLocation); 60 result.Optimizer = optimizer; 61 #endregion 62 63 // collect all parameterizedItems from template 64 CollectParameterizedItems(optimizer); 65 66 if (Config != null) 67 MergeTemplateWithConfig(); 68 69 // get algorithm root item 70 IJsonItem rootItem = Objects.First().Value; 71 72 // inject configuration 73 JsonItemConverter.Inject(optimizer, rootItem); 74 75 result.ConfiguredResultItems = CollectResults(); 76 77 return result; 78 } 79 80 81 private IEnumerable<IResultJsonItem> CollectResults() { 82 IList<IResultJsonItem> res = new List<IResultJsonItem>(); 83 foreach(JObject obj in Template[Constants.Results]) { 84 string name = obj.Property("Name").Value.ToString(); 85 res.Add(new ResultJsonItem() { Name = name }); 83 86 } 84 87 return res; 85 88 } 86 89 87 private static void CollectParameterizedItems(InstData instData) {88 IJsonItem root = JsonItemConverter.Extract( instData.Optimizer);89 instData.Objects.Add(root.Path, root);90 private void CollectParameterizedItems(IOptimizer optimizer) { 91 IJsonItem root = JsonItemConverter.Extract(optimizer); 92 Objects.Add(root.Path, root); 90 93 91 foreach (JObject obj in instData.Template[Constants.Parameters]) { 92 string[] pathParts = obj.Property("Path").Value.ToString().Split('.'); 93 IJsonItem tmp = root; 94 IJsonItem old = null; 95 for(int i = 1; i < pathParts.Length; ++i) { 96 foreach(var c in tmp.Children) { 97 if (c.Name == pathParts[i]) 98 tmp = c; 94 foreach (JObject obj in Template[Constants.Parameters]) { 95 string path = obj.Property("Path").Value.ToString(); 96 foreach(var tmp in root) { 97 if(tmp.Path == path) { 98 tmp.SetJObject(obj); 99 Objects.Add(tmp.Path, tmp); 99 100 } 100 if (old == tmp)101 throw new Exception($"Invalid path '{string.Join(".", pathParts)}'");102 else old = tmp;103 101 } 104 tmp.SetFromJObject(obj);105 instData.Objects.Add(tmp.Path, tmp);106 102 } 107 //IList<IJsonItem> faultyItems = new List<IJsonItem>();108 //root.GetValidator().Validate(ref faultyItems);109 103 } 110 104 111 private static void MergeTemplateWithConfig(InstData instData) {112 foreach (JObject obj in instData.Config) {105 private void MergeTemplateWithConfig() { 106 foreach (JObject obj in Config) { 113 107 // build item from config object 114 108 string path = obj.Property("Path").Value.ToString(); 115 109 // override default value 116 if ( instData.Objects.TryGetValue(path, out IJsonItem param)) {110 if (Objects.TryGetValue(path, out IJsonItem param)) { 117 111 // remove fixed template parameter => dont allow to copy them from concrete config 118 112 obj.Property(nameof(IIntervalRestrictedJsonItem<int>.Minimum))?.Remove(); … … 120 114 obj.Property(nameof(IConcreteRestrictedJsonItem<string>.ConcreteRestrictedItems))?.Remove(); 121 115 // merge 122 param.Set FromJObject(obj);116 param.SetJObject(obj); 123 117 } else throw new InvalidDataException($"No parameter with path='{path}' defined!"); 124 118 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/ArrayJsonItem.cs
r17473 r17477 9 9 public abstract class ArrayJsonItem<T> : ValueJsonItem<T[]>, IArrayJsonItem { 10 10 public virtual bool Resizable { get; set; } 11 public override void Set FromJObject(JObject jObject) {12 base.Set FromJObject(jObject);11 public override void SetJObject(JObject jObject) { 12 base.SetJObject(jObject); 13 13 Resizable = (jObject[nameof(IArrayJsonItem.Resizable)]?.ToObject<bool>()).GetValueOrDefault(); 14 14 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/DateTimeJsonItem.cs
r17473 r17477 4 4 using System.Text; 5 5 using System.Threading.Tasks; 6 using Newtonsoft.Json.Linq; 6 7 7 8 namespace HeuristicLab.JsonInterface { 8 9 public class DateTimeJsonItem : IntervalRestrictedValueJsonItem<DateTime> { 9 10 protected override bool Validate() => Minimum.CompareTo(Value) >= 0 && Maximum.CompareTo(Value) <= 0; 11 12 public override JObject GenerateJObject() { 13 var obj = base.GenerateJObject(); 14 15 if (Minimum.CompareTo(DateTime.MinValue) == 0) 16 obj.Property("Minimum").Remove(); 17 18 if (Maximum.CompareTo(DateTime.MaxValue) == 0) 19 obj.Property("Maximum").Remove(); 20 21 return obj; 22 } 23 24 public override void SetJObject(JObject jObject) { 25 Minimum = DateTime.MinValue; 26 Maximum = DateTime.MaxValue; 27 base.SetJObject(jObject); 28 } 10 29 } 11 30 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/DoubleJsonItems.cs
r17473 r17477 4 4 using System.Text; 5 5 using System.Threading.Tasks; 6 using Newtonsoft.Json.Linq; 6 7 7 8 namespace HeuristicLab.JsonInterface { 8 public class DoubleJsonItem : IntervalRestrictedValueJsonItem<double> { }9 public class DoubleArrayJsonItem : IntervalRestrictedArrayJsonItem<double> { }10 public class DoubleRangeJsonItem : RangedJsonItem<double> { }9 public class DoubleJsonItem : IntervalRestrictedValueJsonItem<double> { 10 public override JObject GenerateJObject() { 11 var obj = base.GenerateJObject(); 11 12 13 if (Minimum.CompareTo(double.MinValue) == 0) 14 obj.Property("Minimum").Remove(); 15 16 if (Maximum.CompareTo(double.MaxValue) == 0) 17 obj.Property("Maximum").Remove(); 18 19 return obj; 20 } 21 22 public override void SetJObject(JObject jObject) { 23 Minimum = double.MinValue; 24 Maximum = double.MaxValue; 25 base.SetJObject(jObject); 26 } 27 } 28 public class DoubleArrayJsonItem : IntervalRestrictedArrayJsonItem<double> { 29 public override JObject GenerateJObject() { 30 var obj = base.GenerateJObject(); 31 32 if (Minimum.CompareTo(double.MinValue) == 0) 33 obj.Property("Minimum").Remove(); 34 35 if (Maximum.CompareTo(double.MaxValue) == 0) 36 obj.Property("Maximum").Remove(); 37 38 return obj; 39 } 40 41 public override void SetJObject(JObject jObject) { 42 Minimum = double.MinValue; 43 Maximum = double.MaxValue; 44 base.SetJObject(jObject); 45 } 46 } 47 public class DoubleRangeJsonItem : RangedJsonItem<double> { 48 public override JObject GenerateJObject() { 49 var obj = base.GenerateJObject(); 50 51 if (Minimum.CompareTo(double.MinValue) == 0) 52 obj.Property("Minimum").Remove(); 53 54 if (Maximum.CompareTo(double.MaxValue) == 0) 55 obj.Property("Maximum").Remove(); 56 57 return obj; 58 } 59 60 public override void SetJObject(JObject jObject) { 61 Minimum = double.MinValue; 62 Maximum = double.MaxValue; 63 base.SetJObject(jObject); 64 } 65 } 12 66 public class DoubleMatrixJsonItem : IntervalRestrictedMatrixJsonItem<double> { 13 /* 14 protected override bool IsInRange() { 15 for (int c = 0; c < Value.Length; ++c) { 16 for (int r = 0; r < Value[c].Length; ++r) { 17 if (Value[c][r] < Range.First() && Range.Last() < Value[c][r]) 18 return false; 19 } 20 } 21 return true; 67 public override JObject GenerateJObject() { 68 var obj = base.GenerateJObject(); 69 70 if (Minimum.CompareTo(double.MinValue) == 0) 71 obj.Property("Minimum").Remove(); 72 73 if (Maximum.CompareTo(double.MaxValue) == 0) 74 obj.Property("Maximum").Remove(); 75 76 return obj; 22 77 } 23 */ 78 79 public override void SetJObject(JObject jObject) { 80 Minimum = double.MinValue; 81 Maximum = double.MaxValue; 82 base.SetJObject(jObject); 83 } 24 84 } 25 85 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/IntJsonItems.cs
r17473 r17477 4 4 using System.Text; 5 5 using System.Threading.Tasks; 6 using Newtonsoft.Json.Linq; 6 7 7 8 namespace HeuristicLab.JsonInterface { 8 public class IntJsonItem : IntervalRestrictedValueJsonItem<int> { } 9 public class IntArrayJsonItem : IntervalRestrictedArrayJsonItem<int> { } 10 public class IntRangeJsonItem : RangedJsonItem<int> { } 9 public class IntJsonItem : IntervalRestrictedValueJsonItem<int> { 10 public override JObject GenerateJObject() { 11 var obj = base.GenerateJObject(); 12 13 if (Minimum.CompareTo(int.MinValue) == 0) 14 obj.Property("Minimum").Remove(); 15 16 if (Maximum.CompareTo(int.MaxValue) == 0) 17 obj.Property("Maximum").Remove(); 18 19 return obj; 20 } 21 22 public override void SetJObject(JObject jObject) { 23 Minimum = int.MinValue; 24 Maximum = int.MaxValue; 25 base.SetJObject(jObject); 26 } 27 } 28 public class IntArrayJsonItem : IntervalRestrictedArrayJsonItem<int> { 29 public override JObject GenerateJObject() { 30 var obj = base.GenerateJObject(); 31 32 if (Minimum.CompareTo(int.MinValue) == 0) 33 obj.Property("Minimum").Remove(); 34 35 if (Maximum.CompareTo(int.MaxValue) == 0) 36 obj.Property("Maximum").Remove(); 37 38 return obj; 39 } 40 41 public override void SetJObject(JObject jObject) { 42 Minimum = int.MinValue; 43 Maximum = int.MaxValue; 44 base.SetJObject(jObject); 45 } 46 } 47 public class IntRangeJsonItem : RangedJsonItem<int> { 48 public override JObject GenerateJObject() { 49 var obj = base.GenerateJObject(); 50 51 if (Minimum.CompareTo(int.MinValue) == 0) 52 obj.Property("Minimum").Remove(); 53 54 if (Maximum.CompareTo(int.MaxValue) == 0) 55 obj.Property("Maximum").Remove(); 56 57 return obj; 58 } 59 60 public override void SetJObject(JObject jObject) { 61 Minimum = int.MinValue; 62 Maximum = int.MaxValue; 63 base.SetJObject(jObject); 64 } 65 } 11 66 public class IntMatrixJsonItem : IntervalRestrictedMatrixJsonItem<int> { 12 /* 13 protected override bool IsInRange() { 14 for (int c = 0; c < Value.Length; ++c) { 15 for (int r = 0; r < Value[c].Length; ++r) { 16 if (Value[c][r] < Range.First() && Range.Last() < Value[c][r]) 17 return false; 18 } 19 } 20 return true; 67 public override JObject GenerateJObject() { 68 var obj = base.GenerateJObject(); 69 70 if (Minimum.CompareTo(int.MinValue) == 0) 71 obj.Property("Minimum").Remove(); 72 73 if (Maximum.CompareTo(int.MaxValue) == 0) 74 obj.Property("Maximum").Remove(); 75 76 return obj; 21 77 } 22 */ 78 79 public override void SetJObject(JObject jObject) { 80 Minimum = int.MinValue; 81 Maximum = int.MaxValue; 82 base.SetJObject(jObject); 83 } 23 84 } 24 85 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/IntervalRestrictedArrayJsonItem.cs
r17473 r17477 20 20 } 21 21 22 public override void SetFromJObject(JObject jObject) { 23 base.SetFromJObject(jObject); 24 Minimum = jObject[nameof(IIntervalRestrictedJsonItem<T>.Minimum)].ToObject<T>(); 25 Maximum = jObject[nameof(IIntervalRestrictedJsonItem<T>.Maximum)].ToObject<T>(); 22 public override void SetJObject(JObject jObject) { 23 base.SetJObject(jObject); 24 25 var minProp = jObject[nameof(IIntervalRestrictedJsonItem<T>.Minimum)]; 26 if (minProp != null) Minimum = minProp.ToObject<T>(); 27 28 29 var maxProp = jObject[nameof(IIntervalRestrictedJsonItem<T>.Maximum)]; 30 if (maxProp != null) Maximum = maxProp.ToObject<T>(); 26 31 } 27 32 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/IntervalRestrictedMatrixJsonItem.cs
r17473 r17477 20 20 return true; 21 21 } 22 public override void SetFromJObject(JObject jObject) { 23 base.SetFromJObject(jObject); 24 Minimum = jObject[nameof(IIntervalRestrictedJsonItem<T>.Minimum)].ToObject<T>(); 25 Maximum = jObject[nameof(IIntervalRestrictedJsonItem<T>.Maximum)].ToObject<T>(); 22 public override void SetJObject(JObject jObject) { 23 base.SetJObject(jObject); 24 25 var minProp = jObject[nameof(IIntervalRestrictedJsonItem<T>.Minimum)]; 26 if (minProp != null) Minimum = minProp.ToObject<T>(); 27 28 29 var maxProp = jObject[nameof(IIntervalRestrictedJsonItem<T>.Maximum)]; 30 if (maxProp != null) Maximum = maxProp.ToObject<T>(); 26 31 } 27 32 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/IntervalRestrictedValueJsonItem.cs
r17473 r17477 14 14 protected override bool Validate() => Minimum.CompareTo(Value) <= 0 && Maximum.CompareTo(Value) >= 0; 15 15 16 public override void SetFromJObject(JObject jObject) { 17 base.SetFromJObject(jObject); 18 Minimum = jObject[nameof(IIntervalRestrictedJsonItem<T>.Minimum)].ToObject<T>(); 19 Maximum = jObject[nameof(IIntervalRestrictedJsonItem<T>.Maximum)].ToObject<T>(); 16 public override void SetJObject(JObject jObject) { 17 base.SetJObject(jObject); 18 19 var minProp = jObject[nameof(IIntervalRestrictedJsonItem<T>.Minimum)]; 20 if (minProp != null) Minimum = minProp.ToObject<T>(); 21 22 23 var maxProp = jObject[nameof(IIntervalRestrictedJsonItem<T>.Maximum)]; 24 if (maxProp != null) Maximum = maxProp.ToObject<T>(); 20 25 } 21 26 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/JsonItem.cs
r17473 r17477 62 62 } 63 63 } 64 65 //public virtual object Value { get; set; } 66 67 //public virtual IEnumerable<object> Range { get; set; } 68 64 69 65 // TODO jsonIgnore dataType? 70 66 … … 105 101 public void LoosenPath() => fixedPath = ""; 106 102 107 public virtual void SetFromJObject(JObject jObject) { 108 //Value = jObject[nameof(IJsonItem.Value)]?.ToObject<object>(); 109 //Range = jObject[nameof(IJsonItem.Range)]?.ToObject<object[]>(); 110 } 103 public virtual JObject GenerateJObject() => 104 JObject.FromObject(this, new JsonSerializer() { 105 TypeNameHandling = TypeNameHandling.None, 106 NullValueHandling = NullValueHandling.Ignore, 107 ReferenceLoopHandling = ReferenceLoopHandling.Ignore 108 }); 109 110 public virtual void SetJObject(JObject jObject) { } 111 111 #endregion 112 112 113 #region Helper 114 /* 115 * TODO protected abstract bool Validate(); 116 */ 113 #region Abstract Methods 117 114 protected abstract bool Validate(); 118 /* 119 protected virtual bool IsInRange() { 120 bool b1 = true, b2 = true; 121 if (Value is IEnumerable && !(Value is string)) { 122 foreach (var x in (IEnumerable)Value) { 123 b1 = b1 ? IsInRangeList(x) : b1; 124 b2 = b2 ? IsInNumericRange(x) : b2; 115 #endregion 116 117 #region IEnumerable Support 118 public virtual IEnumerator<IJsonItem> GetEnumerator() { 119 yield return this; 120 121 if (Children != null) { 122 foreach (var x in Children) { 123 foreach (var c in x) { 124 yield return c; 125 } 125 126 } 126 } 127 else { 128 b1 = IsInRangeList(Value); 129 b2 = IsInNumericRange(Value); 130 } 131 return b1 || b2; 127 } 132 128 } 133 129 134 protected bool IsInRangeList(object value) { 135 foreach (var x in Range) 136 if (x.Equals(value)) return true; 137 return false; 138 } 139 140 protected bool IsInNumericRange(object value) => 141 IsInNumericRange<ulong>(value) 142 || IsInNumericRange<uint>(value) 143 || IsInNumericRange<ushort>(value) 144 || IsInNumericRange<long>(value) 145 || IsInNumericRange<int>(value) 146 || IsInNumericRange<short>(value) 147 || IsInNumericRange<byte>(value) 148 || IsInNumericRange<float>(value) 149 || IsInNumericRange<double>(value) 150 || (value is float && float.IsNaN((float)value)) 151 || (value is double && double.IsNaN((double)value)); 152 153 protected bool IsInNumericRange<T>(object value) where T : IComparable { 154 object min = Range.First(), max = Range.Last(); 155 return 156 value != null && min != null && max != null && value is T && min is T && max is T && 157 (((T)min).CompareTo(value) == -1 || ((T)min).CompareTo(value) == 0) && 158 (((T)max).CompareTo(value) == 1 || ((T)max).CompareTo(value) == 0); 159 } 160 */ 130 IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); 161 131 #endregion 162 132 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/LookupJsonItem.cs
r17473 r17477 10 10 public virtual string ActualName { get; set; } 11 11 12 public override void Set FromJObject(JObject jObject) {13 base.Set FromJObject(jObject);12 public override void SetJObject(JObject jObject) { 13 base.SetJObject(jObject); 14 14 ActualName = jObject[nameof(ILookupJsonItem.ActualName)]?.ToString(); 15 15 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/MatrixJsonItem.cs
r17473 r17477 23 23 } 24 24 25 public override void Set FromJObject(JObject jObject) {26 base.Set FromJObject(jObject);25 public override void SetJObject(JObject jObject) { 26 base.SetJObject(jObject); 27 27 RowsResizable = (jObject[nameof(IMatrixJsonItem.RowsResizable)]?.ToObject<bool>()).GetValueOrDefault(); 28 28 ColumnsResizable = (jObject[nameof(IMatrixJsonItem.ColumnsResizable)]?.ToObject<bool>()).GetValueOrDefault(); -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/ValueJsonItem.cs
r17473 r17477 10 10 public object Value { get; set; } 11 11 12 //public IEnumerable<object> Range { get; set; } 13 14 public override void SetFromJObject(JObject jObject) { 12 public override void SetJObject(JObject jObject) { 15 13 Value = jObject[nameof(IValueJsonItem.Value)]?.ToObject<object>(); 16 //Range = jObject[nameof(IValueJsonItem.Range)]?.ToObject<object[]>();17 14 } 18 15 … … 24 21 set => base.Value = value; 25 22 } 26 27 /*28 public new IEnumerable<T> Range {29 get => base.Range?.Cast<T>();30 set => base.Range = value.Cast<object>();31 }32 */33 23 34 24 private T ConvertObject(object obj) { … … 42 32 } 43 33 44 public override void Set FromJObject(JObject jObject) {34 public override void SetJObject(JObject jObject) { 45 35 if(jObject[nameof(IValueJsonItem<T>.Value)] != null) 46 36 Value = jObject[nameof(IValueJsonItem<T>.Value)].ToObject<T>(); 47 //Range = jObject[nameof(IValueJsonItem<T>.Range)]?.ToObject<T[]>();48 37 } 49 38 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/ValueLookupJsonItem.cs
r17473 r17477 5 5 using System.Threading.Tasks; 6 6 using Newtonsoft.Json; 7 using Newtonsoft.Json.Linq; 7 8 8 9 namespace HeuristicLab.JsonInterface { 9 10 public class ValueLookupJsonItem : LookupJsonItem, IValueLookupJsonItem { 10 [JsonIgnore]11 public IJsonItem JsonItemReference { get; set; }11 12 public IJsonItem ActualValue { get; set; } 12 13 13 14 protected override bool Validate() { 14 if ( JsonItemReference == null) return true;15 if (ActualValue == null) return true; 15 16 IList<IJsonItem> faultyItems = new List<IJsonItem>(); 16 return JsonItemReference.GetValidator().Validate(ref faultyItems); 17 return ActualValue.GetValidator().Validate(ref faultyItems); 18 } 19 20 public override JObject GenerateJObject() { 21 var obj = base.GenerateJObject(); 22 if(ActualValue != null) { 23 var actualValue = ActualValue.GenerateJObject(); 24 obj.Add(nameof(IValueLookupJsonItem.ActualValue), actualValue); 25 } 26 return obj; 27 } 28 29 public override IEnumerator<IJsonItem> GetEnumerator() { 30 using (var it = base.GetEnumerator()) { 31 while(it.MoveNext()) { 32 yield return it.Current; 33 } 34 } 35 if(ActualValue != null) { 36 using (var it = ActualValue.GetEnumerator()) { 37 while (it.MoveNext()) { 38 yield return it.Current; 39 } 40 } 41 } 17 42 } 18 43 } -
branches/3026_IntegrationIntoSymSpace/Heuristiclab.ConfigStarter/Program.cs
r17473 r17477 47 47 IJsonItem root = JsonItemConverter.Extract(alg); 48 48 ActivateJsonItems(root); 49 //var x = root.Children[0];50 //root.Children.Remove(x);51 //x.Parent = null;52 49 53 54 JCGenerator generator = new JCGenerator(); 55 generator.GenerateTemplate(@"C:\Workspace", "Template", alg, root); 56 //JsonTemplateInstantiator.Instantiate(@"C:\Workspace\Template.json"); 50 JCGenerator.GenerateTemplate(@"C:\Workspace", "Template", alg, root); 57 51 58 52 List<ICommandLineArgument> arguments = new List<ICommandLineArgument>(); 59 53 arguments.Add(new StartArgument("JsonInterface")); 60 54 arguments.Add(new OpenArgument(@"C:\Workspace\Template.json")); 61 arguments.Add(new OpenArgument(@"C:\Workspace\Config Proto1.json"));55 arguments.Add(new OpenArgument(@"C:\Workspace\Config.json")); 62 56 arguments.Add(new StringArgument(@"C:\Workspace\Output.json")); 63 57 … … 67 61 68 62 private static void ActivateJsonItems(IJsonItem item) { 69 item.Active = true; 70 if(item.Children != null) { 71 foreach (var x in item.Children) 72 ActivateJsonItems(x); 63 foreach (var x in item) { 64 x.Active = true; 65 if (x is ValueLookupJsonItem i) { 66 i.Active = true; 67 } 73 68 } 74 69 }
Note: See TracChangeset
for help on using the changeset viewer.