Changeset 17330


Ignore:
Timestamp:
10/15/19 15:29:36 (2 years ago)
Author:
dpiringe
Message:

#3026

  • changed the access modifiers in Constants from public to internal
  • added comments in JCGenerator
  • the config file for JCInstantiator is now optional
  • added Runner.cs (forgot last commit)
Location:
branches/3026_IntegrationIntoSymSpace
Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab 3.3.sln

    r17324 r17330  
    477477EndProject
    478478Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.JsonInterface.App", "HeuristicLab.JsonInterface.App\HeuristicLab.JsonInterface.App.csproj", "{039D995F-1AA5-4461-92B3-B466F612D998}"
     479EndProject
     480Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.JsonInterface.OptimizerIntegration", "HeuristicLab.JsonInterface.OptimizerIntegration\HeuristicLab.JsonInterface.OptimizerIntegration.csproj", "{5A3413F8-0928-4167-80E7-AAD9D6E38A94}"
    479481EndProject
    480482Global
     
    23322334    {039D995F-1AA5-4461-92B3-B466F612D998}.Release|x86.ActiveCfg = Release|Any CPU
    23332335    {039D995F-1AA5-4461-92B3-B466F612D998}.Release|x86.Build.0 = Release|Any CPU
     2336    {5A3413F8-0928-4167-80E7-AAD9D6E38A94}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
     2337    {5A3413F8-0928-4167-80E7-AAD9D6E38A94}.Debug|Any CPU.Build.0 = Debug|Any CPU
     2338    {5A3413F8-0928-4167-80E7-AAD9D6E38A94}.Debug|x64.ActiveCfg = Debug|Any CPU
     2339    {5A3413F8-0928-4167-80E7-AAD9D6E38A94}.Debug|x64.Build.0 = Debug|Any CPU
     2340    {5A3413F8-0928-4167-80E7-AAD9D6E38A94}.Debug|x86.ActiveCfg = Debug|Any CPU
     2341    {5A3413F8-0928-4167-80E7-AAD9D6E38A94}.Debug|x86.Build.0 = Debug|Any CPU
     2342    {5A3413F8-0928-4167-80E7-AAD9D6E38A94}.Release|Any CPU.ActiveCfg = Release|Any CPU
     2343    {5A3413F8-0928-4167-80E7-AAD9D6E38A94}.Release|Any CPU.Build.0 = Release|Any CPU
     2344    {5A3413F8-0928-4167-80E7-AAD9D6E38A94}.Release|x64.ActiveCfg = Release|Any CPU
     2345    {5A3413F8-0928-4167-80E7-AAD9D6E38A94}.Release|x64.Build.0 = Release|Any CPU
     2346    {5A3413F8-0928-4167-80E7-AAD9D6E38A94}.Release|x86.ActiveCfg = Release|Any CPU
     2347    {5A3413F8-0928-4167-80E7-AAD9D6E38A94}.Release|x86.Build.0 = Release|Any CPU
    23342348  EndGlobalSection
    23352349  GlobalSection(SolutionProperties) = preSolution
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.App/Plugin.cs.frame

    r17324 r17330  
    2020#endregion
    2121
    22 using System;
    23 using System.IO;
    24 using HeuristicLab.JsonInterface;
    25 using HeuristicLab.Optimization;
    2622using HeuristicLab.PluginInfrastructure;
    2723
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Constants.cs

    r17287 r17330  
    88  internal class Constants {
    99
    10     public const string Metadata = "Metadata";
    11     public const string Algorithm = "Algorithm";
    12     public const string Problem = "Problem";
    13     public const string Objects = "Objects";
    14     public const string Types = "Types";
    15     public const string StaticParameters = "StaticParameters";
    16     public const string FreeParameters = "FreeParameters";
     10    internal const string Metadata = "Metadata";
     11    internal const string Algorithm = "Algorithm";
     12    internal const string Problem = "Problem";
     13    internal const string Objects = "Objects";
     14    internal const string Types = "Types";
     15    internal const string StaticParameters = "StaticParameters";
     16    internal const string FreeParameters = "FreeParameters";
    1717
    18     public const string Template = @"{
     18    internal const string Template = @"{
    1919      'Metadata': {
    2020        'Algorithm':'',
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ParameterizedItemConverter.cs

    r17284 r17330  
    2727        JsonItem data = JsonItemConverter.Extract(param);
    2828        data.Name = param.Name;
    29         data.Path = param.Name;
     29        data.Path = data.Name;
    3030        data.PrependPath(item.Path);
    3131        data.UpdatePaths();
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JCGenerator.cs

    r17324 r17330  
    1515    private JArray JsonItems { get; set; } = new JArray();
    1616
     17    public string GenerateTemplate(IAlgorithm algorithm) {
     18      JsonItems.Clear();
     19      TypeList.Clear();
     20
     21      // 1.1. extract JsonItem, save the name in the metadata section of the
     22      // template and save it an JArray incl. all parameters of the JsonItem,
     23      // which have parameters aswell
     24      AddInstantiableIItem(Constants.Algorithm, algorithm);
     25      if (algorithm.Problem != null) // 1.2. only when an problem exists
     26        AddInstantiableIItem(Constants.Problem, algorithm.Problem);
     27
     28      // 2. save the JArray with JsonItems (= IParameterizedItems)
     29      template[Constants.Objects] = JsonItems;
     30      // 3. save the types of the JsonItems (for instatiation)
     31      template[Constants.Types] = JObject.FromObject(TypeList);
     32      // 4. serialize template and return string
     33      return CustomJsonWriter.Serialize(template);
     34    }
     35
     36    #region Helper
     37    private void AddInstantiableIItem(string metaDataTagName, IItem item) {
     38      JsonItem jsonItem = JsonItemConverter.Extract(item);
     39      template[Constants.Metadata][metaDataTagName] = item.ItemName;
     40      PopulateJsonItems(jsonItem);
     41    }
     42
    1743    private void PopulateJsonItems(JsonItem item) {
    1844      if (item.Parameters != null) {
    19         if(item.Range == null)
     45        if (item.Range == null)
    2046          JsonItems.Add(Serialize(item));
    2147        foreach (var p in item.Parameters)
    22           if(p.Parameters != null)
     48          if (p.Parameters != null)
    2349            PopulateJsonItems(p);
    2450      }
     
    3157
    3258      obj.Property(nameof(JsonItem.Parameters))?.Remove();
    33       RefactorFreeParameters(obj, null);
     59      RefactorFreeParameters(obj);
    3460      RefactorStaticParameters(obj);
    3561
     
    4167    }
    4268
    43     public string GenerateTemplate(IAlgorithm algorithm, IProblem problem, params string[] freeParameters) {
    44       JsonItems.Clear();
    45       TypeList.Clear();
    46 
    47       algorithm.Problem = problem;
    48       JsonItem algorithmData = JsonItemConverter.Extract(algorithm);
    49       JsonItem problemData = JsonItemConverter.Extract(problem);
    50       PopulateJsonItems(algorithmData);
    51       PopulateJsonItems(problemData);
    52 
    53       template[Constants.Metadata][Constants.Algorithm] = algorithm.Name;
    54       template[Constants.Metadata][Constants.Problem] = problem.Name;
    55       template[Constants.Objects] = JsonItems;
    56       template[Constants.Types] = JObject.FromObject(TypeList);
    57 
    58       return CustomJsonWriter.Serialize(template);
    59     }
    60 
    61     #region Helper
    62     private void RefactorFreeParameters(JToken token, string[] freeParameters) {
     69    private void RefactorFreeParameters(JToken token) {
    6370      IList<JObject> objToRemove = new List<JObject>();
    6471      TransformNodes(x => {
    6572        var p = x.ToObject<JsonItem>();
    66 
    67         /*bool isSelected = false;
    68         string name = x["Name"].ToObject<string>();
    69         foreach (var selected in freeParameters)
    70           isSelected = (name == selected || isSelected);
    71         */
    72         if (/*!isSelected ||*/ p.Default == null || (p.Default != null && p.Default.GetType() == typeof(string) && p.Range == null)) {
     73        if (p.Default == null || (p.Default != null && p.Default.GetType() == typeof(string) && p.Range == null)) {
    7374          objToRemove.Add(x);
    7475        } else {
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JCInstantiator.cs

    r17324 r17330  
    2323    private IDictionary<string, JsonItem> ConfigurableItems { get; set; } = new Dictionary<string, JsonItem>();
    2424   
    25     public IAlgorithm Instantiate(string templateFile, string configFile) {
     25    public IAlgorithm Instantiate(string templateFile, string configFile = "") {
    2626
    2727      //1. Parse Template and Config files
    2828      Template = JToken.Parse(File.ReadAllText(templateFile));
    29       Config = JArray.Parse(File.ReadAllText(configFile));
     29      if(!string.IsNullOrEmpty(configFile))
     30        Config = JArray.Parse(File.ReadAllText(configFile));
    3031      TypeList = Template[Constants.Types].ToObject<Dictionary<string, string>>();
    3132      string algorithmName = Template[Constants.Metadata][Constants.Algorithm].ToString();
     
    3839      SelectConfigurableItems();
    3940
    40       //4. Merge Template and Config
    41       MergeTemplateWithConfig();
     41      //4. if config != null -> merge Template and Config
     42      if (Config != null)
     43        MergeTemplateWithConfig();
    4244
    4345      //5. resolve the references between parameterizedItems
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Plugin.cs.frame

    r17324 r17330  
    2020#endregion
    2121
    22 using System;
    23 using System.Collections.Generic;
    24 using System.Linq;
    25 using System.IO;
    26 using System.Text;
    27 using System.Threading.Tasks;
    2822using HeuristicLab.PluginInfrastructure;
    2923
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.Tests/HeuristicLab.JsonInterface/GeneratorInstantiatorTest.cs

    r17324 r17330  
    1919    public void CreateTempFiles() {
    2020      GeneticAlgorithm alg = new GeneticAlgorithm();
    21       TravelingSalesmanProblem tsp = new TravelingSalesmanProblem();
     21      alg.Problem = new TravelingSalesmanProblem();
    2222      JCGenerator gen = new JCGenerator();
    2323      //File.WriteAllText(@"C:\Workspace\Template.json", gen.GenerateTemplate(alg, tsp));
    24       File.WriteAllText(templateFilePath, gen.GenerateTemplate(alg, tsp));
     24      File.WriteAllText(templateFilePath, gen.GenerateTemplate(alg));
    2525      File.WriteAllText(configFilePath, "["+
    2626        "{\"Name\": \"Seed\",\"Default\": 55555,\"Path\": \"Genetic Algorithm (GA).Seed\"},"+
Note: See TracChangeset for help on using the changeset viewer.