1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Services.Optimization.ControllerService.Model;
|
---|
6 | using Newtonsoft.Json.Linq;
|
---|
7 |
|
---|
8 | namespace 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 | }
|
---|