Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Operators/HookOperator.cs @ 11530

Last change on this file since 11530 was 11530, checked in by swagner, 9 years ago

#2205: Implemented review comments

  • renamed GenericPort to MessagePort
  • refactored CanConnectToPort
  • refactored PrepareMessage
  • removed IConnectedPort

Additional changes:

  • added UserDefinedMessagePort
  • refactored CloneConnectedPortParameters to CloneParametersFromPort and moved it to ParameterizedPort
  • added ports to NetworkView
File size: 2.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Core.Networks;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using System;
27using System.Linq;
28
29namespace HeuristicLab.Operators {
30  [Item("HookOperator", "An operator which interacts with an optimization network port.")]
31  [StorableClass]
32  public class HookOperator : Operator, IHookOperator {
33    public new ParameterCollection Parameters {
34      get { return base.Parameters; }
35    }
36
37    [Storable]
38    protected IMessagePort port;
39    public IMessagePort Port {
40      get { return port; }
41      set {
42        if (port != value) {
43          port = value;
44          OnPortChanged();
45        }
46      }
47    }
48
49    [StorableConstructor]
50    protected HookOperator(bool deserializing) : base(deserializing) { }
51    protected HookOperator(HookOperator original, Cloner cloner)
52      : base(original, cloner) {
53      // ATTENTION: port is intentionally NOT cloned
54      port = original.port;
55    }
56    public HookOperator() : base() { }
57
58    public override IDeepCloneable Clone(Cloner cloner) {
59      return new HookOperator(this, cloner);
60    }
61
62    public event EventHandler PortChanged;
63    protected virtual void OnPortChanged() {
64      var handler = PortChanged;
65      if (handler != null) handler(this, EventArgs.Empty);
66    }
67
68    public override IOperation Apply() {
69      if (Port != null) {
70        var msg = Port.PrepareMessage();
71        foreach (var output in Port.Parameters.Where(p => p.Type.HasFlag(PortParameterType.Output))) {
72          IParameter param;
73          if (Parameters.TryGetValue(output.Name, out param)) {
74            msg[output.Name] = param.ActualValue;
75          }
76        }
77
78        Port.SendMessage(msg, CancellationToken);
79
80        foreach (var input in Port.Parameters.Where(p => p.Type.HasFlag(PortParameterType.Input))) {
81          IParameter param;
82          if (Parameters.TryGetValue(input.Name, out param)) {
83            param.ActualValue = msg[input.Name];
84          }
85        }
86      }
87      return null;
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.