[592] | 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 |
|
---|
| 22 | using System;
|
---|
[583] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using System.Xml;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Communication.Data;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Communication.Operators {
|
---|
| 31 | public class SubscopeCurrentStateLooseDataDistributor : OperatorBase {
|
---|
| 32 | public override string Description {
|
---|
| 33 | get {
|
---|
| 34 | return base.Description;
|
---|
| 35 | }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | public SubscopeCurrentStateLooseDataDistributor() {
|
---|
| 39 | AddVariableInfo(new VariableInfo("CurrentState", "The current state in the execution of the protocol", typeof(ProtocolState), VariableKind.In));
|
---|
| 40 | AddVariableInfo(new VariableInfo("ReceivedData", "The data that has been received", typeof(StringData), VariableKind.Deleted));
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | public override IOperation Apply(IScope scope) {
|
---|
| 44 | ProtocolState currentState = GetVariableValue<ProtocolState>("CurrentState", scope, true);
|
---|
| 45 | StringData data = GetVariableValue<StringData>("ReceivedData", scope, false, false);
|
---|
| 46 |
|
---|
| 47 | if (data != null && currentState.ReceivingData.Count > 0) {
|
---|
| 48 | XmlDocument document = new XmlDocument();
|
---|
| 49 | document.LoadXml(data.Data);
|
---|
| 50 | XmlNode root = document.SelectSingleNode("COMBINED_DATA");
|
---|
| 51 | ConstrainedItemList definedResponse = currentState.ReceivingData;
|
---|
| 52 |
|
---|
| 53 | foreach (IScope subScope in scope.SubScopes) {
|
---|
| 54 | ConstrainedItemList receivedResponse = new ConstrainedItemList();
|
---|
| 55 | XmlNode node = root.SelectSingleNode("DATA");
|
---|
| 56 | receivedResponse.Populate(node, new Dictionary<Guid, IStorable>());
|
---|
| 57 |
|
---|
| 58 | int receivedResponseCount = receivedResponse.Count;
|
---|
| 59 |
|
---|
| 60 | for (int i = 0; i < definedResponse.Count; i++) {
|
---|
| 61 | Variable nextDefinedVariable = (Variable)definedResponse[i];
|
---|
| 62 | int location = -1;
|
---|
| 63 | for (int j = i; j < receivedResponseCount + i; j++) {
|
---|
| 64 | Variable nextReceivedVariable = (Variable)receivedResponse[j % receivedResponseCount];
|
---|
| 65 | if (nextDefinedVariable.Name.Equals(nextReceivedVariable.Name)) {
|
---|
| 66 | location = j % receivedResponseCount;
|
---|
| 67 | break;
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 | if (location < 0) throw new InvalidOperationException("did not receive variable " + nextDefinedVariable.Name); // FIXME: needs more sophisticated error handling
|
---|
| 71 | IItem value = subScope.GetVariableValue(nextDefinedVariable.Name, false, false);
|
---|
| 72 | if (value == null)
|
---|
| 73 | subScope.AddVariable((Variable)receivedResponse[location]);
|
---|
| 74 | else
|
---|
| 75 | subScope.GetVariable(nextDefinedVariable.Name).Value = ((Variable)receivedResponse[location]).Value;
|
---|
| 76 | }
|
---|
| 77 | root.RemoveChild(node);
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | if (data != null) {
|
---|
| 81 | IVariableInfo info = GetVariableInfo("ReceivedData");
|
---|
| 82 | if (info.Local) {
|
---|
| 83 | RemoveVariable(info.ActualName);
|
---|
| 84 | } else {
|
---|
| 85 | scope.RemoveVariable(scope.TranslateName(info.FormalName));
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 | return null;
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | }
|
---|