Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.OKB.AlgorithmHost/OKBRunnerExtensions.cs @ 7259

Last change on this file since 7259 was 4311, checked in by swagner, 14 years ago

Integrated OKB clients for HL 3.3 (#1166)

File size: 4.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Data;
6using HeuristicLab.Core;
7using HeuristicLab.Persistence.Default.Xml;
8using System.IO;
9using HeuristicLab.OKB.AlgorithmHost.OKBRunner;
10
11namespace HeuristicLab.OKB.AlgorithmHost {
12  public static class OKBExtensions {
13
14    public static void AddParameterValue(this Algorithm a, string name, object value) {     
15      if (value == null) return;
16      if (value is StringValue) {
17        value = ((StringValue)value).Value;
18      } else if (value is IntValue) {
19        value = ((IntValue)value).Value;
20      } else if (value is DoubleValue) {
21        value = ((DoubleValue)value).Value;
22      } else if (value is BoolValue) {
23        value = ((BoolValue)value).Value;
24      }
25      Type t = value.GetType();
26      Parameter p = new Parameter() {
27        DataType = new DataType() { ClrName = t.FullName },
28        Name = name
29      };
30      if (t == typeof(int)) {
31        p.IntParameterValues = new List<IntParameterValue>() { new IntParameterValue() { Value = (int)value } };
32      } else if (t == typeof(double)) {
33        p.FloatParameterValues = new List<FloatParameterValue>() { new FloatParameterValue() { Value = (double)value } };
34      } else if (t == typeof(string)) {
35        p.CharParameterValues = new List<CharParameterValue>() { new CharParameterValue() { Value = (string)value } };     
36      } else if (t == typeof(bool)) {
37        p.IntParameterValues = new List<IntParameterValue>() { new IntParameterValue() { Value = ((bool)value) ? 1 : 0 } };
38      } else if (value is Item) {
39        byte[] data;
40        using (var stream = new MemoryStream()) {
41          XmlGenerator.Serialize(value, stream);
42          data = stream.ToArray();
43        }
44        p.DataType.ClrName = "IOperator";
45        p.OperatorParameterValues = new List<OperatorParameterValue>() {
46          new OperatorParameterValue() {
47            DataType = new DataType() { ClrName = t.FullName },
48            Value = new Binary() { Bytes = data },
49          },
50        };
51        if (value is IParameterizedItem) {
52          foreach (var param in ((IParameterizedItem)value).Parameters) {
53            try {
54              a.AddParameterValue(string.Format("{0}:{1}.{2}", name, t.Name, param.Name), param.ActualValue);
55            } catch (Exception x) {             
56              // swallow exceptions
57            }
58          }
59        }
60      } else {
61        throw new ArgumentException("invalid parameter data type" + t.ToString());
62      }     
63      if (a.Algorithm_Parameters.Any(ap => ap.Parameter.Name == name)) {
64        throw new ArgumentException("duplicate parameter " + name);
65      }
66      a.Algorithm_Parameters.Add(new Algorithm_Parameter() {
67        Algorithm = a,
68        Parameter = p,
69      });           
70    }
71
72    public static void AddResultValue(this Algorithm a, string name, object value) {
73      if (value == null) return;
74      if (value is StringValue) {
75        value = ((StringValue)value).Value;
76      } else if (value is IntValue) {
77        value = ((IntValue)value).Value;
78      } else if (value is DoubleValue) {
79        value = ((DoubleValue)value).Value;
80      } else if (value is BoolValue) {
81        value = ((BoolValue)value).Value;
82      }
83      Type t = value.GetType();
84      Result r = new Result() {
85        DataType = new DataType() { ClrName = t.FullName },
86        Name = name
87      };
88      if (t == typeof(int)) {
89        r.IntResultValues = new List<IntResultValue>() { new IntResultValue() { Value = (int)value } };
90      } else if (t == typeof(double)) {
91        r.FloatResultValues = new List<FloatResultValue>() { new FloatResultValue() { Value = (double)value } };
92      } else if (t == typeof(string)) {
93        r.CharResultValues = new List<CharResultValue>() { new CharResultValue() { Value = (string)value } };
94      } else if (value is Item) {
95        byte[] data;
96        using (var stream = new MemoryStream()) {
97          XmlGenerator.Serialize(value, stream);
98          data = stream.ToArray();
99        }
100        r.BlobResultValues = new List<BlobResultValue>() {
101          new BlobResultValue() {
102            Value = new Binary() { Bytes = data },
103          }
104        };
105      } else {
106        throw new ArgumentException("unsupported result data type" + t.ToString());
107      }
108      a.Algorithm_Results.Add(new Algorithm_Result() {
109        Algorithm = a,
110        Result = r,
111      });
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.