Changeset 17280


Ignore:
Timestamp:
10/01/19 12:58:25 (2 weeks ago)
Author:
dpiringe
Message:

#3026

  • renamed CustomWriter to CustomJsonWriter and extracted it into a separate file
  • removed property ParameterizedItems from Component
  • added helper methods for path generation in Component
  • reverted the single parameter array idea back to the FreeParameters and StaticParameters idea
  • now there hidden parameters are also handled
Location:
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture
Files:
1 added
13 edited

Legend:

Unmodified
Added
Removed
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Component.cs

    r17275 r17280  
    99
    1010namespace HeuristicLab.Manufacture {
     11  //JsonItem
    1112  public class Component {
    1213    private IList<object> range;
     
    2324      }
    2425    }
    25     public string Path { get; set; }
     26    public string Path { get; set; } = "";
    2627
    2728    public IList<object> Range {
     
    4344
    4445    [JsonIgnore]
    45     public IList<Component> ParameterizedItems { get; set; }
    46 
    47     [JsonIgnore]
    4846    public Component Reference { get; set; }
    4947   
     
    5654      target.Reference = from.Reference ?? target.Reference;
    5755      target.Parameters = from.Parameters ?? target.Parameters;
    58       target.ParameterizedItems = from.ParameterizedItems ?? target.ParameterizedItems;
    5956      target.Operators = from.Operators ?? target.Operators;
    6057    }
     
    7269      IsInNumericRange<double>(data.Default, data.Range[0], data.Range[1]));
    7370
     71
     72
     73    public void UpdatePaths() {
     74      if (Parameters != null) {
     75        foreach (var p in Parameters) {
     76          p.Path = Path + "." + p.Name;
     77        }
     78      }
     79
     80      if (Operators != null) {
     81        foreach (var p in Operators) {
     82          p.Path = Path + "." + p.Name;
     83        }
     84      }
     85    }
     86
     87    public void PrependPath(string str) {
     88      Path = $"{str}.{Path}";
     89      PrependPathHelper(Parameters, str);
     90      PrependPathHelper(Operators, str);
     91    }
     92
     93    private void PrependPathHelper(IEnumerable<Component> components, string str) {
     94      if (components != null) {
     95        foreach (var p in components)
     96          p.PrependPath(str);
     97      }
     98    }
     99
    74100    #region Helper
    75 
    76101    private static bool IsInRangeList(IEnumerable<object> list, object value) {
    77102      foreach (var x in list)
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/HeuristicLab.Manufacture.csproj

    r17275 r17280  
    5151  </ItemGroup>
    5252  <ItemGroup>
     53    <Compile Include="CustomJsonWriter.cs" />
    5354    <Compile Include="Extensions\ObjectExtensions.cs" />
    5455    <Compile Include="Extensions\TypeExtensions.cs" />
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Interfaces/ITypeTransformer.cs

    r17271 r17280  
    88
    99namespace HeuristicLab.Manufacture {
    10   //IDataProcessor?
    1110  public interface ITypeTransformer {
     11    /// <summary>
     12    /// Extracts all infos out of an IItem to create a Component.
     13    /// (For template generation.)
     14    /// </summary>
     15    /// <param name="value">The IItem to extract infos.</param>
     16    /// <returns>Component with infos to reinitialise the IItem.</returns>
    1217    Component Extract(IItem value);
     18
     19    /// <summary>
     20    /// Injects the saved infos from the Component into the IItem.
     21    /// (Sets the necessary values.)
     22    /// </summary>
     23    /// <param name="item">The IItem which get the data injected.</param>
     24    /// <param name="data">The Component with the saved values.</param>
    1325    void Inject(IItem item, Component data);
    1426  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/JCGenerator.cs

    r17275 r17280  
    11using System;
    22using System.Collections.Generic;
    3 using System.IO;
    43using System.Linq;
    54using HeuristicLab.Algorithms.GeneticAlgorithm;
     
    1413
    1514namespace HeuristicLab.Manufacture {
    16 
    17   public class CustomWriter : JsonTextWriter {
    18     private const int ArrayFlatterLvl = 1;
    19     private Stack<Formatting> formattings = new Stack<Formatting>();
    20     private int lvl = 0;
    21     private bool isRangeArray = false;
    22     public override void WriteStartArray() {
    23       base.WriteStartArray();
    24       /*
    25       if(lvl > ArrayFlatterLvl) {
    26         formattings.Push(base.Formatting);
    27         base.Formatting = Formatting.None;
    28       }
    29       lvl++;
    30       */
    31       if(isRangeArray) base.Formatting = Formatting.None;
    32     }
    33 
    34     public override void WriteEndArray() {
    35       base.WriteEndArray();
    36       /*
    37       lvl--;
    38       if (lvl > ArrayFlatterLvl)
    39         base.Formatting = formattings.Pop();
    40       */
    41       if(isRangeArray) {
    42         base.Formatting = Formatting.Indented;
    43         isRangeArray = false;
    44       }
    45     }
    46 
    47     public override void WritePropertyName(string name) {
    48       base.WritePropertyName(name);
    49       isRangeArray = name == "Range";
    50     }
    51 
    52     public CustomWriter(TextWriter writer) : base(writer) { }
    53 
    54     public static string Serialize(JToken token) {
    55       JsonSerializer serializer = new JsonSerializer();
    56       StringWriter sw = new StringWriter();
    57       CustomWriter writer = new CustomWriter(sw);
    58       writer.Formatting = Formatting.Indented;
    59       serializer.Serialize(writer, token);
    60       return sw.ToString();
    61     }
    62   }
    63 
    6415  public class JCGenerator {
    6516
     
    7425
    7526    private Dictionary<string, string> TypeList = new Dictionary<string, string>();
    76    
     27
     28    JArray jsonItems = new JArray();
     29    private void PopulateJsonItems(Component component) {
     30      if (component.Parameters != null) {
     31        if(component.Range == null)
     32          jsonItems.Add(Serialize(component));
     33        foreach (var p in component.Parameters)
     34          if(p.Parameters != null)
     35            PopulateJsonItems(p);
     36      }
     37    }
     38
     39    private JObject Serialize(Component component) {
     40      JObject obj = JObject.FromObject(component, Settings());
     41      obj["StaticParameters"] = obj["Parameters"];
     42      obj["FreeParameters"] = obj["Parameters"];
     43
     44      obj.Property("Parameters")?.Remove();
     45      RefactorFreeParameters(obj, null);
     46      RefactorStaticParameters(obj);
     47
     48      obj.Property("Default")?.Remove();
     49      obj.Property("Type")?.Remove();
     50
     51      TypeList.Add(component.Path, component.Type);
     52      return obj;
     53    }
     54
    7755    public string GenerateTemplate(IAlgorithm algorithm, IProblem problem, params string[] freeParameters) {
    7856      algorithm.Problem = problem;
    7957      Component algorithmData = Transformer.Extract(algorithm);
    8058      Component problemData = Transformer.Extract(problem);
    81       IList<Component> items = algorithmData.ParameterizedItems;
    82       foreach (var pItem in problemData.ParameterizedItems) items.Add(pItem);
    83       JArray jsonItems = new JArray();
    84      
    85       foreach(var item in items.Distinct()) {
    86         JObject token = JObject.FromObject(item, Settings());
    87         //token["StaticParameters"] = token["Parameters"];
    88         //token["FreeParameters"] = token["Parameters"];
    89 
    90         foreach (JObject x in token["Parameters"]) {
    91           x.Property("Parameters")?.Remove();
    92           x.Property("Type")?.Remove();
    93         }
    94         TypeList.Add(item.Name, item.Type);
    95         RefactorParameters(token);
    96         /*
    97         foreach (JObject x in token["StaticParameters"]) {
    98           x.Property("Parameters")?.Remove();
    99           x.Property("Type")?.Remove();
    100         }
    101         foreach (JObject x in token["FreeParameters"]) {
    102           x.Property("Parameters")?.Remove();
    103           x.Property("Type")?.Remove();
    104         }
    105         */
    106         //token.Cast<JObject>().Property("Parameters")?.Remove();
    107         token.Property("Default")?.Remove();
    108         token.Property("Type")?.Remove();
    109 
    110         //RefactorFreeParameters(token, freeParameters);
    111         //RefactorStaticParameters(token);
    112         if (/*token["StaticParameters"].HasValues || token["FreeParameters"].HasValues ||*/ token["Parameters"].HasValues || (token["Operators"]?.HasValues ?? false))
    113           jsonItems.Add(token);
    114 
    115         if(!token["Parameters"].HasValues) {
    116           token.Property("Parameters")?.Remove();
    117         }
    118       }
     59      PopulateJsonItems(algorithmData);
     60      PopulateJsonItems(problemData);
    11961
    12062      template["Metadata"]["Algorithm"] = algorithm.Name;
     
    12365      template["Types"] = JObject.FromObject(TypeList);
    12466
    125       return CustomWriter.Serialize(template);
     67      return CustomJsonWriter.Serialize(template);
    12668    }
    12769
     
    12971    private void RefactorFreeParameters(JToken token, string[] freeParameters) {
    13072
    131       //token["FreeParameters"] = token["StaticParameters"];
    13273
    13374      IList<JObject> objToRemove = new List<JObject>();
     
    14586          x.Property("Path")?.Remove();
    14687          x.Property("Type")?.Remove();
     88          x.Property("Parameters")?.Remove();
    14789        }
    14890      }, token["FreeParameters"]);
     
    15698        var p = x.ToObject<Component>();
    15799        x.Property("Range")?.Remove();
     100        x.Property("Operators")?.Remove();
     101        x.Property("Parameters")?.Remove();
     102        x.Property("Type")?.Remove();
    158103        if (p.Default == null) objToRemove.Add(x);
    159104      }, token["StaticParameters"]);
     
    161106    }
    162107
    163     private void RefactorParameters(JToken token) {
    164       IList<JObject> objToRemove = new List<JObject>();
    165       TransformNodes(x => {
    166         var p = x.ToObject<Component>();
    167         if (p.Default == null) objToRemove.Add(x);
    168       }, token["Parameters"]);
    169       foreach (var x in objToRemove) x.Remove();
    170     }
    171108
    172109    private JsonSerializer Settings() => new JsonSerializer() {
    173110      TypeNameHandling = TypeNameHandling.None,
    174       NullValueHandling = NullValueHandling.Ignore
     111      NullValueHandling = NullValueHandling.Ignore,
     112      ReferenceLoopHandling = ReferenceLoopHandling.Serialize
    175113    };
    176114
    177 
    178115    private void TransformNodes(Action<JObject> action, params JToken[] tokens) {
    179       foreach(JObject obj in tokens.SelectMany(x => x.Children<JObject>())) {
     116      foreach(JObject obj in tokens.SelectMany(x => x.Children<JObject>()))
    180117        action(obj);
    181       }
    182118    }
    183119    #endregion
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/JCInstantiator.cs

    r17275 r17280  
    4747      foreach(JObject item in Config["Objects"])
    4848      {
    49         Component data = item.ToObject<Component>();// Component.Build(item);
     49        Component data = BuildComponent(item);
    5050        if (data.Name == key) return data;
    5151      }
     
    8080          Component tmp = BuildComponent(param);
    8181          Component comp = null;
    82           foreach (var p in list)
     82          foreach (var p in list) // TODO: nicht notwendig, da immer alle params im static block sind
    8383            if (p.Name == tmp.Name) comp = p;
    8484          if (comp != null)
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/ParameterTransformer/ConstrainedValueParameterTransformer.cs

    r17271 r17280  
    1717    }
    1818
    19     public override Component ExtractData(IParameter value) {
    20 
    21       return new Component() {
     19    public override Component ExtractData(IParameter value) =>
     20      new Component() {
    2221        Name = value.Name,
    2322        Default = value.ActualValue?.GetType().Name,
    2423        Range = GetValidValues(value),
    25         ParameterizedItems = GetParameterizedChilds(value)
     24        Parameters = GetParameterizedChilds(value)
    2625      };
    27     }
    2826
    2927    #region Helper
     
    3432      return list.ToArray();
    3533    }
    36 
     34    // id = kombi aus path + default
    3735    private IList<Component> GetParameterizedChilds(IParameter value) {
    3836      List<Component> list = new List<Component>();
    3937      var values = value.Cast<dynamic>().ValidValues;
    4038      foreach(var x in values) {
    41         if (x is IParameterizedItem &&
    42             ((IParameterizedItem)x).Parameters.Any(p => !p.Hidden)) {
     39        if (x is IParameterizedItem) {
    4340          Component tmp = Transformer.Extract(x);
    44           if (tmp.ParameterizedItems != null)
    45             list.AddRange(tmp.ParameterizedItems);
    46           else
    47             list.Add(tmp);
     41          tmp.PrependPath(value.Name);
     42          list.Add(tmp);
    4843        }
    4944      }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/ParameterTransformer/ParameterBaseTransformer.cs

    r17271 r17280  
    88namespace HeuristicLab.Manufacture {
    99  public abstract class ParameterBaseTransformer : BaseTransformer {
    10     public override Component ExtractData(IItem value) => ExtractData(value.Cast<IParameter>());
     10    public override Component ExtractData(IItem value) {
     11      IParameter param = value.Cast<IParameter>();
     12      Component comp = ExtractData(param);
     13      //comp.Path = value.Cast<IParameter>().ActualValue?.ItemName;
     14      /*comp.Path = value.Cast<IParameter>().Name + "." + comp.Path;
     15      if(comp.ParameterizedItems != null) {
     16        foreach (var item in comp.ParameterizedItems) {
     17          item.Path = value.Cast<IParameter>().Name + "." + item.Path;
     18        }
     19      }*/
     20      return comp;
     21    }
    1122    public abstract Component ExtractData(IParameter value);
    1223
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/ParameterTransformer/ParameterizedItemTransformer.cs

    r17275 r17280  
    99{
    1010  public class ParameterizedItemTransformer : BaseTransformer {
    11    
    1211    public override void InjectData(IItem item, Component data) {
    1312      IParameterizedItem pItem = item.Cast<IParameterizedItem>();
     
    2120
    2221    public override Component ExtractData(IItem value) {
    23       List<Component> list = new List<Component>();
    24 
    2522      Component obj = new Component();
    2623      obj.Name = value.ItemName;
    2724      obj.Type = value.GetType().AssemblyQualifiedName;
    28       obj.ParameterizedItems = list;
    29       obj.Parameters = new List<Component>();
    30       list.Add(obj);
     25      obj.Path = value.ItemName;
    3126
    3227      foreach (var param in value.Cast<IParameterizedItem>().Parameters) {
    33         if (!param.Hidden) {
    34           Component data = Transformer.Extract(param);
    35           obj.Parameters.Add(data);
    36           if(data.ParameterizedItems != null)
    37             list.AddRange(data.ParameterizedItems);
    38         }
     28        Component data = Transformer.Extract(param);
     29        data.Name = param.Name;
     30        data.Path = param.Name;
     31        data.PrependPath(obj.Path);
     32        data.UpdatePaths();
     33       
     34        if (obj.Parameters == null)
     35          obj.Parameters = new List<Component>();
     36        obj.Parameters.Add(data);
    3937      }
    4038      return obj;
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Program.cs

    r17275 r17280  
    3535
    3636      JCGenerator gen = new JCGenerator();
    37       File.WriteAllText(@"C:\Workspace\TemplateProto10.json", gen.GenerateTemplate(alg, tsp));
     37      //File.WriteAllText(@"C:\Workspace\TemplateProto11.json", gen.GenerateTemplate(alg, tsp));
    3838     
    3939      JCInstantiator configurator = new JCInstantiator();
    40       configurator.Instantiate(@"C:\Workspace\TemplateProto10.json");
     40      configurator.Instantiate(@"C:\Workspace\TemplateProto11.json");
    4141
    4242      //Console.ReadLine();
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Transformer.cs

    r17271 r17280  
    1111    private static IDictionary<Type, ITypeTransformer> transformers = new Dictionary<Type, ITypeTransformer>();
    1212   
    13     public static void Register(Type type, ITypeTransformer transformer) {
    14       if(!transformers.ContainsKey(type))
     13    public static void Register(Type type, ITypeTransformer transformer) { // TODO: explizit
     14      if (!transformers.ContainsKey(type))
    1515        transformers.Add(type, transformer);
    1616    }
     
    1919    public static void Register<T>(ITypeTransformer transformer) => Register(typeof(T), transformer);
    2020
    21     public static IEnumerable<ITypeTransformer> Get(Type type) {
     21    public static IEnumerable<ITypeTransformer> Get(Type type) { 
    2222      IList<KeyValuePair<Type, ITypeTransformer>> possibleTransformers = new List<KeyValuePair<Type, ITypeTransformer>>();
    2323
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/TypeTransformer/EnumTypeTransformer.cs

    r17271 r17280  
    99namespace HeuristicLab.Manufacture {
    1010  public class EnumTypeTransformer : BaseTransformer {
    11 
    1211    public override void InjectData(IItem item, Component data) =>
    1312      item.Cast<dynamic>().Value = Enum.Parse(
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/TypeTransformer/MultiCheckedOperatorTransformer.cs

    r17275 r17280  
    1010    public override Component ExtractData(IItem value) {
    1111      Component data = base.ExtractData(value);
     12
    1213      data.Default = value.GetType().Name;
    13 
    1414      data.Operators = new List<Component>();
     15      /*
     16      if (data.ParameterizedItems == null)
     17        data.ParameterizedItems = new List<Component>();
     18      data.ParameterizedItems.Add(data);
     19      */
    1520      dynamic val = value.Cast<dynamic>();
    1621      foreach (var op in val.Operators) {
     
    1823          Name = op.Name,
    1924          Default = val.Operators.ItemChecked(op),
    20           Range = new object[] { false, true }
     25          Range = new object[] { false, true },
     26          Path = data.Path + "." + op.Name
    2127        });
    2228      }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/TypeTransformer/ValueTypeValueTransformer.cs

    r17271 r17280  
    1818      new Component() {
    1919        Default = value.Cast<ValueType>().Value,
    20         Range = new object[] { default(T), GetMaxValue() }
     20        Range = new object[] { GetMinValue(), GetMaxValue() }
    2121      };
    2222
     
    4343      }
    4444    }
     45
     46    private object GetMinValue() {
     47      TypeCode typeCode = Type.GetTypeCode(typeof(T));
     48
     49      if (typeof(ValueType).IsEqualTo(typeof(PercentValue)))
     50        return 0.0d;
     51
     52      switch (typeCode) {
     53        case TypeCode.Int16: return Int16.MinValue;
     54        case TypeCode.Int32: return Int32.MinValue;
     55        case TypeCode.Int64: return Int64.MinValue;
     56        case TypeCode.UInt16: return UInt16.MinValue;
     57        case TypeCode.UInt32: return UInt32.MinValue;
     58        case TypeCode.UInt64: return UInt64.MinValue;
     59        case TypeCode.Single: return Single.MinValue;
     60        case TypeCode.Double: return Double.MinValue;
     61        case TypeCode.Decimal: return Decimal.MinValue;
     62        case TypeCode.Byte: return Byte.MinValue;
     63        case TypeCode.Boolean: return false;
     64        default: return default(T);
     65      }
     66    }
    4567    #endregion
    4668  }
Note: See TracChangeset for help on using the changeset viewer.