Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Communication.Operators/XMLConstrainedItemListDeserializer.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.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Xml;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Communication.Data;
8
9namespace HeuristicLab.Communication.Operators {
10  public class XMLConstrainedItemListDeserializer : OperatorBase {
11    public override string Description {
12      get {
13        return @"TODO";
14      }
15    }
16
17    public XMLConstrainedItemListDeserializer() {
18      AddVariableInfo(new VariableInfo("CurrentState", "The current state of the protocol", typeof(ProtocolState), VariableKind.In));
19      AddVariableInfo(new VariableInfo("SerializedItem", "The string serialization that is to be deserialized", typeof(StringData), VariableKind.In | VariableKind.Deleted));
20      AddVariableInfo(new VariableInfo("Dictionary", "The dictionary that translates received variable names", typeof(ItemDictionary<StringData, StringData>), VariableKind.In));
21    }
22
23    public override IOperation Apply(IScope scope) {
24      ProtocolState currentState = GetVariableValue<ProtocolState>("CurrentState", scope, true);
25      ItemDictionary<StringData, StringData> dict = GetVariableValue<ItemDictionary<StringData, StringData>>("Dictionary", scope, true, false);
26      StringData serializationStringData = GetVariableValue<StringData>("SerializedItem", scope, false, false);
27
28      if (serializationStringData != null) {
29        string serialization = serializationStringData.Data;
30
31        XmlDocument document = new XmlDocument();
32        document.LoadXml(serialization);
33        ConstrainedItemList target = new ConstrainedItemList();
34        target.Populate(document.SelectSingleNode("DATA"), new Dictionary<Guid, IStorable>());
35
36        for (int i = 0; i < target.Count; i++) {
37          if (dict != null) { // if a dictionary exists try to lookup a translation
38            StringData tmp;
39            if (dict.TryGetValue(new StringData(((Variable)target[i]).Name), out tmp))
40              ((Variable)target[i]).Name = tmp.Data;
41          }
42          IVariable scopeVar = scope.GetVariable(((Variable)target[i]).Name);
43          if (scopeVar == null)
44            scope.AddVariable((Variable)target[i]);
45          else
46            scopeVar.Value = ((Variable)target[i]).Value;
47        }
48
49        IVariableInfo info = GetVariableInfo("SerializedItem");
50        if (info.Local) {
51          RemoveVariable(info.ActualName);
52        } else {
53          scope.RemoveVariable(scope.TranslateName(info.FormalName));
54        }
55      }
56      return null;
57    }
58  }
59}
Note: See TracBrowser for help on using the repository browser.