Changeset 17287


Ignore:
Timestamp:
10/02/19 12:44:44 (12 days ago)
Author:
dpiringe
Message:

#3026

  • added class for constants -> Constants
  • added new converter -> ValueLookupParameterConverter
  • fixed a bug with type extension IsEqualTo
  • fixed converter priorities
Location:
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface
Files:
2 added
7 edited

Legend:

Unmodified
Added
Removed
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/LookupParameterConverter.cs

    r17284 r17287  
    88namespace HeuristicLab.JsonInterface {
    99  public class LookupParameterConverter : ParameterBaseConverter {
    10     public override JsonItem ExtractData(IParameter value) {
    11       return new JsonItem() { Name = value.Name };
    12     }
     10    public override JsonItem ExtractData(IParameter value) =>
     11      new JsonItem() {
     12        Name = value.Name,
     13        Default = value.Cast<ILookupParameter>().ActualName
     14      };
    1315
    14     public override void InjectData(IParameter parameter, JsonItem data) {
    15      
    16     }
     16    public override void InjectData(IParameter parameter, JsonItem data) =>
     17      parameter.Cast<ILookupParameter>().ActualName = data.Default.Cast<string>();
     18   
    1719  }
    1820}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Extensions/TypeExtensions.cs

    r17284 r17287  
    3030        }
    3131      }
    32       return type.IsAssignableFrom(other);
     32      return false;//type.IsAssignableFrom(other);
    3333    }
    3434  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/HeuristicLab.JsonInterface.csproj

    r17284 r17287  
    5151  </ItemGroup>
    5252  <ItemGroup>
     53    <Compile Include="Constants.cs" />
     54    <Compile Include="Converters\ValueLookupParameterConverter.cs" />
    5355    <Compile Include="CustomJsonWriter.cs" />
    5456    <Compile Include="Extensions\ObjectExtensions.cs" />
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JCGenerator.cs

    r17284 r17287  
    1515  public class JCGenerator {
    1616
    17     private JObject template = JObject.Parse(@"{
    18       'Metadata': {
    19         'Algorithm':'',
    20         'Problem':''
    21       },
    22       'Objects': [],
    23       'Types': {}
    24     }");
     17    private JObject template = JObject.Parse(Constants.Template);
    2518
    2619    private Dictionary<string, string> TypeList = new Dictionary<string, string>();
     
    3932    private JObject Serialize(JsonItem item) {
    4033      JObject obj = JObject.FromObject(item, Settings());
    41       obj["StaticParameters"] = obj["Parameters"];
    42       obj["FreeParameters"] = obj["Parameters"];
     34      obj[Constants.StaticParameters] = obj[nameof(JsonItem.Parameters)];
     35      obj[Constants.FreeParameters] = obj[nameof(JsonItem.Parameters)];
    4336
    44       obj.Property("Parameters")?.Remove();
     37      obj.Property(nameof(JsonItem.Parameters))?.Remove();
    4538      RefactorFreeParameters(obj, null);
    4639      RefactorStaticParameters(obj);
    4740
    48       obj.Property("Default")?.Remove();
    49       obj.Property("Type")?.Remove();
     41      obj.Property(nameof(JsonItem.Default))?.Remove();
     42      obj.Property(nameof(JsonItem.Type))?.Remove();
    5043
    5144      TypeList.Add(item.Path, item.Type);
     
    6053      PopulateJsonItems(problemData);
    6154
    62       template["Metadata"]["Algorithm"] = algorithm.Name;
    63       template["Metadata"]["Problem"] = problem.Name;
    64       template["Objects"] = jsonItems;
    65       template["Types"] = JObject.FromObject(TypeList);
     55      template[Constants.Metadata][Constants.Algorithm] = algorithm.Name;
     56      template[Constants.Metadata][Constants.Problem] = problem.Name;
     57      template[Constants.Objects] = jsonItems;
     58      template[Constants.Types] = JObject.FromObject(TypeList);
    6659
    6760      return CustomJsonWriter.Serialize(template);
     
    8477          objToRemove.Add(x);
    8578        } else {
    86           x.Property("Path")?.Remove();
    87           x.Property("Type")?.Remove();
    88           x.Property("Parameters")?.Remove();
     79          x.Property(nameof(JsonItem.Path))?.Remove();
     80          x.Property(nameof(JsonItem.Type))?.Remove();
     81          x.Property(nameof(JsonItem.Parameters))?.Remove();
    8982        }
    90       }, token["FreeParameters"]);
     83      }, token[Constants.FreeParameters]);
    9184      foreach (var x in objToRemove) x.Remove();
    92 
    9385    }
    9486
     
    9789      TransformNodes(x => {
    9890        var p = x.ToObject<JsonItem>();
    99         x.Property("Range")?.Remove();
    100         x.Property("Operators")?.Remove();
    101         x.Property("Parameters")?.Remove();
    102         x.Property("Type")?.Remove();
     91        x.Property(nameof(JsonItem.Range))?.Remove();
     92        x.Property(nameof(JsonItem.Operators))?.Remove();
     93        x.Property(nameof(JsonItem.Parameters))?.Remove();
     94        x.Property(nameof(JsonItem.Type))?.Remove();
    10395        if (p.Default == null) objToRemove.Add(x);
    104       }, token["StaticParameters"]);
     96      }, token[Constants.StaticParameters]);
    10597      foreach (var x in objToRemove) x.Remove();
    10698    }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JCInstantiator.cs

    r17284 r17287  
    2020    public IAlgorithm Instantiate(string configFile) {
    2121      Config = JToken.Parse(File.ReadAllText(configFile));
    22       TypeList = Config["Types"].ToObject<Dictionary<string, string>>();
     22      TypeList = Config[Constants.Types].ToObject<Dictionary<string, string>>();
    2323
    24       JsonItem algorithmData = GetData(Config["Metadata"]["Algorithm"].ToString());
     24      JsonItem algorithmData = GetData(Config[Constants.Metadata][Constants.Algorithm].ToString());
    2525      ResolveReferences(algorithmData);
    2626      IAlgorithm algorithm = CreateObject<IAlgorithm>(algorithmData);
    2727     
    28       JsonItem problemData = GetData(Config["Metadata"]["Problem"].ToString());
     28      JsonItem problemData = GetData(Config[Constants.Metadata][Constants.Problem].ToString());
    2929      ResolveReferences(problemData);
    3030      IProblem problem = CreateObject<IProblem>(problemData);
     
    4545    private JsonItem GetData(string key)
    4646    {
    47       foreach(JObject item in Config["Objects"])
     47      foreach(JObject item in Config[Constants.Objects])
    4848      {
    4949        JsonItem data = BuildJsonItem(item);
     
    6666        Range = obj[nameof(JsonItem.Range)]?.ToObject<object[]>(),
    6767        Type = obj[nameof(JsonItem.Type)]?.ToObject<string>(),
     68        Reference = obj[nameof(JsonItem.Type)] == null ?
     69                    null :
     70                    BuildJsonItem(obj[nameof(JsonItem.Type)].ToObject<JObject>()),
    6871        Parameters = PopulateParameters(obj),
    6972        Operators = PopulateOperators(obj)
     
    7275    private IList<JsonItem> PopulateParameters(JObject obj) {
    7376      IList<JsonItem> list = new List<JsonItem>();
    74       if (obj["StaticParameters"] != null)
    75         foreach (JObject param in obj["StaticParameters"])
     77      if (obj[Constants.StaticParameters] != null)
     78        foreach (JObject param in obj[Constants.StaticParameters])
    7679          list.Add(BuildJsonItem(param));
    7780
    78       if (obj["FreeParameters"] != null) {
    79         foreach (JObject param in obj["FreeParameters"]) {
     81      if (obj[Constants.FreeParameters] != null) {
     82        foreach (JObject param in obj[Constants.FreeParameters]) {
    8083          JsonItem tmp = BuildJsonItem(param);
    8184          JsonItem comp = null;
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonItem.cs

    r17284 r17287  
    4242    public override int GetHashCode() => Name.GetHashCode();
    4343
    44     [JsonIgnore]
    4544    public JsonItem Reference { get; set; }
    4645   
     
    6867      IsInNumericRange<double>(data.Default, data.Range[0], data.Range[1]));
    6968
     69    public void UpdatePaths() {
     70      if (Parameters != null)
     71        foreach (var p in Parameters)
     72          p.Path = Path + "." + p.Name;
    7073
    71 
    72     public void UpdatePaths() {
    73       if (Parameters != null) {
    74         foreach (var p in Parameters) {
     74      if (Operators != null)
     75        foreach (var p in Operators)
    7576          p.Path = Path + "." + p.Name;
    76         }
    77       }
    78 
    79       if (Operators != null) {
    80         foreach (var p in Operators) {
    81           p.Path = Path + "." + p.Name;
    82         }
    83       }
    8477    }
    8578
     
    9184
    9285    private void PrependPathHelper(IEnumerable<JsonItem> items, string str) {
    93       if (items != null) {
     86      if (items != null)
    9487        foreach (var p in items)
    9588          p.PrependPath(str);
    96       }
    9789    }
    9890
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonItemConverter.cs

    r17284 r17287  
    5252      Register<IntValue>(new ValueTypeValueConverter<IntValue, int>(), 1);
    5353      Register<DoubleValue>(new ValueTypeValueConverter<DoubleValue, double>(), 1);
    54       Register<PercentValue>(new ValueTypeValueConverter<PercentValue, double>(), 1);
     54      Register<PercentValue>(new ValueTypeValueConverter<PercentValue, double>(), 2);
    5555      Register<BoolValue>(new ValueTypeValueConverter<BoolValue, bool>(), 1);
    5656      Register<DateTimeValue>(new ValueTypeValueConverter<DateTimeValue, DateTime>(), 1);
     
    5959      Register<IntArray>(new ValueTypeArrayConverter<IntArray, int>(), 1);
    6060      Register<DoubleArray>(new ValueTypeArrayConverter<DoubleArray, double>(), 1);
    61       Register<PercentArray>(new ValueTypeArrayConverter<PercentArray, double>(), 1);
     61      Register<PercentArray>(new ValueTypeArrayConverter<PercentArray, double>(), 2);
    6262      Register<BoolArray>(new ValueTypeArrayConverter<BoolArray, bool>(), 1);
    6363
    6464      Register<IntMatrix>(new ValueTypeMatrixConverter<IntMatrix, int>(), 1);
    6565      Register<DoubleMatrix>(new ValueTypeMatrixConverter<DoubleMatrix, double>(), 1);
    66       Register<PercentMatrix>(new ValueTypeMatrixConverter<PercentMatrix, double>(), 1);
     66      Register<PercentMatrix>(new ValueTypeMatrixConverter<PercentMatrix, double>(), 2);
    6767      Register<BoolMatrix>(new ValueTypeMatrixConverter<BoolMatrix, bool>(), 1);
    6868
    69       Register(typeof(IConstrainedValueParameter<>), new ConstrainedValueParameterConverter(), 1);
    70       Register(typeof(ILookupParameter), new LookupParameterConverter(), 1);
    71       Register(typeof(IValueParameter), new ValueParameterConverter(), 1);
    72       Register(typeof(IParameterizedItem), new ParameterizedItemConverter(), 1);
     69      Register(typeof(EnumValue<>), new EnumTypeConverter(), 1);
     70
     71      Register<IValueParameter>(new ValueParameterConverter(), 1);
     72      Register<IParameterizedItem>(new ParameterizedItemConverter(), 1);
     73      Register<ILookupParameter>(new LookupParameterConverter(), 2);
     74      Register<IValueLookupParameter>(new ValueLookupParameterConverter(), 3);
     75
     76      Register(typeof(IConstrainedValueParameter<>), new ConstrainedValueParameterConverter(), 2);
    7377      Register(typeof(ICheckedMultiOperator<>), new MultiCheckedOperatorConverter(), 2);
    74       Register(typeof(EnumValue<>), new EnumTypeConverter(), 1);
    7578    }
    76 
    7779  }
    7880}
Note: See TracChangeset for help on using the changeset viewer.