Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HL-3.2-MonoMigration/HeuristicLab.Communication.Operators/XMLConstrainedItemListDeserializer.cs @ 1104

Last change on this file since 1104 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: 3.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.Text;
25using System.Xml;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Communication.Data;
29
30namespace HeuristicLab.Communication.Operators {
31  public class XMLConstrainedItemListDeserializer : OperatorBase {
32    public override string Description {
33      get {
34        return @"TODO";
35      }
36    }
37
38    public XMLConstrainedItemListDeserializer() {
39      AddVariableInfo(new VariableInfo("CurrentState", "The current state of the protocol", typeof(ProtocolState), VariableKind.In));
40      AddVariableInfo(new VariableInfo("SerializedItem", "The string serialization that is to be deserialized", typeof(StringData), VariableKind.In | VariableKind.Deleted));
41      AddVariableInfo(new VariableInfo("Dictionary", "The dictionary that translates received variable names", typeof(ItemDictionary<StringData, StringData>), VariableKind.In));
42    }
43
44    public override IOperation Apply(IScope scope) {
45      ProtocolState currentState = GetVariableValue<ProtocolState>("CurrentState", scope, true);
46      ItemDictionary<StringData, StringData> dict = GetVariableValue<ItemDictionary<StringData, StringData>>("Dictionary", scope, true, false);
47      StringData serializationStringData = GetVariableValue<StringData>("SerializedItem", scope, false, false);
48
49      if (serializationStringData != null) {
50        string serialization = serializationStringData.Data;
51
52        XmlDocument document = new XmlDocument();
53        document.LoadXml(serialization);
54        ConstrainedItemList target = new ConstrainedItemList();
55        target.Populate(document.SelectSingleNode("DATA"), new Dictionary<Guid, IStorable>());
56
57        for (int i = 0; i < target.Count; i++) {
58          if (dict != null) { // if a dictionary exists try to lookup a translation
59            StringData tmp;
60            if (dict.TryGetValue(new StringData(((Variable)target[i]).Name), out tmp))
61              ((Variable)target[i]).Name = tmp.Data;
62          }
63          IVariable scopeVar = scope.GetVariable(((Variable)target[i]).Name);
64          if (scopeVar == null)
65            scope.AddVariable((Variable)target[i]);
66          else
67            scopeVar.Value = ((Variable)target[i]).Value;
68        }
69
70        IVariableInfo info = GetVariableInfo("SerializedItem");
71        if (info.Local) {
72          RemoveVariable(info.ActualName);
73        } else {
74          scope.RemoveVariable(scope.TranslateName(info.FormalName));
75        }
76      }
77      return null;
78    }
79  }
80}
Note: See TracBrowser for help on using the repository browser.