[583] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Text;
|
---|
| 4 | using System.Xml;
|
---|
| 5 | using HeuristicLab.Core;
|
---|
| 6 | using HeuristicLab.Data;
|
---|
| 7 | using HeuristicLab.Communication.Data;
|
---|
| 8 |
|
---|
| 9 | namespace 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 | }
|
---|