Changeset 17322 for branches/3026_IntegrationIntoSymSpace
- Timestamp:
- 10/08/19 14:13:42 (5 years ago)
- Location:
- branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface
- Files:
-
- 2 deleted
- 5 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/MultiCheckedOperatorConverter.cs
r17284 r17322 29 29 30 30 dynamic val = item.Cast<dynamic>(); 31 foreach (var op in val.Operators) {31 foreach (var op in val.Operators) 32 32 val.Operators.SetItemCheckedState(op, GetOperatorState(op.Name, data)); 33 }34 33 } 35 34 -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/HeuristicLab.JsonInterface.csproj
r17287 r17322 6 6 <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> 7 7 <ProjectGuid>{0E3AAB5E-F152-44E0-A054-4D9A83ECEE08}</ProjectGuid> 8 <OutputType> Exe</OutputType>8 <OutputType>Library</OutputType> 9 9 <RootNamespace>HeuristicLab.JsonInterface</RootNamespace> 10 10 <AssemblyName>HeuristicLab.JsonInterface</AssemblyName> 11 <TargetFrameworkVersion>v4.6. 2</TargetFrameworkVersion>11 <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> 12 12 <FileAlignment>512</FileAlignment> 13 13 <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> 14 14 <Deterministic>true</Deterministic> 15 <TargetFrameworkProfile /> 15 16 </PropertyGroup> 16 17 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> … … 19 20 <DebugType>full</DebugType> 20 21 <Optimize>false</Optimize> 21 <OutputPath> bin\Debug\</OutputPath>22 <OutputPath>..\bin\</OutputPath> 22 23 <DefineConstants>DEBUG;TRACE</DefineConstants> 23 24 <ErrorReport>prompt</ErrorReport> … … 32 33 <ErrorReport>prompt</ErrorReport> 33 34 <WarningLevel>4</WarningLevel> 35 </PropertyGroup> 36 <PropertyGroup> 37 <StartupObject /> 38 </PropertyGroup> 39 <PropertyGroup> 40 <SignAssembly>true</SignAssembly> 41 </PropertyGroup> 42 <PropertyGroup> 43 <AssemblyOriginatorKeyFile>HeuristicLab.snk</AssemblyOriginatorKeyFile> 34 44 </PropertyGroup> 35 45 <ItemGroup> … … 71 81 <Compile Include="Converters\ValueTypeMatrixConverter.cs" /> 72 82 <Compile Include="Converters\ValueTypeValueConverter.cs" /> 73 <Compile Include="Program.cs" />74 83 <Compile Include="Properties\AssemblyInfo.cs" /> 75 84 <Compile Include="JsonItem.cs" /> … … 78 87 </ItemGroup> 79 88 <ItemGroup> 80 <None Include=" App.config" />89 <None Include="HeuristicLab.snk" /> 81 90 <None Include="packages.config" /> 82 91 </ItemGroup> -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JCGenerator.cs
r17287 r17322 77 77 objToRemove.Add(x); 78 78 } else { 79 x.Property(nameof(JsonItem.Path))?.Remove();79 //x.Property(nameof(JsonItem.Path))?.Remove(); 80 80 x.Property(nameof(JsonItem.Type))?.Remove(); 81 81 x.Property(nameof(JsonItem.Parameters))?.Remove(); -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JCInstantiator.cs
r17287 r17322 15 15 public class JCInstantiator { 16 16 17 private JToken Config { get; set; } 17 private JToken Template { get; set; } 18 private JArray Config { get; set; } 19 18 20 private Dictionary<string, string> TypeList = new Dictionary<string, string>(); 21 private IDictionary<string, JsonItem> ParameterizedItems { get; set; } = new Dictionary<string, JsonItem>(); 22 private IDictionary<string, JsonItem> ConfigurableItems { get; set; } = new Dictionary<string, JsonItem>(); 23 24 public IAlgorithm Instantiate(string templateFile, string configFile) { 19 25 20 public IAlgorithm Instantiate(string configFile) { 21 Config = JToken.Parse(File.ReadAllText(configFile)); 22 TypeList = Config[Constants.Types].ToObject<Dictionary<string, string>>(); 26 //1. Parse Template and Config files 27 Template = JToken.Parse(File.ReadAllText(templateFile)); 28 Config = JArray.Parse(File.ReadAllText(configFile)); 29 TypeList = Template[Constants.Types].ToObject<Dictionary<string, string>>(); 30 string algorithmName = Template[Constants.Metadata][Constants.Algorithm].ToString(); 31 string problemName = Template[Constants.Metadata][Constants.Problem].ToString(); 23 32 24 JsonItem algorithmData = GetData(Config[Constants.Metadata][Constants.Algorithm].ToString()); 25 ResolveReferences(algorithmData); 33 //2. Collect all parameterizedItems from template 34 CollectParameterizedItems(); 35 36 //3. select all ConfigurableItems 37 SelectConfigurableItems(); 38 39 //4. Merge Template and Config 40 MergeTemplateWithConfig(); 41 42 //5. resolve the references between parameterizedItems 43 ResolveReferences(); 44 45 //6. get algorthm data and object 46 JsonItem algorithmData = GetData(algorithmName); 26 47 IAlgorithm algorithm = CreateObject<IAlgorithm>(algorithmData); 27 28 JsonItem problemData = GetData(Config[Constants.Metadata][Constants.Problem].ToString());29 ResolveReferences(problemData);48 49 //7. get problem data and object 50 JsonItem problemData = GetData(problemName); 30 51 IProblem problem = CreateObject<IProblem>(problemData); 31 52 algorithm.Problem = problem; 32 53 54 //8. inject configuration 33 55 JsonItemConverter.Inject(algorithm, algorithmData); 34 JsonItemConverter.Inject( algorithm, problemData);56 JsonItemConverter.Inject(problem, problemData); 35 57 36 58 return algorithm; 37 59 } 38 60 39 private void ResolveReferences(JsonItem data) { 40 foreach (var p in data.Parameters) 41 if (p.Default is string && p.Reference == null) 42 p.Reference = GetData(p.Default.Cast<string>()); 61 private void CollectParameterizedItems() { 62 foreach (JObject item in Template[Constants.Objects]) { 63 JsonItem data = BuildJsonItem(item); 64 ParameterizedItems.Add(data.Path, data); 65 } 66 } 67 68 private void SelectConfigurableItems() { 69 foreach (var item in ParameterizedItems.Values) { 70 if (item.Parameters != null) 71 AddConfigurableItems(item.Parameters); 72 73 if (item.Operators != null) 74 AddConfigurableItems(item.Operators); 75 } 76 } 77 78 private void AddConfigurableItems(IEnumerable<JsonItem> items) { 79 foreach (var item in items) 80 if (item.IsConfigurable) 81 ConfigurableItems.Add(item.Path, item); 82 } 83 84 private void ResolveReferences() { 85 foreach(var x in ParameterizedItems.Values) 86 foreach (var p in x.Parameters) 87 if (p.Default is string) { 88 string key = $"{p.Path}.{p.Default.Cast<string>()}"; 89 if (ParameterizedItems.TryGetValue(key, out JsonItem value)) 90 p.Reference = value; 91 } 92 } 93 94 private void MergeTemplateWithConfig() { 95 foreach (JObject obj in Config) { 96 // build item from config object 97 JsonItem item = BuildJsonItem(obj); 98 // override default value 99 if (ConfigurableItems.TryGetValue(item.Path, out JsonItem param)) { 100 param.Default = item.Default; 101 // override default value of reference (for ValueLookupParameters) 102 if (param.Reference != null) 103 param.Reference.Default = item.Reference?.Default; 104 } else throw new InvalidDataException($"No {Constants.FreeParameters.Trim('s')} with path='{item.Path}' defined!"); 105 } 43 106 } 44 107 45 108 private JsonItem GetData(string key) 46 109 { 47 foreach(JObject item in Config[Constants.Objects]) 48 { 49 JsonItem data = BuildJsonItem(item); 50 if (data.Name == key) return data; 51 } 52 return null; 110 if (ParameterizedItems.TryGetValue(key, out JsonItem value)) 111 return value; 112 else 113 throw new InvalidDataException($"Type of item '{key}' is not defined!"); 53 114 } 54 115 … … 60 121 } 61 122 123 #region BuildJsonItemMethods 62 124 private JsonItem BuildJsonItem(JObject obj) => 63 125 new JsonItem() { 64 126 Name = obj[nameof(JsonItem.Name)]?.ToString(), 127 Path = obj[nameof(JsonItem.Path)]?.ToString(), 65 128 Default = obj[nameof(JsonItem.Default)]?.ToObject<object>(), 66 129 Range = obj[nameof(JsonItem.Range)]?.ToObject<object[]>(), 67 Type = obj[nameof(JsonItem.Type)]?.ToObject<string>(),130 Type = GetType(obj[nameof(JsonItem.Path)]?.ToObject<string>()), 68 131 Reference = obj[nameof(JsonItem.Type)] == null ? 69 132 null : … … 73 136 }; 74 137 138 private string GetType(string path) { 139 if(!string.IsNullOrEmpty(path)) 140 if (TypeList.TryGetValue(path, out string value)) 141 return value; 142 return null; 143 } 144 75 145 private IList<JsonItem> PopulateParameters(JObject obj) { 76 146 IList<JsonItem> list = new List<JsonItem>(); 147 148 // add staticParameters 77 149 if (obj[Constants.StaticParameters] != null) 78 150 foreach (JObject param in obj[Constants.StaticParameters]) 79 151 list.Add(BuildJsonItem(param)); 80 152 153 // merge staticParameter with freeParameter 81 154 if (obj[Constants.FreeParameters] != null) { 82 155 foreach (JObject param in obj[Constants.FreeParameters]) { 83 156 JsonItem tmp = BuildJsonItem(param); 157 158 // search staticParameter from list 84 159 JsonItem comp = null; 85 foreach (var p in list) // TODO: nicht notwendig, da immer alle params im static block sind160 foreach (var p in list) 86 161 if (p.Name == tmp.Name) comp = p; 87 if (comp != null) 88 JsonItem.Merge(comp, tmp); 89 else list.Add(tmp); 162 if (comp == null) 163 throw new InvalidDataException($"Invalid {Constants.FreeParameters.Trim('s')}: '{tmp.Name}'!"); 164 165 JsonItem.Merge(comp, tmp); 90 166 } 91 167 } … … 95 171 private IList<JsonItem> PopulateOperators(JObject obj) { 96 172 IList<JsonItem> list = new List<JsonItem>(); 97 if (obj[nameof(Operators)] != null) 98 foreach (JObject sp in obj[nameof(Operators)]) { 99 JsonItem tmp = BuildJsonItem(sp); 100 list.Add(tmp); 101 } 173 JToken operators = obj[nameof(Operators)]; 174 if (operators != null) 175 foreach (JObject sp in operators) 176 list.Add(BuildJsonItem(sp)); 102 177 return list; 103 178 } 179 #endregion 104 180 } 105 181 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonItem.cs
r17287 r17322 15 15 public string Name { get; set; } 16 16 public string Type { get; set; } 17 public string Path { get; set; } = ""; 18 public IList<JsonItem> Parameters { get; set; } 19 public IList<JsonItem> Operators { get; set; } 20 17 21 public object Default { 18 22 get => defaultValue; … … 23 27 } 24 28 } 25 public string Path { get; set; } = "";26 29 27 30 public IList<object> Range { … … 33 36 } 34 37 } 38 39 public JsonItem Reference { get; set; } 35 40 36 public IList<JsonItem> Parameters { get; set; } 37 public IList<JsonItem> Operators { get; set; } 38 39 public override bool Equals(object obj) => 40 (obj is JsonItem ? (obj.Cast<JsonItem>().Name == this.Name) : false); 41 42 public override int GetHashCode() => Name.GetHashCode(); 41 [JsonIgnore] 42 public bool IsConfigurable => (Default != null && Range != null); 43 43 44 public JsonItem Reference { get; set; }45 46 44 public static void Merge(JsonItem target, JsonItem from) { 47 45 target.Name = from.Name ?? target.Name; … … 70 68 if (Parameters != null) 71 69 foreach (var p in Parameters) 72 p.Path = Path + "." + p.Name;70 p.Path = $"{Path}.{p.Name}"; 73 71 74 72 if (Operators != null) 75 73 foreach (var p in Operators) 76 p.Path = Path + "." + p.Name;74 p.Path = $"{Path}.{p.Name}"; 77 75 } 78 76 … … 81 79 PrependPathHelper(Parameters, str); 82 80 PrependPathHelper(Operators, str); 83 }84 85 private void PrependPathHelper(IEnumerable<JsonItem> items, string str) {86 if (items != null)87 foreach (var p in items)88 p.PrependPath(str);89 81 } 90 82 … … 100 92 (((T)min).CompareTo(value) == -1 || ((T)min).CompareTo(value) == 0) && 101 93 (((T)max).CompareTo(value) == 1 || ((T)max).CompareTo(value) == 0)); 94 95 private void PrependPathHelper(IEnumerable<JsonItem> items, string str) { 96 if (items != null) 97 foreach (var p in items) 98 p.PrependPath(str); 99 } 102 100 #endregion 103 101 }
Note: See TracChangeset
for help on using the changeset viewer.