Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Communication.Operators/SimpleSendItemSerializer.cs @ 583

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

Adding communication framework to branch 3.1 (ticket #278)

File size: 2.1 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 SimpleSendItemSerializer : OperatorBase {
11    public override string Description {
12      get {
13        return @"Serializes the data defined under ""Send"" in the current state of the protocol. It uses a simple scheme that prints in each line: the name of the variable, the type of the containing value and the value itself.";
14      }
15    }
16
17    public SimpleSendItemSerializer() {
18      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));
19      AddVariableInfo(new VariableInfo("SerializedItem", "The string serialization of the item", typeof(StringData), VariableKind.New));
20    }
21
22    public override IOperation Apply(IScope scope) {
23      ProtocolState currentState = GetVariableValue<ProtocolState>("CurrentState", scope, true);
24      ConstrainedItemList sendingData = currentState.SendingData;
25
26      if (sendingData.Count > 0) {
27        StringBuilder sb = new StringBuilder();
28        for (int i = 0; i < sendingData.Count; i++) {
29          sb.AppendLine(((Variable)sendingData[i]).Name);
30          IVariable tmp = scope.GetVariable(((Variable)sendingData[i]).Name);
31          if (tmp == null) throw new InvalidOperationException("ERROR in SimpleSendItemSerializer: variable " + ((Variable)sendingData[i]).Name + " not found!");
32          sb.AppendLine(tmp.Value.GetType().ToString());
33          sb.AppendLine(tmp.Value.ToString());
34        }
35
36        IVariableInfo info = GetVariableInfo("SerializedItem");
37        if (info.Local) {
38          AddVariable(new Variable(info.ActualName, new StringData(sb.ToString())));
39        } else {
40          scope.AddVariable(new Variable(scope.TranslateName(info.FormalName), new StringData(sb.ToString())));
41        }
42      }
43
44      return null;
45    }
46  }
47}
Note: See TracBrowser for help on using the repository browser.