1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.IO;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Communication.Data;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Communication.Operators {
|
---|
10 | public class SimpleReceiveItemDeserializer : OperatorBase {
|
---|
11 | public override string Description {
|
---|
12 | get {
|
---|
13 | return @"Deserializes data that was serialized by the SimpleSendItemSerializer"; }
|
---|
14 | }
|
---|
15 |
|
---|
16 | public SimpleReceiveItemDeserializer() {
|
---|
17 | AddVariableInfo(new VariableInfo("CurrentState", "The current state defines the items which are to be sent and thus need to be serialized", typeof(ProtocolState), VariableKind.In));
|
---|
18 | AddVariableInfo(new VariableInfo("SerializedItem", "The string serialization of the item", typeof(StringData), VariableKind.Deleted));
|
---|
19 | AddVariableInfo(new VariableInfo("Dictionary", "The dictionary that translates received variable names", typeof(ItemDictionary<StringData, StringData>), VariableKind.In));
|
---|
20 | }
|
---|
21 |
|
---|
22 | public override IOperation Apply(IScope scope) {
|
---|
23 | ProtocolState currentState = GetVariableValue<ProtocolState>("CurrentState", scope, true);
|
---|
24 | ItemDictionary<StringData, StringData> dict = GetVariableValue<ItemDictionary<StringData, StringData>>("Dictionary", scope, true, false);
|
---|
25 | ConstrainedItemList receivingData = currentState.ReceivingData;
|
---|
26 |
|
---|
27 | if (receivingData.Count > 0) {
|
---|
28 | string received = GetVariableValue<StringData>("SerializedItem", scope, false).Data;
|
---|
29 | StringReader reader = new StringReader(received);
|
---|
30 |
|
---|
31 | do {
|
---|
32 |
|
---|
33 | string line = reader.ReadLine();
|
---|
34 | if (line == null) break;
|
---|
35 | string name = line;
|
---|
36 | line = reader.ReadLine();
|
---|
37 | string type = line;
|
---|
38 | line = reader.ReadLine();
|
---|
39 | string value = line;
|
---|
40 |
|
---|
41 | int index = 0;
|
---|
42 | while (index < receivingData.Count && !name.Equals(((Variable)receivingData[index]).Name))
|
---|
43 | index++;
|
---|
44 |
|
---|
45 | if (index != receivingData.Count) {
|
---|
46 | Type itemType = ((Variable)receivingData[index]).Value.GetType();
|
---|
47 | Variable item = (Variable)receivingData[index].Clone();
|
---|
48 |
|
---|
49 | if (itemType.Equals(typeof(IntData)))
|
---|
50 | ((IntData)item.Value).Data = int.Parse(value);
|
---|
51 | else if (itemType.Equals(typeof(DoubleData)))
|
---|
52 | ((DoubleData)item.Value).Data = double.Parse(value);
|
---|
53 | else if (itemType.Equals(typeof(ConstrainedIntData)))
|
---|
54 | ((ConstrainedIntData)item.Value).Data = int.Parse(value);
|
---|
55 | else if (itemType.Equals(typeof(ConstrainedDoubleData)))
|
---|
56 | ((ConstrainedDoubleData)item.Value).Data = double.Parse(value);
|
---|
57 | else throw new NotImplementedException();
|
---|
58 |
|
---|
59 | // if a dictionary exists try to lookup a translation
|
---|
60 | if (dict != null) {
|
---|
61 | StringData tmp;
|
---|
62 | if (dict.TryGetValue(new StringData(item.Name), out tmp))
|
---|
63 | item.Name = tmp.Data;
|
---|
64 | }
|
---|
65 |
|
---|
66 | // add/overwrite the read variable to scope
|
---|
67 | IVariable scopeVar = scope.GetVariable(item.Name);
|
---|
68 | if (scopeVar == null)
|
---|
69 | scope.AddVariable(item);
|
---|
70 | else
|
---|
71 | scopeVar.Value = item.Value;
|
---|
72 | }
|
---|
73 | } while (true);
|
---|
74 | reader.Close();
|
---|
75 |
|
---|
76 | IVariableInfo info = GetVariableInfo("SerializedItem");
|
---|
77 | if (info.Local) {
|
---|
78 | RemoveVariable(info.ActualName);
|
---|
79 | } else {
|
---|
80 | scope.RemoveVariable(scope.TranslateName(info.FormalName));
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | return null;
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|