1 | using System.Threading;
|
---|
2 | using System.Threading.Tasks;
|
---|
3 | using HeuristicLab.Collections;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Core.Networks;
|
---|
7 | using HeuristicLab.Data;
|
---|
8 | using HeuristicLab.Optimization;
|
---|
9 | using HeuristicLab.Parameters;
|
---|
10 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.Networks.IntegratedOptimization {
|
---|
13 | [Item("OrchestratorNode", "An orchestrator node.")]
|
---|
14 | [StorableClass]
|
---|
15 | public abstract class OrchestratorNode : Node, IOrchestratorNode {
|
---|
16 | #region Constants
|
---|
17 | private const string OrchestrationMessageParameterName = "OrchestrationMessage";
|
---|
18 | protected const string OrchestrationPortNameSuffix = "OrchestrationPort";
|
---|
19 | protected const string EvaluationPortNameSuffix = "EvaluationPort";
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | [Storable]
|
---|
23 | private ParameterCollection parameters;
|
---|
24 | protected ParameterCollection Parameters {
|
---|
25 | get { return parameters; }
|
---|
26 | }
|
---|
27 | private ReadOnlyKeyedItemCollection<string, IParameter> readOnlyParameters;
|
---|
28 | IKeyedItemCollection<string, IParameter> IOrchestratorNode.Parameters {
|
---|
29 | get {
|
---|
30 | if (readOnlyParameters == null) readOnlyParameters = parameters.AsReadOnly();
|
---|
31 | return readOnlyParameters;
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | [Storable]
|
---|
36 | private ResultCollection results;
|
---|
37 | public ResultCollection Results {
|
---|
38 | get { return results; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | [StorableConstructor]
|
---|
42 | protected OrchestratorNode(bool deserializing) : base(deserializing) { }
|
---|
43 | protected OrchestratorNode(OrchestratorNode original, Cloner cloner) : base(original, cloner) {
|
---|
44 | results = cloner.Clone(original.results);
|
---|
45 | parameters = cloner.Clone(original.parameters);
|
---|
46 | readOnlyParameters = null;
|
---|
47 | RegisterEvents();
|
---|
48 | }
|
---|
49 |
|
---|
50 | protected OrchestratorNode() : base("OrchestratorNode") {
|
---|
51 | results = new ResultCollection();
|
---|
52 | parameters = new ParameterCollection();
|
---|
53 | Parameters.Add(new ValueParameter<EnumValue<OrchestrationMessage>>("OrchestrationMessage", new EnumValue<OrchestrationMessage>()));
|
---|
54 | readOnlyParameters = null;
|
---|
55 | }
|
---|
56 | protected OrchestratorNode(string name) : base(name) {
|
---|
57 | results = new ResultCollection();
|
---|
58 | parameters = new ParameterCollection();
|
---|
59 | Parameters.Add(new ValueParameter<EnumValue<OrchestrationMessage>>("OrchestrationMessage", new EnumValue<OrchestrationMessage>()));
|
---|
60 | readOnlyParameters = null;
|
---|
61 | }
|
---|
62 | protected OrchestratorNode(string name, string description) : base(name, description) {
|
---|
63 | results = new ResultCollection();
|
---|
64 | parameters = new ParameterCollection();
|
---|
65 | Parameters.Add(new ValueParameter<EnumValue<OrchestrationMessage>>("OrchestrationMessage", new EnumValue<OrchestrationMessage>()));
|
---|
66 | readOnlyParameters = null;
|
---|
67 | }
|
---|
68 |
|
---|
69 | [StorableHook(HookType.AfterDeserialization)]
|
---|
70 | private void AfterDeserialization() {
|
---|
71 | RegisterEvents();
|
---|
72 | }
|
---|
73 |
|
---|
74 | private void RegisterEvents() {
|
---|
75 | foreach (var p in Ports) RegisterPortEvents(p);
|
---|
76 | }
|
---|
77 |
|
---|
78 | protected void AddOrchestrationPort<T>(string solverName)
|
---|
79 | where T : class, IProblem {
|
---|
80 | var orchestrationPort = new MessagePort(solverName + OrchestrationPortNameSuffix);
|
---|
81 | orchestrationPort.Parameters.Add(new PortParameter<EnumValue<OrchestrationMessage>>("OrchestrationMessage") {
|
---|
82 | Type = PortParameterType.Output
|
---|
83 | });
|
---|
84 | orchestrationPort.Parameters.Add(new PortParameter<T>("Problem") {
|
---|
85 | Type = PortParameterType.Output
|
---|
86 | });
|
---|
87 | orchestrationPort.Parameters.Add(new PortParameter<ResultCollection>("Results") {
|
---|
88 | Type = PortParameterType.Input
|
---|
89 | });
|
---|
90 | Ports.Add(orchestrationPort);
|
---|
91 | }
|
---|
92 |
|
---|
93 | protected void AddEvaluationPort<T>(string solverName, string solutionName, string qualityName) where T : class, IItem {
|
---|
94 | var evaluationPort = new MessagePort(solverName + EvaluationPortNameSuffix);
|
---|
95 | evaluationPort.Parameters.Add(new PortParameter<T>(solutionName) {
|
---|
96 | Type = PortParameterType.Input
|
---|
97 | });
|
---|
98 | evaluationPort.Parameters.Add(new PortParameter<DoubleValue>(qualityName) {
|
---|
99 | Type = PortParameterType.Input | PortParameterType.Output
|
---|
100 | });
|
---|
101 | Ports.Add(evaluationPort);
|
---|
102 | }
|
---|
103 |
|
---|
104 | protected virtual void ProcessMessage(IMessage message, IMessagePort port, CancellationToken token) { }
|
---|
105 |
|
---|
106 | public abstract void Prepare();
|
---|
107 | public async Task PrepareAsync() { await Task.Run(() => Prepare()); }
|
---|
108 | public abstract void Start();
|
---|
109 | public async Task StartAsync() { await Task.Run(() => Start()); }
|
---|
110 | public abstract void Pause();
|
---|
111 | public async Task PauseAsync() { await Task.Run(() => Pause()); }
|
---|
112 | public abstract void Stop();
|
---|
113 | public async Task StopAsync() { await Task.Run(() => Stop()); }
|
---|
114 |
|
---|
115 | #region Events
|
---|
116 | protected override void Ports_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IPort> e) {
|
---|
117 | base.Ports_ItemsAdded(sender, e);
|
---|
118 | foreach (var p in e.Items)
|
---|
119 | RegisterPortEvents(p);
|
---|
120 | }
|
---|
121 | protected override void Ports_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IPort> e) {
|
---|
122 | base.Ports_ItemsRemoved(sender, e);
|
---|
123 | foreach (var p in e.Items)
|
---|
124 | DeregisterPortEvents(p);
|
---|
125 | }
|
---|
126 | protected override void Ports_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IPort> e) {
|
---|
127 | base.Ports_ItemsReplaced(sender, e);
|
---|
128 | foreach (var p in e.OldItems)
|
---|
129 | DeregisterPortEvents(p);
|
---|
130 | foreach (var p in e.Items)
|
---|
131 | RegisterPortEvents(p);
|
---|
132 | }
|
---|
133 | protected override void Ports_CollectionReset(object sender, CollectionItemsChangedEventArgs<IPort> e) {
|
---|
134 | base.Ports_CollectionReset(sender, e);
|
---|
135 | foreach (var p in e.OldItems)
|
---|
136 | DeregisterPortEvents(p);
|
---|
137 | foreach (var p in e.Items)
|
---|
138 | RegisterPortEvents(p);
|
---|
139 | }
|
---|
140 | private void MessagePort_MessageReceived(object sender, EventArgs<IMessage, CancellationToken> e) {
|
---|
141 | ProcessMessage(e.Value, (IMessagePort)sender, e.Value2);
|
---|
142 | }
|
---|
143 | #endregion
|
---|
144 |
|
---|
145 | #region Port Events
|
---|
146 | protected virtual void RegisterPortEvents(IPort port) {
|
---|
147 | var mp = port as IMessagePort;
|
---|
148 | if (mp != null)
|
---|
149 | mp.MessageReceived += MessagePort_MessageReceived;
|
---|
150 | }
|
---|
151 | protected virtual void DeregisterPortEvents(IPort port) {
|
---|
152 | var mp = port as IMessagePort;
|
---|
153 | if (mp != null)
|
---|
154 | mp.MessageReceived -= MessagePort_MessageReceived;
|
---|
155 | }
|
---|
156 | #endregion
|
---|
157 | }
|
---|
158 | }
|
---|