Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Communication.Operators/SimpleReceiveItemDeserializer.cs @ 584

Last change on this file since 584 was 584, checked in by abeham, 16 years ago

merged communication framework to trunk (ticket #279)

File size: 3.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Communication.Data;
8
9namespace 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}
Note: See TracBrowser for help on using the repository browser.