Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HL-3.2-MonoMigration/HeuristicLab.Communication.Operators/SimpleReceiveItemDeserializer.cs @ 2950

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

Put the GPL license in the files from the communication framework and simulation optimization project

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.IO;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Communication.Data;
29
30namespace HeuristicLab.Communication.Operators {
31  public class SimpleReceiveItemDeserializer : OperatorBase {
32    public override string Description {
33      get {
34        return @"Deserializes data that was serialized by the SimpleSendItemSerializer";      }
35    }
36
37    public SimpleReceiveItemDeserializer() {
38      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));
39      AddVariableInfo(new VariableInfo("SerializedItem", "The string serialization of the item", typeof(StringData), VariableKind.Deleted));
40      AddVariableInfo(new VariableInfo("Dictionary", "The dictionary that translates received variable names", typeof(ItemDictionary<StringData, StringData>), VariableKind.In));
41    }
42
43    public override IOperation Apply(IScope scope) {
44      ProtocolState currentState = GetVariableValue<ProtocolState>("CurrentState", scope, true);
45      ItemDictionary<StringData, StringData> dict = GetVariableValue<ItemDictionary<StringData, StringData>>("Dictionary", scope, true, false);
46      ConstrainedItemList receivingData = currentState.ReceivingData;
47
48      if (receivingData.Count > 0) {
49        string received = GetVariableValue<StringData>("SerializedItem", scope, false).Data;
50        StringReader reader = new StringReader(received);
51
52        do {
53         
54          string line = reader.ReadLine();
55          if (line == null) break;
56          string name = line;
57          line = reader.ReadLine();
58          string type = line;
59          line = reader.ReadLine();
60          string value = line;
61
62          int index = 0;
63          while (index < receivingData.Count && !name.Equals(((Variable)receivingData[index]).Name))
64            index++;
65
66          if (index != receivingData.Count) {
67            Type itemType = ((Variable)receivingData[index]).Value.GetType();
68            Variable item = (Variable)receivingData[index].Clone();
69
70            if (itemType.Equals(typeof(IntData)))
71              ((IntData)item.Value).Data = int.Parse(value);
72            else if (itemType.Equals(typeof(DoubleData)))
73              ((DoubleData)item.Value).Data = double.Parse(value);
74            else if (itemType.Equals(typeof(ConstrainedIntData)))
75              ((ConstrainedIntData)item.Value).Data = int.Parse(value);
76            else if (itemType.Equals(typeof(ConstrainedDoubleData)))
77              ((ConstrainedDoubleData)item.Value).Data = double.Parse(value);
78            else throw new NotImplementedException();
79
80            // if a dictionary exists try to lookup a translation
81            if (dict != null) {
82              StringData tmp;
83              if (dict.TryGetValue(new StringData(item.Name), out tmp))
84                item.Name = tmp.Data;
85            }
86
87            // add/overwrite the read variable to scope
88            IVariable scopeVar = scope.GetVariable(item.Name);
89            if (scopeVar == null)
90              scope.AddVariable(item);
91            else
92              scopeVar.Value = item.Value;
93          }
94        } while (true);
95        reader.Close();
96
97        IVariableInfo info = GetVariableInfo("SerializedItem");
98        if (info.Local) {
99          RemoveVariable(info.ActualName);
100        } else {
101          scope.RemoveVariable(scope.TranslateName(info.FormalName));
102        }
103      }
104
105      return null;
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.