Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Communication.Operators/SubscopeCurrentStateLooseDataDistributor.cs @ 584

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

merged communication framework to trunk (ticket #279)

File size: 3.0 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 SubscopeCurrentStateLooseDataDistributor : OperatorBase {
11    public override string Description {
12      get {
13        return base.Description;
14      }
15    }
16
17    public SubscopeCurrentStateLooseDataDistributor() {
18      AddVariableInfo(new VariableInfo("CurrentState", "The current state in the execution of the protocol", typeof(ProtocolState), VariableKind.In));
19      AddVariableInfo(new VariableInfo("ReceivedData", "The data that has been received", typeof(StringData), VariableKind.Deleted));
20    }
21
22    public override IOperation Apply(IScope scope) {
23      ProtocolState currentState = GetVariableValue<ProtocolState>("CurrentState", scope, true);
24      StringData data = GetVariableValue<StringData>("ReceivedData", scope, false, false);
25
26      if (data != null && currentState.ReceivingData.Count > 0) {
27        XmlDocument document = new XmlDocument();
28        document.LoadXml(data.Data);
29        XmlNode root = document.SelectSingleNode("COMBINED_DATA");
30        ConstrainedItemList definedResponse = currentState.ReceivingData;
31
32        foreach (IScope subScope in scope.SubScopes) {
33          ConstrainedItemList receivedResponse = new ConstrainedItemList();
34          XmlNode node = root.SelectSingleNode("DATA");
35          receivedResponse.Populate(node, new Dictionary<Guid, IStorable>());
36
37          int receivedResponseCount = receivedResponse.Count;
38
39          for (int i = 0; i < definedResponse.Count; i++) {
40            Variable nextDefinedVariable = (Variable)definedResponse[i];
41            int location = -1;
42            for (int j = i; j < receivedResponseCount + i; j++) {
43              Variable nextReceivedVariable = (Variable)receivedResponse[j % receivedResponseCount];
44              if (nextDefinedVariable.Name.Equals(nextReceivedVariable.Name)) {
45                location = j % receivedResponseCount;
46                break;
47              }
48            }
49            if (location < 0) throw new InvalidOperationException("did not receive variable " + nextDefinedVariable.Name); // FIXME: needs more sophisticated error handling
50            IItem value = subScope.GetVariableValue(nextDefinedVariable.Name, false, false);
51            if (value == null)
52              subScope.AddVariable((Variable)receivedResponse[location]);
53            else
54              subScope.GetVariable(nextDefinedVariable.Name).Value = ((Variable)receivedResponse[location]).Value;
55          }
56          root.RemoveChild(node);
57        }
58      }
59      if (data != null) {
60        IVariableInfo info = GetVariableInfo("ReceivedData");
61        if (info.Local) {
62          RemoveVariable(info.ActualName);
63        } else {
64          scope.RemoveVariable(scope.TranslateName(info.FormalName));
65        }
66      }
67      return null;
68    }
69  }
70}
Note: See TracBrowser for help on using the repository browser.