Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.Controller/Parsers/AlgorithmConverter.cs @ 9215

Last change on this file since 9215 was 9215, checked in by fschoepp, 12 years ago

#1888:

  • Janitor is now working as expected in Windows Azure
  • Added basic support for experiments (draggable experiments)
  • Added methods to save/read experiments from TableStorage
  • The job status can now be retrieved by using the GetTasks/GetTaskData methods
  • Added a class to convert JSON-objects to Algorithm instances
  • Web page: Added experiment button to navigation
File size: 3.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Services.Optimization.ControllerService.Model;
6using Newtonsoft.Json.Linq;
7
8namespace HeuristicLab.Services.Optimization.ControllerService.Parsers {
9  public static class AlgorithmConverter {
10
11    public static Algorithm Convert(string json) {
12      var algorithm = new Algorithm();
13      var o = JObject.Parse(json);
14      foreach (JProperty param in o["Algorithm Parameters"]) {
15        Parameter parameter = CreateParameter(param);
16        algorithm.Parameters.Items.Add(parameter);
17      }
18
19      var problemParams = o["Problem Parameters"];
20      // TODO: Store problem parameters
21      if (problemParams != null) {
22        algorithm.Problem = new Problem();
23        foreach (JProperty param in problemParams) {
24          Parameter parameter = CreateParameter(param);
25          algorithm.Problem.Parameters.Items.Add(parameter);
26        }
27      }
28      return algorithm;
29    }
30
31    public static Algorithm ConvertAppend(string json, Algorithm algorithm) {
32      var o = JObject.Parse(json);
33      foreach (JProperty param in o["Algorithm Parameters"]) {
34        Parameter parameter = CreateParameter(param);
35        var oldParameter = (from p in algorithm.Parameters.Items where p.Value.Name == parameter.Value.Name select p).FirstOrDefault();
36        if (oldParameter != null) {
37          oldParameter.Value = parameter.Value;
38        }
39        else {
40          algorithm.Parameters.Items.Add(parameter);
41        }
42      }
43
44      var problemParams = o["Problem Parameters"];
45      if (problemParams != null) {
46        algorithm.Problem = new Problem();
47        foreach (JProperty param in problemParams) {
48          Parameter parameter = CreateParameter(param);         
49          var oldParameter = (from p in algorithm.Problem.Parameters.Items where p.Value.Name == parameter.Value.Name select p).FirstOrDefault();
50          if (oldParameter != null) {
51            oldParameter.Value = parameter.Value;
52          }
53          else {
54            algorithm.Problem.Parameters.Items.Add(parameter);
55          }
56        }
57      }
58      return algorithm;
59    }
60
61    private static Parameter CreateParameter(JProperty property) {
62      JToken token = property.Value;
63      switch (token.Type) {
64        case JTokenType.Integer:
65        case JTokenType.Float:
66          return new Parameter() { Type = ParameterType.Decimal, Value = new DecimalValue() { Name = property.Name, Value = (double)property.Value } };
67        case JTokenType.Boolean:
68          return new Parameter() { Type = ParameterType.Boolean, Value = new BoolValue() { Name = property.Name, Value = (bool)property.Value } };
69        case JTokenType.String:
70          return new Parameter() { Type = ParameterType.Type, Value = new TypeValue() { Name = property.Name, Value = (string)property.Value } };                 
71        default:
72          throw new Exception("Unhandled datatype: " + property.Type);
73      }
74    }
75  }
76}
Note: See TracBrowser for help on using the repository browser.