1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Text;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.Communication.Data;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Communication.Operators {
|
---|
9 | public class MessageInjector : OperatorBase {
|
---|
10 | public override string Description {
|
---|
11 | get {
|
---|
12 | return @"Creates a message template for a communicator to send.";
|
---|
13 | }
|
---|
14 | }
|
---|
15 |
|
---|
16 | public MessageInjector()
|
---|
17 | : base() {
|
---|
18 | AddVariableInfo(new VariableInfo("Protocol", "", typeof(Protocol), VariableKind.In));
|
---|
19 | AddVariableInfo(new VariableInfo("PeerName", "", typeof(StringData), VariableKind.In));
|
---|
20 | AddVariableInfo(new VariableInfo("CurrentState", "The current state which to create a message", typeof(ProtocolState), VariableKind.In));
|
---|
21 | AddVariableInfo(new VariableInfo("Message", "The message to inject", typeof(Message), VariableKind.New));
|
---|
22 | }
|
---|
23 |
|
---|
24 | public override IOperation Apply(IScope scope) {
|
---|
25 | Protocol protocol = GetVariableValue<Protocol>("Protocol", scope, true);
|
---|
26 | ProtocolState currentState = GetVariableValue<ProtocolState>("CurrentState", scope, true);
|
---|
27 | string iPeer = GetVariableValue<StringData>("PeerName", scope, true).Data;
|
---|
28 | Message message = new Message();
|
---|
29 | message.Protocol = protocol.Name;
|
---|
30 | message.Source = iPeer;
|
---|
31 | message.Destination = "";
|
---|
32 | for (int i = 0; i < currentState.Give.Count; i++)
|
---|
33 | message.Give.Add((IVariable)currentState.Give[i].Clone());
|
---|
34 | for (int i = 0; i < currentState.Expect.Count; i++)
|
---|
35 | message.Expect.Add((IVariable)currentState.Expect[i].Clone());
|
---|
36 |
|
---|
37 | scope.AddVariable(new Variable(scope.TranslateName("Message"),message));
|
---|
38 | return null;
|
---|
39 | }
|
---|
40 | }
|
---|
41 | } |
---|